From 2399b610e3c104c03f11efd94878829702efb7a6 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 16 Oct 2017 12:09:20 -0700 Subject: [PATCH] Clear previous message fields in parser fix #818 --- CHANGELOG.md | 1 + include/boost/beast/http/fields.hpp | 8 +++++--- include/boost/beast/http/impl/parser.ipp | 1 + test/beast/http/parser.cpp | 15 +++++++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b9fa9da..d6e6109b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Version 123: * Use unit-test subtree * Fix spurious race in websocket close test * Check compiler feature in Jamfile +* Clear previous message fields in parser -------------------------------------------------------------------------------- diff --git a/include/boost/beast/http/fields.hpp b/include/boost/beast/http/fields.hpp index e4ef229f..127fbc54 100644 --- a/include/boost/beast/http/fields.hpp +++ b/include/boost/beast/http/fields.hpp @@ -373,17 +373,19 @@ public: // //-------------------------------------------------------------------------- -private: - // VFALCO But this leaves behind the method, target, and reason! /** Remove all fields from the container All references, pointers, or iterators referring to contained elements are invalidated. All past-the-end iterators are also invalidated. + + @par Postconditions: + @code + std::distance(this->begin(), this->end()) == 0 + @encode */ void clear(); -public: /** Insert a field. diff --git a/include/boost/beast/http/impl/parser.ipp b/include/boost/beast/http/impl/parser.ipp index f1df69ca..476d537d 100644 --- a/include/boost/beast/http/impl/parser.ipp +++ b/include/boost/beast/http/impl/parser.ipp @@ -32,6 +32,7 @@ parser(Arg1&& arg1, ArgN&&... argn) std::forward(argn)...) , wr_(m_) { + m_.clear(); } template diff --git a/test/beast/http/parser.cpp b/test/beast/http/parser.cpp index 1a182a90..1bef3b27 100644 --- a/test/beast/http/parser.cpp +++ b/test/beast/http/parser.cpp @@ -21,6 +21,7 @@ #include #include #include +#include namespace boost { namespace beast { @@ -329,6 +330,19 @@ public: BEAST_EXPECT(used == 0); } + void + testIssue818() + { + // Make sure that the parser clears pre-existing fields + request m; + m.set(field::accept, "html/text"); + BEAST_EXPECT(std::distance(m.begin(), m.end()) == 1); + request_parser p{std::move(m)}; + BEAST_EXPECT(std::distance(m.begin(), m.end()) == 0); + auto& m1 = p.get(); + BEAST_EXPECT(std::distance(m1.begin(), m1.end()) == 0); + } + void run() override { @@ -336,6 +350,7 @@ public: testNeedMore(); testNeedMore(); testGotSome(); + testIssue818(); } };