From cc1a751b3f89f19c53e357ce7dbf6c009da2dc25 Mon Sep 17 00:00:00 2001 From: Mohammad Nejati Date: Tue, 20 Aug 2024 17:47:03 +0000 Subject: [PATCH] basic_parser: set `state_` before calling `on_finish_impl` --- .../boost/beast/http/impl/basic_parser.ipp | 22 ++++------------- test/beast/http/basic_parser.cpp | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/include/boost/beast/http/impl/basic_parser.ipp b/include/boost/beast/http/impl/basic_parser.ipp index 952e67ebaf..be5426b346 100644 --- a/include/boost/beast/http/impl/basic_parser.ipp +++ b/include/boost/beast/http/impl/basic_parser.ipp @@ -182,13 +182,11 @@ loop: case state::trailer_fields: parse_fields(p, n, ec); - if(ec) - goto done; - this->on_finish_impl(ec); if(ec) goto done; state_ = state::complete; - break; + this->on_finish_impl(ec); + goto done; case state::chunk_body: parse_chunk_body(p, n, ec); @@ -231,11 +229,9 @@ put_eof(error_code& ec) ec = {}; return; } + state_ = state::complete; ec = {}; this->on_finish_impl(ec); - if(ec) - return; - state_ = state::complete; } template @@ -474,11 +470,7 @@ finish_header(error_code& ec, std::true_type) if(ec) return; if(state_ == state::complete) - { this->on_finish_impl(ec); - if(ec) - return; - } } template @@ -535,11 +527,7 @@ finish_header(error_code& ec, std::false_type) if(ec) return; if(state_ == state::complete) - { this->on_finish_impl(ec); - if(ec) - return; - } } template @@ -557,10 +545,8 @@ parse_body(char const*& p, return; if(len_ > 0) return; - this->on_finish_impl(ec); - if(ec) - return; state_ = state::complete; + this->on_finish_impl(ec); } template diff --git a/test/beast/http/basic_parser.cpp b/test/beast/http/basic_parser.cpp index a31842097a..60c5ddd972 100644 --- a/test/beast/http/basic_parser.cpp +++ b/test/beast/http/basic_parser.cpp @@ -363,6 +363,18 @@ class basic_parser_test : public beast::unit_test::suite void testCallbacks() { + parsegrind>( + "GET / HTTP/1.1\r\n" + "\r\n", + [&](test_parser const& p) + { + BEAST_EXPECT(p.got_on_begin == 1); + BEAST_EXPECT(p.got_on_field == 0); + BEAST_EXPECT(p.got_on_header == 1); + BEAST_EXPECT(p.got_on_body == 0); + BEAST_EXPECT(p.got_on_chunk == 0); + BEAST_EXPECT(p.got_on_complete == 1); + }); parsegrind>( "GET / HTTP/1.1\r\n" "User-Agent: test\r\n" @@ -378,6 +390,18 @@ class basic_parser_test : public beast::unit_test::suite BEAST_EXPECT(p.got_on_chunk == 0); BEAST_EXPECT(p.got_on_complete == 1); }); + parsegrind>( + "HTTP/1.1 100 Continue\r\n" + "\r\n", + [&](test_parser const& p) + { + BEAST_EXPECT(p.got_on_begin == 1); + BEAST_EXPECT(p.got_on_field == 0); + BEAST_EXPECT(p.got_on_header == 1); + BEAST_EXPECT(p.got_on_body == 0); + BEAST_EXPECT(p.got_on_chunk == 0); + BEAST_EXPECT(p.got_on_complete == 1); + }); parsegrind>( "HTTP/1.1 200 OK\r\n" "Server: test\r\n"