diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e7b8a5d..3653254b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Add test_allocator to extras/test * More flat_streambuf tests * WebSocket doc work +* Prevent basic_fields operator[] assignment API Changes: diff --git a/include/beast/http/fields.hpp b/include/beast/http/fields.hpp index c62105b1..0c301199 100644 --- a/include/beast/http/fields.hpp +++ b/include/beast/http/fields.hpp @@ -216,7 +216,7 @@ public: If more than one field with the specified name exists, the first field defined by insertion order is returned. */ - boost::string_ref + boost::string_ref const operator[](boost::string_ref const& name) const; /// Clear the contents of the basic_fields. diff --git a/include/beast/http/impl/fields.ipp b/include/beast/http/impl/fields.ipp index c4a7c9b6..ff9d3127 100644 --- a/include/beast/http/impl/fields.ipp +++ b/include/beast/http/impl/fields.ipp @@ -192,7 +192,7 @@ find(boost::string_ref const& name) const -> } template -boost::string_ref +boost::string_ref const basic_fields:: operator[](boost::string_ref const& name) const { diff --git a/test/http/fields.cpp b/test/http/fields.cpp index e16408e0..6e1a6402 100644 --- a/test/http/fields.cpp +++ b/test/http/fields.cpp @@ -18,17 +18,17 @@ class basic_fields_test : public beast::unit_test::suite { public: template - using bha = basic_fields; + using fa_t = basic_fields; - using bh = basic_fields>; + using f_t = fa_t>; template static void - fill(std::size_t n, basic_fields& h) + fill(std::size_t n, basic_fields& f) { for(std::size_t i = 1; i<= n; ++i) - h.insert(boost::lexical_cast(i), i); + f.insert(boost::lexical_cast(i), i); } template @@ -41,46 +41,46 @@ public: void testHeaders() { - bh h1; - BEAST_EXPECT(h1.empty()); - fill(1, h1); - BEAST_EXPECT(h1.size() == 1); - bh h2; - h2 = h1; - BEAST_EXPECT(h2.size() == 1); - h2.insert("2", "2"); - BEAST_EXPECT(std::distance(h2.begin(), h2.end()) == 2); - h1 = std::move(h2); - BEAST_EXPECT(h1.size() == 2); - BEAST_EXPECT(h2.size() == 0); - bh h3(std::move(h1)); - BEAST_EXPECT(h3.size() == 2); - BEAST_EXPECT(h1.size() == 0); - self_assign(h3, std::move(h3)); - BEAST_EXPECT(h3.size() == 2); - BEAST_EXPECT(h2.erase("Not-Present") == 0); + f_t f1; + BEAST_EXPECT(f1.empty()); + fill(1, f1); + BEAST_EXPECT(f1.size() == 1); + f_t f2; + f2 = f1; + BEAST_EXPECT(f2.size() == 1); + f2.insert("2", "2"); + BEAST_EXPECT(std::distance(f2.begin(), f2.end()) == 2); + f1 = std::move(f2); + BEAST_EXPECT(f1.size() == 2); + BEAST_EXPECT(f2.size() == 0); + f_t f3(std::move(f1)); + BEAST_EXPECT(f3.size() == 2); + BEAST_EXPECT(f1.size() == 0); + self_assign(f3, std::move(f3)); + BEAST_EXPECT(f3.size() == 2); + BEAST_EXPECT(f2.erase("Not-Present") == 0); } void testRFC2616() { - bh h; - h.insert("a", "w"); - h.insert("a", "x"); - h.insert("aa", "y"); - h.insert("b", "z"); - BEAST_EXPECT(h.count("a") == 2); + f_t f; + f.insert("a", "w"); + f.insert("a", "x"); + f.insert("aa", "y"); + f.insert("b", "z"); + BEAST_EXPECT(f.count("a") == 2); } void testErase() { - bh h; - h.insert("a", "w"); - h.insert("a", "x"); - h.insert("aa", "y"); - h.insert("b", "z"); - BEAST_EXPECT(h.size() == 4); - h.erase("a"); - BEAST_EXPECT(h.size() == 2); + f_t f; + f.insert("a", "w"); + f.insert("a", "x"); + f.insert("aa", "y"); + f.insert("b", "z"); + BEAST_EXPECT(f.size() == 4); + f.erase("a"); + BEAST_EXPECT(f.size() == 2); } void run() override diff --git a/test/http/write.cpp b/test/http/write.cpp index 2ffe7a15..76b2220f 100644 --- a/test/http/write.cpp +++ b/test/http/write.cpp @@ -634,7 +634,7 @@ public: m.method = "GET"; m.version = 11; m.url = "/"; - m.fields["Content-Length"] = "5"; + m.fields.insert("Content-Length", 5); m.body = "*****"; async_write(os, m, handler{}); BEAST_EXPECT(handler::count() > 0); @@ -656,7 +656,7 @@ public: m.method = "GET"; m.version = 11; m.url = "/"; - m.fields["Content-Length"] = "5"; + m.fields.insert("Content-Length", 5); m.body = "*****"; async_write(is, m, handler{}); BEAST_EXPECT(handler::count() > 0);