mirror of
https://github.com/boostorg/beast.git
synced 2026-05-05 12:14:23 +02:00
Examples use http::message_generator
This commit is contained in:
@@ -100,18 +100,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 =
|
||||
@@ -155,13 +152,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());
|
||||
@@ -175,11 +172,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();
|
||||
@@ -192,7 +189,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
|
||||
@@ -204,7 +201,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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -239,49 +236,10 @@ fail(beast::error_code ec, char const* what)
|
||||
// Handles an HTTP server connection
|
||||
class session : public std::enable_shared_from_this<session>
|
||||
{
|
||||
// This is the C++11 equivalent of a generic lambda.
|
||||
// The function object is used to send an HTTP message.
|
||||
struct send_lambda
|
||||
{
|
||||
session& self_;
|
||||
|
||||
explicit
|
||||
send_lambda(session& self)
|
||||
: self_(self)
|
||||
{
|
||||
}
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
operator()(http::message<isRequest, Body, Fields>&& msg) const
|
||||
{
|
||||
// The lifetime of the message has to extend
|
||||
// for the duration of the async operation so
|
||||
// we use a shared_ptr to manage it.
|
||||
auto sp = std::make_shared<
|
||||
http::message<isRequest, Body, Fields>>(std::move(msg));
|
||||
|
||||
// Store a type-erased version of the shared
|
||||
// pointer in the class to keep it alive.
|
||||
self_.res_ = sp;
|
||||
|
||||
// Write the response
|
||||
http::async_write(
|
||||
self_.stream_,
|
||||
*sp,
|
||||
beast::bind_front_handler(
|
||||
&session::on_write,
|
||||
self_.shared_from_this(),
|
||||
sp->need_eof()));
|
||||
}
|
||||
};
|
||||
|
||||
beast::ssl_stream<beast::tcp_stream> stream_;
|
||||
beast::flat_buffer buffer_;
|
||||
std::shared_ptr<std::string const> doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
std::shared_ptr<void> res_;
|
||||
send_lambda lambda_;
|
||||
|
||||
public:
|
||||
// Take ownership of the socket
|
||||
@@ -292,7 +250,6 @@ public:
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
: stream_(std::move(socket), ctx)
|
||||
, doc_root_(doc_root)
|
||||
, lambda_(*this)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -367,12 +324,28 @@ public:
|
||||
return fail(ec, "read");
|
||||
|
||||
// Send the response
|
||||
handle_request(*doc_root_, std::move(req_), lambda_);
|
||||
send_response(
|
||||
handle_request(*doc_root_, std::move(req_)));
|
||||
}
|
||||
|
||||
void
|
||||
send_response(http::message_generator&& msg)
|
||||
{
|
||||
bool keep_alive = msg.keep_alive();
|
||||
|
||||
// Write the response
|
||||
beast::async_write(
|
||||
stream_,
|
||||
std::move(msg),
|
||||
beast::bind_front_handler(
|
||||
&session::on_write,
|
||||
this->shared_from_this(),
|
||||
keep_alive));
|
||||
}
|
||||
|
||||
void
|
||||
on_write(
|
||||
bool close,
|
||||
bool keep_alive,
|
||||
beast::error_code ec,
|
||||
std::size_t bytes_transferred)
|
||||
{
|
||||
@@ -381,16 +354,13 @@ public:
|
||||
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.
|
||||
return do_close();
|
||||
}
|
||||
|
||||
// We're done with the response so delete it
|
||||
res_ = nullptr;
|
||||
|
||||
// Read another request
|
||||
do_read();
|
||||
}
|
||||
|
||||
@@ -96,18 +96,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 =
|
||||
@@ -151,13 +148,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());
|
||||
@@ -171,11 +168,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();
|
||||
@@ -188,7 +185,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
|
||||
@@ -200,7 +197,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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -215,49 +212,10 @@ fail(beast::error_code ec, char const* what)
|
||||
// Handles an HTTP server connection
|
||||
class session : public std::enable_shared_from_this<session>
|
||||
{
|
||||
// This is the C++11 equivalent of a generic lambda.
|
||||
// The function object is used to send an HTTP message.
|
||||
struct send_lambda
|
||||
{
|
||||
session& self_;
|
||||
|
||||
explicit
|
||||
send_lambda(session& self)
|
||||
: self_(self)
|
||||
{
|
||||
}
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
operator()(http::message<isRequest, Body, Fields>&& msg) const
|
||||
{
|
||||
// The lifetime of the message has to extend
|
||||
// for the duration of the async operation so
|
||||
// we use a shared_ptr to manage it.
|
||||
auto sp = std::make_shared<
|
||||
http::message<isRequest, Body, Fields>>(std::move(msg));
|
||||
|
||||
// Store a type-erased version of the shared
|
||||
// pointer in the class to keep it alive.
|
||||
self_.res_ = sp;
|
||||
|
||||
// Write the response
|
||||
http::async_write(
|
||||
self_.stream_,
|
||||
*sp,
|
||||
beast::bind_front_handler(
|
||||
&session::on_write,
|
||||
self_.shared_from_this(),
|
||||
sp->need_eof()));
|
||||
}
|
||||
};
|
||||
|
||||
beast::tcp_stream stream_;
|
||||
beast::flat_buffer buffer_;
|
||||
std::shared_ptr<std::string const> doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
std::shared_ptr<void> res_;
|
||||
send_lambda lambda_;
|
||||
|
||||
public:
|
||||
// Take ownership of the stream
|
||||
@@ -266,7 +224,6 @@ public:
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
: stream_(std::move(socket))
|
||||
, doc_root_(doc_root)
|
||||
, lambda_(*this)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -316,12 +273,26 @@ public:
|
||||
return fail(ec, "read");
|
||||
|
||||
// Send the response
|
||||
handle_request(*doc_root_, std::move(req_), lambda_);
|
||||
send_response(
|
||||
handle_request(*doc_root_, std::move(req_)));
|
||||
}
|
||||
|
||||
void
|
||||
send_response(http::message_generator&& msg)
|
||||
{
|
||||
bool keep_alive = msg.keep_alive();
|
||||
|
||||
// Write the response
|
||||
beast::async_write(
|
||||
stream_,
|
||||
std::move(msg),
|
||||
beast::bind_front_handler(
|
||||
&session::on_write, shared_from_this(), keep_alive));
|
||||
}
|
||||
|
||||
void
|
||||
on_write(
|
||||
bool close,
|
||||
bool keep_alive,
|
||||
beast::error_code ec,
|
||||
std::size_t bytes_transferred)
|
||||
{
|
||||
@@ -330,16 +301,13 @@ public:
|
||||
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.
|
||||
return do_close();
|
||||
}
|
||||
|
||||
// We're done with the response so delete it
|
||||
res_ = nullptr;
|
||||
|
||||
// Read another request
|
||||
do_read();
|
||||
}
|
||||
|
||||
@@ -98,18 +98,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 =
|
||||
@@ -153,13 +150,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());
|
||||
@@ -173,11 +170,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();
|
||||
@@ -190,7 +187,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
|
||||
@@ -202,7 +199,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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -234,42 +231,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.
|
||||
struct send_lambda
|
||||
{
|
||||
beast::ssl_stream<beast::tcp_stream>& stream_;
|
||||
bool& close_;
|
||||
beast::error_code& ec_;
|
||||
net::yield_context yield_;
|
||||
|
||||
send_lambda(
|
||||
beast::ssl_stream<beast::tcp_stream>& stream,
|
||||
bool& close,
|
||||
beast::error_code& ec,
|
||||
net::yield_context yield)
|
||||
: stream_(stream)
|
||||
, close_(close)
|
||||
, ec_(ec)
|
||||
, yield_(yield)
|
||||
{
|
||||
}
|
||||
|
||||
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::async_write(stream_, sr, yield_[ec_]);
|
||||
}
|
||||
};
|
||||
|
||||
// Handles an HTTP server connection
|
||||
void
|
||||
do_session(
|
||||
@@ -277,7 +238,7 @@ do_session(
|
||||
std::shared_ptr<std::string const> const& doc_root,
|
||||
net::yield_context yield)
|
||||
{
|
||||
bool close = false;
|
||||
bool keep_alive = true;
|
||||
beast::error_code ec;
|
||||
|
||||
// Set the timeout.
|
||||
@@ -291,9 +252,6 @@ do_session(
|
||||
// This buffer is required to persist across reads
|
||||
beast::flat_buffer buffer;
|
||||
|
||||
// This lambda is used to send messages
|
||||
send_lambda lambda{stream, close, ec, yield};
|
||||
|
||||
for(;;)
|
||||
{
|
||||
// Set the timeout.
|
||||
@@ -307,11 +265,19 @@ do_session(
|
||||
if(ec)
|
||||
return fail(ec, "read");
|
||||
|
||||
// Handle the request
|
||||
http::message_generator res =
|
||||
handle_request(*doc_root, std::move(req));
|
||||
|
||||
// Determine if we should close the connection
|
||||
keep_alive = res.keep_alive();
|
||||
|
||||
// Send the response
|
||||
handle_request(*doc_root, std::move(req), lambda);
|
||||
beast::async_write(stream, std::move(res), yield[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.
|
||||
|
||||
@@ -95,18 +95,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 =
|
||||
@@ -150,13 +147,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());
|
||||
@@ -170,11 +167,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();
|
||||
@@ -187,7 +184,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
|
||||
@@ -199,7 +196,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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -211,41 +208,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.
|
||||
struct send_lambda
|
||||
{
|
||||
beast::tcp_stream& stream_;
|
||||
bool& close_;
|
||||
beast::error_code& ec_;
|
||||
net::yield_context yield_;
|
||||
|
||||
send_lambda(
|
||||
beast::tcp_stream& stream,
|
||||
bool& close,
|
||||
beast::error_code& ec,
|
||||
net::yield_context yield)
|
||||
: stream_(stream)
|
||||
, close_(close)
|
||||
, ec_(ec)
|
||||
, yield_(yield)
|
||||
{
|
||||
}
|
||||
|
||||
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::async_write(stream_, sr, yield_[ec_]);
|
||||
}
|
||||
};
|
||||
|
||||
// Handles an HTTP server connection
|
||||
void
|
||||
@@ -254,15 +216,12 @@ do_session(
|
||||
std::shared_ptr<std::string const> const& doc_root,
|
||||
net::yield_context yield)
|
||||
{
|
||||
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 lambda{stream, close, ec, yield};
|
||||
|
||||
for(;;)
|
||||
{
|
||||
// Set the timeout.
|
||||
@@ -276,11 +235,20 @@ do_session(
|
||||
if(ec)
|
||||
return fail(ec, "read");
|
||||
|
||||
// Handle the 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::async_write(stream, std::move(msg), yield[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.
|
||||
|
||||
@@ -99,18 +99,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 =
|
||||
@@ -154,13 +151,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());
|
||||
@@ -174,11 +171,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();
|
||||
@@ -191,7 +188,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
|
||||
@@ -203,7 +200,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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -249,47 +246,8 @@ class session
|
||||
return static_cast<Derived&>(*this);
|
||||
}
|
||||
|
||||
// This is the C++11 equivalent of a generic lambda.
|
||||
// The function object is used to send an HTTP message.
|
||||
struct send_lambda
|
||||
{
|
||||
session& self_;
|
||||
|
||||
explicit
|
||||
send_lambda(session& self)
|
||||
: self_(self)
|
||||
{
|
||||
}
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
operator()(http::message<isRequest, Body, Fields>&& msg) const
|
||||
{
|
||||
// The lifetime of the message has to extend
|
||||
// for the duration of the async operation so
|
||||
// we use a shared_ptr to manage it.
|
||||
auto sp = std::make_shared<
|
||||
http::message<isRequest, Body, Fields>>(std::move(msg));
|
||||
|
||||
// Store a type-erased version of the shared
|
||||
// pointer in the class to keep it alive.
|
||||
self_.res_ = sp;
|
||||
|
||||
// Write the response
|
||||
http::async_write(
|
||||
self_.derived().stream(),
|
||||
*sp,
|
||||
beast::bind_front_handler(
|
||||
&session::on_write,
|
||||
self_.derived().shared_from_this(),
|
||||
sp->need_eof()));
|
||||
}
|
||||
};
|
||||
|
||||
std::shared_ptr<std::string const> doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
std::shared_ptr<void> res_;
|
||||
send_lambda lambda_;
|
||||
|
||||
protected:
|
||||
beast::flat_buffer buffer_;
|
||||
@@ -300,7 +258,6 @@ public:
|
||||
beast::flat_buffer buffer,
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
: doc_root_(doc_root)
|
||||
, lambda_(*this)
|
||||
, buffer_(std::move(buffer))
|
||||
{
|
||||
}
|
||||
@@ -337,12 +294,28 @@ public:
|
||||
return fail(ec, "read");
|
||||
|
||||
// Send the response
|
||||
handle_request(*doc_root_, std::move(req_), lambda_);
|
||||
send_response(
|
||||
handle_request(*doc_root_, std::move(req_)));
|
||||
}
|
||||
|
||||
void
|
||||
send_response(http::message_generator&& msg)
|
||||
{
|
||||
bool keep_alive = msg.keep_alive();
|
||||
|
||||
// Write the response
|
||||
beast::async_write(
|
||||
derived().stream(),
|
||||
std::move(msg),
|
||||
beast::bind_front_handler(
|
||||
&session::on_write,
|
||||
derived().shared_from_this(),
|
||||
keep_alive));
|
||||
}
|
||||
|
||||
void
|
||||
on_write(
|
||||
bool close,
|
||||
bool keep_alive,
|
||||
beast::error_code ec,
|
||||
std::size_t bytes_transferred)
|
||||
{
|
||||
@@ -351,16 +324,13 @@ public:
|
||||
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.
|
||||
return derived().do_eof();
|
||||
}
|
||||
|
||||
// We're done with the response so delete it
|
||||
res_ = nullptr;
|
||||
|
||||
// Read another request
|
||||
do_read();
|
||||
}
|
||||
|
||||
@@ -101,18 +101,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 =
|
||||
@@ -156,13 +153,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());
|
||||
@@ -176,11 +173,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();
|
||||
@@ -193,7 +190,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
|
||||
@@ -205,7 +202,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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -242,51 +239,11 @@ class session
|
||||
: public boost::asio::coroutine
|
||||
, public std::enable_shared_from_this<session>
|
||||
{
|
||||
// This is the C++11 equivalent of a generic lambda.
|
||||
// The function object is used to send an HTTP message.
|
||||
struct send_lambda
|
||||
{
|
||||
session& self_;
|
||||
|
||||
explicit
|
||||
send_lambda(session& self)
|
||||
: self_(self)
|
||||
{
|
||||
}
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
operator()(http::message<isRequest, Body, Fields>&& msg) const
|
||||
{
|
||||
// The lifetime of the message has to extend
|
||||
// for the duration of the async operation so
|
||||
// we use a shared_ptr to manage it.
|
||||
auto sp = std::make_shared<
|
||||
http::message<isRequest, Body, Fields>>(std::move(msg));
|
||||
|
||||
// Store a type-erased version of the shared
|
||||
// pointer in the class to keep it alive.
|
||||
self_.res_ = sp;
|
||||
|
||||
// Write the response
|
||||
http::async_write(
|
||||
self_.stream_,
|
||||
*sp,
|
||||
std::bind(
|
||||
&session::loop,
|
||||
self_.shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
sp->need_eof()));
|
||||
}
|
||||
};
|
||||
|
||||
beast::ssl_stream<beast::tcp_stream> stream_;
|
||||
beast::flat_buffer buffer_;
|
||||
std::shared_ptr<std::string const> doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
std::shared_ptr<void> res_;
|
||||
send_lambda lambda_;
|
||||
bool keep_alive_ = true;
|
||||
|
||||
public:
|
||||
// Take ownership of the socket
|
||||
@@ -297,7 +254,6 @@ public:
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
: stream_(std::move(socket), ctx)
|
||||
, doc_root_(doc_root)
|
||||
, lambda_(*this)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -313,17 +269,13 @@ public:
|
||||
beast::bind_front_handler(&session::loop,
|
||||
shared_from_this(),
|
||||
beast::error_code{},
|
||||
0,
|
||||
false));
|
||||
0));
|
||||
}
|
||||
|
||||
#include <boost/asio/yield.hpp>
|
||||
|
||||
void
|
||||
loop(
|
||||
beast::error_code ec,
|
||||
std::size_t bytes_transferred,
|
||||
bool close)
|
||||
loop(beast::error_code ec, std::size_t bytes_transferred)
|
||||
{
|
||||
boost::ignore_unused(bytes_transferred);
|
||||
reenter(*this)
|
||||
@@ -335,11 +287,8 @@ public:
|
||||
yield stream_.async_handshake(
|
||||
ssl::stream_base::server,
|
||||
std::bind(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
0,
|
||||
false));
|
||||
&session::loop, shared_from_this(),
|
||||
std::placeholders::_1, 0));
|
||||
if(ec)
|
||||
return fail(ec, "handshake");
|
||||
|
||||
@@ -353,34 +302,41 @@ public:
|
||||
req_ = {};
|
||||
|
||||
// Read a request
|
||||
yield http::async_read(stream_, buffer_, req_,
|
||||
std::bind(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2,
|
||||
false));
|
||||
if(ec == http::error::end_of_stream)
|
||||
{
|
||||
yield http::async_read(
|
||||
stream_, buffer_, req_,
|
||||
beast::bind_front_handler(
|
||||
&session::loop, shared_from_this()));
|
||||
|
||||
if (ec == http::error::end_of_stream) {
|
||||
// The remote host closed the connection
|
||||
break;
|
||||
}
|
||||
if(ec)
|
||||
return fail(ec, "read");
|
||||
|
||||
// Send the response
|
||||
yield handle_request(*doc_root_, std::move(req_), lambda_);
|
||||
yield {
|
||||
// Handle request
|
||||
http::message_generator msg =
|
||||
handle_request(*doc_root_, std::move(req_));
|
||||
|
||||
// Determine if we should close the connection
|
||||
keep_alive_ = msg.keep_alive();
|
||||
|
||||
// Send the response
|
||||
beast::async_write(
|
||||
stream_, std::move(msg),
|
||||
beast::bind_front_handler(
|
||||
&session::loop, shared_from_this()));
|
||||
}
|
||||
|
||||
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.
|
||||
break;
|
||||
}
|
||||
|
||||
// We're done with the response so delete it
|
||||
res_ = nullptr;
|
||||
}
|
||||
|
||||
// Set the timeout.
|
||||
@@ -392,8 +348,7 @@ public:
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
0,
|
||||
false));
|
||||
0));
|
||||
if(ec)
|
||||
return fail(ec, "shutdown");
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -218,50 +215,11 @@ class session
|
||||
: public boost::asio::coroutine
|
||||
, public std::enable_shared_from_this<session>
|
||||
{
|
||||
// This is the C++11 equivalent of a generic lambda.
|
||||
// The function object is used to send an HTTP message.
|
||||
struct send_lambda
|
||||
{
|
||||
session& self_;
|
||||
std::shared_ptr<void> res_;
|
||||
|
||||
explicit
|
||||
send_lambda(session& self)
|
||||
: self_(self)
|
||||
{
|
||||
}
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
operator()(http::message<isRequest, Body, Fields>&& msg) const
|
||||
{
|
||||
// The lifetime of the message has to extend
|
||||
// for the duration of the async operation so
|
||||
// we use a shared_ptr to manage it.
|
||||
auto sp = std::make_shared<
|
||||
http::message<isRequest, Body, Fields>>(std::move(msg));
|
||||
|
||||
// Store a type-erased version of the shared
|
||||
// pointer in the class to keep it alive.
|
||||
self_.res_ = sp;
|
||||
|
||||
// Write the response
|
||||
http::async_write(
|
||||
self_.stream_,
|
||||
*sp,
|
||||
beast::bind_front_handler(
|
||||
&session::loop,
|
||||
self_.shared_from_this(),
|
||||
sp->need_eof()));
|
||||
}
|
||||
};
|
||||
|
||||
beast::tcp_stream stream_;
|
||||
beast::flat_buffer buffer_;
|
||||
std::shared_ptr<std::string const> doc_root_;
|
||||
http::request<http::string_body> req_;
|
||||
std::shared_ptr<void> res_;
|
||||
send_lambda lambda_;
|
||||
bool keep_alive_ = true;
|
||||
|
||||
public:
|
||||
// Take ownership of the socket
|
||||
@@ -271,7 +229,6 @@ public:
|
||||
std::shared_ptr<std::string const> const& doc_root)
|
||||
: stream_(std::move(socket))
|
||||
, doc_root_(doc_root)
|
||||
, lambda_(*this)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -286,7 +243,6 @@ public:
|
||||
net::dispatch(stream_.get_executor(),
|
||||
beast::bind_front_handler(&session::loop,
|
||||
shared_from_this(),
|
||||
false,
|
||||
beast::error_code{},
|
||||
0));
|
||||
}
|
||||
@@ -294,10 +250,7 @@ public:
|
||||
#include <boost/asio/yield.hpp>
|
||||
|
||||
void
|
||||
loop(
|
||||
bool close,
|
||||
beast::error_code ec,
|
||||
std::size_t bytes_transferred)
|
||||
loop(beast::error_code ec, std::size_t bytes_transferred)
|
||||
{
|
||||
boost::ignore_unused(bytes_transferred);
|
||||
reenter(*this)
|
||||
@@ -315,8 +268,8 @@ public:
|
||||
yield http::async_read(stream_, buffer_, req_,
|
||||
beast::bind_front_handler(
|
||||
&session::loop,
|
||||
shared_from_this(),
|
||||
false));
|
||||
shared_from_this()));
|
||||
|
||||
if(ec == http::error::end_of_stream)
|
||||
{
|
||||
// The remote host closed the connection
|
||||
@@ -325,19 +278,30 @@ public:
|
||||
if(ec)
|
||||
return fail(ec, "read");
|
||||
|
||||
// Send the response
|
||||
yield handle_request(*doc_root_, std::move(req_), lambda_);
|
||||
yield {
|
||||
// Handle request
|
||||
http::message_generator msg =
|
||||
handle_request(*doc_root_, std::move(req_));
|
||||
|
||||
// Determine if we should close the connection
|
||||
keep_alive_ = msg.keep_alive();
|
||||
|
||||
// Send the response
|
||||
beast::async_write(
|
||||
stream_,
|
||||
std::move(msg),
|
||||
beast::bind_front_handler(
|
||||
&session::loop, shared_from_this()));
|
||||
}
|
||||
|
||||
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.
|
||||
break;
|
||||
}
|
||||
|
||||
// We're done with the response so delete it
|
||||
res_ = nullptr;
|
||||
}
|
||||
|
||||
// Send a TCP shutdown
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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