Skip to content

Commit

Permalink
Fix handling empty HTTP headers in parser_v1.hpp
Browse files Browse the repository at this point in the history
This patch rectifies flush() of beast::http::parser_v1
to properly handle the case when an HTTP header has
empty value.

Without the fix an empty-valued HTTP header is being
concatenated with the header directly following it.
This situation can be replicated using eg. curl:

  curl <url> -H "X-First;" -H "X-Second: bla"

What Beast's client would see is a single header named
as "X-FirstX-Second".
  • Loading branch information
rzarzynski authored and vinniefalco committed Oct 7, 2016
1 parent cee2a42 commit 193f1ce
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Fix message_v1 constructor
* Tidy up DynamicBuffer requirements
* Tidy up error types and headers
* Fix handling empty HTTP headers in parser_v1

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

Expand Down
16 changes: 10 additions & 6 deletions include/beast/http/parser_v1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <beast/http/concepts.hpp>
#include <beast/http/message_v1.hpp>
#include <beast/core/error.hpp>
#include <boost/assert.hpp>
#include <functional>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -94,6 +95,7 @@ class parser_v1
message_type m_;
typename message_type::body_type::reader r_;
std::uint8_t skip_body_ = 0;
bool flush_ = false;

public:
parser_v1(parser_v1&&) = default;
Expand Down Expand Up @@ -161,12 +163,13 @@ class parser_v1

void flush()
{
if(! value_.empty())
{
m_.headers.insert(field_, value_);
field_.clear();
value_.clear();
}
if(! flush_)
return;
flush_ = false;
BOOST_ASSERT(! field_.empty());
m_.headers.insert(field_, value_);
field_.clear();
value_.clear();
}

void on_start(error_code&)
Expand Down Expand Up @@ -197,6 +200,7 @@ class parser_v1
void on_value(boost::string_ref const& s, error_code&)
{
value_.append(s.data(), s.size());
flush_ = true;
}

void set(std::true_type)
Expand Down
2 changes: 1 addition & 1 deletion test/websocket/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class stream_test
}
catch(system_error const& se)
{
BEAST_EXPECT(se.code() == ev);
BEAST_EXPECTS(se.code() == ev, se.what());
}
}
};
Expand Down

0 comments on commit 193f1ce

Please sign in to comment.