From 0c340de237a9f15f67667d598d6a1fb947ac9ee3 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 15 Jun 2016 11:52:59 -0400 Subject: [PATCH] Add skip_body parser option --- CHANGELOG | 1 + doc/quickref.xml | 1 + include/beast/http/parser_v1.hpp | 41 +++++++++++++++++++++++++++++++- test/http/parser_v1.cpp | 13 ++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 20b1d18837..b97aebae22 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ * Fixes and documentation for teardown and use with SSL: * Add example code to rfc7230 javadocs * Remove extraneous header file +* Add skip_body parser option -------------------------------------------------------------------------------- diff --git a/doc/quickref.xml b/doc/quickref.xml index 7662a3577a..729a0ad5fb 100644 --- a/doc/quickref.xml +++ b/doc/quickref.xml @@ -43,6 +43,7 @@ body_max_size headers_max_size + skip_body Type Traits diff --git a/include/beast/http/parser_v1.hpp b/include/beast/http/parser_v1.hpp index 0909c298c9..aada5243b5 100644 --- a/include/beast/http/parser_v1.hpp +++ b/include/beast/http/parser_v1.hpp @@ -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 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 @@ -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; @@ -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`. @@ -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) diff --git a/test/http/parser_v1.cpp b/test/http/parser_v1.cpp index 238e13052b..b1ee8cab6d 100644 --- a/test/http/parser_v1.cpp +++ b/test/http/parser_v1.cpp @@ -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 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()); + } } };