Friday, February 13, 2015

Boost.Asio HTTPS client

The boost asio official documentation gives two examples of making HTTP requests and using SSL for a connection. However, none of these examples do a HTTPS GET request.
In other words, if you want to make a HTTP GET request over a secure connection, you need to combine the two examples.
The links to the examples are http client and ssl client.


Let's see the difference between a HTTP client and a HTTPS client.
First, for the HTTP client the asio "flow", as per the http client example in the link above, is as follows:

  1. Resolve
  2. Connect
  3. Send request
  4. Receive response (process status, headers, body, etc.)
For a HTTPS client, a few modifications are required. First, regarding the code, one needs an ssl context boost::asio::ssl::context and the socket must support ssl. I've used  boost::asio::ssl::stream<boost::asio::ip::tcp::socket>.
The application flow changes also:
  1. Resolve (use https for the resolve query)
  2. Certificate verification
  3. Connect
  4. SSL handshake (via async_handshake)
  5. Send request
  6. Receive response  (process status, headers, body, etc.)
So, a few additional steps are required to implement the SSL handshake and send requests only after the handshake is completed.
A full source code example of a HTTPS client is availale here: https://github.com/alexandruc/SimpleHttpsClient 


1 comment: