Pet*_*vic 6 c++ boost-asio boost-beast
我已经修改了 http_sync 示例以通过代理连接,我已经用 Wireshark 进行了测试,问题是在我发送 http 连接请求后,代理返回代码 200 OK,而我的程序无法读取,它得到在 http::read 上等待大约 1 分钟,然后服务器断开了我的连接。我试过解决这个问题,只是没有读取响应,但是当我尝试进行 ssl 握手时,我收到一个错误:未知协议(就wireshark 而言,我的 ssl 握手发送了一个客户端你好,然后我收到了完整的服务器你好,但我的程序在此期间发送了一个 FIN 并给了我错误,我认为这是因为他阅读了 200 OK 而不是服务器的答案并感到困惑)。所以我想我的问题是,有没有办法解决这个问题?我不知道为什么 http::read 卡住了。它在没有中间代理的情况下正常工作。这是我的代码:
using tcp = boost::asio::ip::tcp;       // from <boost/asio/ip/tcp.hpp>
namespace ssl = boost::asio::ssl;       // from <boost/asio/ssl.hpp>
namespace http = boost::beast::http;    // from <boost/beast/http.hpp>
int main(int argc, char** argv)
{
try
{
    // Check command line arguments.
    auto const target = "www.discogs.com:443";
    auto const host = "192.116.142.153";
    auto const port = "443";
    int version = 11;
    // The io_context is required for all I/O
    boost::asio::io_context ioc;
    // These objects perform our I/O
    ssl::context ctx{ ssl::context::sslv23_client };
    load_root_certificates(ctx);
    tcp::resolver resolver{ ioc };
    ssl::stream<tcp::socket> stream{ ioc, ctx };
    tcp::resolver::iterator sock = resolver.resolve("192.116.142.153", "8080");
    boost::asio::connect(stream.next_layer(), sock);
    http::request<http::string_body> req1{ http::verb::connect, target, version };
    req1.set(http::field::host, target);
    http::write(stream.next_layer(), req1);
    boost::beast::flat_buffer buffer;
    http::response<http::dynamic_body> res;
    http::read(stream.next_layer(), buffer, res);
    // Write the message to standard out
    std::cout << res << std::endl;
    // Perform the SSL handshake
    stream.handshake(ssl::stream_base::client);
    // Set up an HTTP GET request message
    http::request<http::string_body> req{ http::verb::get, target, version };
    req.set(http::field::host, host);
    req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
    // Send the HTTP request to the remote host
    http::write(stream, req);
    // This buffer is used for reading and must be persisted
    //boost::beast::flat_buffer buffer;
    // Declare a container to hold the response
    //http::response<http::dynamic_body> res;
    // Receive the HTTP response
    http::read(stream, buffer, res);
    // Write the message to standard out
    std::cout << res << std::endl;
    getchar();
    // Gracefully close the stream
    boost::system::error_code ec;
    stream.next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_send);
    if (ec == boost::asio::error::eof)
    {
        // Rationale:
        // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
        ec.assign(0, ec.category());
    }
    if (ec)
        throw boost::system::system_error{ ec };
    // If we get here then the connection is closed gracefully
}
catch (std::exception const& e)
{
    std::cerr << "Error: " << e.what() << std::endl;
    return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
搜索文档让proxy我找到了skip(). 这导致我使用我自己的响应解析器实例:
http::response<http::empty_body> res;\nhttp::parser<false, http::empty_body> p(res);\np.skip(true);\nhttp::read(stream.next_layer(), buffer, p);\n这样可行。好吧,我将请求目标更改为有效的目标。在本例中,我通过您的 Fortinet 代理检索了https://www.boost.org/users/license.html :
\n\n#include <boost/asio.hpp>\n#include <boost/asio/ssl.hpp>\n#include <boost/beast.hpp>\n\nusing tcp = boost::asio::ip::tcp;    // from <boost/asio/ip/tcp.hpp>\nnamespace ssl = boost::asio::ssl;    // from <boost/asio/ssl.hpp>\nnamespace http = boost::beast::http; // from <boost/beast/http.hpp>\n\nstatic void load_root_certificates(ssl::context &ctx);\n\nint main() try {\n    // Check command line arguments.\n    auto const target = "www.boost.org:443";\n    auto const host = "www.boost.org";\n    int version = 11;\n\n    // The io_context is required for all I/O\n    boost::asio::io_context ioc;\n\n    // These objects perform our I/O\n\n    ssl::context ctx{ ssl::context::sslv23_client };\n\n    load_root_certificates(ctx);\n\n    tcp::resolver resolver{ ioc };\n    ssl::stream<tcp::socket> stream{ ioc, ctx };\n\n    stream.next_layer().connect({boost::asio::ip::address_v4::from_string("192.116.142.153"), 8080});\n\n    {\n        http::request<http::string_body> req1{ http::verb::connect, target, version };\n        req1.set(http::field::host, target);\n\n        http::write(stream.next_layer(), req1);\n\n        boost::beast::flat_buffer buffer;\n\n        http::response<http::empty_body> res;\n        http::parser<false, http::empty_body> p(res);\n        p.skip(true);\n        http::read(stream.next_layer(), buffer, p);\n\n        // Write the message to standard out\n        std::cout << res << std::endl;\n    }\n\n    // Perform the SSL handshake\n    stream.handshake(ssl::stream_base::client);\n\n    // Set up an HTTP GET request message\n    {\n        http::request<http::string_body> req{ http::verb::get, "/users/license.html", version };\n        req.set(http::field::host, host);\n        req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);\n\n        // Send the HTTP request to the remote host\n        http::write(stream, req);\n\n        boost::beast::flat_buffer buffer;\n        http::response<http::dynamic_body> res;\n        http::read(stream, buffer, res);\n        // Write the message to standard out\n        std::cout << res << std::endl;\n    }\n\n    getchar();\n\n    // Gracefully close the stream\n    boost::system::error_code ec;\n    stream.next_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_send);\n    if (ec == boost::asio::error::eof) {\n        // Rationale:\n        // http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error\n        ec.assign(0, ec.category());\n    }\n    if (ec)\n        throw boost::system::system_error{ ec };\n    // If we get here then the connection is closed gracefully\n} catch (std::exception const &e) {\n    std::cerr << "Error: " << e.what() << std::endl;\n    return EXIT_FAILURE;\n}\n\n\nnamespace detail {\n\nstatic void load_root_certificates(ssl::context &ctx, boost::system::error_code &ec) {\n    std::string const cert =\n        /*  This is the DigiCert root certificate.\n\n            CN = DigiCert High Assurance EV Root CA\n            OU = www.digicert.com\n            O = DigiCert Inc\n            C = US\n\n            Valid to: Sunday, ?November ?9, ?2031 5:00:00 PM\n\n            Thumbprint(sha1):\n            5f b7 ee 06 33 e2 59 db ad 0c 4c 9a e6 d3 8f 1a 61 c7 dc 25\n        */\n        "-----BEGIN CERTIFICATE-----\\n"\n        "MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\\n"\n        "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\\n"\n        "d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\\n"\n        "ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL\\n"\n        "MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3\\n"\n        "LmRpZ2ljZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgSGlnaCBBc3N1cmFuY2Ug\\n"\n        "RVYgUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMbM5XPm\\n"\n        "+9S75S0tMqbf5YE/yc0lSbZxKsPVlDRnogocsF9ppkCxxLeyj9CYpKlBWTrT3JTW\\n"\n        "PNt0OKRKzE0lgvdKpVMSOO7zSW1xkX5jtqumX8OkhPhPYlG++MXs2ziS4wblCJEM\\n"\n        "xChBVfvLWokVfnHoNb9Ncgk9vjo4UFt3MRuNs8ckRZqnrG0AFFoEt7oT61EKmEFB\\n"\n        "Ik5lYYeBQVCmeVyJ3hlKV9Uu5l0cUyx+mM0aBhakaHPQNAQTXKFx01p8VdteZOE3\\n"\n        "hzBWBOURtCmAEvF5OYiiAhF8J2a3iLd48soKqDirCmTCv2ZdlYTBoSUeh10aUAsg\\n"\n        "EsxBu24LUTi4S8sCAwEAAaNjMGEwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQF\\n"\n        "MAMBAf8wHQYDVR0OBBYEFLE+w2kD+L9HAdSYJhoIAu9jZCvDMB8GA1UdIwQYMBaA\\n"\n        "FLE+w2kD+L9HAdSYJhoIAu9jZCvDMA0GCSqGSIb3DQEBBQUAA4IBAQAcGgaX3Nec\\n"\n        "nzyIZgYIVyHbIUf4KmeqvxgydkAQV8GK83rZEWWONfqe/EW1ntlMMUu4kehDLI6z\\n"\n        "eM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF\\n"\n        "hS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2\\n"\n        "Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe\\n"\n        "vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\\n"\n        "+OkuE6N36B9K\\n"\n        "-----END CERTIFICATE-----\\n"\n        /*  This is the GeoTrust root certificate.\n\n            CN = GeoTrust Global CA\n            O = GeoTrust Inc.\n            C = US\n            Valid to: Friday, \xe2\x80\x8eMay \xe2\x80\x8e20, \xe2\x80\x8e2022 9:00:00 PM\n\n            Thumbprint(sha1):\n            \xe2\x80\x8ede 28 f4 a4 ff e5 b9 2f a3 c5 03 d1 a3 49 a7 f9 96 2a 82 12\n        */\n        "-----BEGIN CERTIFICATE-----\\n"\n        "MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\\n"\n        "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\\n"\n        "d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\\n"\n        "ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL\\n"\n        "MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3\\n"\n        "LmRpZ2ljZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgSGlnaCBBc3N1cmFuY2Ug\\n"\n        "RVYgUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMbM5XPm\\n"\n        "+9S75S0tMqbf5YE/yc0lSbZxKsPVlDRnogocsF9ppkCxxLeyj9CYpKlBWTrT3JTW\\n"\n        "PNt0OKRKzE0lgvdKpVMSOO7zSW1xkX5jtqumX8OkhPhPYlG++MXs2ziS4wblCJEM\\n"\n        "xChBVfvLWokVfnHoNb9Ncgk9vjo4UFt3MRuNs8ckRZqnrG0AFFoEt7oT61EKmEFB\\n"\n        "Ik5lYYeBQVCmeVyJ3hlKV9Uu5l0cUyx+mM0aBhakaHPQNAQTXKFx01p8VdteZOE3\\n"\n        "hzBWBOURtCmAEvF5OYiiAhF8J2a3iLd48soKqDirCmTCv2ZdlYTBoSUeh10aUAsg\\n"\n        "EsxBu24LUTi4S8sCAwEAAaNjMGEwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQF\\n"\n        "MAMBAf8wHQYDVR0OBBYEFLE+w2kD+L9HAdSYJhoIAu9jZCvDMB8GA1UdIwQYMBaA\\n"\n        "FLE+w2kD+L9HAdSYJhoIAu9jZCvDMA0GCSqGSIb3DQEBBQUAA4IBAQAcGgaX3Nec\\n"\n        "nzyIZgYIVyHbIUf4KmeqvxgydkAQV8GK83rZEWWONfqe/EW1ntlMMUu4kehDLI6z\\n"\n        "eM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF\\n"\n        "hS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2\\n"\n        "Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe\\n"\n        "vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\\n"\n        "+OkuE6N36B9K\\n"\n        "-----END CERTIFICATE-----\\n";\n\n    ctx.add_certificate_authority(boost::asio::buffer(cert.data(), cert.size()), ec);\n    if (ec)\n        return;\n}\n\n} // namespace detail\n\nstatic void load_root_certificates(ssl::context &ctx) {\n    boost::system::error_code ec;\n    detail::load_root_certificates(ctx, ec);\n    if (ec)\n        throw boost::system::system_error{ ec };\n}\n印刷
\n\nHTTP/1.1 200 OK\n\n\nHTTP/1.1 200 OK\nDate: Sun, 15 Apr 2018 00:32:52 GMT\nServer: Apache/2.2.15 (CentOS)\nAccept-Ranges: bytes\nConnection: close\nTransfer-Encoding: chunked\nContent-Type: text/html; charset=UTF-8\n\n8ee3\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n\n<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">\n<head>\n  <title>Boost Software License</title>\n  <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />\n  <link rel="icon" href="/favicon.ico" type="image/ico" />\n  <link rel="stylesheet" type="text/css" href="../style-v2/section-boost.css" />\n  <!--[if IE 7]> <style type="text/css"> body { behavior: url(/style-v2/csshover3.htc); } </style> <![endif]-->\n</head><!--\nNote: Editing website content is documented at:\nhttps://www.boost.org/development/website_updating.html\n-->\n\n<body>\n  <div id="heading">\n    <div class="heading-inner">\n  <div class="heading-placard"></div>\n\n  <h1 class="heading-title">\n  <a href="/">\n  <img src="/gfx/space.png" alt= "Boost C++ Libraries" class="heading-logo" />\n  <span class="heading-boost">Boost</span>\n  <span class="heading-cpplibraries">C++ Libraries</span>\n  </a></h1>\n\n  <p class="heading-quote">\n  <q>...one of the most highly\n  regarded and expertly designed C++ library projects in the\n  world.</q> <span class="heading-attribution">— <a href=\n  "http://www.gotw.ca/" class="external">Herb Sutter</a> and <a href=\n  "http://en.wikipedia.org/wiki/Andrei_Alexandrescu" class="external">Andrei\n  Alexandrescu</a>, <a href=\n  "http://safari.awprofessional.com/?XmlId=0321113586" class="external">C++\n  Coding Standards</a></span></p>\n</div>\n\n  </div>\n\n  <div id="body">\n    <div id="body-inner">\n      <div id="content">\n        <div class="section" id="intro">\n          <div class="section-0">\n            <div class="section-title">\n              <h1>Boost Software License</h1>\n            </div>\n\n            <div class="section-body">\n              <ul class="toc">\n                <li><a href="../LICENSE_1_0.txt">License text</a></li>\n(其余部分被剪掉)
\n