Examples use http::message_generator

This commit is contained in:
sehe
2022-05-21 22:12:13 -07:00
committed by Vinnie Falco
parent 740879a995
commit 87ddb16151
12 changed files with 429 additions and 898 deletions
@@ -97,18 +97,15 @@ path_cat(
return result;
}
// This function produces an HTTP response for the given
// request. The type of the response object depends on the
// contents of the request, so the interface requires the
// caller to pass a generic lambda for receiving the response.
template<
class Body, class Allocator,
class Send>
void
// Return a response for the given request.
//
// The concrete type of the response message (which depends on the
// request), is type-erased in message_generator.
template <class Body, class Allocator>
http::message_generator
handle_request(
beast::string_view doc_root,
http::request<Body, http::basic_fields<Allocator>>&& req,
Send&& send)
http::request<Body, http::basic_fields<Allocator>>&& req)
{
// Returns a bad request response
auto const bad_request =
@@ -152,13 +149,13 @@ handle_request(
// Make sure we can handle the method
if( req.method() != http::verb::get &&
req.method() != http::verb::head)
return send(bad_request("Unknown HTTP-method"));
return bad_request("Unknown HTTP-method");
// Request path must be absolute and not contain "..".
if( req.target().empty() ||
req.target()[0] != '/' ||
req.target().find("..") != beast::string_view::npos)
return send(bad_request("Illegal request-target"));
return bad_request("Illegal request-target");
// Build the path to the requested file
std::string path = path_cat(doc_root, req.target());
@@ -172,11 +169,11 @@ handle_request(
// Handle the case where the file doesn't exist
if(ec == beast::errc::no_such_file_or_directory)
return send(not_found(req.target()));
return not_found(req.target());
// Handle an unknown error
if(ec)
return send(server_error(ec.message()));
return server_error(ec.message());
// Cache the size since we need it after the move
auto const size = body.size();
@@ -189,7 +186,7 @@ handle_request(
res.set(http::field::content_type, mime_type(path));
res.content_length(size);
res.keep_alive(req.keep_alive());
return send(std::move(res));
return res;
}
// Respond to GET request
@@ -201,7 +198,7 @@ handle_request(
res.set(http::field::content_type, mime_type(path));
res.content_length(size);
res.keep_alive(req.keep_alive());
return send(std::move(res));
return res;
}
//------------------------------------------------------------------------------
@@ -213,41 +210,6 @@ fail(beast::error_code ec, char const* what)
std::cerr << what << ": " << ec.message() << "\n";
}
// This is the C++11 equivalent of a generic lambda.
// The function object is used to send an HTTP message.
template<class Stream>
struct send_lambda
{
Stream& stream_;
bool& close_;
beast::error_code& ec_;
explicit
send_lambda(
Stream& stream,
bool& close,
beast::error_code& ec)
: stream_(stream)
, close_(close)
, ec_(ec)
{
}
template<bool isRequest, class Body, class Fields>
void
operator()(http::message<isRequest, Body, Fields>&& msg) const
{
// Determine if we should close the connection after
close_ = msg.need_eof();
// We need the serializer here because the serializer requires
// a non-const file_body, and the message oriented version of
// http::write only works with const messages.
http::serializer<isRequest, Body, Fields> sr{msg};
http::write(stream_, sr, ec_);
}
};
// Handles an HTTP server connection
void
do_session(
@@ -255,7 +217,6 @@ do_session(
ssl::context& ctx,
std::shared_ptr<std::string const> const& doc_root)
{
bool close = false;
beast::error_code ec;
// Construct the stream around the socket
@@ -269,9 +230,6 @@ do_session(
// This buffer is required to persist across reads
beast::flat_buffer buffer;
// This lambda is used to send messages
send_lambda<beast::ssl_stream<tcp::socket&>> lambda{stream, close, ec};
for(;;)
{
// Read a request
@@ -282,11 +240,20 @@ do_session(
if(ec)
return fail(ec, "read");
// Handle request
http::message_generator msg =
handle_request(*doc_root, std::move(req));
// Determine if we should close the connection
bool keep_alive = msg.keep_alive();
// Send the response
handle_request(*doc_root, std::move(req), lambda);
beast::write(stream, std::move(msg), ec);
if(ec)
return fail(ec, "write");
if(close)
if(! keep_alive)
{
// This means we should close the connection, usually because
// the response indicated the "Connection: close" semantic.