mirror of
https://github.com/boostorg/beast.git
synced 2026-03-24 08:17:22 +01: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:
@@ -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