From 14c3d50b570b1d7bf32453f38be77c759007fdb0 Mon Sep 17 00:00:00 2001 From: Richard Hodges Date: Thu, 4 Jun 2020 19:18:44 +0200 Subject: [PATCH] Deprecate `string_param` (API Change): API Changes: `string_param`, which was previously the argument type when setting field values has been replaced by `string_view`. Because of this, it is no longer possible to set message field values directly as integrals. Users are required to convert numeric arguments to a string type prior to calling `fields::set` et. al. Beast provides the non-allocating `to_static_string()` function for this purpose. To set Content-Length field manually, call `message::content_length`. fixes #1956 --- CHANGELOG.md | 14 ++++ doc/qbk/quickref.xml | 1 - example/doc/http_examples.hpp | 2 +- .../http/server/small/http_server_small.cpp | 2 +- include/boost/beast/core.hpp | 1 - include/boost/beast/core/string_param.hpp | 5 ++ include/boost/beast/http/fields.hpp | 21 +++-- include/boost/beast/http/impl/fields.hpp | 20 +++-- test/beast/core/CMakeLists.txt | 1 - test/beast/core/Jamfile | 1 - test/beast/core/string_param.cpp | 83 ------------------- test/beast/http/fields.cpp | 53 ++++++------ test/beast/http/file_body.cpp | 3 +- test/beast/http/write.cpp | 5 +- 14 files changed, 75 insertions(+), 137 deletions(-) delete mode 100644 test/beast/core/string_param.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 918b807e..44029fd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,18 @@ * Fix `max` compile error +* Deprecate `string_param` (API Change) + +API Changes: + +* `string_param`, which was previously the argument type when setting field values + has been replaced by `string_view`. Because of this, it is no longer possible to + set message field values directly as integrals. + + Users are required to convert numeric arguments to a string type prior to calling + `fields::set` et. al. + + Beast provides the non-allocating `to_static_string()` function for this purpose. + + To set Content-Length field manually, call `message::content_length`. -------------------------------------------------------------------------------- diff --git a/doc/qbk/quickref.xml b/doc/qbk/quickref.xml index d656987d..0b40c44f 100644 --- a/doc/qbk/quickref.xml +++ b/doc/qbk/quickref.xml @@ -44,7 +44,6 @@ span static_string stable_async_base - string_param string_view tcp_stream unlimited_rate_policy diff --git a/example/doc/http_examples.hpp b/example/doc/http_examples.hpp index af8e65ce..d0b8da7f 100644 --- a/example/doc/http_examples.hpp +++ b/example/doc/http_examples.hpp @@ -316,7 +316,7 @@ void do_server_head( // set of headers that would be sent for a GET request, // including the Content-Length, except for the body. res.result(status::ok); - res.set(field::content_length, payload.size()); + res.content_length(payload.size()); // For GET requests, we include the body if(req.method() == verb::get) diff --git a/example/http/server/small/http_server_small.cpp b/example/http/server/small/http_server_small.cpp index 53b6a2b3..c4f526f2 100644 --- a/example/http/server/small/http_server_small.cpp +++ b/example/http/server/small/http_server_small.cpp @@ -173,7 +173,7 @@ private: { auto self = shared_from_this(); - response_.set(http::field::content_length, response_.body().size()); + response_.content_length(response_.body().size()); http::async_write( socket_, diff --git a/include/boost/beast/core.hpp b/include/boost/beast/core.hpp index 905476d3..e6dd3be8 100644 --- a/include/boost/beast/core.hpp +++ b/include/boost/beast/core.hpp @@ -45,7 +45,6 @@ #include #include #include -#include #include #endif diff --git a/include/boost/beast/core/string_param.hpp b/include/boost/beast/core/string_param.hpp index cc55bfd1..71e231c2 100644 --- a/include/boost/beast/core/string_param.hpp +++ b/include/boost/beast/core/string_param.hpp @@ -10,6 +10,9 @@ #ifndef BOOST_BEAST_STRING_PARAM_HPP #define BOOST_BEAST_STRING_PARAM_HPP +#if defined(BOOST_BEAST_ALLOW_DEPRECATED) && !defined(BOOST_BEAST_DOXYGEN) + + #include #include #include @@ -126,4 +129,6 @@ public: #include +#endif // defined(BOOST_BEAST_ALLOW_DEPRECATED) && !BOOST_BEAST_DOXYGEN + #endif diff --git a/include/boost/beast/http/fields.hpp b/include/boost/beast/http/fields.hpp index 24b1a122..cf502282 100644 --- a/include/boost/beast/http/fields.hpp +++ b/include/boost/beast/http/fields.hpp @@ -11,7 +11,6 @@ #define BOOST_BEAST_HTTP_FIELDS_HPP #include -#include #include #include #include @@ -421,10 +420,10 @@ public: @param name The field name. - @param value The value of the field, as a @ref string_param + @param value The value of the field, as a @ref string_view */ void - insert(field name, string_param const& value); + insert(field name, string_view const& value); /** Insert a field. @@ -434,10 +433,10 @@ public: @param name The field name. - @param value The value of the field, as a @ref string_param + @param value The value of the field, as a @ref string_view */ void - insert(string_view name, string_param const& value); + insert(string_view name, string_view const& value); /** Insert a field. @@ -452,11 +451,11 @@ public: must be equal to `to_string(name)` using a case-insensitive comparison, otherwise the behavior is undefined. - @param value The value of the field, as a @ref string_param + @param value The value of the field, as a @ref string_view */ void insert(field name, string_view name_string, - string_param const& value); + string_view const& value); /** Set a field value, removing any other instances of that field. @@ -465,12 +464,12 @@ public: @param name The field name. - @param value The value of the field, as a @ref string_param + @param value The value of the field, as a @ref string_view @return The field value. */ void - set(field name, string_param const& value); + set(field name, string_view const& value); /** Set a field value, removing any other instances of that field. @@ -479,10 +478,10 @@ public: @param name The field name. - @param value The value of the field, as a @ref string_param + @param value The value of the field, as a @ref string_view */ void - set(string_view name, string_param const& value); + set(string_view name, string_view const& value); /** Remove a field. diff --git a/include/boost/beast/http/impl/fields.hpp b/include/boost/beast/http/impl/fields.hpp index dde92b92..f8492bd4 100644 --- a/include/boost/beast/http/impl/fields.hpp +++ b/include/boost/beast/http/impl/fields.hpp @@ -14,7 +14,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -541,7 +543,7 @@ template inline void basic_fields:: -insert(field name, string_param const& value) +insert(field name, string_view const& value) { BOOST_ASSERT(name != field::unknown); insert(name, to_string(name), value); @@ -550,7 +552,7 @@ insert(field name, string_param const& value) template void basic_fields:: -insert(string_view sname, string_param const& value) +insert(string_view sname, string_view const& value) { auto const name = string_to_field(sname); @@ -561,7 +563,7 @@ template void basic_fields:: insert(field name, - string_view sname, string_param const& value) + string_view sname, string_view const& value) { auto& e = new_element(name, sname, static_cast(value)); @@ -591,7 +593,7 @@ insert(field name, template void basic_fields:: -set(field name, string_param const& value) +set(field name, string_view const& value) { BOOST_ASSERT(name != field::unknown); set_element(new_element(name, to_string(name), @@ -601,11 +603,10 @@ set(field name, string_param const& value) template void basic_fields:: -set(string_view sname, string_param const& value) +set(string_view sname, string_view const& value) { set_element(new_element( - string_to_field(sname), sname, - static_cast(value))); + string_to_field(sname), sname, value)); } template @@ -931,7 +932,10 @@ set_content_length_impl( if(! value) erase(field::content_length); else - set(field::content_length, *value); + { + set(field::content_length, + to_static_string(*value)); + } } template diff --git a/test/beast/core/CMakeLists.txt b/test/beast/core/CMakeLists.txt index ffc12ad2..43cd6ebd 100644 --- a/test/beast/core/CMakeLists.txt +++ b/test/beast/core/CMakeLists.txt @@ -61,7 +61,6 @@ add_executable (tests-beast-core static_string.cpp stream_traits.cpp string.cpp - string_param.cpp tcp_stream.cpp ) diff --git a/test/beast/core/Jamfile b/test/beast/core/Jamfile index 9ec47c1a..e251293b 100644 --- a/test/beast/core/Jamfile +++ b/test/beast/core/Jamfile @@ -52,7 +52,6 @@ local SOURCES = static_string.cpp stream_traits.cpp string.cpp - string_param.cpp tcp_stream.cpp ; diff --git a/test/beast/core/string_param.cpp b/test/beast/core/string_param.cpp deleted file mode 100644 index 395dea24..00000000 --- a/test/beast/core/string_param.cpp +++ /dev/null @@ -1,83 +0,0 @@ -// -// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// -// Official repository: https://github.com/boostorg/beast -// - -// Test that header file is self-contained. -#include - -#include -#include - -namespace boost { -namespace beast { - -class string_param_test : public unit_test::suite -{ -public: - struct nop {}; - - void - check(string_param const& v, string_view s) - { - BEAST_EXPECT(static_cast(v) == s); - } - - class repeater - { - std::size_t n_; - - public: - explicit - repeater(std::size_t n) - : n_(n) - { - } - - friend - std::ostream& - operator<<(std::ostream& os, repeater const& v) - { - return os << std::string(v.n_, '*'); - } - }; - - void - testConversion() - { - // Make sure things convert correctly - check(std::string("hello"), "hello"); - check("xyz", "xyz"); - check(1, "1"); - check(12, "12"); - check(123, "123"); - check(1234, "1234"); - check(12345, "12345"); - check({"a", "b"}, "ab"); - check({1, 2, 3}, "123"); - } - - void - testStaticOstream() - { - // exercise static_ostream for coverage - std::string s(500, '*'); - check(repeater{500}, s); - } - - void - run() override - { - testConversion(); - testStaticOstream(); - } -}; - -BEAST_DEFINE_TESTSUITE(beast,core,string_param); - -} // beast -} // boost diff --git a/test/beast/http/fields.cpp b/test/beast/http/fields.cpp index 7ddb9d44..962db4fc 100644 --- a/test/beast/http/fields.cpp +++ b/test/beast/http/fields.cpp @@ -10,6 +10,7 @@ // Test that header file is self-contained. #include +#include #include #include #include @@ -94,7 +95,7 @@ public: fill(std::size_t n, basic_fields& f) { for(std::size_t i = 1; i<= n; ++i) - f.insert(std::to_string(i), i); + f.insert(std::to_string(i), to_static_string(i)); } template @@ -411,10 +412,10 @@ public: { // group fields fields f; - f.insert(field::age, 1); - f.insert(field::body, 2); - f.insert(field::close, 3); - f.insert(field::body, 4); + f.insert(field::age, to_static_string(1)); + f.insert(field::body, to_static_string(2)); + f.insert(field::close, to_static_string(3)); + f.insert(field::body, to_static_string(4)); BEAST_EXPECT(std::next(f.begin(), 0)->name() == field::age); BEAST_EXPECT(std::next(f.begin(), 1)->name() == field::body); BEAST_EXPECT(std::next(f.begin(), 2)->name() == field::body); @@ -434,10 +435,10 @@ public: { // group fields, case insensitive fields f; - f.insert("a", 1); - f.insert("ab", 2); - f.insert("b", 3); - f.insert("AB", 4); + f.insert("a", to_static_string(1)); + f.insert("ab", to_static_string(2)); + f.insert("b", to_static_string(3)); + f.insert("AB", to_static_string(4)); BEAST_EXPECT(std::next(f.begin(), 0)->name() == field::unknown); BEAST_EXPECT(std::next(f.begin(), 1)->name() == field::unknown); BEAST_EXPECT(std::next(f.begin(), 2)->name() == field::unknown); @@ -457,14 +458,14 @@ public: { // verify insertion orde fields f; - f.insert( "a", 1); - f.insert("dd", 2); - f.insert("b", 3); - f.insert("dD", 4); - f.insert("c", 5); - f.insert("Dd", 6); - f.insert("DD", 7); - f.insert( "e", 8); + f.insert( "a", to_static_string(1)); + f.insert("dd", to_static_string(2)); + f.insert("b", to_static_string(3)); + f.insert("dD", to_static_string(4)); + f.insert("c", to_static_string(5)); + f.insert("Dd", to_static_string(6)); + f.insert("DD", to_static_string(7)); + f.insert( "e", to_static_string(8)); BEAST_EXPECT(f.count("dd") == 4); BEAST_EXPECT(std::next(f.begin(), 1)->name_string() == "dd"); BEAST_EXPECT(std::next(f.begin(), 2)->name_string() == "dD"); @@ -478,13 +479,13 @@ public: // equal_range { fields f; - f.insert("E", 1); - f.insert("B", 2); - f.insert("D", 3); - f.insert("B", 4); - f.insert("C", 5); - f.insert("B", 6); - f.insert("A", 7); + f.insert("E", to_static_string(1)); + f.insert("B", to_static_string(2)); + f.insert("D", to_static_string(3)); + f.insert("B", to_static_string(4)); + f.insert("C", to_static_string(5)); + f.insert("B", to_static_string(6)); + f.insert("A", to_static_string(7)); auto const rng = f.equal_range("B"); BEAST_EXPECT(std::distance(rng.first, rng.second) == 3); BEAST_EXPECT(std::next(rng.first, 0)->value() == "2"); @@ -944,7 +945,7 @@ public: }; res.erase(field::transfer_encoding); - res.set(field::content_length, 32); + res.set(field::content_length, to_static_string(32)); chunked(true); BEAST_EXPECT(res[field::transfer_encoding] == "chunked"); @@ -953,7 +954,7 @@ public: BEAST_EXPECT(res[field::transfer_encoding] == "chunked"); res.erase(field::transfer_encoding); - res.set(field::content_length, 32); + res.set(field::content_length, to_static_string(32)); chunked(false); BEAST_EXPECT(res.count(field::transfer_encoding) == 0); diff --git a/test/beast/http/file_body.cpp b/test/beast/http/file_body.cpp index 96873b3a..a84ca899 100644 --- a/test/beast/http/file_body.cpp +++ b/test/beast/http/file_body.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -174,7 +175,7 @@ public: header.version(11); header.result(status::accepted); header.set(field::server, "test"); - header.set(field::content_length, 4097); + header.set(field::content_length, to_static_string(4097)); typename file_body_type::writer w(header, value); auto maybe_range = w.get(ec); diff --git a/test/beast/http/write.cpp b/test/beast/http/write.cpp index 2a4ac999..e0be03d4 100644 --- a/test/beast/http/write.cpp +++ b/test/beast/http/write.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -642,7 +643,7 @@ public: m.method(verb::get); m.version(11); m.target("/"); - m.set("Content-Length", 5); + m.set("Content-Length", to_static_string(5)); m.body() = "*****"; async_write(ts, m, handler{}); BEAST_EXPECT(handler::count() > 0); @@ -665,7 +666,7 @@ public: m.method(verb::get); m.version(11); m.target("/"); - m.set("Content-Length", 5); + m.set("Content-Length", to_static_string(5)); m.body() = "*****"; async_write(ts, m, handler{}); BEAST_EXPECT(handler::count() > 0);