forked from boostorg/beast
Examples use http::message_generator
This commit is contained in:
@@ -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");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user