Separate beast::http::body from beast::http::message (RIPD-660):

This changes the http::message object to no longer contain a body. It modifies
the parser to store the body in a separate object, or to pass the body data
to a functor. This allows the body to be stored in more flexible ways. For
example, in HTTP responses the body can be generated procedurally instead
of being required to exist entirely in memory at once.
This commit is contained in:
Vinnie Falco
2014-10-29 13:22:57 -07:00
parent c1a5e88752
commit acaa1098f7
10 changed files with 92 additions and 37 deletions

View File

@@ -113,7 +113,7 @@ void
ServerHandlerImp::onRequest (HTTP::Session& session)
{
// Check user/password authorization
auto const headers (build_map (session.message().headers));
auto const headers = build_map (session.request().headers);
if (! HTTPAuthorized (headers))
{
session.write (HTTPReply (403, "Forbidden"));
@@ -146,11 +146,10 @@ ServerHandlerImp::onStopped (HTTP::Server&)
void
ServerHandlerImp::processSession (Job& job, HTTP::Session& session)
{
auto const s (to_string(session.message().body));
session.write (processRequest (to_string(session.message().body),
session.write (processRequest (to_string(session.body()),
session.remoteAddress().at_port(0)));
if (session.message().keep_alive())
if (session.request().keep_alive())
{
session.complete();
}

View File

@@ -20,6 +20,7 @@
#ifndef RIPPLE_HTTP_SESSION_H_INCLUDED
#define RIPPLE_HTTP_SESSION_H_INCLUDED
#include <beast/http/body.h>
#include <beast/http/message.h>
#include <beast/net/IPEndpoint.h>
#include <beast/utility/Journal.h>
@@ -59,10 +60,15 @@ public:
beast::IP::Endpoint
remoteAddress() = 0;
/** Returns the currently known set of headers. */
/** Returns the current HTTP request. */
virtual
beast::http::message&
message() = 0;
request() = 0;
/** Returns the Content-Body of the current HTTP request. */
virtual
beast::http::body const&
body() = 0;
/** Send a copy of data asynchronously. */
/** @{ */

View File

@@ -103,6 +103,7 @@ protected:
boost::asio::streambuf read_buf_;
beast::http::message message_;
beast::http::body body_;
std::list <buffer> write_queue_;
std::mutex mutex_;
bool graceful_ = false;
@@ -181,11 +182,17 @@ protected:
}
beast::http::message&
message() override
request() override
{
return message_;
}
beast::http::body const&
body() override
{
return body_;
}
void
write (void const* buffer, std::size_t bytes) override;
@@ -481,7 +488,8 @@ Peer<Impl>::do_read (boost::asio::yield_context yield)
error_code ec;
bool eof = false;
beast::http::parser parser (message_, true);
body_.clear();
beast::http::parser parser (message_, body_, true);
for(;;)
{
if (read_buf_.size() == 0)

View File

@@ -66,7 +66,7 @@ public:
onRequest (Session& session) override
{
session.write (std::string ("Hello, world!\n"));
if (session.message().keep_alive())
if (session.request().keep_alive())
session.complete();
else
session.close (true);

View File

@@ -413,7 +413,9 @@ PeerImp::on_write_http_request (error_code ec, std::size_t bytes_transferred)
{
// done sending request, now read the response
http_message_ = boost::in_place ();
http_parser_ = boost::in_place (std::ref(*http_message_), false);
http_body_.clear();
http_parser_ = boost::in_place (std::ref(*http_message_),
std::ref(http_body_), false);
on_read_http_response (error_code(), 0);
return;
}
@@ -559,7 +561,9 @@ PeerImp::on_read_http_detect (error_code ec, std::size_t bytes_transferred)
else if (! is_peer_protocol)
{
http_message_ = boost::in_place ();
http_parser_ = boost::in_place (std::ref(*http_message_), true);
http_body_.clear();
http_parser_ = boost::in_place (std::ref(*http_message_),
std::ref(http_body_), true);
on_read_http_request (error_code(), 0);
return;
}

View File

@@ -173,6 +173,7 @@ private:
beast::asio::streambuf read_buffer_;
boost::optional <beast::http::message> http_message_;
boost::optional <beast::http::parser> http_parser_;
beast::http::body http_body_;
message_stream message_stream_;
beast::asio::streambuf write_buffer_;