是否可以将 keep-alive 与 boost::beast 同步 https 客户端一起使用?
使用同步 ssl 客户端时,是否可以将 http 1.1 keep-alive 与 boosteast 库一起使用?我有一个类似如下的流程:
- 使用 https 连接到 Web 服务器。发送初始请求,等待
- https 本机套接字句柄上的响应 select()/epoll()/etc
- 当本机句柄准备就绪时,我在
ssl_stream
。
第一个 http::read() 工作得很好。但是,当套接字下次准备好时,在套接字上发出 http::read() 时会出现流结束异常。我想要的是套接字对于多个请求保持活动状态。
代码并不太复杂,但它是分成几部分的:
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();
}
其中,m_HTTPSConnection
是 boost::beast::ssl_stream
,并且m_HTTPSBuffer
是一个flat_buffer
。唯一的其他细节是本机套接字的提取。
我正在尝试将从安全套接字读取数据放入不使用异步 I/O 的遗留应用程序中——我希望 http::read() 上的后续读取能够完成并且不会引发异常。
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:
- Connection to web server using https. Send an initial request, wait for a response
- select()/epoll()/etc on the https native socket handle
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
服务器可能会断开连接,例如,当您的请求可能具有不确定的内容长度时,从而无法保持连接打开(服务器如何知道请求不完整?)。
所以,想象一些更完整的代码:
1a。使用 https
1b 连接到 Web 服务器。发送初始请求
2. 然后等待响应(select/epoll):
3. 在流上读取问题
这一切都运行良好:
Live On Coliru
打印
The server could be dropping the connection, e.g. when your request maybe of indeterminate content-length, making it impossible to keep the connection open (how would the server know that the request was incomplete?).
So, just imagining some more complete code:
1a. connection to web server using https
1b. send an initial request
2. Then wait for a response (select/epoll):
3. Issue read on the stream
It all works nicely:
Live On Coliru
Prints