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:
Vinnie Falco
2018-12-02 17:51:43 -08:00
parent a5739c3ea0
commit f995fd41a3
185 changed files with 2486 additions and 2392 deletions
@@ -32,20 +32,22 @@
#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 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>
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";
@@ -76,8 +78,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();
@@ -108,13 +110,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);
@@ -127,7 +129,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);
@@ -140,7 +142,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);
@@ -159,7 +161,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
@@ -168,12 +170,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
@@ -210,7 +212,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";
}
@@ -248,7 +250,7 @@ class session : public std::enable_shared_from_this<session>
http::async_write(
self_.stream_,
*sp,
boost::asio::bind_executor(
net::bind_executor(
self_.strand_,
std::bind(
&session::on_write,
@@ -261,9 +263,9 @@ class session : public std::enable_shared_from_this<session>
tcp::socket socket_;
ssl::stream<tcp::socket&> stream_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::flat_buffer buffer_;
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
std::shared_ptr<void> res_;
@@ -291,7 +293,7 @@ public:
// Perform the SSL handshake
stream_.async_handshake(
ssl::stream_base::server,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_handshake,
@@ -300,7 +302,7 @@ public:
}
void
on_handshake(boost::system::error_code ec)
on_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "handshake");
@@ -317,7 +319,7 @@ public:
// Read a request
http::async_read(stream_, buffer_, req_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_read,
@@ -328,7 +330,7 @@ public:
void
on_read(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -346,7 +348,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred,
bool close)
{
@@ -374,7 +376,7 @@ public:
{
// Perform the SSL shutdown
stream_.async_shutdown(
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_shutdown,
@@ -383,7 +385,7 @@ public:
}
void
on_shutdown(boost::system::error_code ec)
on_shutdown(beast::error_code ec)
{
if(ec)
return fail(ec, "shutdown");
@@ -404,7 +406,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)
@@ -413,7 +415,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);
@@ -424,7 +426,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");
@@ -441,7 +443,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");
@@ -470,7 +472,7 @@ public:
}
void
on_accept(boost::system::error_code ec)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -503,13 +505,13 @@ int main(int argc, char* argv[])
" http-server-async-ssl 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};
+34 -32
View File
@@ -29,19 +29,21 @@
#include <thread>
#include <vector>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.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>
// 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";
@@ -72,8 +74,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();
@@ -104,13 +106,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);
@@ -123,7 +125,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);
@@ -136,7 +138,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);
@@ -155,7 +157,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
@@ -164,12 +166,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
@@ -206,7 +208,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";
}
@@ -244,7 +246,7 @@ class session : public std::enable_shared_from_this<session>
http::async_write(
self_.socket_,
*sp,
boost::asio::bind_executor(
net::bind_executor(
self_.strand_,
std::bind(
&session::on_write,
@@ -256,9 +258,9 @@ class session : public std::enable_shared_from_this<session>
};
tcp::socket socket_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::flat_buffer buffer_;
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
std::shared_ptr<void> res_;
@@ -293,7 +295,7 @@ public:
// Read a request
http::async_read(socket_, buffer_, req_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_read,
@@ -304,7 +306,7 @@ public:
void
on_read(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -322,7 +324,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred,
bool close)
{
@@ -349,7 +351,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
@@ -367,14 +369,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);
@@ -385,7 +387,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");
@@ -402,7 +404,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");
@@ -431,7 +433,7 @@ public:
}
void
on_accept(boost::system::error_code ec)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -463,13 +465,13 @@ int main(int argc, char* argv[])
" http-server-async 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>(
@@ -30,20 +30,22 @@
#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 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>
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";
@@ -74,8 +76,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();
@@ -106,13 +108,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);
@@ -125,7 +127,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);
@@ -138,7 +140,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);
@@ -157,7 +159,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
@@ -166,12 +168,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
@@ -208,7 +210,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,15 +222,15 @@ struct send_lambda
{
Stream& stream_;
bool& close_;
boost::system::error_code& ec_;
boost::asio::yield_context yield_;
beast::error_code& ec_;
net::yield_context yield_;
explicit
send_lambda(
Stream& stream,
bool& close,
boost::system::error_code& ec,
boost::asio::yield_context yield)
beast::error_code& ec,
net::yield_context yield)
: stream_(stream)
, close_(close)
, ec_(ec)
@@ -257,10 +259,10 @@ do_session(
tcp::socket& socket,
ssl::context& ctx,
std::shared_ptr<std::string const> const& doc_root,
boost::asio::yield_context yield)
net::yield_context yield)
{
bool close = false;
boost::system::error_code ec;
beast::error_code ec;
// Construct the stream around the socket
ssl::stream<tcp::socket&> stream{socket, ctx};
@@ -271,7 +273,7 @@ do_session(
return fail(ec, "handshake");
// This buffer is required to persist across reads
boost::beast::flat_buffer buffer;
beast::flat_buffer buffer;
// This lambda is used to send messages
send_lambda<ssl::stream<tcp::socket&>> lambda{stream, close, ec, yield};
@@ -311,13 +313,13 @@ do_session(
// Accepts incoming connections and launches the sessions
void
do_listen(
boost::asio::io_context& ioc,
net::io_context& ioc,
ssl::context& ctx,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root,
boost::asio::yield_context yield)
net::yield_context yield)
{
boost::system::error_code ec;
beast::error_code ec;
// Open the acceptor
tcp::acceptor acceptor(ioc);
@@ -326,7 +328,7 @@ do_listen(
return fail(ec, "open");
// 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)
return fail(ec, "set_option");
@@ -336,7 +338,7 @@ do_listen(
return fail(ec, "bind");
// Start listening for connections
acceptor.listen(boost::asio::socket_base::max_listen_connections, ec);
acceptor.listen(net::socket_base::max_listen_connections, ec);
if(ec)
return fail(ec, "listen");
@@ -347,7 +349,7 @@ do_listen(
if(ec)
fail(ec, "accept");
else
boost::asio::spawn(
net::spawn(
acceptor.get_executor().context(),
std::bind(
&do_session,
@@ -369,13 +371,13 @@ int main(int argc, char* argv[])
" http-server-coro-ssl 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};
@@ -384,7 +386,7 @@ int main(int argc, char* argv[])
load_server_certificate(ctx);
// Spawn a listening port
boost::asio::spawn(ioc,
net::spawn(ioc,
std::bind(
&do_listen,
std::ref(ioc),
+35 -33
View File
@@ -27,19 +27,21 @@
#include <thread>
#include <vector>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.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>
// 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";
@@ -70,8 +72,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();
@@ -102,13 +104,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);
@@ -121,7 +123,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);
@@ -134,7 +136,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);
@@ -153,7 +155,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
@@ -162,12 +164,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
@@ -204,7 +206,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";
}
@@ -216,15 +218,15 @@ struct send_lambda
{
Stream& stream_;
bool& close_;
boost::system::error_code& ec_;
boost::asio::yield_context yield_;
beast::error_code& ec_;
net::yield_context yield_;
explicit
send_lambda(
Stream& stream,
bool& close,
boost::system::error_code& ec,
boost::asio::yield_context yield)
beast::error_code& ec,
net::yield_context yield)
: stream_(stream)
, close_(close)
, ec_(ec)
@@ -252,13 +254,13 @@ void
do_session(
tcp::socket& socket,
std::shared_ptr<std::string const> const& doc_root,
boost::asio::yield_context yield)
net::yield_context yield)
{
bool close = false;
boost::system::error_code ec;
beast::error_code ec;
// This buffer is required to persist across reads
boost::beast::flat_buffer buffer;
beast::flat_buffer buffer;
// This lambda is used to send messages
send_lambda<tcp::socket> lambda{socket, close, ec, yield};
@@ -296,12 +298,12 @@ do_session(
// Accepts incoming connections and launches the sessions
void
do_listen(
boost::asio::io_context& ioc,
net::io_context& ioc,
tcp::endpoint endpoint,
std::shared_ptr<std::string const> const& doc_root,
boost::asio::yield_context yield)
net::yield_context yield)
{
boost::system::error_code ec;
beast::error_code ec;
// Open the acceptor
tcp::acceptor acceptor(ioc);
@@ -310,7 +312,7 @@ do_listen(
return fail(ec, "open");
// 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)
return fail(ec, "set_option");
@@ -320,7 +322,7 @@ do_listen(
return fail(ec, "bind");
// Start listening for connections
acceptor.listen(boost::asio::socket_base::max_listen_connections, ec);
acceptor.listen(net::socket_base::max_listen_connections, ec);
if(ec)
return fail(ec, "listen");
@@ -331,7 +333,7 @@ do_listen(
if(ec)
fail(ec, "accept");
else
boost::asio::spawn(
net::spawn(
acceptor.get_executor().context(),
std::bind(
&do_session,
@@ -352,16 +354,16 @@ int main(int argc, char* argv[])
" http-server-coro 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};
// Spawn a listening port
boost::asio::spawn(ioc,
net::spawn(ioc,
std::bind(
&do_listen,
std::ref(ioc),
+24 -23
View File
@@ -28,20 +28,21 @@
#include <memory>
#include <string>
namespace ip = boost::asio::ip; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.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";
@@ -88,7 +89,7 @@ public:
private:
using alloc_t = fields_alloc<char>;
//using request_body_t = http::basic_dynamic_body<boost::beast::flat_static_buffer<1024 * 1024>>;
//using request_body_t = http::basic_dynamic_body<beast::flat_static_buffer<1024 * 1024>>;
using request_body_t = http::string_body;
// The acceptor used to listen for incoming connections.
@@ -101,7 +102,7 @@ private:
tcp::socket socket_{acceptor_.get_executor().context()};
// The buffer for performing reads
boost::beast::flat_static_buffer<8192> buffer_;
beast::flat_static_buffer<8192> buffer_;
// The allocator used for the fields in the request and reply.
alloc_t alloc_{8192};
@@ -110,7 +111,7 @@ private:
boost::optional<http::request_parser<request_body_t, alloc_t>> parser_;
// The timer putting a time limit on requests.
boost::asio::basic_waitable_timer<std::chrono::steady_clock> request_deadline_{
net::basic_waitable_timer<std::chrono::steady_clock> request_deadline_{
acceptor_.get_executor().context(), (std::chrono::steady_clock::time_point::max)()};
// The string-based response message.
@@ -128,13 +129,13 @@ private:
void accept()
{
// Clean up any previous connection.
boost::beast::error_code ec;
beast::error_code ec;
socket_.close(ec);
buffer_.consume(buffer_.size());
acceptor_.async_accept(
socket_,
[this](boost::beast::error_code ec)
[this](beast::error_code ec)
{
if (ec)
{
@@ -173,7 +174,7 @@ private:
socket_,
buffer_,
*parser_,
[this](boost::beast::error_code ec, std::size_t)
[this](beast::error_code ec, std::size_t)
{
if (ec)
accept();
@@ -221,7 +222,7 @@ private:
http::async_write(
socket_,
*string_serializer_,
[this](boost::beast::error_code ec, std::size_t)
[this](beast::error_code ec, std::size_t)
{
socket_.shutdown(tcp::socket::shutdown_send, ec);
string_serializer_.reset();
@@ -230,7 +231,7 @@ private:
});
}
void send_file(boost::beast::string_view target)
void send_file(beast::string_view target)
{
// Request path must be absolute and not contain "..".
if (target.empty() || target[0] != '/' || target.find("..") != std::string::npos)
@@ -247,10 +248,10 @@ private:
target.size());
http::file_body::value_type file;
boost::beast::error_code ec;
beast::error_code ec;
file.open(
full_path.c_str(),
boost::beast::file_mode::read,
beast::file_mode::read,
ec);
if(ec)
{
@@ -277,7 +278,7 @@ private:
http::async_write(
socket_,
*file_serializer_,
[this](boost::beast::error_code ec, std::size_t)
[this](beast::error_code ec, std::size_t)
{
socket_.shutdown(tcp::socket::shutdown_send, ec);
file_serializer_.reset();
@@ -292,7 +293,7 @@ private:
if (request_deadline_.expiry() <= std::chrono::steady_clock::now())
{
// Close socket to cancel any outstanding operation.
boost::beast::error_code ec;
beast::error_code ec;
socket_.close();
// Sleep indefinitely until we're given a new deadline.
@@ -301,7 +302,7 @@ private:
}
request_deadline_.async_wait(
[this](boost::beast::error_code)
[this](beast::error_code)
{
check_deadline();
});
@@ -323,13 +324,13 @@ int main(int argc, char* argv[])
return EXIT_FAILURE;
}
auto const address = boost::asio::ip::make_address(argv[1]);
auto const address = net::ip::make_address(argv[1]);
unsigned short port = static_cast<unsigned short>(std::atoi(argv[2]));
std::string doc_root = argv[3];
int num_workers = std::atoi(argv[4]);
bool spin = (std::strcmp(argv[5], "spin") == 0);
boost::asio::io_context ioc{1};
net::io_context ioc{1};
tcp::acceptor acceptor{ioc, {address, port}};
std::list<http_worker> workers;
+54 -52
View File
@@ -32,20 +32,22 @@
#include <string>
#include <thread>
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 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>
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";
@@ -76,8 +78,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();
@@ -108,13 +110,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);
@@ -127,7 +129,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);
@@ -140,7 +142,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);
@@ -159,7 +161,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
@@ -168,12 +170,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
@@ -210,7 +212,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";
}
@@ -259,7 +261,7 @@ class session
http::async_write(
self_.derived().stream(),
*sp,
boost::asio::bind_executor(
net::bind_executor(
self_.strand_,
std::bind(
&session::on_write,
@@ -276,16 +278,16 @@ class session
send_lambda lambda_;
protected:
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::flat_buffer buffer_;
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_;
public:
// Take ownership of the buffer
explicit
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)
, lambda_(*this)
@@ -302,7 +304,7 @@ public:
derived().stream(),
buffer_,
req_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::on_read,
@@ -313,7 +315,7 @@ public:
void
on_read(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -331,7 +333,7 @@ public:
void
on_write(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred,
bool close)
{
@@ -361,14 +363,14 @@ class plain_session
, public std::enable_shared_from_this<plain_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 session
plain_session(
tcp::socket socket,
boost::beast::flat_buffer buffer,
beast::flat_buffer buffer,
std::shared_ptr<std::string const> const& doc_root)
: session<plain_session>(
socket.get_executor().context(),
@@ -397,7 +399,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
@@ -411,15 +413,15 @@ class ssl_session
{
tcp::socket socket_;
ssl::stream<tcp::socket&> stream_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
net::strand<
net::io_context::executor_type> strand_;
public:
// Create the session
ssl_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)
: session<ssl_session>(
socket.get_executor().context(),
@@ -447,7 +449,7 @@ public:
stream_.async_handshake(
ssl::stream_base::server,
buffer_.data(),
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&ssl_session::on_handshake,
@@ -457,7 +459,7 @@ public:
}
void
on_handshake(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_used)
{
if(ec)
@@ -474,7 +476,7 @@ public:
{
// Perform the SSL shutdown
stream_.async_shutdown(
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&ssl_session::on_shutdown,
@@ -483,7 +485,7 @@ public:
}
void
on_shutdown(boost::system::error_code ec)
on_shutdown(beast::error_code ec)
{
if(ec)
return fail(ec, "shutdown");
@@ -499,10 +501,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
@@ -524,7 +526,7 @@ public:
async_detect_ssl(
socket_,
buffer_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&detect_session::on_detect,
@@ -535,7 +537,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");
@@ -563,15 +565,15 @@ public:
class listener : public std::enable_shared_from_this<listener>
{
ssl::context& ctx_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
net::strand<
net::io_context::executor_type> strand_;
tcp::acceptor acceptor_;
tcp::socket socket_;
std::shared_ptr<std::string const> doc_root_;
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)
@@ -581,7 +583,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);
@@ -592,7 +594,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");
@@ -609,7 +611,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");
@@ -638,7 +640,7 @@ public:
}
void
on_accept(boost::system::error_code ec)
on_accept(beast::error_code ec)
{
if(ec)
{
@@ -671,13 +673,13 @@ int main(int argc, char* argv[])
" http-server-flex 0.0.0.0 8080 .\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};
+16 -15
View File
@@ -24,9 +24,10 @@
#include <memory>
#include <string>
namespace ip = boost::asio::ip; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio.hpp>
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace my_program_state
{
@@ -65,7 +66,7 @@ private:
tcp::socket socket_;
// The buffer for performing reads.
boost::beast::flat_buffer buffer_{8192};
beast::flat_buffer buffer_{8192};
// The request message.
http::request<http::dynamic_body> request_;
@@ -74,7 +75,7 @@ private:
http::response<http::dynamic_body> response_;
// The timer for putting a deadline on connection processing.
boost::asio::basic_waitable_timer<std::chrono::steady_clock> deadline_{
net::basic_waitable_timer<std::chrono::steady_clock> deadline_{
socket_.get_executor().context(), std::chrono::seconds(60)};
// Asynchronously receive a complete request message.
@@ -87,7 +88,7 @@ private:
socket_,
buffer_,
request_,
[self](boost::beast::error_code ec,
[self](beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
@@ -116,7 +117,7 @@ private:
// we do not recognize the request method.
response_.result(http::status::bad_request);
response_.set(http::field::content_type, "text/plain");
boost::beast::ostream(response_.body())
beast::ostream(response_.body())
<< "Invalid request-method '"
<< request_.method_string().to_string()
<< "'";
@@ -133,7 +134,7 @@ private:
if(request_.target() == "/count")
{
response_.set(http::field::content_type, "text/html");
boost::beast::ostream(response_.body())
beast::ostream(response_.body())
<< "<html>\n"
<< "<head><title>Request count</title></head>\n"
<< "<body>\n"
@@ -147,7 +148,7 @@ private:
else if(request_.target() == "/time")
{
response_.set(http::field::content_type, "text/html");
boost::beast::ostream(response_.body())
beast::ostream(response_.body())
<< "<html>\n"
<< "<head><title>Current time</title></head>\n"
<< "<body>\n"
@@ -162,7 +163,7 @@ private:
{
response_.result(http::status::not_found);
response_.set(http::field::content_type, "text/plain");
boost::beast::ostream(response_.body()) << "File not found\r\n";
beast::ostream(response_.body()) << "File not found\r\n";
}
}
@@ -177,7 +178,7 @@ private:
http::async_write(
socket_,
response_,
[self](boost::beast::error_code ec, std::size_t)
[self](beast::error_code ec, std::size_t)
{
self->socket_.shutdown(tcp::socket::shutdown_send, ec);
self->deadline_.cancel();
@@ -191,7 +192,7 @@ private:
auto self = shared_from_this();
deadline_.async_wait(
[self](boost::beast::error_code ec)
[self](beast::error_code ec)
{
if(!ec)
{
@@ -207,7 +208,7 @@ void
http_server(tcp::acceptor& acceptor, tcp::socket& socket)
{
acceptor.async_accept(socket,
[&](boost::beast::error_code ec)
[&](beast::error_code ec)
{
if(!ec)
std::make_shared<http_connection>(std::move(socket))->start();
@@ -231,10 +232,10 @@ main(int argc, char* argv[])
return EXIT_FAILURE;
}
auto const address = boost::asio::ip::make_address(argv[1]);
auto const address = net::ip::make_address(argv[1]);
unsigned short port = static_cast<unsigned short>(std::atoi(argv[2]));
boost::asio::io_context ioc{1};
net::io_context ioc{1};
tcp::acceptor acceptor{ioc, {address, port}};
tcp::socket socket{ioc};
@@ -33,20 +33,22 @@
#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 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>
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";
@@ -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,14 +213,14 @@ 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";
}
// Handles an HTTP server connection
class session
: public boost::asio::coroutine
: public net::coroutine
, public std::enable_shared_from_this<session>
{
// This is the C++11 equivalent of a generic lambda.
@@ -251,7 +253,7 @@ class session
http::async_write(
self_.stream_,
*sp,
boost::asio::bind_executor(
net::bind_executor(
self_.strand_,
std::bind(
&session::loop,
@@ -264,9 +266,9 @@ class session
tcp::socket socket_;
ssl::stream<tcp::socket&> stream_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::flat_buffer buffer_;
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
std::shared_ptr<void> res_;
@@ -297,7 +299,7 @@ public:
#include <boost/asio/yield.hpp>
void
loop(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred,
bool close)
{
@@ -307,7 +309,7 @@ public:
// Perform the SSL handshake
yield stream_.async_handshake(
ssl::stream_base::server,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -326,7 +328,7 @@ public:
// Read a request
yield http::async_read(stream_, buffer_, req_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -359,7 +361,7 @@ public:
// Perform the SSL shutdown
yield stream_.async_shutdown(
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -380,7 +382,7 @@ public:
// Accepts incoming connections and launches the sessions
class listener
: public boost::asio::coroutine
: public net::coroutine
, public std::enable_shared_from_this<listener>
{
ssl::context& ctx_;
@@ -390,7 +392,7 @@ class 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)
@@ -399,7 +401,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);
@@ -410,7 +412,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");
@@ -427,7 +429,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");
@@ -446,7 +448,7 @@ public:
#include <boost/asio/yield.hpp>
void
loop(boost::system::error_code ec = {})
loop(beast::error_code ec = {})
{
reenter(*this)
{
@@ -489,13 +491,13 @@ int main(int argc, char* argv[])
" http-server-stackless-ssl 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};
@@ -30,19 +30,21 @@
#include <thread>
#include <vector>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.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>
// 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";
@@ -73,8 +75,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();
@@ -105,13 +107,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);
@@ -124,7 +126,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);
@@ -137,7 +139,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);
@@ -156,7 +158,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
@@ -165,12 +167,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
@@ -207,14 +209,14 @@ 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";
}
// Handles an HTTP server connection
class session
: public boost::asio::coroutine
: public net::coroutine
, public std::enable_shared_from_this<session>
{
// This is the C++11 equivalent of a generic lambda.
@@ -248,7 +250,7 @@ class session
http::async_write(
self_.socket_,
*sp,
boost::asio::bind_executor(
net::bind_executor(
self_.strand_,
std::bind(
&session::loop,
@@ -260,9 +262,9 @@ class session
};
tcp::socket socket_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
boost::beast::flat_buffer buffer_;
net::strand<
net::io_context::executor_type> strand_;
beast::flat_buffer buffer_;
std::shared_ptr<std::string const> doc_root_;
http::request<http::string_body> req_;
std::shared_ptr<void> res_;
@@ -291,7 +293,7 @@ public:
#include <boost/asio/yield.hpp>
void
loop(
boost::system::error_code ec,
beast::error_code ec,
std::size_t bytes_transferred,
bool close)
{
@@ -306,7 +308,7 @@ public:
// Read a request
yield http::async_read(socket_, buffer_, req_,
boost::asio::bind_executor(
net::bind_executor(
strand_,
std::bind(
&session::loop,
@@ -350,7 +352,7 @@ public:
// Accepts incoming connections and launches the sessions
class listener
: public boost::asio::coroutine
: public net::coroutine
, public std::enable_shared_from_this<listener>
{
tcp::acceptor acceptor_;
@@ -359,14 +361,14 @@ class 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);
@@ -377,7 +379,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");
@@ -393,7 +395,7 @@ public:
}
// Start listening for connections
acceptor_.listen(boost::asio::socket_base::max_listen_connections, ec);
acceptor_.listen(net::socket_base::max_listen_connections, ec);
if(ec)
{
fail(ec, "listen");
@@ -412,7 +414,7 @@ public:
#include <boost/asio/yield.hpp>
void
loop(boost::system::error_code ec = {})
loop(beast::error_code ec = {})
{
reenter(*this)
{
@@ -454,13 +456,13 @@ int main(int argc, char* argv[])
" http-server-stackless 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>(
@@ -27,20 +27,22 @@
#include <string>
#include <thread>
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 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>
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";
@@ -71,8 +73,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();
@@ -103,13 +105,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);
@@ -122,7 +124,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);
@@ -135,7 +137,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);
@@ -154,7 +156,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
@@ -163,12 +165,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
@@ -205,7 +207,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";
}
@@ -217,13 +219,13 @@ struct send_lambda
{
Stream& stream_;
bool& close_;
boost::system::error_code& ec_;
beast::error_code& ec_;
explicit
send_lambda(
Stream& stream,
bool& close,
boost::system::error_code& ec)
beast::error_code& ec)
: stream_(stream)
, close_(close)
, ec_(ec)
@@ -253,7 +255,7 @@ do_session(
std::shared_ptr<std::string const> const& doc_root)
{
bool close = false;
boost::system::error_code ec;
beast::error_code ec;
// Construct the stream around the socket
ssl::stream<tcp::socket&> stream{socket, ctx};
@@ -264,7 +266,7 @@ do_session(
return fail(ec, "handshake");
// This buffer is required to persist across reads
boost::beast::flat_buffer buffer;
beast::flat_buffer buffer;
// This lambda is used to send messages
send_lambda<ssl::stream<tcp::socket&>> lambda{stream, close, ec};
@@ -314,12 +316,12 @@ int main(int argc, char* argv[])
" http-server-sync-ssl 0.0.0.0 8080 .\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]);
// The io_context is required for all I/O
boost::asio::io_context ioc{1};
net::io_context ioc{1};
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23};
+25 -23
View File
@@ -24,21 +24,23 @@
#include <string>
#include <thread>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.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>
//------------------------------------------------------------------------------
// 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";
@@ -69,8 +71,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();
@@ -101,13 +103,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);
@@ -120,7 +122,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);
@@ -133,7 +135,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);
@@ -152,7 +154,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
@@ -161,12 +163,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
@@ -203,7 +205,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";
}
@@ -215,13 +217,13 @@ struct send_lambda
{
Stream& stream_;
bool& close_;
boost::system::error_code& ec_;
beast::error_code& ec_;
explicit
send_lambda(
Stream& stream,
bool& close,
boost::system::error_code& ec)
beast::error_code& ec)
: stream_(stream)
, close_(close)
, ec_(ec)
@@ -250,10 +252,10 @@ do_session(
std::shared_ptr<std::string const> const& doc_root)
{
bool close = false;
boost::system::error_code ec;
beast::error_code ec;
// This buffer is required to persist across reads
boost::beast::flat_buffer buffer;
beast::flat_buffer buffer;
// This lambda is used to send messages
send_lambda<tcp::socket> lambda{socket, close, ec};
@@ -301,12 +303,12 @@ int main(int argc, char* argv[])
" http-server-sync 0.0.0.0 8080 .\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]);
// The io_context is required for all I/O
boost::asio::io_context ioc{1};
net::io_context ioc{1};
// The acceptor receives incoming connections
tcp::acceptor acceptor{ioc, {address, port}};