Fixes to support Asio changes (API Change):

This adjusts Beast's interfaces and implementation to match
the changes in Boost.Asio.
This commit is contained in:
Vinnie Falco
2019-02-19 16:23:30 -08:00
parent ac24e58fb3
commit 6baa607295
58 changed files with 762 additions and 723 deletions
@@ -255,7 +255,7 @@ class session : public std::enable_shared_from_this<session>
}
};
beast::ssl_stream<beast::tcp_stream<net::io_context::strand>> stream_;
beast::ssl_stream<beast::tcp_stream> stream_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
@@ -387,8 +387,10 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
public:
@@ -397,8 +399,10 @@ public:
ssl::context& ctx,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: ctx_(ctx)
: ioc_(ioc)
, ctx_(ctx)
, acceptor_(ioc)
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -450,13 +454,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -466,11 +471,14 @@ public:
{
// Create the session and run it
std::make_shared<session>(
std::move(socket),
std::move(socket_),
ctx_,
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
@@ -251,7 +251,7 @@ class session : public std::enable_shared_from_this<session>
}
};
beast::tcp_stream<net::io_context::strand> stream_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
@@ -259,7 +259,7 @@ class session : public std::enable_shared_from_this<session>
send_lambda lambda_;
public:
// Take ownership of the socket
// Take ownership of the stream
session(
tcp::socket&& socket,
std::shared_ptr<std::string const> const& doc_root)
@@ -352,7 +352,9 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
public:
@@ -360,7 +362,9 @@ public:
net::io_context& ioc,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: acceptor_(ioc)
: ioc_(ioc)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -412,13 +416,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket socket)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -428,10 +433,13 @@ public:
{
// Create the session and run it
std::make_shared<session>(
std::move(socket),
std::move(socket_),
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
@@ -207,11 +207,6 @@ 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
void
fail(beast::error_code ec, char const* what)
@@ -223,13 +218,13 @@ fail(beast::error_code ec, char const* what)
// The function object is used to send an HTTP message.
struct send_lambda
{
stream_type& stream_;
beast::ssl_stream<beast::tcp_stream>& stream_;
bool& close_;
beast::error_code& ec_;
net::yield_context yield_;
send_lambda(
stream_type& stream,
beast::ssl_stream<beast::tcp_stream>& stream,
bool& close,
beast::error_code& ec,
net::yield_context yield)
@@ -258,7 +253,7 @@ struct send_lambda
// Handles an HTTP server connection
void
do_session(
stream_type& stream,
beast::ssl_stream<beast::tcp_stream>& stream,
std::shared_ptr<std::string const> const& doc_root,
net::yield_context yield)
{
@@ -357,10 +352,11 @@ do_listen(
fail(ec, "accept");
else
net::spawn(
acceptor.get_executor().context(),
acceptor.get_executor(),
std::bind(
&do_session,
stream_type(std::move(socket), ctx),
beast::ssl_stream<beast::tcp_stream>(
std::move(socket), ctx),
doc_root,
std::placeholders::_1));
}
@@ -204,10 +204,6 @@ 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
void
fail(beast::error_code ec, char const* what)
@@ -219,13 +215,13 @@ fail(beast::error_code ec, char const* what)
// The function object is used to send an HTTP message.
struct send_lambda
{
stream_type& stream_;
beast::tcp_stream& stream_;
bool& close_;
beast::error_code& ec_;
net::yield_context yield_;
send_lambda(
stream_type& stream,
beast::tcp_stream& stream,
bool& close,
beast::error_code& ec,
net::yield_context yield)
@@ -254,7 +250,7 @@ struct send_lambda
// Handles an HTTP server connection
void
do_session(
stream_type& stream,
beast::tcp_stream& stream,
std::shared_ptr<std::string const> const& doc_root,
net::yield_context yield)
{
@@ -339,10 +335,10 @@ do_listen(
fail(ec, "accept");
else
net::spawn(
acceptor.get_executor().context(),
acceptor.get_executor(),
std::bind(
&do_session,
stream_type(std::move(socket)),
beast::tcp_stream(std::move(socket)),
doc_root,
std::placeholders::_1));
}
@@ -99,7 +99,7 @@ private:
std::string doc_root_;
// The socket for the currently connected client.
tcp::socket socket_{acceptor_.get_executor().context()};
tcp::socket socket_{acceptor_.get_executor()};
// The buffer for performing reads
beast::flat_static_buffer<8192> buffer_;
@@ -112,7 +112,7 @@ private:
// The timer putting a time limit on requests.
net::basic_waitable_timer<std::chrono::steady_clock> request_deadline_{
acceptor_.get_executor().context(), (std::chrono::steady_clock::time_point::max)()};
acceptor_.get_executor(), (std::chrono::steady_clock::time_point::max)()};
// The string-based response message.
boost::optional<http::response<http::string_body, http::basic_fields<alloc_t>>> string_response_;
+19 -17
View File
@@ -208,13 +208,6 @@ 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
void
fail(beast::error_code ec, char const* what)
@@ -358,7 +351,7 @@ class plain_session
: public session<plain_session>
, public std::enable_shared_from_this<plain_session>
{
plain_stream_type stream_;
beast::tcp_stream stream_;
public:
// Create the session
@@ -374,7 +367,7 @@ public:
}
// Called by the base class
plain_stream_type&
beast::tcp_stream&
stream()
{
return stream_;
@@ -403,7 +396,7 @@ class ssl_session
: public session<ssl_session>
, public std::enable_shared_from_this<ssl_session>
{
ssl_stream_type stream_;
beast::ssl_stream<beast::tcp_stream> stream_;
public:
// Create the session
@@ -420,7 +413,7 @@ public:
}
// Called by the base class
ssl_stream_type&
beast::ssl_stream<beast::tcp_stream>&
stream()
{
return stream_;
@@ -442,6 +435,7 @@ public:
&ssl_session::on_handshake,
shared_from_this()));
}
void
on_handshake(
beast::error_code ec,
@@ -484,14 +478,14 @@ public:
// Detects SSL handshakes
class detect_session : public std::enable_shared_from_this<detect_session>
{
plain_stream_type stream_;
beast::tcp_stream stream_;
ssl::context& ctx_;
std::shared_ptr<std::string const> doc_root_;
beast::flat_buffer buffer_;
public:
detect_session(
tcp::socket socket,
tcp::socket&& socket,
ssl::context& ctx,
std::shared_ptr<std::string const> const& doc_root)
: stream_(std::move(socket))
@@ -544,8 +538,10 @@ public:
// Accepts incoming connections and launches the sessions
class listener : public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
public:
@@ -554,8 +550,10 @@ public:
ssl::context& ctx,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: ctx_(ctx)
, acceptor_(ioc)
: ioc_(ioc)
, ctx_(ctx)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -607,13 +605,14 @@ public:
do_accept()
{
acceptor_.async_accept(
socket_,
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
}
void
on_accept(beast::error_code ec, tcp::socket sock)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -623,11 +622,14 @@ public:
{
// Create the detector session and run it
std::make_shared<detect_session>(
std::move(sock),
std::move(socket_),
ctx_,
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
// Accept another connection
do_accept();
}
@@ -76,7 +76,7 @@ private:
// The timer for putting a deadline on connection processing.
net::basic_waitable_timer<std::chrono::steady_clock> deadline_{
socket_.get_executor().context(), std::chrono::seconds(60)};
socket_.get_executor(), std::chrono::seconds(60)};
// Asynchronously receive a complete request message.
void
@@ -209,10 +209,6 @@ handle_request(
//------------------------------------------------------------------------------
// The type of TLS streams
using ssl_stream_type =
beast::ssl_stream<beast::tcp_stream<net::io_context::strand>>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -264,7 +260,7 @@ class session
}
};
ssl_stream_type stream_;
beast::ssl_stream<beast::tcp_stream> stream_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
@@ -383,6 +379,7 @@ class listener
: public net::coroutine
, public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
ssl::context& ctx_;
tcp::acceptor acceptor_;
tcp::socket socket_;
@@ -394,9 +391,10 @@ public:
ssl::context& ctx,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: ctx_(ctx)
, acceptor_(ioc)
, socket_(ioc)
: ioc_(ioc)
, ctx_(ctx)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -470,6 +468,9 @@ public:
ctx_,
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
}
}
}
@@ -205,9 +205,6 @@ handle_request(
//------------------------------------------------------------------------------
// The type of stream to use
using stream_type = beast::tcp_stream<net::io_context::strand>;
// Report a failure
void
fail(beast::error_code ec, char const* what)
@@ -258,7 +255,7 @@ class session
}
};
stream_type stream_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
@@ -269,7 +266,7 @@ public:
// Take ownership of the socket
explicit
session(
tcp::socket socket,
tcp::socket&& socket,
std::shared_ptr<std::string const> const& doc_root)
: stream_(std::move(socket))
, doc_root_(doc_root)
@@ -348,6 +345,7 @@ class listener
: public net::coroutine
, public std::enable_shared_from_this<listener>
{
net::io_context& ioc_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
@@ -357,8 +355,9 @@ public:
net::io_context& ioc,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root)
: acceptor_(ioc)
, socket_(ioc)
: ioc_(ioc)
, acceptor_(beast::make_strand(ioc))
, socket_(beast::make_strand(ioc))
, doc_root_(doc_root)
{
beast::error_code ec;
@@ -429,6 +428,9 @@ public:
std::move(socket_),
doc_root_)->run();
}
// Make sure each session gets its own strand
socket_ = tcp::socket(beast::make_strand(ioc_));
}
}
}