是否可以使用keep-alive with boost::beast sync https客户端



在使用同步ssl客户端时,是否可以将http 1.1与boost beast库一起使用?我有一个类似的过程:

  1. 使用https连接到web服务器。发送初始请求,等待响应
  2. https本地套接字句柄上的select((/epoll((/etc
  3. 当本机句柄准备就绪时,我在ssl_stream<beast::tcp_stream>上发出一个http::read((

第一个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_HTTPSConnectionboost::beast::ssl_stream<boost::beast::tcp_stream>m_HTTPSBufferflat_buffer。唯一的其他细节是本机套接字的提取。

我正在尝试将来自安全套接字的读取放入不使用异步I/O的遗留应用程序中——我希望http::read((上的后续读取能够完成,而不会引发异常。

服务器可能会断开连接,例如,当您的请求可能具有不确定的内容长度时,使连接无法保持打开状态(服务器怎么知道请求不完整?(。

所以,想象一下更完整的代码:

1a。使用https 连接到web服务器

ssl::context ctx(ssl::context::sslv23);
beast::ssl_stream<beast::tcp_stream> s(ioc, ctx);
s.next_layer().connect({address_v4{{34, 227, 133, 25}}, 443});
s.handshake(ssl::stream_base::handshake_type::client);

1b。发送初始请求

http::request<http::string_body> req(http::verb::post, "/post", 10,
R"~({"data":42})~");
req.set(http::field::host, "httpbin.org");
req.keep_alive(true); // no difference on HTTP/1.1+
req.prepare_payload();
write(s, req);

2.然后等待响应(选择/epoll(:

std::future<void> ready = s.next_layer().socket().async_wait(
net::socket_base::wait_type::wait_read, net::use_future);
ready.wait();

(显然,这可以通过基于native_handle()的外部代码来实现(

3.在流上读取问题

http::response<http::dynamic_body> res;
beast::flat_buffer                 buf;
http::read(s, buf, res);

一切都很好:

在Coliru上直播

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/beast.hpp>
#include <boost/beast/ssl.hpp>
#include <iostream>
namespace net   = boost::asio;
namespace beast = boost::beast;
namespace http  = beast::http;
namespace ssl   = net::ssl;
using boost::system::error_code;
using net::ip::address_v4;
using net::ip::tcp;
using namespace std::chrono_literals;
using std::this_thread::sleep_for;
static auto now = std::chrono::steady_clock::now;
int main() {
net::thread_pool ioc;
// connection to web server using https
ssl::context ctx(ssl::context::sslv23);
beast::ssl_stream<beast::tcp_stream> s(ioc, ctx);
s.next_layer().connect({address_v4{{34, 227, 133, 25}}, 443});
s.handshake(ssl::stream_base::handshake_type::client);
for (auto demo_end = now() + 1s; now() <= demo_end; sleep_for(10ms)) {
// send an initial request
http::request<http::string_body> req(http::verb::post, "/post", 10,
R"~({"data":42})~");
req.set(http::field::host, "httpbin.org");
req.keep_alive(true); // no difference on HTTP/1.1+
req.prepare_payload();
write(s, req);
// wait for a response
// (obviously, this can be done by external code based on the
// native_handle() instead)
std::future<void> ready = s.next_layer().socket().async_wait(
net::socket_base::wait_type::wait_read, net::use_future);
ready.wait();
// issue read on the stream
http::response<http::dynamic_body> res;
beast::flat_buffer                 buf;
http::read(s, buf, res);
std::cout << "Https Connection Response: " << res << std::endl;
std::cout << "Keep-Alive Result: " << res.keep_alive() << std::endl;
if (!res.keep_alive())
break;
}
// bye
ioc.join();
}

打印

Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:50 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00a-124910e0751e35dd54340615"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:50 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00a-5fa263b33e2cc5937d6204a5"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:50 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00a-0430cdac1b7924210e9ea13d"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:50 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00a-7683e03c727619fc5e5db850"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:50 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00a-5c733376197f2e0d68afb4c8"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:50 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00a-604db5aa0f4abccc4c721740"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:50 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00a-3ea4400e52caa293041cd83f"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-05d2949e37556119199d2627"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-57e377492da2d9f26c205699"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-526a207d11c3aa9d231ec537"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-64f2d0df238d5d14388390db"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-1612613c2520b2a908706e58"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-00d80af2769dbc023e8ab652"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-505398b91878748a45d04795"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-087616e737a56957076e0a9c"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-31987540136993062764c373"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-37efb18d7fdc07ef1d4051cf"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-666c753e62ff049e375253d3"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-524b4fea230a31273a1264e6"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-4352dd83652fa4d74d8910e8"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
Https Connection Response: HTTP/1.1 200 OK
Date: Thu, 03 Mar 2022 16:42:51 GMT
Content-Type: application/json
Content-Length: 321
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {}, 
"data": "{"data":42}", 
"files": {}, 
"form": {}, 
"headers": {
"Content-Length": "11", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-6220f00b-36f9eb3918e3fbf215e0c5a7"
}, 
"json": {
"data": 42
}, 
"origin": "173.203.57.63", 
"url": "https://httpbin.org/post"
}
Keep-Alive Result: 1
This file can be also found using the Coliru command line: cat /Archive2/c2/f4e2478d335b42/main.cpp

最新更新