'Is it possible to use keep-alive with boost::beast sync https client?

Is it possible to use http 1.1 keep-alive with the boost beast library when using a sync ssl client? I have a process that works something like this:

  1. Connection to web server using https. Send an initial request, wait for a response
  2. select()/epoll()/etc on the https native socket handle
  3. When the native handle has something ready, I issue an http::read() on the ssl_stream<beast::tcp_stream>.

The first http::read() works perfectly fine. However, when the socket is ready the next time, I get an end of stream exception when issuing an http::read() on the socket. What I want is for the socket to stay alive for multiple requests.

The code isn't too complicated, but it's in pieces:

         if( native_socket_for_https_connection_ready ) {                                                                                                                                                                                                                                                                                             
              boost::beast::http::response<boost::beast::http::dynamic_body> res;                                                                                                                                                                                                                                                                                                                                     
              boost::beast::http::read( m_HTTPSConnection, m_HTTPSBuffer, res, ec );                                                                                                                       
                                                                                                                                                                                                           
              std::cout << "Https Connection Response: " << res << std::endl;                                                                                                                              
              std::cout << "Keep-Alive Result: " << res.keep_alive() << std::endl;                                                                                                                         
                                                                                                                                                                                                           
              m_HTTPSBuffer.clear();                                                                                                                                                                       
          } 

Where, m_HTTPSConnection is a boost::beast::ssl_stream<boost::beast::tcp_stream>, and m_HTTPSBuffer is a flat_buffer. The only other detail is the extraction of the native socket.

I am trying to fit reading from a secure socket into a legacy application that is not using async I/O -- I want the subsequent reads on http::read() to complete and not throw an exception.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source