Connecting to an HTTPS server with boost :: asio

I want to connect to an HTTPS server using boost :: asio. I managed to successfully shake hands with the server, but I just can’t get the server to respond to my POST request.

This is the related code (I left out debugging and try-catch to save some space):

HTTPSClient::HTTPSClient()
{
    ssl::context context(ssl::context::sslv23);
    context.set_verify_mode(ssl::verify_peer);
    context.set_default_verify_paths();
    context.load_verify_file("certificate.pem");
    mSSLSocket = new ssl::stream<ip::tcp::socket>(mIOService, context);
}

void HTTPSClient::SendRequest(const ptree &crPTree, const std::string cHost, 
    const std::string cURI)
{
    tcp::resolver resolver(mIOService);
    tcp::resolver::query query(cHost, "https");
    resolver.async_resolve(query, boost::bind(&HTTPSClient::HandleResolve, this, 
        placeholders::error, placeholders::iterator, request));
}

void HTTPSClient::HandleResolve(const error_code &crError, 
    const iterator &criEndpoints, HTTPSRequest &rRequest)
{
    async_connect(mSSLSocket->lowest_layer(), criEndpoints, 
        boost::bind(&HTTPSClient::HandleConnect, this, placeholders::error, 
        rRequest));
}

void HTTPSClient::HandleConnect(const error_code &crError, HTTPSRequest &rRequest)
{
    mSSLSocket->lowest_layer().set_option(ip::tcp::no_delay(true));
    mSSLSocket->set_verify_callback(ssl::rfc2818_verification(rRequest.mcHost));
    mSSLSocket->handshake(ssl::stream_base::client);

    // Write the json into a stringstream
    std::ostringstream json;
    boost::property_tree::write_json(json, rRequest.mcPTree);
    std::string result;
    result = json.str();

    // Form the request
    streambuf request;
    std::ostream requestStream(&request);
    requestStream << "POST " << rRequest.mcURI << " HTTP/1.1\r\n";
    requestStream << "Host: " << rRequest.mcHost << "\r\n";
    requestStream << "Accept: application/json\r\n";
    requestStream << "Content-Type: application/json; charset=UTF-8\r\n";
    requestStream << "Content-Length: " << result.length() << "\r\n";
    requestStream << result << "\r\n\r\n";

    write(*mSSLSocket, request);

    streambuf response;
    read_until(*mSSLSocket, response, "\r\n");
    std::istream responseStream(&response);
}

read_until hangs until it gives an error read_until: End of file. Everything that has been successful before, including SSL handshaking (which I recently found out).

I did everything asynchronously until I started debugging, and started trying to return to this problem, but to no avail. It would be great if someone could help me after two painful days of debugging.

EDIT I ​​just realized that it would be useful to add the contents of requestStream after the header layout:

POST /authenticate HTTP/1.1
Host: <hostname>
Accept: application/json
Content-Type: application/json; charset=UTF-8
Content-Length: 136
{
    "username": "vijfhoek",
    "password": "test123",
    <other json content>
}
+4
2

, . - boost:: asio:: streambuf std:: ostream. POST std::string :

write(*mSSLSocket, boost::asio::buffer(requestString));

.

+1

( POST)

POST /authenticate HTTP/1.1
Host: <hostname>
Accept: application/json
Content-Type: application/json; charset=UTF-8
Content-Length: 136

{
    "username": "vijfhoek",
    "password": "test123",
    <other json content>
}

, 136 ( , Content-Length , )

, :

requestStream << "Content-Length: " << result.length() << "\r\n";
requestStream << "\r\n"; // THIS LINE ADDED
+6

Source: https://habr.com/ru/post/1529126/


All Articles