forked from boostorg/beast
Examples use http::message_generator
This commit is contained in:
@@ -94,18 +94,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 =
|
||||
@@ -149,13 +146,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());
|
||||
@@ -169,11 +166,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();
|
||||
@@ -186,7 +183,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
|
||||
@@ -198,7 +195,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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -210,56 +207,17 @@ 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(
|
||||
tcp::socket& socket,
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
{
|
||||
bool close = false;
|
||||
beast::error_code ec;
|
||||
|
||||
// This buffer is required to persist across reads
|
||||
beast::flat_buffer buffer;
|
||||
|
||||
// This lambda is used to send messages
|
||||
send_lambda<tcp::socket> lambda{socket, close, ec};
|
||||
|
||||
for(;;)
|
||||
{
|
||||
// Read a request
|
||||
@@ -270,11 +228,19 @@ 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(socket, 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.
|
||||
|
||||
Reference in New Issue
Block a user