Beast.HTTP:

New classes are introduced to represent HTTP messages and their
associated bodies. The parser interface is reworked to use CRTP,
error codes, and trait checks.

New classes:

* basic_headers

  Models field/value pairs in a HTTP message.

* message

  Models a HTTP message, body behavior defined by template argument.
  Parsed message carries metadata generated during parsing.

* parser

  Produces parsed messages.

* empty_body, string_body, basic_streambuf_body

  Classes used to represent content bodies in various ways.

New functions:

* read, async_read, write, async_write

  Read and write HTTP messages on a socket.

New concepts:

* Body: Represents the HTTP Content-Body.
* Field: A HTTP header field.
* FieldSequence: A forward sequence of fields.
* Reader: Parses a Body from a stream of bytes.
* Writer: Serializes a Body to buffers.

basic_parser changes:

* add write methods which throw exceptions instead
* error_code passed via parameter instead of return value
* fold private member calls into existing callbacks
* basic_parser uses CRTP instead of virtual members
* add documentation on Derived requirements for CRTP

impl/http-parser changes:

* joyent renamed to nodejs to reflect upstream changes
This commit is contained in:
Vinnie Falco
2016-03-11 06:40:37 -05:00
parent f25b448a49
commit bcbe22c780
88 changed files with 6843 additions and 1867 deletions

View File

@@ -116,7 +116,7 @@ ServerHandlerImp::onAccept (Session& session,
auto
ServerHandlerImp::onHandoff (Session& session,
std::unique_ptr <beast::asio::ssl_bundle>&& bundle,
beast::http::message&& request,
http_request_type&& request,
boost::asio::ip::tcp::endpoint remote_address) ->
Handoff
{
@@ -138,7 +138,7 @@ ServerHandlerImp::onHandoff (Session& session,
auto
ServerHandlerImp::onHandoff (Session& session,
boost::asio::ip::tcp::socket&& socket,
beast::http::message&& request,
http_request_type&& request,
boost::asio::ip::tcp::endpoint remote_address) ->
Handoff
{
@@ -163,6 +163,23 @@ Json::Output makeOutput (Session& session)
};
}
// HACK!
template<class Allocator>
static
std::map<std::string, std::string>
build_map(beast::http::headers<Allocator> const& h)
{
std::map <std::string, std::string> c;
for (auto const& e : h)
{
auto key (e.first);
// TODO Replace with safe C++14 version
std::transform (key.begin(), key.end(), key.begin(), ::tolower);
c [key] = e.second;
}
return c;
}
void
ServerHandlerImp::onRequest (Session& session)
{
@@ -207,12 +224,32 @@ ServerHandlerImp::onStopped (Server&)
//------------------------------------------------------------------------------
template<class ConstBufferSequence>
static
std::string
buffers_to_string(ConstBufferSequence const& bs)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(auto const& b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
for(auto i = s.size(); i-- > 0;)
if(s[i] == '\r')
s.replace(i, 1, "\\r");
else if(s[i] == '\n')
s.replace(i, 1, "\\n\n");
return s;
}
// Run as a couroutine.
void
ServerHandlerImp::processSession (std::shared_ptr<Session> const& session,
std::shared_ptr<JobCoro> jobCoro)
{
processRequest (session->port(), to_string (session->body()),
processRequest (session->port(), buffers_to_string(session->request().body.data()),
session->remoteAddress().at_port (0), makeOutput (*session), jobCoro,
[&]
{
@@ -233,7 +270,7 @@ ServerHandlerImp::processSession (std::shared_ptr<Session> const& session,
return std::string{};
}());
if (session->request().keep_alive())
if(is_keep_alive(session->request()))
session->complete();
else
session->close (true);
@@ -425,9 +462,9 @@ ServerHandlerImp::processRequest (Port const& port,
// Returns `true` if the HTTP request is a Websockets Upgrade
// http://en.wikipedia.org/wiki/HTTP/1.1_Upgrade_header#Use_with_WebSockets
bool
ServerHandlerImp::isWebsocketUpgrade (beast::http::message const& request)
ServerHandlerImp::isWebsocketUpgrade (http_request_type const& request)
{
if (request.upgrade())
if (is_upgrade(request))
return request.headers["Upgrade"] == "websocket";
return false;
}
@@ -457,7 +494,7 @@ ServerHandlerImp::authorized (Port const& port,
//------------------------------------------------------------------------------
void
ServerHandler::appendStandardFields (beast::http::message& message)
ServerHandler::appendStandardFields (beast::deprecated_http::message& message)
{
}