mirror of
https://github.com/boostorg/beast.git
synced 2025-08-01 05:44:38 +02:00
net is a namespace alias for boost::asio:
The namespace alias beast::net replaces boost::asio in all code and documentation.
This commit is contained in:
@@ -38,21 +38,23 @@
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
namespace beast = boost::beast; // from <boost/beast.hpp>
|
||||
namespace http = beast::http; // from <boost/beast/http.hpp>
|
||||
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
|
||||
namespace net = boost::asio; // from <boost/asio.hpp>
|
||||
namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
|
||||
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
|
||||
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
|
||||
// Return a reasonable mime type based on the extension of a file.
|
||||
boost::beast::string_view
|
||||
mime_type(boost::beast::string_view path)
|
||||
beast::string_view
|
||||
mime_type(beast::string_view path)
|
||||
{
|
||||
using boost::beast::iequals;
|
||||
using beast::iequals;
|
||||
auto const ext = [&path]
|
||||
{
|
||||
auto const pos = path.rfind(".");
|
||||
if(pos == boost::beast::string_view::npos)
|
||||
return boost::beast::string_view{};
|
||||
if(pos == beast::string_view::npos)
|
||||
return beast::string_view{};
|
||||
return path.substr(pos);
|
||||
}();
|
||||
if(iequals(ext, ".htm")) return "text/html";
|
||||
@@ -83,8 +85,8 @@ mime_type(boost::beast::string_view path)
|
||||
// The returned path is normalized for the platform.
|
||||
std::string
|
||||
path_cat(
|
||||
boost::beast::string_view base,
|
||||
boost::beast::string_view path)
|
||||
beast::string_view base,
|
||||
beast::string_view path)
|
||||
{
|
||||
if(base.empty())
|
||||
return path.to_string();
|
||||
@@ -115,13 +117,13 @@ template<
|
||||
class Send>
|
||||
void
|
||||
handle_request(
|
||||
boost::beast::string_view doc_root,
|
||||
beast::string_view doc_root,
|
||||
http::request<Body, http::basic_fields<Allocator>>&& req,
|
||||
Send&& send)
|
||||
{
|
||||
// Returns a bad request response
|
||||
auto const bad_request =
|
||||
[&req](boost::beast::string_view why)
|
||||
[&req](beast::string_view why)
|
||||
{
|
||||
http::response<http::string_body> res{http::status::bad_request, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
@@ -134,7 +136,7 @@ handle_request(
|
||||
|
||||
// Returns a not found response
|
||||
auto const not_found =
|
||||
[&req](boost::beast::string_view target)
|
||||
[&req](beast::string_view target)
|
||||
{
|
||||
http::response<http::string_body> res{http::status::not_found, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
@@ -147,7 +149,7 @@ handle_request(
|
||||
|
||||
// Returns a server error response
|
||||
auto const server_error =
|
||||
[&req](boost::beast::string_view what)
|
||||
[&req](beast::string_view what)
|
||||
{
|
||||
http::response<http::string_body> res{http::status::internal_server_error, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
@@ -166,7 +168,7 @@ handle_request(
|
||||
// Request path must be absolute and not contain "..".
|
||||
if( req.target().empty() ||
|
||||
req.target()[0] != '/' ||
|
||||
req.target().find("..") != boost::beast::string_view::npos)
|
||||
req.target().find("..") != beast::string_view::npos)
|
||||
return send(bad_request("Illegal request-target"));
|
||||
|
||||
// Build the path to the requested file
|
||||
@@ -175,12 +177,12 @@ handle_request(
|
||||
path.append("index.html");
|
||||
|
||||
// Attempt to open the file
|
||||
boost::beast::error_code ec;
|
||||
beast::error_code ec;
|
||||
http::file_body::value_type body;
|
||||
body.open(path.c_str(), boost::beast::file_mode::scan, ec);
|
||||
body.open(path.c_str(), beast::file_mode::scan, ec);
|
||||
|
||||
// Handle the case where the file doesn't exist
|
||||
if(ec == boost::system::errc::no_such_file_or_directory)
|
||||
if(ec == beast::errc::no_such_file_or_directory)
|
||||
return send(not_found(req.target()));
|
||||
|
||||
// Handle an unknown error
|
||||
@@ -217,7 +219,7 @@ handle_request(
|
||||
|
||||
// Report a failure
|
||||
void
|
||||
fail(boost::system::error_code ec, char const* what)
|
||||
fail(beast::error_code ec, char const* what)
|
||||
{
|
||||
std::cerr << what << ": " << ec.message() << "\n";
|
||||
}
|
||||
@@ -238,18 +240,18 @@ class websocket_session
|
||||
return static_cast<Derived&>(*this);
|
||||
}
|
||||
|
||||
boost::beast::multi_buffer buffer_;
|
||||
beast::multi_buffer buffer_;
|
||||
char ping_state_ = 0;
|
||||
|
||||
protected:
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::asio::steady_timer timer_;
|
||||
net::strand<
|
||||
net::io_context::executor_type> strand_;
|
||||
net::steady_timer timer_;
|
||||
|
||||
public:
|
||||
// Construct the session
|
||||
explicit
|
||||
websocket_session(boost::asio::io_context& ioc)
|
||||
websocket_session(net::io_context& ioc)
|
||||
: strand_(ioc.get_executor())
|
||||
, timer_(ioc,
|
||||
(std::chrono::steady_clock::time_point::max)())
|
||||
@@ -276,7 +278,7 @@ public:
|
||||
// Accept the websocket handshake
|
||||
derived().ws().async_accept(
|
||||
req,
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_accept,
|
||||
@@ -285,10 +287,10 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_accept(boost::system::error_code ec)
|
||||
on_accept(beast::error_code ec)
|
||||
{
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -300,9 +302,9 @@ public:
|
||||
|
||||
// Called when the timer expires.
|
||||
void
|
||||
on_timer(boost::system::error_code ec)
|
||||
on_timer(beast::error_code ec)
|
||||
{
|
||||
if(ec && ec != boost::asio::error::operation_aborted)
|
||||
if(ec && ec != net::error::operation_aborted)
|
||||
return fail(ec, "timer");
|
||||
|
||||
// See if the timer really expired since the deadline may have moved.
|
||||
@@ -320,7 +322,7 @@ public:
|
||||
|
||||
// Now send the ping
|
||||
derived().ws().async_ping({},
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_ping,
|
||||
@@ -340,7 +342,7 @@ public:
|
||||
|
||||
// Wait on the timer
|
||||
timer_.async_wait(
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_timer,
|
||||
@@ -361,10 +363,10 @@ public:
|
||||
|
||||
// Called after a ping is sent.
|
||||
void
|
||||
on_ping(boost::system::error_code ec)
|
||||
on_ping(beast::error_code ec)
|
||||
{
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -387,7 +389,7 @@ public:
|
||||
void
|
||||
on_control_callback(
|
||||
websocket::frame_type kind,
|
||||
boost::beast::string_view payload)
|
||||
beast::string_view payload)
|
||||
{
|
||||
boost::ignore_unused(kind, payload);
|
||||
|
||||
@@ -401,7 +403,7 @@ public:
|
||||
// Read a message into our buffer
|
||||
derived().ws().async_read(
|
||||
buffer_,
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_read,
|
||||
@@ -412,13 +414,13 @@ public:
|
||||
|
||||
void
|
||||
on_read(
|
||||
boost::system::error_code ec,
|
||||
beast::error_code ec,
|
||||
std::size_t bytes_transferred)
|
||||
{
|
||||
boost::ignore_unused(bytes_transferred);
|
||||
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
// This indicates that the websocket_session was closed
|
||||
@@ -435,7 +437,7 @@ public:
|
||||
derived().ws().text(derived().ws().got_text());
|
||||
derived().ws().async_write(
|
||||
buffer_.data(),
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_write,
|
||||
@@ -446,13 +448,13 @@ public:
|
||||
|
||||
void
|
||||
on_write(
|
||||
boost::system::error_code ec,
|
||||
beast::error_code ec,
|
||||
std::size_t bytes_transferred)
|
||||
{
|
||||
boost::ignore_unused(bytes_transferred);
|
||||
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -518,7 +520,7 @@ public:
|
||||
// Close the WebSocket Connection
|
||||
ws_.async_close(
|
||||
websocket::close_code::normal,
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&plain_websocket_session::on_close,
|
||||
@@ -527,10 +529,10 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_close(boost::system::error_code ec)
|
||||
on_close(beast::error_code ec)
|
||||
{
|
||||
// Happens when close times out
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -545,13 +547,13 @@ class ssl_websocket_session
|
||||
: public websocket_session<ssl_websocket_session>
|
||||
, public std::enable_shared_from_this<ssl_websocket_session>
|
||||
{
|
||||
websocket::stream<boost::beast::ssl_stream<tcp::socket>> ws_;
|
||||
websocket::stream<beast::ssl_stream<tcp::socket>> ws_;
|
||||
bool eof_ = false;
|
||||
|
||||
public:
|
||||
// Create the http_session
|
||||
explicit
|
||||
ssl_websocket_session(boost::beast::ssl_stream<tcp::socket> stream)
|
||||
ssl_websocket_session(beast::ssl_stream<tcp::socket> stream)
|
||||
: websocket_session<ssl_websocket_session>(
|
||||
stream.get_executor().context())
|
||||
, ws_(std::move(stream))
|
||||
@@ -559,7 +561,7 @@ public:
|
||||
}
|
||||
|
||||
// Called by the base class
|
||||
websocket::stream<boost::beast::ssl_stream<tcp::socket>>&
|
||||
websocket::stream<beast::ssl_stream<tcp::socket>>&
|
||||
ws()
|
||||
{
|
||||
return ws_;
|
||||
@@ -588,7 +590,7 @@ public:
|
||||
|
||||
// Perform the SSL shutdown
|
||||
ws_.next_layer().async_shutdown(
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&ssl_websocket_session::on_shutdown,
|
||||
@@ -597,10 +599,10 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_shutdown(boost::system::error_code ec)
|
||||
on_shutdown(beast::error_code ec)
|
||||
{
|
||||
// Happens when the shutdown times out
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -637,7 +639,7 @@ make_websocket_session(
|
||||
template<class Body, class Allocator>
|
||||
void
|
||||
make_websocket_session(
|
||||
boost::beast::ssl_stream<tcp::socket> stream,
|
||||
beast::ssl_stream<tcp::socket> stream,
|
||||
http::request<Body, http::basic_fields<Allocator>> req)
|
||||
{
|
||||
std::make_shared<ssl_websocket_session>(
|
||||
@@ -733,7 +735,7 @@ class http_session
|
||||
http::async_write(
|
||||
self_.derived().stream(),
|
||||
msg_,
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
self_.strand_,
|
||||
std::bind(
|
||||
&http_session::on_write,
|
||||
@@ -758,16 +760,16 @@ class http_session
|
||||
queue queue_;
|
||||
|
||||
protected:
|
||||
boost::asio::steady_timer timer_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
net::steady_timer timer_;
|
||||
net::strand<
|
||||
net::io_context::executor_type> strand_;
|
||||
beast::flat_buffer buffer_;
|
||||
|
||||
public:
|
||||
// Construct the session
|
||||
http_session(
|
||||
boost::asio::io_context& ioc,
|
||||
boost::beast::flat_buffer buffer,
|
||||
net::io_context& ioc,
|
||||
beast::flat_buffer buffer,
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
: doc_root_(doc_root)
|
||||
, queue_(*this)
|
||||
@@ -793,7 +795,7 @@ public:
|
||||
derived().stream(),
|
||||
buffer_,
|
||||
req_,
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&http_session::on_read,
|
||||
@@ -803,9 +805,9 @@ public:
|
||||
|
||||
// Called when the timer expires.
|
||||
void
|
||||
on_timer(boost::system::error_code ec)
|
||||
on_timer(beast::error_code ec)
|
||||
{
|
||||
if(ec && ec != boost::asio::error::operation_aborted)
|
||||
if(ec && ec != net::error::operation_aborted)
|
||||
return fail(ec, "timer");
|
||||
|
||||
// Check if this has been upgraded to Websocket
|
||||
@@ -818,7 +820,7 @@ public:
|
||||
|
||||
// Wait on the timer
|
||||
timer_.async_wait(
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&http_session::on_timer,
|
||||
@@ -827,10 +829,10 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_read(boost::system::error_code ec)
|
||||
on_read(beast::error_code ec)
|
||||
{
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
// This means they closed the connection
|
||||
@@ -862,10 +864,10 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_write(boost::system::error_code ec, bool close)
|
||||
on_write(beast::error_code ec, bool close)
|
||||
{
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -893,14 +895,14 @@ class plain_http_session
|
||||
, public std::enable_shared_from_this<plain_http_session>
|
||||
{
|
||||
tcp::socket socket_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
net::strand<
|
||||
net::io_context::executor_type> strand_;
|
||||
|
||||
public:
|
||||
// Create the http_session
|
||||
plain_http_session(
|
||||
tcp::socket socket,
|
||||
boost::beast::flat_buffer buffer,
|
||||
beast::flat_buffer buffer,
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
: http_session<plain_http_session>(
|
||||
socket.get_executor().context(),
|
||||
@@ -931,8 +933,8 @@ public:
|
||||
{
|
||||
// Make sure we run on the strand
|
||||
if(! strand_.running_in_this_thread())
|
||||
return boost::asio::post(
|
||||
boost::asio::bind_executor(
|
||||
return net::post(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&plain_http_session::run,
|
||||
@@ -949,7 +951,7 @@ public:
|
||||
do_eof()
|
||||
{
|
||||
// Send a TCP shutdown
|
||||
boost::system::error_code ec;
|
||||
beast::error_code ec;
|
||||
socket_.shutdown(tcp::socket::shutdown_send, ec);
|
||||
|
||||
// At this point the connection is closed gracefully
|
||||
@@ -959,8 +961,8 @@ public:
|
||||
do_timeout()
|
||||
{
|
||||
// Closing the socket cancels all outstanding operations. They
|
||||
// will complete with boost::asio::error::operation_aborted
|
||||
boost::system::error_code ec;
|
||||
// will complete with net::error::operation_aborted
|
||||
beast::error_code ec;
|
||||
socket_.shutdown(tcp::socket::shutdown_both, ec);
|
||||
socket_.close(ec);
|
||||
}
|
||||
@@ -971,9 +973,9 @@ class ssl_http_session
|
||||
: public http_session<ssl_http_session>
|
||||
, public std::enable_shared_from_this<ssl_http_session>
|
||||
{
|
||||
boost::beast::ssl_stream<tcp::socket> stream_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
beast::ssl_stream<tcp::socket> stream_;
|
||||
net::strand<
|
||||
net::io_context::executor_type> strand_;
|
||||
bool eof_ = false;
|
||||
|
||||
public:
|
||||
@@ -981,7 +983,7 @@ public:
|
||||
ssl_http_session(
|
||||
tcp::socket socket,
|
||||
ssl::context& ctx,
|
||||
boost::beast::flat_buffer buffer,
|
||||
beast::flat_buffer buffer,
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
: http_session<ssl_http_session>(
|
||||
socket.get_executor().context(),
|
||||
@@ -993,14 +995,14 @@ public:
|
||||
}
|
||||
|
||||
// Called by the base class
|
||||
boost::beast::ssl_stream<tcp::socket>&
|
||||
beast::ssl_stream<tcp::socket>&
|
||||
stream()
|
||||
{
|
||||
return stream_;
|
||||
}
|
||||
|
||||
// Called by the base class
|
||||
boost::beast::ssl_stream<tcp::socket>
|
||||
beast::ssl_stream<tcp::socket>
|
||||
release_stream()
|
||||
{
|
||||
return std::move(stream_);
|
||||
@@ -1012,8 +1014,8 @@ public:
|
||||
{
|
||||
// Make sure we run on the strand
|
||||
if(! strand_.running_in_this_thread())
|
||||
return boost::asio::post(
|
||||
boost::asio::bind_executor(
|
||||
return net::post(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&ssl_http_session::run,
|
||||
@@ -1031,7 +1033,7 @@ public:
|
||||
stream_.async_handshake(
|
||||
ssl::stream_base::server,
|
||||
buffer_.data(),
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&ssl_http_session::on_handshake,
|
||||
@@ -1041,11 +1043,11 @@ public:
|
||||
}
|
||||
void
|
||||
on_handshake(
|
||||
boost::system::error_code ec,
|
||||
beast::error_code ec,
|
||||
std::size_t bytes_used)
|
||||
{
|
||||
// Happens when the handshake times out
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -1067,7 +1069,7 @@ public:
|
||||
|
||||
// Perform the SSL shutdown
|
||||
stream_.async_shutdown(
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&ssl_http_session::on_shutdown,
|
||||
@@ -1076,10 +1078,10 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_shutdown(boost::system::error_code ec)
|
||||
on_shutdown(beast::error_code ec)
|
||||
{
|
||||
// Happens when the shutdown times out
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -1110,10 +1112,10 @@ class detect_session : public std::enable_shared_from_this<detect_session>
|
||||
{
|
||||
tcp::socket socket_;
|
||||
ssl::context& ctx_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
net::strand<
|
||||
net::io_context::executor_type> strand_;
|
||||
std::shared_ptr<std::string const> doc_root_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
beast::flat_buffer buffer_;
|
||||
|
||||
public:
|
||||
explicit
|
||||
@@ -1135,7 +1137,7 @@ public:
|
||||
async_detect_ssl(
|
||||
socket_,
|
||||
buffer_,
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&detect_session::on_detect,
|
||||
@@ -1146,7 +1148,7 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_detect(boost::system::error_code ec, boost::tribool result)
|
||||
on_detect(beast::error_code ec, boost::tribool result)
|
||||
{
|
||||
if(ec)
|
||||
return fail(ec, "detect");
|
||||
@@ -1180,7 +1182,7 @@ class listener : public std::enable_shared_from_this<listener>
|
||||
|
||||
public:
|
||||
listener(
|
||||
boost::asio::io_context& ioc,
|
||||
net::io_context& ioc,
|
||||
ssl::context& ctx,
|
||||
tcp::endpoint endpoint,
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
@@ -1189,7 +1191,7 @@ public:
|
||||
, socket_(ioc)
|
||||
, doc_root_(doc_root)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
beast::error_code ec;
|
||||
|
||||
// Open the acceptor
|
||||
acceptor_.open(endpoint.protocol(), ec);
|
||||
@@ -1200,7 +1202,7 @@ public:
|
||||
}
|
||||
|
||||
// Allow address reuse
|
||||
acceptor_.set_option(boost::asio::socket_base::reuse_address(true), ec);
|
||||
acceptor_.set_option(net::socket_base::reuse_address(true), ec);
|
||||
if(ec)
|
||||
{
|
||||
fail(ec, "set_option");
|
||||
@@ -1217,7 +1219,7 @@ public:
|
||||
|
||||
// Start listening for connections
|
||||
acceptor_.listen(
|
||||
boost::asio::socket_base::max_listen_connections, ec);
|
||||
net::socket_base::max_listen_connections, ec);
|
||||
if(ec)
|
||||
{
|
||||
fail(ec, "listen");
|
||||
@@ -1246,7 +1248,7 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_accept(boost::system::error_code ec)
|
||||
on_accept(beast::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
{
|
||||
@@ -1279,13 +1281,13 @@ int main(int argc, char* argv[])
|
||||
" advanced-server-flex 0.0.0.0 8080 . 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const address = net::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
auto const doc_root = std::make_shared<std::string>(argv[3]);
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{threads};
|
||||
net::io_context ioc{threads};
|
||||
|
||||
// The SSL context is required, and holds certificates
|
||||
ssl::context ctx{ssl::context::sslv23};
|
||||
@@ -1301,9 +1303,9 @@ int main(int argc, char* argv[])
|
||||
doc_root)->run();
|
||||
|
||||
// Capture SIGINT and SIGTERM to perform a clean shutdown
|
||||
boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
net::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
signals.async_wait(
|
||||
[&](boost::system::error_code const&, int)
|
||||
[&](beast::error_code const&, int)
|
||||
{
|
||||
// Stop the `io_context`. This will cause `run()`
|
||||
// to return immediately, eventually destroying the
|
||||
|
@@ -33,20 +33,22 @@
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace beast = boost::beast; // from <boost/beast.hpp>
|
||||
namespace http = beast::http; // from <boost/beast/http.hpp>
|
||||
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
|
||||
namespace net = boost::asio; // from <boost/asio.hpp>
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
|
||||
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
|
||||
|
||||
// Return a reasonable mime type based on the extension of a file.
|
||||
boost::beast::string_view
|
||||
mime_type(boost::beast::string_view path)
|
||||
beast::string_view
|
||||
mime_type(beast::string_view path)
|
||||
{
|
||||
using boost::beast::iequals;
|
||||
using beast::iequals;
|
||||
auto const ext = [&path]
|
||||
{
|
||||
auto const pos = path.rfind(".");
|
||||
if(pos == boost::beast::string_view::npos)
|
||||
return boost::beast::string_view{};
|
||||
if(pos == beast::string_view::npos)
|
||||
return beast::string_view{};
|
||||
return path.substr(pos);
|
||||
}();
|
||||
if(iequals(ext, ".htm")) return "text/html";
|
||||
@@ -77,8 +79,8 @@ mime_type(boost::beast::string_view path)
|
||||
// The returned path is normalized for the platform.
|
||||
std::string
|
||||
path_cat(
|
||||
boost::beast::string_view base,
|
||||
boost::beast::string_view path)
|
||||
beast::string_view base,
|
||||
beast::string_view path)
|
||||
{
|
||||
if(base.empty())
|
||||
return path.to_string();
|
||||
@@ -109,13 +111,13 @@ template<
|
||||
class Send>
|
||||
void
|
||||
handle_request(
|
||||
boost::beast::string_view doc_root,
|
||||
beast::string_view doc_root,
|
||||
http::request<Body, http::basic_fields<Allocator>>&& req,
|
||||
Send&& send)
|
||||
{
|
||||
// Returns a bad request response
|
||||
auto const bad_request =
|
||||
[&req](boost::beast::string_view why)
|
||||
[&req](beast::string_view why)
|
||||
{
|
||||
http::response<http::string_body> res{http::status::bad_request, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
@@ -128,7 +130,7 @@ handle_request(
|
||||
|
||||
// Returns a not found response
|
||||
auto const not_found =
|
||||
[&req](boost::beast::string_view target)
|
||||
[&req](beast::string_view target)
|
||||
{
|
||||
http::response<http::string_body> res{http::status::not_found, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
@@ -141,7 +143,7 @@ handle_request(
|
||||
|
||||
// Returns a server error response
|
||||
auto const server_error =
|
||||
[&req](boost::beast::string_view what)
|
||||
[&req](beast::string_view what)
|
||||
{
|
||||
http::response<http::string_body> res{http::status::internal_server_error, req.version()};
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
@@ -160,7 +162,7 @@ handle_request(
|
||||
// Request path must be absolute and not contain "..".
|
||||
if( req.target().empty() ||
|
||||
req.target()[0] != '/' ||
|
||||
req.target().find("..") != boost::beast::string_view::npos)
|
||||
req.target().find("..") != beast::string_view::npos)
|
||||
return send(bad_request("Illegal request-target"));
|
||||
|
||||
// Build the path to the requested file
|
||||
@@ -169,12 +171,12 @@ handle_request(
|
||||
path.append("index.html");
|
||||
|
||||
// Attempt to open the file
|
||||
boost::beast::error_code ec;
|
||||
beast::error_code ec;
|
||||
http::file_body::value_type body;
|
||||
body.open(path.c_str(), boost::beast::file_mode::scan, ec);
|
||||
body.open(path.c_str(), beast::file_mode::scan, ec);
|
||||
|
||||
// Handle the case where the file doesn't exist
|
||||
if(ec == boost::system::errc::no_such_file_or_directory)
|
||||
if(ec == beast::errc::no_such_file_or_directory)
|
||||
return send(not_found(req.target()));
|
||||
|
||||
// Handle an unknown error
|
||||
@@ -211,7 +213,7 @@ handle_request(
|
||||
|
||||
// Report a failure
|
||||
void
|
||||
fail(boost::system::error_code ec, char const* what)
|
||||
fail(beast::error_code ec, char const* what)
|
||||
{
|
||||
std::cerr << what << ": " << ec.message() << "\n";
|
||||
}
|
||||
@@ -220,10 +222,10 @@ fail(boost::system::error_code ec, char const* what)
|
||||
class websocket_session : public std::enable_shared_from_this<websocket_session>
|
||||
{
|
||||
websocket::stream<tcp::socket> ws_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::asio::steady_timer timer_;
|
||||
boost::beast::multi_buffer buffer_;
|
||||
net::strand<
|
||||
net::io_context::executor_type> strand_;
|
||||
net::steady_timer timer_;
|
||||
beast::multi_buffer buffer_;
|
||||
char ping_state_ = 0;
|
||||
|
||||
public:
|
||||
@@ -261,7 +263,7 @@ public:
|
||||
// Accept the websocket handshake
|
||||
ws_.async_accept(
|
||||
req,
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_accept,
|
||||
@@ -270,10 +272,10 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_accept(boost::system::error_code ec)
|
||||
on_accept(beast::error_code ec)
|
||||
{
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -285,9 +287,9 @@ public:
|
||||
|
||||
// Called when the timer expires.
|
||||
void
|
||||
on_timer(boost::system::error_code ec)
|
||||
on_timer(beast::error_code ec)
|
||||
{
|
||||
if(ec && ec != boost::asio::error::operation_aborted)
|
||||
if(ec && ec != net::error::operation_aborted)
|
||||
return fail(ec, "timer");
|
||||
|
||||
// See if the timer really expired since the deadline may have moved.
|
||||
@@ -305,7 +307,7 @@ public:
|
||||
|
||||
// Now send the ping
|
||||
ws_.async_ping({},
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_ping,
|
||||
@@ -319,7 +321,7 @@ public:
|
||||
// we never got back a control frame, so close.
|
||||
|
||||
// Closing the socket cancels all outstanding operations. They
|
||||
// will complete with boost::asio::error::operation_aborted
|
||||
// will complete with net::error::operation_aborted
|
||||
ws_.next_layer().shutdown(tcp::socket::shutdown_both, ec);
|
||||
ws_.next_layer().close(ec);
|
||||
return;
|
||||
@@ -328,7 +330,7 @@ public:
|
||||
|
||||
// Wait on the timer
|
||||
timer_.async_wait(
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_timer,
|
||||
@@ -349,10 +351,10 @@ public:
|
||||
|
||||
// Called after a ping is sent.
|
||||
void
|
||||
on_ping(boost::system::error_code ec)
|
||||
on_ping(beast::error_code ec)
|
||||
{
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -375,7 +377,7 @@ public:
|
||||
void
|
||||
on_control_callback(
|
||||
websocket::frame_type kind,
|
||||
boost::beast::string_view payload)
|
||||
beast::string_view payload)
|
||||
{
|
||||
boost::ignore_unused(kind, payload);
|
||||
|
||||
@@ -389,7 +391,7 @@ public:
|
||||
// Read a message into our buffer
|
||||
ws_.async_read(
|
||||
buffer_,
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_read,
|
||||
@@ -400,13 +402,13 @@ public:
|
||||
|
||||
void
|
||||
on_read(
|
||||
boost::system::error_code ec,
|
||||
beast::error_code ec,
|
||||
std::size_t bytes_transferred)
|
||||
{
|
||||
boost::ignore_unused(bytes_transferred);
|
||||
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
// This indicates that the websocket_session was closed
|
||||
@@ -423,7 +425,7 @@ public:
|
||||
ws_.text(ws_.got_text());
|
||||
ws_.async_write(
|
||||
buffer_.data(),
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&websocket_session::on_write,
|
||||
@@ -434,13 +436,13 @@ public:
|
||||
|
||||
void
|
||||
on_write(
|
||||
boost::system::error_code ec,
|
||||
beast::error_code ec,
|
||||
std::size_t bytes_transferred)
|
||||
{
|
||||
boost::ignore_unused(bytes_transferred);
|
||||
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -530,7 +532,7 @@ class http_session : public std::enable_shared_from_this<http_session>
|
||||
http::async_write(
|
||||
self_.socket_,
|
||||
msg_,
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
self_.strand_,
|
||||
std::bind(
|
||||
&http_session::on_write,
|
||||
@@ -551,10 +553,10 @@ class http_session : public std::enable_shared_from_this<http_session>
|
||||
};
|
||||
|
||||
tcp::socket socket_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::asio::steady_timer timer_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
net::strand<
|
||||
net::io_context::executor_type> strand_;
|
||||
net::steady_timer timer_;
|
||||
beast::flat_buffer buffer_;
|
||||
std::shared_ptr<std::string const> doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
queue queue_;
|
||||
@@ -580,8 +582,8 @@ public:
|
||||
{
|
||||
// Make sure we run on the strand
|
||||
if(! strand_.running_in_this_thread())
|
||||
return boost::asio::post(
|
||||
boost::asio::bind_executor(
|
||||
return net::post(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&http_session::run,
|
||||
@@ -606,7 +608,7 @@ public:
|
||||
|
||||
// Read a request
|
||||
http::async_read(socket_, buffer_, req_,
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&http_session::on_read,
|
||||
@@ -616,9 +618,9 @@ public:
|
||||
|
||||
// Called when the timer expires.
|
||||
void
|
||||
on_timer(boost::system::error_code ec)
|
||||
on_timer(beast::error_code ec)
|
||||
{
|
||||
if(ec && ec != boost::asio::error::operation_aborted)
|
||||
if(ec && ec != net::error::operation_aborted)
|
||||
return fail(ec, "timer");
|
||||
|
||||
// Check if this has been upgraded to Websocket
|
||||
@@ -629,7 +631,7 @@ public:
|
||||
if(timer_.expiry() <= std::chrono::steady_clock::now())
|
||||
{
|
||||
// Closing the socket cancels all outstanding operations. They
|
||||
// will complete with boost::asio::error::operation_aborted
|
||||
// will complete with net::error::operation_aborted
|
||||
socket_.shutdown(tcp::socket::shutdown_both, ec);
|
||||
socket_.close(ec);
|
||||
return;
|
||||
@@ -637,7 +639,7 @@ public:
|
||||
|
||||
// Wait on the timer
|
||||
timer_.async_wait(
|
||||
boost::asio::bind_executor(
|
||||
net::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&http_session::on_timer,
|
||||
@@ -646,10 +648,10 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_read(boost::system::error_code ec)
|
||||
on_read(beast::error_code ec)
|
||||
{
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
// This means they closed the connection
|
||||
@@ -681,10 +683,10 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_write(boost::system::error_code ec, bool close)
|
||||
on_write(beast::error_code ec, bool close)
|
||||
{
|
||||
// Happens when the timer closes the socket
|
||||
if(ec == boost::asio::error::operation_aborted)
|
||||
if(ec == net::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if(ec)
|
||||
@@ -709,7 +711,7 @@ public:
|
||||
do_close()
|
||||
{
|
||||
// Send a TCP shutdown
|
||||
boost::system::error_code ec;
|
||||
beast::error_code ec;
|
||||
socket_.shutdown(tcp::socket::shutdown_send, ec);
|
||||
|
||||
// At this point the connection is closed gracefully
|
||||
@@ -727,14 +729,14 @@ class listener : public std::enable_shared_from_this<listener>
|
||||
|
||||
public:
|
||||
listener(
|
||||
boost::asio::io_context& ioc,
|
||||
net::io_context& ioc,
|
||||
tcp::endpoint endpoint,
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
: acceptor_(ioc)
|
||||
, socket_(ioc)
|
||||
, doc_root_(doc_root)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
beast::error_code ec;
|
||||
|
||||
// Open the acceptor
|
||||
acceptor_.open(endpoint.protocol(), ec);
|
||||
@@ -745,7 +747,7 @@ public:
|
||||
}
|
||||
|
||||
// Allow address reuse
|
||||
acceptor_.set_option(boost::asio::socket_base::reuse_address(true), ec);
|
||||
acceptor_.set_option(net::socket_base::reuse_address(true), ec);
|
||||
if(ec)
|
||||
{
|
||||
fail(ec, "set_option");
|
||||
@@ -762,7 +764,7 @@ public:
|
||||
|
||||
// Start listening for connections
|
||||
acceptor_.listen(
|
||||
boost::asio::socket_base::max_listen_connections, ec);
|
||||
net::socket_base::max_listen_connections, ec);
|
||||
if(ec)
|
||||
{
|
||||
fail(ec, "listen");
|
||||
@@ -791,7 +793,7 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
on_accept(boost::system::error_code ec)
|
||||
on_accept(beast::error_code ec)
|
||||
{
|
||||
if(ec)
|
||||
{
|
||||
@@ -823,13 +825,13 @@ int main(int argc, char* argv[])
|
||||
" advanced-server 0.0.0.0 8080 . 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const address = net::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
auto const doc_root = std::make_shared<std::string>(argv[3]);
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{threads};
|
||||
net::io_context ioc{threads};
|
||||
|
||||
// Create and launch a listening port
|
||||
std::make_shared<listener>(
|
||||
@@ -838,9 +840,9 @@ int main(int argc, char* argv[])
|
||||
doc_root)->run();
|
||||
|
||||
// Capture SIGINT and SIGTERM to perform a clean shutdown
|
||||
boost::asio::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
net::signal_set signals(ioc, SIGINT, SIGTERM);
|
||||
signals.async_wait(
|
||||
[&](boost::system::error_code const&, int)
|
||||
[&](beast::error_code const&, int)
|
||||
{
|
||||
// Stop the `io_context`. This will cause `run()`
|
||||
// to return immediately, eventually destroying the
|
||||
|
Reference in New Issue
Block a user