From c6d7fa185b99baf8a4e76bf9a14f0abc24a47d7f Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 8 Jul 2017 14:59:04 -0700 Subject: [PATCH] Tidy up basic_string_body fix #580 --- CHANGELOG.md | 8 ++++ doc/5_02_message.qbk | 9 +++-- doc/concept/BodyReader.qbk | 2 +- doc/concept/BodyWriter.qbk | 2 +- doc/quickref.xml | 1 + include/beast/http/string_body.hpp | 60 ++++++++++++++++++++++++------ test/http/string_body.cpp | 10 +++++ 7 files changed, 74 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 173c073c..32d63638 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +Version 78: + +HTTP: + +* Tidy up basic_string_body + +-------------------------------------------------------------------------------- + Version 77: * file_posix works without large file support diff --git a/doc/5_02_message.qbk b/doc/5_02_message.qbk index 19536836..f6015056 100644 --- a/doc/5_02_message.qbk +++ b/doc/5_02_message.qbk @@ -153,12 +153,13 @@ meet the requirements, or use the ones that come with the library: for streaming and incremental sends and receives. ]] [[ + [link beast.ref.beast__http__basic_string_body `basic_string_body`] [link beast.ref.beast__http__string_body `string_body`] ][ - A body whose `value_type` is `std::string`. Insertion complexity - is amortized constant time, while capacity grows geometrically. - Messages with this body type may be serialized and parsed. This - is the type of body used in the examples. + A body whose `value_type` is `std::basic_string` or `std::string`. + Insertion complexity is amortized constant time, while capacity + grows geometrically. Messages with this body type may be serialized + and parsed. This is the type of body used in the examples. ]] [[ [link beast.ref.beast__http__string_view_body `string_view_body`] diff --git a/doc/concept/BodyReader.qbk b/doc/concept/BodyReader.qbk index 470370df..af7b9aa7 100644 --- a/doc/concept/BodyReader.qbk +++ b/doc/concept/BodyReader.qbk @@ -110,7 +110,7 @@ In this table: * [link beast.ref.beast__http__basic_dynamic_body.reader `basic_dynamic_body::reader`] * [link beast.ref.beast__http__basic_file_body__reader `basic_file_body::reader`] +* [link beast.ref.beast__http__basic_string_body.reader `basic_string_body::reader`] * [link beast.ref.beast__http__empty_body.reader `empty_body::reader`] -* [link beast.ref.beast__http__string_body.reader `string_body::reader`] [endsect] diff --git a/doc/concept/BodyWriter.qbk b/doc/concept/BodyWriter.qbk index 50ae408d..54c93fdc 100644 --- a/doc/concept/BodyWriter.qbk +++ b/doc/concept/BodyWriter.qbk @@ -113,7 +113,7 @@ In this table: * [link beast.ref.beast__http__basic_dynamic_body.writer `basic_dynamic_body::writer`] * [link beast.ref.beast__http__basic_file_body__reader `basic_file_body::writer`] +* [link beast.ref.beast__http__basic_string_body.writer `basic_string_body::writer`] * [link beast.ref.beast__http__empty_body.writer `empty_body::writer`] -* [link beast.ref.beast__http__string_body.writer `string_body::writer`] [endsect] diff --git a/doc/quickref.xml b/doc/quickref.xml index b9384156..13e5b9ff 100644 --- a/doc/quickref.xml +++ b/doc/quickref.xml @@ -33,6 +33,7 @@ basic_file_body basic_fields basic_parser + basic_string_body buffer_body dynamic_body empty_body diff --git a/include/beast/http/string_body.hpp b/include/beast/http/string_body.hpp index 089df6df..cdeaec7c 100644 --- a/include/beast/http/string_body.hpp +++ b/include/beast/http/string_body.hpp @@ -14,32 +14,61 @@ #include #include #include +#include +#include #include +#include #include #include namespace beast { namespace http { -/** An HTTP message body represented by a `std::string`. +/** A @b Body using `std::basic_string` - Meets the requirements of @b Body. + This body uses `std::basic_string` as a memory-based container + for holding message payloads. Messages using this body type + may be serialized and parsed. */ -struct string_body +template< + class CharT, + class Traits = std::char_traits, + class Allocator = std::allocator> +struct basic_string_body { - /// The type of the body member when used in a message. - using value_type = std::string; +private: + static_assert( + std::is_integral::value && + sizeof(CharT) == 1, + "CharT requirements not met"); - /// Returns the content length of the body in a message. +public: + /** The type of container used for the body + + This determines the type of @ref message::body + when this body type is used with a message container. + */ + using value_type = + std::basic_string; + + /** Returns the payload size of the body + + When this body is used with @ref message::prepare_payload, + the Content-Length will be set to the payload size, and + any chunked Transfer-Encoding will be removed. + */ static std::uint64_t - size(value_type const& v) + size(value_type const& body) { - return v.size(); + return body.size(); } + /** The algorithm for serializing the body + + Meets the requirements of @b BodyReader. + */ #if BEAST_DOXYGEN - /// The algorithm to obtain buffers representing the body using reader = implementation_defined; #else class reader @@ -53,7 +82,7 @@ struct string_body template explicit reader(message const& msg) + basic_string_body, Fields> const& msg) : body_(msg.body) { } @@ -74,8 +103,11 @@ struct string_body }; #endif + /** The algorithm for parsing the body + + Meets the requirements of @b BodyReader. + */ #if BEAST_DOXYGEN - /// The algorithm used store buffers in this body using writer = implementation_defined; #else class writer @@ -85,7 +117,8 @@ struct string_body public: template explicit - writer(message& m) + writer(message& m) : body_(m.body) { } @@ -148,6 +181,9 @@ struct string_body #endif }; +/// A @b Body using `std::string` +using string_body = basic_string_body; + } // http } // beast diff --git a/test/http/string_body.cpp b/test/http/string_body.cpp index 4b84a939..71e9dea2 100644 --- a/test/http/string_body.cpp +++ b/test/http/string_body.cpp @@ -7,3 +7,13 @@ // Test that header file is self-contained. #include + +namespace beast { +namespace http { + +BOOST_STATIC_ASSERT(is_body::value); +BOOST_STATIC_ASSERT(is_body_reader::value); +BOOST_STATIC_ASSERT(is_body_writer::value); + +} // http +} // beast