Skip to content

Commit f774a58

Browse files
committed
Fix parser::body() abort on data past the body
1 parent 3e8eef1 commit f774a58

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/parser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,7 @@ class parser::impl
13161316
detail::throw_logic_error();
13171317

13181318
auto cbp = (is_plain() ? cb0_ : cb1_).data();
1319-
BOOST_ASSERT(cbp[1].size() == 0);
1320-
BOOST_ASSERT(cbp[0].size() == body_avail_);
1319+
BOOST_ASSERT(body_avail_ <= cbp[0].size());
13211320
return core::string_view(
13221321
static_cast<char const*>(cbp[0].data()),
13231322
body_avail_);

test/unit/parser.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,50 @@ struct parser_test
17481748
pr.get().payload(), payload::chunked);
17491749
}
17501750

1751+
void
1752+
testBodyWithTrailingData()
1753+
{
1754+
parser_config cfg{false};
1755+
cfg.headers.max_size = 100;
1756+
cfg.min_buffer = 100;
1757+
auto pcfg = make_parser_config(cfg);
1758+
1759+
auto const check = [&pcfg](
1760+
core::string_view octets,
1761+
core::string_view body)
1762+
{
1763+
response_parser pr(pcfg);
1764+
pr.reset();
1765+
pr.start();
1766+
pr.commit(capy::buffer_copy(
1767+
pr.prepare(),
1768+
capy::const_buffer(
1769+
octets.data(), octets.size())));
1770+
1771+
system::error_code ec;
1772+
pr.parse(ec);
1773+
BOOST_TEST(! ec);
1774+
BOOST_TEST(pr.got_header());
1775+
pr.parse(ec);
1776+
BOOST_TEST(! ec);
1777+
BOOST_TEST(pr.is_complete());
1778+
BOOST_TEST_EQ(pr.body(), body);
1779+
};
1780+
1781+
check(
1782+
"HTTP/1.1 200 OK\r\n"
1783+
"Content-Length: 2\r\n"
1784+
"\r\n"
1785+
"ok"
1786+
"HTTP/1.1 200 OK\r\n", "ok");
1787+
1788+
check(
1789+
"HTTP/1.1 200 OK\r\n"
1790+
"Content-Length: 2\r\n"
1791+
"\r\n"
1792+
"okX", "ok");
1793+
}
1794+
17511795
void
17521796
run()
17531797
{
@@ -1764,6 +1808,7 @@ struct parser_test
17641808
testMultipleMessageInPlaceChunked();
17651809
testSetBodyLimit();
17661810
testAccessHeaderAfterBodyError();
1811+
testBodyWithTrailingData();
17671812
#else
17681813
// For profiling
17691814
for(int i = 0; i < 10000; ++i )

0 commit comments

Comments
 (0)