diff options
-rw-r--r-- | http.cpp | 81 | ||||
-rw-r--r-- | https.cpp | 82 |
2 files changed, 44 insertions, 119 deletions
@@ -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 @@ -35,20 +35,6 @@ 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 @@ -81,50 +67,27 @@ 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_; - Server& m_server; - http::request<http::string_body> req_; - std::shared_ptr<void> res_; - send_lambda lambda_; - + beast::ssl_stream<beast::tcp_stream> stream_; + beast::flat_buffer buffer_; + Server& m_server; + http::request<http::string_body> req_; + std::shared_ptr<response_type> res_; + + 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 socket explicit @@ -134,7 +97,6 @@ public: Server& server) : stream_(std::move(socket), ctx) , m_server(server) - , lambda_(*this) { } @@ -207,7 +169,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 |