2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_HTTP_STRING_BODY_HPP
11 #define BOOST_BEAST_HTTP_STRING_BODY_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/http/error.hpp>
15 #include <boost/beast/http/message.hpp>
16 #include <boost/beast/core/detail/type_traits.hpp>
17 #include <boost/asio/buffer.hpp>
18 #include <boost/optional.hpp>
30 /** A @b Body using `std::basic_string`
32 This body uses `std::basic_string` as a memory-based container
33 for holding message payloads. Messages using this body type
34 may be serialized and parsed.
38 class Traits = std::char_traits<CharT>,
39 class Allocator = std::allocator<CharT>>
40 struct basic_string_body
44 std::is_integral<CharT>::value &&
46 "CharT requirements not met");
49 /** The type of container used for the body
51 This determines the type of @ref message::body
52 when this body type is used with a message container.
55 std::basic_string<CharT, Traits, Allocator>;
57 /** Returns the payload size of the body
59 When this body is used with @ref message::prepare_payload,
60 the Content-Length will be set to the payload size, and
61 any chunked Transfer-Encoding will be removed.
65 size(value_type const& body)
70 /** The algorithm for parsing the body
72 Meets the requirements of @b BodyReader.
74 #if BOOST_BEAST_DOXYGEN
75 using reader = implementation_defined;
82 template<bool isRequest, class Fields>
84 reader(message<isRequest,
85 basic_string_body, Fields>& m)
92 std::uint64_t> const& length, error_code& ec)
96 if(static_cast<std::size_t>(*length) != *length)
98 ec = error::buffer_overflow;
104 static_cast<std::size_t>(*length));
106 catch(std::exception const&)
108 ec = error::buffer_overflow;
112 ec.assign(0, ec.category());
115 template<class ConstBufferSequence>
117 put(ConstBufferSequence const& buffers,
120 using boost::asio::buffer_size;
121 using boost::asio::buffer_copy;
122 auto const extra = buffer_size(buffers);
123 auto const size = body_.size();
126 body_.resize(size + extra);
128 catch(std::exception const&)
130 ec = error::buffer_overflow;
133 ec.assign(0, ec.category());
134 CharT* dest = &body_[size];
135 for(auto b : beast::detail::buffers_range(buffers))
137 Traits::copy(dest, reinterpret_cast<
138 CharT const*>(b.data()), b.size());
145 finish(error_code& ec)
147 ec.assign(0, ec.category());
152 /** The algorithm for serializing the body
154 Meets the requirements of @b BodyWriter.
156 #if BOOST_BEAST_DOXYGEN
157 using writer = implementation_defined;
161 value_type const& body_;
164 using const_buffers_type =
165 boost::asio::const_buffer;
167 template<bool isRequest, class Fields>
169 writer(message<isRequest,
170 basic_string_body, Fields> const& msg)
178 ec.assign(0, ec.category());
181 boost::optional<std::pair<const_buffers_type, bool>>
184 ec.assign(0, ec.category());
185 return {{const_buffers_type{
186 body_.data(), body_.size()}, false}};
192 /// A @b Body using `std::string`
193 using string_body = basic_string_body<char>;