summaryrefslogtreecommitdiffhomepage
path: root/http.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-10 17:17:16 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-10 17:17:16 +0200
commitda2666726e48a3dc00f05589cdf4947f22deb3c3 (patch)
treeca02b14cb0fa9ec714ce37a4200d6b86132df1cb /http.cpp
parent0f55f61ee745f38c312a53009b883feb5aa86b49 (diff)
Simplify session handler
Diffstat (limited to 'http.cpp')
-rw-r--r--http.cpp81
1 files changed, 22 insertions, 59 deletions
diff --git a/http.cpp b/http.cpp
index edf935b..9a5b554 100644
--- a/http.cpp
+++ b/http.cpp
@@ -26,75 +26,39 @@ using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace {
-// 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 Send>
-void
-handle_request(
- ::Server& server,
- request_type&& req,
- Send&& send)
-{
- return send(std::move(generate_response(req, server)));
-}
-
//------------------------------------------------------------------------------
// Report a failure
void
fail(beast::error_code ec, char const* what)
{
- std::cerr << what << ": " << ec.message() << "\n";
+ std::cerr << what << ": " << ec.message() << "\n";
}
// 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_;
+ Server& m_server;
+ request_type req_;
+ std::shared_ptr<response_type> res_;
- beast::tcp_stream stream_;
- beast::flat_buffer buffer_;
- Server& m_server;
- http::request<http::string_body> req_;
- std::shared_ptr<void> res_;
- send_lambda lambda_;
+ void handle_request(::Server& server, request_type&& req)
+ {
+ auto sp = std::make_shared<response_type>(generate_response(req, server));
+
+ res_ = sp;
+
+ // Write the response
+ http::async_write(
+ stream_,
+ *sp,
+ beast::bind_front_handler(
+ &session::on_write,
+ shared_from_this(),
+ sp->need_eof()));
+ }
public:
// Take ownership of the stream
@@ -103,7 +67,6 @@ public:
Server& server)
: stream_(std::move(socket))
, m_server(server)
- , lambda_(*this)
{
}
@@ -153,7 +116,7 @@ public:
return fail(ec, "read");
// Send the response
- handle_request(m_server, std::move(req_), lambda_);
+ handle_request(m_server, std::move(req_));
}
void