New HTTP interfaces (API Change):

fix #123
fix #154
fix #265

This completely replaces the HTTP parser used to read and
parse incoming messages. The new version is faster than
the old one, and written in fewer lines. A summary of
changes:

* parse and async_parse renamed to read and async_read

* basic_parser is optimized for linear buffers,
  use flat_streambuf for the best performance with these
  functions:

  - http::read
  - http::read_some
  - http::async_read
  - http::async_read_some

* The overloads of read() and async_read() which take
  just a header have been removed, since they would
  throw away important parse metadata.

* The derived class callbacks for basic_parser have
  been streamlined. All strings passed to callbacks
  are presented in their entirety, instead of being
  provided in pieces.

These changes allow use-cases that were previously
difficult or impossible, such as:

- Efficient relaying

- Late body type commitment

- Expect: 100-continue handling
This commit is contained in:
Vinnie Falco committed 2017-07-20 08:12:15 -07:00
1 parent 34830a1780
commit e8be3fd7d3
74 files changed
+7106 -5972

No files matched your search

+46 -42
View File
@@ -9,7 +9,7 @@
#define BEAST_WEBSOCKET_IMPL_ACCEPT_IPP
#include <beast/http/message.hpp>
#include <beast/http/parser_v1.hpp>
#include <beast/http/header_parser.hpp>
#include <beast/http/read.hpp>
#include <beast/http/string_body.hpp>
#include <beast/http/write.hpp>
@@ -35,13 +35,13 @@ class stream<NextLayer>::response_op
{
bool cont;
stream<NextLayer>& ws;
http::response<http::string_body> res;
http::response_header res;
error_code final_ec;
int state = 0;
template<class Body, class Fields>
template<class Fields>
data(Handler&, stream<NextLayer>& ws_,
http::request<Body, Fields> const& req,
http::header<true, Fields> const& req,
bool cont_)
: cont(cont_)
, ws(ws_)
@@ -151,7 +151,7 @@ class stream<NextLayer>::accept_op
{
bool cont;
stream<NextLayer>& ws;
http::request<http::string_body> req;
http::header_parser<true, http::fields> p;
int state = 0;
template<class Buffers>
@@ -190,8 +190,8 @@ public:
(*this)(ec, 0);
}
void operator()(error_code const& ec,
std::size_t bytes_transferred, bool again = true);
void operator()(error_code ec,
std::size_t bytes_used, bool again = true);
friend
void* asio_handler_allocate(
@@ -228,36 +228,39 @@ template<class NextLayer>
template<class Handler>
void
stream<NextLayer>::accept_op<Handler>::
operator()(error_code const& ec,
std::size_t bytes_transferred, bool again)
operator()(error_code ec,
std::size_t bytes_used, bool again)
{
beast::detail::ignore_unused(bytes_transferred);
auto& d = *d_;
d.cont = d.cont || again;
while(! ec && d.state != 99)
if(ec)
goto upcall;
switch(d.state)
{
switch(d.state)
{
case 0:
// read message
d.state = 1;
http::async_read(d.ws.next_layer(),
d.ws.stream_.buffer(), d.req,
std::move(*this));
return;
case 0:
// read message
d.state = 1;
http::async_read_some(d.ws.next_layer(),
d.ws.stream_.buffer(), d.p,
std::move(*this));
return;
// got message
case 1:
{
// respond to request
auto& ws = d.ws;
auto req = std::move(d.req);
response_op<Handler>{
d_.release_handler(), ws, req, true};
return;
}
}
case 1:
{
BOOST_ASSERT(d.p.got_header());
d.ws.stream_.buffer().consume(bytes_used);
// Arguments from our state must be
// moved to the stack before releasing
// the handler.
auto& ws = d.ws;
auto m = d.p.release();
response_op<Handler>{
d_.release_handler(),
ws, std::move(m), true};
return;
}
}
upcall:
d_.invoke(ec);
}
@@ -295,11 +298,11 @@ async_accept(ConstBufferSequence const& bs, AcceptHandler&& handler)
}
template<class NextLayer>
template<class Body, class Fields, class AcceptHandler>
template<class Fields, class AcceptHandler>
typename async_completion<
AcceptHandler, void(error_code)>::result_type
stream<NextLayer>::
async_accept(http::request<Body, Fields> const& req,
async_accept(http::header<true, Fields> const& req,
AcceptHandler&& handler)
{
static_assert(is_AsyncStream<next_layer_type>::value,
@@ -372,18 +375,21 @@ accept(ConstBufferSequence const& buffers, error_code& ec)
stream_.buffer().commit(buffer_copy(
stream_.buffer().prepare(
buffer_size(buffers)), buffers));
http::request<http::string_body> m;
http::read(next_layer(), stream_.buffer(), m, ec);
http::header_parser<true, http::fields> p;
auto const bytes_used = http::read_some(
next_layer(), stream_.buffer(), p, ec);
if(ec)
return;
accept(m, ec);
BOOST_ASSERT(p.got_header());
stream_.buffer().consume(bytes_used);
accept(p.get(), ec);
}
template<class NextLayer>
template<class Body, class Fields>
template<class Fields>
void
stream<NextLayer>::
accept(http::request<Body, Fields> const& request)
accept(http::header<true, Fields> const& request)
{
static_assert(is_SyncStream<next_layer_type>::value,
"SyncStream requirements not met");
@@ -394,10 +400,10 @@ accept(http::request<Body, Fields> const& request)
}
template<class NextLayer>
template<class Body, class Fields>
template<class Fields>
void
stream<NextLayer>::
accept(http::request<Body, Fields> const& req,
accept(http::header<true, Fields> const& req,
error_code& ec)
{
static_assert(is_SyncStream<next_layer_type>::value,
@@ -418,8 +424,6 @@ accept(http::request<Body, Fields> const& req,
open(detail::role_type::server);
}
//------------------------------------------------------------------------------
} // websocket
} // beast
+6 -6
View File
@@ -8,9 +8,9 @@
#ifndef BEAST_WEBSOCKET_IMPL_HANDSHAKE_IPP
#define BEAST_WEBSOCKET_IMPL_HANDSHAKE_IPP
#include <beast/http/empty_body.hpp>
#include <beast/http/message.hpp>
#include <beast/http/read.hpp>
#include <beast/http/streambuf_body.hpp>
#include <beast/http/write.hpp>
#include <beast/core/handler_helpers.hpp>
#include <beast/core/handler_ptr.hpp>
@@ -34,8 +34,8 @@ class stream<NextLayer>::handshake_op
bool cont;
stream<NextLayer>& ws;
std::string key;
http::request<http::empty_body> req;
http::response<http::string_body> resp;
http::request_header req;
http::response<http::streambuf_body> res;
int state = 0;
data(Handler& handler, stream<NextLayer>& ws_,
@@ -129,14 +129,14 @@ operator()(error_code ec, bool again)
// read http response
d.state = 2;
http::async_read(d.ws.next_layer(),
d.ws.stream_.buffer(), d.resp,
d.ws.stream_.buffer(), d.res,
std::move(*this));
return;
// got response
case 2:
{
d.ws.do_response(d.resp, d.key, ec);
d.ws.do_response(d.res, d.key, ec);
// call handler
d.state = 99;
break;
@@ -196,7 +196,7 @@ handshake(boost::string_ref const& host,
}
if(ec)
return;
http::response<http::string_body> res;
http::response<http::streambuf_body> res;
http::read(next_layer(), stream_.buffer(), res, ec);
if(ec)
return;
+9 -11
View File
@@ -83,17 +83,18 @@ reset()
}
template<class NextLayer>
http::request<http::empty_body>
http::request_header
stream<NextLayer>::
build_request(boost::string_ref const& host,
boost::string_ref const& resource, std::string& key)
{
http::request<http::empty_body> req;
http::request_header req;
req.url = { resource.data(), resource.size() };
req.version = 11;
req.method = "GET";
req.fields.insert("Host", host);
req.fields.insert("Upgrade", "websocket");
req.fields.insert("Connection", "upgrade");
key = detail::make_sec_ws_key(maskgen_);
req.fields.insert("Sec-WebSocket-Key", key);
req.fields.insert("Sec-WebSocket-Version", "13");
@@ -113,15 +114,13 @@ build_request(boost::string_ref const& host,
req.fields, config);
}
d_(req);
http::prepare(req, http::connection::upgrade);
return req;
}
template<class NextLayer>
template<class Body, class Fields>
http::response<http::string_body>
http::response_header
stream<NextLayer>::
build_response(http::request<Body, Fields> const& req)
build_response(http::request_header const& req)
{
auto err =
[&](std::string const& text)
@@ -170,7 +169,7 @@ build_response(http::request<Body, Fields> const& req)
return res;
}
}
http::response<http::string_body> res;
http::response_header res;
{
detail::pmd_offer offer;
detail::pmd_offer unused;
@@ -182,23 +181,22 @@ build_response(http::request<Body, Fields> const& req)
res.reason = http::reason_string(res.status);
res.version = req.version;
res.fields.insert("Upgrade", "websocket");
res.fields.insert("Connection", "upgrade");
{
auto const key =
req.fields["Sec-WebSocket-Key"];
res.fields.insert("Sec-WebSocket-Accept",
detail::make_sec_ws_accept(key));
}
res.fields.replace("Server", "Beast.WSProto");
res.fields.replace("Server", "Beast.WebSocket");
d_(res);
http::prepare(res, http::connection::upgrade);
return res;
}
template<class NextLayer>
template<class Body, class Fields>
void
stream<NextLayer>::
do_response(http::response<Body, Fields> const& res,
do_response(http::response_header const& res,
boost::string_ref const& key, error_code& ec)
{
// VFALCO Review these error codes