Skip to content

Commit

Permalink
Add skip_body parser option
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Jun 21, 2016
1 parent e360093 commit 0c340de
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Fixes and documentation for teardown and use with SSL:
* Add example code to rfc7230 javadocs
* Remove extraneous header file <beast/http/status.hpp>
* Add skip_body parser option

--------------------------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions doc/quickref.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<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__headers_max_size">headers_max_size</link></member>
<member><link linkend="beast.ref.http__skip_body">skip_body</link></member>
</simplelist>
<bridgehead renderas="sect3">Type Traits</bridgehead>
<simplelist type="vert" columns="1">
Expand Down
41 changes: 40 additions & 1 deletion include/beast/http/parser_v1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ struct parser_response

} // 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.
This class uses the basic HTTP/1 wire format parser to convert
Expand Down Expand Up @@ -62,6 +93,7 @@ class parser_v1
std::string value_;
message_type m_;
typename message_type::body_type::reader r_;
std::uint8_t skip_body_ = 0;

public:
parser_v1(parser_v1&&) = default;
Expand All @@ -81,6 +113,13 @@ class parser_v1
{
}

/// Set the expect body option.
void
set_option(skip_body const& o)
{
skip_body_ = o.value ? 1 : 0;
}

/** Returns the parsed message.
Only valid if `complete()` would return `true`.
Expand Down Expand Up @@ -176,7 +215,7 @@ class parser_v1
{
flush();
m_.version = 10 * this->http_major() + this->http_minor();
return 0;
return skip_body_;
}

void on_request(error_code& ec)
Expand Down
13 changes: 13 additions & 0 deletions test/http/parser_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ class parser_v1_test : public beast::unit_test::suite
expect(m.headers["Server"] == "test");
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());
}
}
};

Expand Down

0 comments on commit 0c340de

Please sign in to comment.