Add skip_body parser option

This commit is contained in:
Vinnie Falco
2016-06-15 11:52:59 -04:00
parent 8b71afa63f
commit 344e4c942c
4 changed files with 55 additions and 1 deletions

View File

@ -9,6 +9,7 @@
* Fixes and documentation for teardown and use with SSL: * Fixes and documentation for teardown and use with SSL:
* Add example code to rfc7230 javadocs * Add example code to rfc7230 javadocs
* Remove extraneous header file <beast/http/status.hpp> * Remove extraneous header file <beast/http/status.hpp>
* Add skip_body parser option
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------

View File

@ -43,6 +43,7 @@
<simplelist type="vert" columns="1"> <simplelist type="vert" columns="1">
<member><link linkend="beast.ref.http__body_max_size">body_max_size</link></member> <member><link linkend="beast.ref.http__body_max_size">body_max_size</link></member>
<member><link linkend="beast.ref.http__headers_max_size">headers_max_size</link></member> <member><link linkend="beast.ref.http__headers_max_size">headers_max_size</link></member>
<member><link linkend="beast.ref.http__skip_body">skip_body</link></member>
</simplelist> </simplelist>
<bridgehead renderas="sect3">Type Traits</bridgehead> <bridgehead renderas="sect3">Type Traits</bridgehead>
<simplelist type="vert" columns="1"> <simplelist type="vert" columns="1">

View File

@ -35,6 +35,37 @@ struct parser_response
} // detail } // detail
/** Skip body option.
The options controls whether or not the parser expects to see a
HTTP body, regardless of the presence or absence of certain fields
such as Content-Length.
Depending on the request, some responses do not carry a body.
For example, a 200 response to a CONNECT request from a tunneling
proxy. In these cases, callers use the @ref skip_body option to
inform the parser that no body is expected. The parser will consider
the message complete after the all headers have been received.
Example:
@code
parser_v1<true, empty_body, headers> p;
p.set_option(skip_body{true});
@endcode
@note Objects of this type are passed to @ref basic_parser_v1::set_option.
*/
struct skip_body
{
bool value;
explicit
skip_body(bool v)
: value(v)
{
}
};
/** A parser for producing HTTP/1 messages. /** A parser for producing HTTP/1 messages.
This class uses the basic HTTP/1 wire format parser to convert This class uses the basic HTTP/1 wire format parser to convert
@ -62,6 +93,7 @@ private:
std::string value_; std::string value_;
message_type m_; message_type m_;
typename message_type::body_type::reader r_; typename message_type::body_type::reader r_;
std::uint8_t skip_body_ = 0;
public: public:
parser_v1(parser_v1&&) = default; parser_v1(parser_v1&&) = default;
@ -81,6 +113,13 @@ public:
{ {
} }
/// Set the expect body option.
void
set_option(skip_body const& o)
{
skip_body_ = o.value ? 1 : 0;
}
/** Returns the parsed message. /** Returns the parsed message.
Only valid if `complete()` would return `true`. Only valid if `complete()` would return `true`.
@ -176,7 +215,7 @@ private:
{ {
flush(); flush();
m_.version = 10 * this->http_major() + this->http_minor(); m_.version = 10 * this->http_major() + this->http_minor();
return 0; return skip_body_;
} }
void on_request(error_code& ec) void on_request(error_code& ec)

View File

@ -61,6 +61,19 @@ public:
expect(m.headers["Server"] == "test"); expect(m.headers["Server"] == "test");
expect(m.body == "*"); expect(m.body == "*");
} }
// skip body
{
error_code ec;
parser_v1<false, string_body, headers> p;
std::string const s =
"HTTP/1.1 200 Connection Established\r\n"
"Proxy-Agent: Zscaler/5.1\r\n"
"\r\n";
p.set_option(skip_body{true});
p.write(buffer(s), ec);
expect(! ec);
expect(p.complete());
}
} }
}; };