Tidy up basic_string_body

fix #580
This commit is contained in:
Vinnie Falco
2017-07-08 14:59:04 -07:00
parent 222eca839a
commit c6d7fa185b
7 changed files with 74 additions and 18 deletions

View File

@ -1,3 +1,11 @@
Version 78:
HTTP:
* Tidy up basic_string_body
--------------------------------------------------------------------------------
Version 77: Version 77:
* file_posix works without large file support * file_posix works without large file support

View File

@ -153,12 +153,13 @@ meet the requirements, or use the ones that come with the library:
for streaming and incremental sends and receives. 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`] [link beast.ref.beast__http__string_body `string_body`]
][ ][
A body whose `value_type` is `std::string`. Insertion complexity A body whose `value_type` is `std::basic_string` or `std::string`.
is amortized constant time, while capacity grows geometrically. Insertion complexity is amortized constant time, while capacity
Messages with this body type may be serialized and parsed. This grows geometrically. Messages with this body type may be serialized
is the type of body used in the examples. and parsed. This is the type of body used in the examples.
]] ]]
[[ [[
[link beast.ref.beast__http__string_view_body `string_view_body`] [link beast.ref.beast__http__string_view_body `string_view_body`]

View File

@ -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_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_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__empty_body.reader `empty_body::reader`]
* [link beast.ref.beast__http__string_body.reader `string_body::reader`]
[endsect] [endsect]

View File

@ -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_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_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__empty_body.writer `empty_body::writer`]
* [link beast.ref.beast__http__string_body.writer `string_body::writer`]
[endsect] [endsect]

View File

@ -33,6 +33,7 @@
<member><link linkend="beast.ref.beast__http__basic_file_body">basic_file_body</link></member> <member><link linkend="beast.ref.beast__http__basic_file_body">basic_file_body</link></member>
<member><link linkend="beast.ref.beast__http__basic_fields">basic_fields</link></member> <member><link linkend="beast.ref.beast__http__basic_fields">basic_fields</link></member>
<member><link linkend="beast.ref.beast__http__basic_parser">basic_parser</link></member> <member><link linkend="beast.ref.beast__http__basic_parser">basic_parser</link></member>
<member><link linkend="beast.ref.beast__http__basic_string_body">basic_string_body</link></member>
<member><link linkend="beast.ref.beast__http__buffer_body">buffer_body</link></member> <member><link linkend="beast.ref.beast__http__buffer_body">buffer_body</link></member>
<member><link linkend="beast.ref.beast__http__dynamic_body">dynamic_body</link></member> <member><link linkend="beast.ref.beast__http__dynamic_body">dynamic_body</link></member>
<member><link linkend="beast.ref.beast__http__empty_body">empty_body</link></member> <member><link linkend="beast.ref.beast__http__empty_body">empty_body</link></member>

View File

@ -14,32 +14,61 @@
#include <beast/core/detail/type_traits.hpp> #include <beast/core/detail/type_traits.hpp>
#include <boost/asio/buffer.hpp> #include <boost/asio/buffer.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <cstdint>
#include <limits>
#include <memory> #include <memory>
#include <stdexcept>
#include <string> #include <string>
#include <utility> #include <utility>
namespace beast { namespace beast {
namespace http { 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<CharT>,
class Allocator = std::allocator<CharT>>
struct basic_string_body
{ {
/// The type of the body member when used in a message. private:
using value_type = std::string; static_assert(
std::is_integral<CharT>::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<CharT, Traits, Allocator>;
/** 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 static
std::uint64_t 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 #if BEAST_DOXYGEN
/// The algorithm to obtain buffers representing the body
using reader = implementation_defined; using reader = implementation_defined;
#else #else
class reader class reader
@ -53,7 +82,7 @@ struct string_body
template<bool isRequest, class Fields> template<bool isRequest, class Fields>
explicit explicit
reader(message<isRequest, reader(message<isRequest,
string_body, Fields> const& msg) basic_string_body, Fields> const& msg)
: body_(msg.body) : body_(msg.body)
{ {
} }
@ -74,8 +103,11 @@ struct string_body
}; };
#endif #endif
/** The algorithm for parsing the body
Meets the requirements of @b BodyReader.
*/
#if BEAST_DOXYGEN #if BEAST_DOXYGEN
/// The algorithm used store buffers in this body
using writer = implementation_defined; using writer = implementation_defined;
#else #else
class writer class writer
@ -85,7 +117,8 @@ struct string_body
public: public:
template<bool isRequest, class Fields> template<bool isRequest, class Fields>
explicit explicit
writer(message<isRequest, string_body, Fields>& m) writer(message<isRequest,
basic_string_body, Fields>& m)
: body_(m.body) : body_(m.body)
{ {
} }
@ -148,6 +181,9 @@ struct string_body
#endif #endif
}; };
/// A @b Body using `std::string`
using string_body = basic_string_body<char>;
} // http } // http
} // beast } // beast

View File

@ -7,3 +7,13 @@
// Test that header file is self-contained. // Test that header file is self-contained.
#include <beast/http/string_body.hpp> #include <beast/http/string_body.hpp>
namespace beast {
namespace http {
BOOST_STATIC_ASSERT(is_body<string_body>::value);
BOOST_STATIC_ASSERT(is_body_reader<string_body>::value);
BOOST_STATIC_ASSERT(is_body_writer<string_body>::value);
} // http
} // beast