Use timeouts in HTTP server examples

This commit is contained in:
Vinnie Falco
2019-02-09 20:26:48 -08:00
parent 9b14774ada
commit 5b97fbb966
8 changed files with 225 additions and 251 deletions

View File

@@ -2,6 +2,7 @@ Version 213:
* Fix posix_file::close handling of EINTR * Fix posix_file::close handling of EINTR
* basic_stream subsumes stranded_stream: * basic_stream subsumes stranded_stream:
* Use timeouts in HTTP server examples
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@@ -18,9 +18,7 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/http.hpp> #include <boost/beast/http.hpp>
#include <boost/beast/version.hpp> #include <boost/beast/version.hpp>
#include <boost/asio/bind_executor.hpp> #include <boost/beast/_experimental/core/ssl_stream.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/asio/strand.hpp> #include <boost/asio/strand.hpp>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <algorithm> #include <algorithm>
@@ -250,21 +248,16 @@ class session : public std::enable_shared_from_this<session>
http::async_write( http::async_write(
self_.stream_, self_.stream_,
*sp, *sp,
net::bind_executor(
self_.strand_,
std::bind( std::bind(
&session::on_write, &session::on_write,
self_.shared_from_this(), self_.shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
sp->need_eof()))); sp->need_eof()));
} }
}; };
tcp::socket socket_; beast::ssl_stream<beast::tcp_stream<net::io_context::strand>> stream_;
ssl::stream<tcp::socket&> stream_;
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_; beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_; std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_; http::request<http::string_body> req_;
@@ -275,12 +268,10 @@ public:
// Take ownership of the socket // Take ownership of the socket
explicit explicit
session( session(
tcp::socket socket, tcp::socket&& socket,
ssl::context& ctx, ssl::context& ctx,
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: socket_(std::move(socket)) : stream_(std::move(socket), ctx)
, stream_(socket_, ctx)
, strand_(socket_.get_executor())
, doc_root_(doc_root) , doc_root_(doc_root)
, lambda_(*this) , lambda_(*this)
{ {
@@ -290,15 +281,16 @@ public:
void void
run() run()
{ {
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Perform the SSL handshake // Perform the SSL handshake
stream_.async_handshake( stream_.async_handshake(
ssl::stream_base::server, ssl::stream_base::server,
net::bind_executor(
strand_,
std::bind( std::bind(
&session::on_handshake, &session::on_handshake,
shared_from_this(), shared_from_this(),
std::placeholders::_1))); std::placeholders::_1));
} }
void void
@@ -317,15 +309,16 @@ public:
// otherwise the operation behavior is undefined. // otherwise the operation behavior is undefined.
req_ = {}; req_ = {};
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Read a request // Read a request
http::async_read(stream_, buffer_, req_, http::async_read(stream_, buffer_, req_,
net::bind_executor(
strand_,
std::bind( std::bind(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2))); std::placeholders::_2));
} }
void void
@@ -374,14 +367,15 @@ public:
void void
do_close() do_close()
{ {
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Perform the SSL shutdown // Perform the SSL shutdown
stream_.async_shutdown( stream_.async_shutdown(
net::bind_executor(
strand_,
std::bind( std::bind(
&session::on_shutdown, &session::on_shutdown,
shared_from_this(), shared_from_this(),
std::placeholders::_1))); std::placeholders::_1));
} }
void void
@@ -401,7 +395,6 @@ class listener : public std::enable_shared_from_this<listener>
{ {
ssl::context& ctx_; ssl::context& ctx_;
tcp::acceptor acceptor_; tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_; std::shared_ptr<std::string const> doc_root_;
public: public:
@@ -412,7 +405,6 @@ public:
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: ctx_(ctx) : ctx_(ctx)
, acceptor_(ioc) , acceptor_(ioc)
, socket_(ioc)
, doc_root_(doc_root) , doc_root_(doc_root)
{ {
beast::error_code ec; beast::error_code ec;
@@ -464,15 +456,15 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
socket_,
std::bind( std::bind(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this(),
std::placeholders::_1)); std::placeholders::_1,
std::placeholders::_2));
} }
void void
on_accept(beast::error_code ec) on_accept(beast::error_code ec, tcp::socket socket)
{ {
if(ec) if(ec)
{ {
@@ -482,7 +474,7 @@ public:
{ {
// Create the session and run it // Create the session and run it
std::make_shared<session>( std::make_shared<session>(
std::move(socket_), std::move(socket),
ctx_, ctx_,
doc_root_)->run(); doc_root_)->run();
} }

View File

@@ -16,8 +16,6 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/http.hpp> #include <boost/beast/http.hpp>
#include <boost/beast/version.hpp> #include <boost/beast/version.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/strand.hpp> #include <boost/asio/strand.hpp>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <algorithm> #include <algorithm>
@@ -244,22 +242,18 @@ class session : public std::enable_shared_from_this<session>
// Write the response // Write the response
http::async_write( http::async_write(
self_.socket_, self_.stream_,
*sp, *sp,
net::bind_executor(
self_.strand_,
std::bind( std::bind(
&session::on_write, &session::on_write,
self_.shared_from_this(), self_.shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
sp->need_eof()))); sp->need_eof()));
} }
}; };
tcp::socket socket_; beast::tcp_stream<net::io_context::strand> stream_;
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_; beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_; std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_; http::request<http::string_body> req_;
@@ -268,12 +262,10 @@ class session : public std::enable_shared_from_this<session>
public: public:
// Take ownership of the socket // Take ownership of the socket
explicit
session( session(
tcp::socket socket, tcp::socket&& socket,
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: socket_(std::move(socket)) : stream_(std::move(socket))
, strand_(socket_.get_executor())
, doc_root_(doc_root) , doc_root_(doc_root)
, lambda_(*this) , lambda_(*this)
{ {
@@ -293,15 +285,16 @@ public:
// otherwise the operation behavior is undefined. // otherwise the operation behavior is undefined.
req_ = {}; req_ = {};
// Set the timeout.
stream_.expires_after(std::chrono::seconds(30));
// Read a request // Read a request
http::async_read(socket_, buffer_, req_, http::async_read(stream_, buffer_, req_,
net::bind_executor(
strand_,
std::bind( std::bind(
&session::on_read, &session::on_read,
shared_from_this(), shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2))); std::placeholders::_2));
} }
void void
@@ -352,7 +345,7 @@ public:
{ {
// Send a TCP shutdown // Send a TCP shutdown
beast::error_code ec; beast::error_code ec;
socket_.shutdown(tcp::socket::shutdown_send, ec); stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
// At this point the connection is closed gracefully // At this point the connection is closed gracefully
} }
@@ -364,7 +357,6 @@ public:
class listener : public std::enable_shared_from_this<listener> class listener : public std::enable_shared_from_this<listener>
{ {
tcp::acceptor acceptor_; tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_; std::shared_ptr<std::string const> doc_root_;
public: public:
@@ -373,7 +365,6 @@ public:
tcp::endpoint endpoint, tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: acceptor_(ioc) : acceptor_(ioc)
, socket_(ioc)
, doc_root_(doc_root) , doc_root_(doc_root)
{ {
beast::error_code ec; beast::error_code ec;
@@ -425,15 +416,15 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
socket_,
std::bind( std::bind(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this(),
std::placeholders::_1)); std::placeholders::_1,
std::placeholders::_2));
} }
void void
on_accept(beast::error_code ec) on_accept(beast::error_code ec, tcp::socket socket)
{ {
if(ec) if(ec)
{ {
@@ -443,7 +434,7 @@ public:
{ {
// Create the session and run it // Create the session and run it
std::make_shared<session>( std::make_shared<session>(
std::move(socket_), std::move(socket),
doc_root_)->run(); doc_root_)->run();
} }

View File

@@ -18,9 +18,8 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/http.hpp> #include <boost/beast/http.hpp>
#include <boost/beast/version.hpp> #include <boost/beast/version.hpp>
#include <boost/asio/ip/tcp.hpp> #include <boost/beast/_experimental/core/ssl_stream.hpp>
#include <boost/asio/spawn.hpp> #include <boost/asio/spawn.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <algorithm> #include <algorithm>
#include <cstdlib> #include <cstdlib>
@@ -208,6 +207,11 @@ handle_request(
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// The type of stream to use
// Stackful coroutines are already stranded.
using stream_type =
beast::ssl_stream<beast::tcp_stream<net::io_context::executor_type>>;
// Report a failure // Report a failure
void void
fail(beast::error_code ec, char const* what) fail(beast::error_code ec, char const* what)
@@ -217,17 +221,15 @@ fail(beast::error_code ec, char const* what)
// This is the C++11 equivalent of a generic lambda. // This is the C++11 equivalent of a generic lambda.
// The function object is used to send an HTTP message. // The function object is used to send an HTTP message.
template<class Stream>
struct send_lambda struct send_lambda
{ {
Stream& stream_; stream_type& stream_;
bool& close_; bool& close_;
beast::error_code& ec_; beast::error_code& ec_;
net::yield_context yield_; net::yield_context yield_;
explicit
send_lambda( send_lambda(
Stream& stream, stream_type& stream,
bool& close, bool& close,
beast::error_code& ec, beast::error_code& ec,
net::yield_context yield) net::yield_context yield)
@@ -256,16 +258,15 @@ struct send_lambda
// Handles an HTTP server connection // Handles an HTTP server connection
void void
do_session( do_session(
tcp::socket& socket, stream_type& stream,
ssl::context& ctx,
std::shared_ptr<std::string const> const& doc_root, std::shared_ptr<std::string const> const& doc_root,
net::yield_context yield) net::yield_context yield)
{ {
bool close = false; bool close = false;
beast::error_code ec; beast::error_code ec;
// Construct the stream around the socket // Set the timeout.
ssl::stream<tcp::socket&> stream{socket, ctx}; beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
// Perform the SSL handshake // Perform the SSL handshake
stream.async_handshake(ssl::stream_base::server, yield[ec]); stream.async_handshake(ssl::stream_base::server, yield[ec]);
@@ -276,10 +277,13 @@ do_session(
beast::flat_buffer buffer; beast::flat_buffer buffer;
// This lambda is used to send messages // This lambda is used to send messages
send_lambda<ssl::stream<tcp::socket&>> lambda{stream, close, ec, yield}; send_lambda lambda{stream, close, ec, yield};
for(;;) for(;;)
{ {
// Set the timeout.
beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
// Read a request // Read a request
http::request<http::string_body> req; http::request<http::string_body> req;
http::async_read(stream, buffer, req, yield[ec]); http::async_read(stream, buffer, req, yield[ec]);
@@ -300,6 +304,9 @@ do_session(
} }
} }
// Set the timeout.
beast::get_lowest_layer(stream).expires_after(std::chrono::seconds(30));
// Perform the SSL shutdown // Perform the SSL shutdown
stream.async_shutdown(yield[ec]); stream.async_shutdown(yield[ec]);
if(ec) if(ec)
@@ -353,8 +360,7 @@ do_listen(
acceptor.get_executor().context(), acceptor.get_executor().context(),
std::bind( std::bind(
&do_session, &do_session,
std::move(socket), stream_type(std::move(socket), ctx),
std::ref(ctx),
doc_root, doc_root,
std::placeholders::_1)); std::placeholders::_1));
} }

View File

@@ -204,6 +204,10 @@ handle_request(
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// The type of stream to use.
// Stackful coroutines are already stranded.
using stream_type = beast::tcp_stream<net::io_context::executor_type>;
// Report a failure // Report a failure
void void
fail(beast::error_code ec, char const* what) fail(beast::error_code ec, char const* what)
@@ -213,17 +217,15 @@ fail(beast::error_code ec, char const* what)
// This is the C++11 equivalent of a generic lambda. // This is the C++11 equivalent of a generic lambda.
// The function object is used to send an HTTP message. // The function object is used to send an HTTP message.
template<class Stream>
struct send_lambda struct send_lambda
{ {
Stream& stream_; stream_type& stream_;
bool& close_; bool& close_;
beast::error_code& ec_; beast::error_code& ec_;
net::yield_context yield_; net::yield_context yield_;
explicit
send_lambda( send_lambda(
Stream& stream, stream_type& stream,
bool& close, bool& close,
beast::error_code& ec, beast::error_code& ec,
net::yield_context yield) net::yield_context yield)
@@ -252,7 +254,7 @@ struct send_lambda
// Handles an HTTP server connection // Handles an HTTP server connection
void void
do_session( do_session(
tcp::socket& socket, stream_type& stream,
std::shared_ptr<std::string const> const& doc_root, std::shared_ptr<std::string const> const& doc_root,
net::yield_context yield) net::yield_context yield)
{ {
@@ -263,13 +265,16 @@ do_session(
beast::flat_buffer buffer; beast::flat_buffer buffer;
// This lambda is used to send messages // This lambda is used to send messages
send_lambda<tcp::socket> lambda{socket, close, ec, yield}; send_lambda lambda{stream, close, ec, yield};
for(;;) for(;;)
{ {
// Set the timeout.
stream.expires_after(std::chrono::seconds(30));
// Read a request // Read a request
http::request<http::string_body> req; http::request<http::string_body> req;
http::async_read(socket, buffer, req, yield[ec]); http::async_read(stream, buffer, req, yield[ec]);
if(ec == http::error::end_of_stream) if(ec == http::error::end_of_stream)
break; break;
if(ec) if(ec)
@@ -288,7 +293,7 @@ do_session(
} }
// Send a TCP shutdown // Send a TCP shutdown
socket.shutdown(tcp::socket::shutdown_send, ec); stream.socket().shutdown(tcp::socket::shutdown_send, ec);
// At this point the connection is closed gracefully // At this point the connection is closed gracefully
} }
@@ -337,7 +342,7 @@ do_listen(
acceptor.get_executor().context(), acceptor.get_executor().context(),
std::bind( std::bind(
&do_session, &do_session,
std::move(socket), stream_type(std::move(socket)),
doc_root, doc_root,
std::placeholders::_1)); std::placeholders::_1));
} }

View File

@@ -19,9 +19,7 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/http.hpp> #include <boost/beast/http.hpp>
#include <boost/beast/version.hpp> #include <boost/beast/version.hpp>
#include <boost/asio/bind_executor.hpp> #include <boost/beast/_experimental/core/ssl_stream.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/asio/strand.hpp> #include <boost/asio/strand.hpp>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <algorithm> #include <algorithm>
@@ -210,6 +208,13 @@ handle_request(
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// The type of plain streams
using plain_stream_type = beast::tcp_stream<net::io_context::strand>;
// The type of TLS streams
using ssl_stream_type =
beast::ssl_stream<beast::tcp_stream<net::io_context::strand>>;
// Report a failure // Report a failure
void void
fail(beast::error_code ec, char const* what) fail(beast::error_code ec, char const* what)
@@ -261,14 +266,12 @@ class session
http::async_write( http::async_write(
self_.derived().stream(), self_.derived().stream(),
*sp, *sp,
net::bind_executor(
self_.strand_,
std::bind( std::bind(
&session::on_write, &session::on_write,
self_.derived().shared_from_this(), self_.derived().shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
sp->need_eof()))); sp->need_eof()));
} }
}; };
@@ -278,20 +281,15 @@ class session
send_lambda lambda_; send_lambda lambda_;
protected: protected:
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_; beast::flat_buffer buffer_;
public: public:
// Take ownership of the buffer // Take ownership of the buffer
explicit
session( session(
net::io_context& ioc,
beast::flat_buffer buffer, beast::flat_buffer buffer,
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: doc_root_(doc_root) : doc_root_(doc_root)
, lambda_(*this) , lambda_(*this)
, strand_(ioc.get_executor())
, buffer_(std::move(buffer)) , buffer_(std::move(buffer))
{ {
} }
@@ -299,18 +297,20 @@ public:
void void
do_read() do_read()
{ {
// Set the timeout.
beast::get_lowest_layer(
derived().stream()).expires_after(std::chrono::seconds(30));
// Read a request // Read a request
http::async_read( http::async_read(
derived().stream(), derived().stream(),
buffer_, buffer_,
req_, req_,
net::bind_executor(
strand_,
std::bind( std::bind(
&session::on_read, &session::on_read,
derived().shared_from_this(), derived().shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2))); std::placeholders::_2));
} }
void void
@@ -362,30 +362,26 @@ class plain_session
: public session<plain_session> : public session<plain_session>
, public std::enable_shared_from_this<plain_session> , public std::enable_shared_from_this<plain_session>
{ {
tcp::socket socket_; plain_stream_type stream_;
net::strand<
net::io_context::executor_type> strand_;
public: public:
// Create the session // Create the session
plain_session( plain_session(
tcp::socket socket, tcp::socket&& socket,
beast::flat_buffer buffer, beast::flat_buffer buffer,
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: session<plain_session>( : session<plain_session>(
socket.get_executor().context(),
std::move(buffer), std::move(buffer),
doc_root) doc_root)
, socket_(std::move(socket)) , stream_(std::move(socket))
, strand_(socket_.get_executor())
{ {
} }
// Called by the base class // Called by the base class
tcp::socket& plain_stream_type&
stream() stream()
{ {
return socket_; return stream_;
} }
// Start the asynchronous operation // Start the asynchronous operation
@@ -400,7 +396,7 @@ public:
{ {
// Send a TCP shutdown // Send a TCP shutdown
beast::error_code ec; beast::error_code ec;
socket_.shutdown(tcp::socket::shutdown_send, ec); stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
// At this point the connection is closed gracefully // At this point the connection is closed gracefully
} }
@@ -411,30 +407,24 @@ class ssl_session
: public session<ssl_session> : public session<ssl_session>
, public std::enable_shared_from_this<ssl_session> , public std::enable_shared_from_this<ssl_session>
{ {
tcp::socket socket_; ssl_stream_type stream_;
ssl::stream<tcp::socket&> stream_;
net::strand<
net::io_context::executor_type> strand_;
public: public:
// Create the session // Create the session
ssl_session( ssl_session(
tcp::socket socket, tcp::socket&& socket,
ssl::context& ctx, ssl::context& ctx,
beast::flat_buffer buffer, beast::flat_buffer buffer,
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: session<ssl_session>( : session<ssl_session>(
socket.get_executor().context(),
std::move(buffer), std::move(buffer),
doc_root) doc_root)
, socket_(std::move(socket)) , stream_(std::move(socket), ctx)
, stream_(socket_, ctx)
, strand_(stream_.get_executor())
{ {
} }
// Called by the base class // Called by the base class
ssl::stream<tcp::socket&>& ssl_stream_type&
stream() stream()
{ {
return stream_; return stream_;
@@ -444,18 +434,19 @@ public:
void void
run() run()
{ {
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Perform the SSL handshake // Perform the SSL handshake
// Note, this is the buffered version of the handshake. // Note, this is the buffered version of the handshake.
stream_.async_handshake( stream_.async_handshake(
ssl::stream_base::server, ssl::stream_base::server,
buffer_.data(), buffer_.data(),
net::bind_executor(
strand_,
std::bind( std::bind(
&ssl_session::on_handshake, &ssl_session::on_handshake,
shared_from_this(), shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2))); std::placeholders::_2));
} }
void void
on_handshake( on_handshake(
@@ -474,14 +465,15 @@ public:
void void
do_eof() do_eof()
{ {
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Perform the SSL shutdown // Perform the SSL shutdown
stream_.async_shutdown( stream_.async_shutdown(
net::bind_executor(
strand_,
std::bind( std::bind(
&ssl_session::on_shutdown, &ssl_session::on_shutdown,
shared_from_this(), shared_from_this(),
std::placeholders::_1))); std::placeholders::_1));
} }
void void
@@ -499,22 +491,18 @@ public:
// Detects SSL handshakes // Detects SSL handshakes
class detect_session : public std::enable_shared_from_this<detect_session> class detect_session : public std::enable_shared_from_this<detect_session>
{ {
tcp::socket socket_; plain_stream_type stream_;
ssl::context& ctx_; ssl::context& ctx_;
net::strand<
net::io_context::executor_type> strand_;
std::shared_ptr<std::string const> doc_root_; std::shared_ptr<std::string const> doc_root_;
beast::flat_buffer buffer_; beast::flat_buffer buffer_;
public: public:
explicit
detect_session( detect_session(
tcp::socket socket, tcp::socket socket,
ssl::context& ctx, ssl::context& ctx,
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: socket_(std::move(socket)) : stream_(std::move(socket))
, ctx_(ctx) , ctx_(ctx)
, strand_(socket_.get_executor())
, doc_root_(doc_root) , doc_root_(doc_root)
{ {
} }
@@ -523,17 +511,18 @@ public:
void void
run() run()
{ {
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Detect a TLS handshake
async_detect_ssl( async_detect_ssl(
socket_, stream_,
buffer_, buffer_,
net::bind_executor(
strand_,
std::bind( std::bind(
&detect_session::on_detect, &detect_session::on_detect,
shared_from_this(), shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2))); std::placeholders::_2));
} }
void void
@@ -546,7 +535,7 @@ public:
{ {
// Launch SSL session // Launch SSL session
std::make_shared<ssl_session>( std::make_shared<ssl_session>(
std::move(socket_), stream_.release_socket(),
ctx_, ctx_,
std::move(buffer_), std::move(buffer_),
doc_root_)->run(); doc_root_)->run();
@@ -555,7 +544,7 @@ public:
// Launch plain session // Launch plain session
std::make_shared<plain_session>( std::make_shared<plain_session>(
std::move(socket_), stream_.release_socket(),
std::move(buffer_), std::move(buffer_),
doc_root_)->run(); doc_root_)->run();
} }
@@ -565,10 +554,7 @@ public:
class listener : public std::enable_shared_from_this<listener> class listener : public std::enable_shared_from_this<listener>
{ {
ssl::context& ctx_; ssl::context& ctx_;
net::strand<
net::io_context::executor_type> strand_;
tcp::acceptor acceptor_; tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_; std::shared_ptr<std::string const> doc_root_;
public: public:
@@ -578,9 +564,7 @@ public:
tcp::endpoint endpoint, tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: ctx_(ctx) : ctx_(ctx)
, strand_(ioc.get_executor())
, acceptor_(ioc) , acceptor_(ioc)
, socket_(ioc)
, doc_root_(doc_root) , doc_root_(doc_root)
{ {
beast::error_code ec; beast::error_code ec;
@@ -632,15 +616,15 @@ public:
do_accept() do_accept()
{ {
acceptor_.async_accept( acceptor_.async_accept(
socket_,
std::bind( std::bind(
&listener::on_accept, &listener::on_accept,
shared_from_this(), shared_from_this(),
std::placeholders::_1)); std::placeholders::_1,
std::placeholders::_2));
} }
void void
on_accept(beast::error_code ec) on_accept(beast::error_code ec, tcp::socket sock)
{ {
if(ec) if(ec)
{ {
@@ -650,7 +634,7 @@ public:
{ {
// Create the detector session and run it // Create the detector session and run it
std::make_shared<detect_session>( std::make_shared<detect_session>(
std::move(socket_), std::move(sock),
ctx_, ctx_,
doc_root_)->run(); doc_root_)->run();
} }

View File

@@ -18,10 +18,8 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/http.hpp> #include <boost/beast/http.hpp>
#include <boost/beast/version.hpp> #include <boost/beast/version.hpp>
#include <boost/asio/bind_executor.hpp> #include <boost/beast/_experimental/core/ssl_stream.hpp>
#include <boost/asio/coroutine.hpp> #include <boost/asio/coroutine.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/asio/strand.hpp> #include <boost/asio/strand.hpp>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <algorithm> #include <algorithm>
@@ -211,6 +209,10 @@ handle_request(
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// The type of TLS streams
using ssl_stream_type =
beast::ssl_stream<beast::tcp_stream<net::io_context::strand>>;
// Report a failure // Report a failure
void void
fail(beast::error_code ec, char const* what) fail(beast::error_code ec, char const* what)
@@ -253,21 +255,16 @@ class session
http::async_write( http::async_write(
self_.stream_, self_.stream_,
*sp, *sp,
net::bind_executor(
self_.strand_,
std::bind( std::bind(
&session::loop, &session::loop,
self_.shared_from_this(), self_.shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
sp->need_eof()))); sp->need_eof()));
} }
}; };
tcp::socket socket_; ssl_stream_type stream_;
ssl::stream<tcp::socket&> stream_;
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_; beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_; std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_; http::request<http::string_body> req_;
@@ -278,12 +275,10 @@ public:
// Take ownership of the socket // Take ownership of the socket
explicit explicit
session( session(
tcp::socket socket, tcp::socket&& socket,
ssl::context& ctx, ssl::context& ctx,
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: socket_(std::move(socket)) : stream_(std::move(socket), ctx)
, stream_(socket_, ctx)
, strand_(socket_.get_executor())
, doc_root_(doc_root) , doc_root_(doc_root)
, lambda_(*this) , lambda_(*this)
{ {
@@ -306,36 +301,38 @@ public:
boost::ignore_unused(bytes_transferred); boost::ignore_unused(bytes_transferred);
reenter(*this) reenter(*this)
{ {
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Perform the SSL handshake // Perform the SSL handshake
yield stream_.async_handshake( yield stream_.async_handshake(
ssl::stream_base::server, ssl::stream_base::server,
net::bind_executor(
strand_,
std::bind( std::bind(
&session::loop, &session::loop,
shared_from_this(), shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
0, 0,
false))); false));
if(ec) if(ec)
return fail(ec, "handshake"); return fail(ec, "handshake");
for(;;) for(;;)
{ {
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Make the request empty before reading, // Make the request empty before reading,
// otherwise the operation behavior is undefined. // otherwise the operation behavior is undefined.
req_ = {}; req_ = {};
// Read a request // Read a request
yield http::async_read(stream_, buffer_, req_, yield http::async_read(stream_, buffer_, req_,
net::bind_executor(
strand_,
std::bind( std::bind(
&session::loop, &session::loop,
shared_from_this(), shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
false))); false));
if(ec == http::error::end_of_stream) if(ec == http::error::end_of_stream)
{ {
// The remote host closed the connection // The remote host closed the connection
@@ -359,16 +356,17 @@ public:
res_ = nullptr; res_ = nullptr;
} }
// Set the timeout.
beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30));
// Perform the SSL shutdown // Perform the SSL shutdown
yield stream_.async_shutdown( yield stream_.async_shutdown(
net::bind_executor(
strand_,
std::bind( std::bind(
&session::loop, &session::loop,
shared_from_this(), shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
0, 0,
false))); false));
if(ec) if(ec)
return fail(ec, "shutdown"); return fail(ec, "shutdown");

View File

@@ -16,9 +16,7 @@
#include <boost/beast/core.hpp> #include <boost/beast/core.hpp>
#include <boost/beast/http.hpp> #include <boost/beast/http.hpp>
#include <boost/beast/version.hpp> #include <boost/beast/version.hpp>
#include <boost/asio/bind_executor.hpp>
#include <boost/asio/coroutine.hpp> #include <boost/asio/coroutine.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/strand.hpp> #include <boost/asio/strand.hpp>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <algorithm> #include <algorithm>
@@ -207,6 +205,9 @@ handle_request(
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// The type of stream to use
using stream_type = beast::tcp_stream<net::io_context::strand>;
// Report a failure // Report a failure
void void
fail(beast::error_code ec, char const* what) fail(beast::error_code ec, char const* what)
@@ -248,22 +249,18 @@ class session
// Write the response // Write the response
http::async_write( http::async_write(
self_.socket_, self_.stream_,
*sp, *sp,
net::bind_executor(
self_.strand_,
std::bind( std::bind(
&session::loop, &session::loop,
self_.shared_from_this(), self_.shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
sp->need_eof()))); sp->need_eof()));
} }
}; };
tcp::socket socket_; stream_type stream_;
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_; beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_; std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_; http::request<http::string_body> req_;
@@ -276,8 +273,7 @@ public:
session( session(
tcp::socket socket, tcp::socket socket,
std::shared_ptr<std::string const> const& doc_root) std::shared_ptr<std::string const> const& doc_root)
: socket_(std::move(socket)) : stream_(std::move(socket))
, strand_(socket_.get_executor())
, doc_root_(doc_root) , doc_root_(doc_root)
, lambda_(*this) , lambda_(*this)
{ {
@@ -306,16 +302,17 @@ public:
// otherwise the operation behavior is undefined. // otherwise the operation behavior is undefined.
req_ = {}; req_ = {};
// Set the timeout.
stream_.expires_after(std::chrono::seconds(30));
// Read a request // Read a request
yield http::async_read(socket_, buffer_, req_, yield http::async_read(stream_, buffer_, req_,
net::bind_executor(
strand_,
std::bind( std::bind(
&session::loop, &session::loop,
shared_from_this(), shared_from_this(),
std::placeholders::_1, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_2,
false))); false));
if(ec == http::error::end_of_stream) if(ec == http::error::end_of_stream)
{ {
// The remote host closed the connection // The remote host closed the connection
@@ -340,7 +337,7 @@ public:
} }
// Send a TCP shutdown // Send a TCP shutdown
socket_.shutdown(tcp::socket::shutdown_send, ec); stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
// At this point the connection is closed gracefully // At this point the connection is closed gracefully
} }