0
votes

So I have this code that works just fine using boost::asio to connect with ssl:

Opened up Fiddler, but I don't see those connections at all. From what I've read I gathered that I must pass the requests through the proxy. So I tried that with all the examples and answers I could find but I couldn't make it work.

the best I got was that the handshake never fired the callback.

What am I missing?

void performHTTPRequest(std::string host, std::string path) {
    std::ostream request_stream(&mRequest);
    request_stream << "GET " << path << " HTTP/1.1\r\n";
    request_stream << "Host: " << host << "\r\n";
    request_stream << "Accept: */*\r\n";
    request_stream << "Connection: close\r\n\r\n";
    tcp::resolver::query query(host, "https");
    mResolver.async_resolve(query,
        boost::bind(handle_resolve,
        this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::iterator));
}

void handle_resolve(const boost::system::error_code& err,
        tcp::resolver::iterator endpoint_iterator)
{
    if (!err)
    {
        boost::asio::async_connect(mSocket.lowest_layer(), 
            endpoint_iterator,
            boost::bind(&handle_connect, this,
            boost::asio::placeholders::error));
    }
    else
    {
        delete this;
    }
}

void handle_connect(const boost::system::error_code& err)
{
    if (!err)
    {
        mSocket.async_handshake(
            boost::asio::ssl::stream_base::handshake_type::client,
            boost::bind(&handle_handshake, this,
            boost::asio::placeholders::error));
    }
    else
    {
        delete this;
    }
}


void handle_handshake(const boost::system::error_code& err)
{
    if (!err)
    {
        boost::asio::async_write(mSocket, mRequest,
            boost::bind(&handle_write_request, this,
            boost::asio::placeholders::error));
    }
    else
    {
        delete this;
    }
}

void handle_write_request(const boost::system::error_code& err)
{
    if (!err)
    {
        boost::asio::async_read_until(mSocket, mResponse, "\r\n",
            boost::bind(&handle_read_status_line, this,
            boost::asio::placeholders::error));
    }
    else
    {
        delete this;
    }
}

void handle_read_status_line(const boost::system::error_code& err)
{
    if (!err)
    {
        std::istream response_stream(&mResponse);
        std::string http_version;
        response_stream >> http_version;
        unsigned int status_code;
        response_stream >> status_code;
        std::string status_message;
        std::getline(response_stream, status_message);
        if (!response_stream || http_version.substr(0, 5) != "HTTP/")
        {
            return;
        }
        if (status_code != 200)
        {
            return;
        }
    }
    else
    {
        delete this;
    }
}
1
Do you call ioservice::run() at any point? Just asking, since this is a common mistake. - Michaƫl Roy
Put some debugging messages in every handler and let us know how far you get, especially what endpoint(s) or IP addresses the host gets resolved to. But since you say that Fiddler doesn't show anything, my guess is the connect is failing as well. Also, remove Fiddler from the equation and use Wireshark/tcpdump instead for the testing. - Tom Trebicky

1 Answers

1
votes

It appears that just changing this line:

tcp::resolver::query query(host, "https");

With this:

tcp::resolver::query query(proxyUrl, proxyPort);

Did the trick.

The problem was with the proxy configuration which did not allow HTTPS communication.