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
This commit is contained in:
Richard Hodges
2020-06-04 19:18:44 +02:00
parent ae2b699e04
commit 14c3d50b57
14 changed files with 75 additions and 137 deletions

View File

@@ -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`.
--------------------------------------------------------------------------------

View File

@@ -44,7 +44,6 @@
<member><link linkend="beast.ref.boost__beast__span">span</link></member>
<member><link linkend="beast.ref.boost__beast__static_string">static_string</link></member>
<member><link linkend="beast.ref.boost__beast__stable_async_base">stable_async_base</link></member>
<member><link linkend="beast.ref.boost__beast__string_param">string_param</link></member>
<member><link linkend="beast.ref.boost__beast__string_view">string_view</link></member>
<member><link linkend="beast.ref.boost__beast__tcp_stream">tcp_stream</link></member>
<member><link linkend="beast.ref.boost__beast__unlimited_rate_policy">unlimited_rate_policy</link></member>

View File

@@ -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)

View File

@@ -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_,

View File

@@ -45,7 +45,6 @@
#include <boost/beast/core/static_string.hpp>
#include <boost/beast/core/stream_traits.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/core/string_param.hpp>
#include <boost/beast/core/tcp_stream.hpp>
#endif

View File

@@ -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 <boost/beast/core/detail/config.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/core/static_string.hpp>
@@ -126,4 +129,6 @@ public:
#include <boost/beast/core/impl/string_param.hpp>
#endif // defined(BOOST_BEAST_ALLOW_DEPRECATED) && !BOOST_BEAST_DOXYGEN
#endif

View File

@@ -11,7 +11,6 @@
#define BOOST_BEAST_HTTP_FIELDS_HPP
#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/core/string_param.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/core/detail/allocator.hpp>
#include <boost/beast/http/field.hpp>
@@ -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.

View File

@@ -14,7 +14,9 @@
#include <boost/beast/core/string.hpp>
#include <boost/beast/core/detail/buffers_ref.hpp>
#include <boost/beast/core/detail/clamp.hpp>
#include <boost/beast/core/detail/static_string.hpp>
#include <boost/beast/core/detail/temporary_buffer.hpp>
#include <boost/beast/core/static_string.hpp>
#include <boost/beast/http/verb.hpp>
#include <boost/beast/http/rfc7230.hpp>
#include <boost/beast/http/status.hpp>
@@ -541,7 +543,7 @@ template<class Allocator>
inline
void
basic_fields<Allocator>::
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<class Allocator>
void
basic_fields<Allocator>::
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<class Allocator>
void
basic_fields<Allocator>::
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<string_view>(value));
@@ -591,7 +593,7 @@ insert(field name,
template<class Allocator>
void
basic_fields<Allocator>::
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<class Allocator>
void
basic_fields<Allocator>::
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<string_view>(value)));
string_to_field(sname), sname, value));
}
template<class Allocator>
@@ -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<class Allocator>

View File

@@ -61,7 +61,6 @@ add_executable (tests-beast-core
static_string.cpp
stream_traits.cpp
string.cpp
string_param.cpp
tcp_stream.cpp
)

View File

@@ -52,7 +52,6 @@ local SOURCES =
static_string.cpp
stream_traits.cpp
string.cpp
string_param.cpp
tcp_stream.cpp
;

View File

@@ -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 <boost/beast/core/string_param.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/beast/core/detail/type_traits.hpp>
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<string_view>(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

View File

@@ -10,6 +10,7 @@
// Test that header file is self-contained.
#include <boost/beast/http/fields.hpp>
#include <boost/beast/core/static_string.hpp>
#include <boost/beast/http/empty_body.hpp>
#include <boost/beast/http/message.hpp>
#include <boost/beast/http/type_traits.hpp>
@@ -94,7 +95,7 @@ public:
fill(std::size_t n, basic_fields<Allocator>& 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<class U, class V>
@@ -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);

View File

@@ -14,6 +14,7 @@
#include <boost/beast/core/buffers_prefix.hpp>
#include <boost/beast/core/file_stdio.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/static_string.hpp>
#include <boost/beast/http/parser.hpp>
#include <boost/beast/http/serializer.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
@@ -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);

View File

@@ -18,6 +18,7 @@
#include <boost/beast/core/buffers_to_string.hpp>
#include <boost/beast/core/error.hpp>
#include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/core/static_string.hpp>
#include <boost/beast/_experimental/test/stream.hpp>
#include <boost/beast/test/yield_to.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
@@ -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);