buffer_body.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  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)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_HTTP_BUFFER_BODY_HPP
  10. #define BOOST_BEAST_HTTP_BUFFER_BODY_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/buffer_traits.hpp>
  13. #include <boost/beast/http/error.hpp>
  14. #include <boost/beast/http/message.hpp>
  15. #include <boost/beast/http/type_traits.hpp>
  16. #include <boost/optional.hpp>
  17. #include <type_traits>
  18. #include <utility>
  19. namespace boost {
  20. namespace beast {
  21. namespace http {
  22. /** A <em>Body</em> using a caller provided buffer
  23. Messages using this body type may be serialized and parsed.
  24. To use this class, the caller must initialize the members
  25. of @ref buffer_body::value_type to appropriate values before
  26. each call to read or write during a stream operation.
  27. */
  28. struct buffer_body
  29. {
  30. /// The type of the body member when used in a message.
  31. struct value_type
  32. {
  33. /** A pointer to a contiguous area of memory of @ref size octets, else `nullptr`.
  34. @par When Serializing
  35. If this is `nullptr` and `more` is `true`, the error
  36. @ref error::need_buffer will be returned from @ref serializer::get
  37. Otherwise, the serializer will use the memory pointed to
  38. by `data` having `size` octets of valid storage as the
  39. next buffer representing the body.
  40. @par When Parsing
  41. If this is `nullptr`, the error @ref error::need_buffer
  42. will be returned from @ref parser::put. Otherwise, the
  43. parser will store body octets into the memory pointed to
  44. by `data` having `size` octets of valid storage. After
  45. octets are stored, the `data` and `size` members are
  46. adjusted: `data` is incremented to point to the next
  47. octet after the data written, while `size` is decremented
  48. to reflect the remaining space at the memory location
  49. pointed to by `data`.
  50. */
  51. void* data = nullptr;
  52. /** The number of octets in the buffer pointed to by @ref data.
  53. @par When Serializing
  54. If `data` is `nullptr` during serialization, this value
  55. is ignored. Otherwise, it represents the number of valid
  56. body octets pointed to by `data`.
  57. @par When Parsing
  58. The value of this field will be decremented during parsing
  59. to indicate the number of remaining free octets in the
  60. buffer pointed to by `data`. When it reaches zero, the
  61. parser will return @ref error::need_buffer, indicating to
  62. the caller that the values of `data` and `size` should be
  63. updated to point to a new memory buffer.
  64. */
  65. std::size_t size = 0;
  66. /** `true` if this is not the last buffer.
  67. @par When Serializing
  68. If this is `true` and `data` is `nullptr`, the error
  69. @ref error::need_buffer will be returned from @ref serializer::get
  70. @par When Parsing
  71. This field is not used during parsing.
  72. */
  73. bool more = true;
  74. };
  75. /** The algorithm for parsing the body
  76. Meets the requirements of <em>BodyReader</em>.
  77. */
  78. #if BOOST_BEAST_DOXYGEN
  79. using reader = __implementation_defined__;
  80. #else
  81. class reader
  82. {
  83. value_type& body_;
  84. public:
  85. template<bool isRequest, class Fields>
  86. explicit
  87. reader(header<isRequest, Fields>&, value_type& b)
  88. : body_(b)
  89. {
  90. }
  91. void
  92. init(boost::optional<std::uint64_t> const&, error_code& ec)
  93. {
  94. ec = {};
  95. }
  96. template<class ConstBufferSequence>
  97. std::size_t
  98. put(ConstBufferSequence const& buffers,
  99. error_code& ec)
  100. {
  101. if(! body_.data)
  102. {
  103. BOOST_BEAST_ASSIGN_EC(ec, error::need_buffer);
  104. return 0;
  105. }
  106. auto const bytes_transferred =
  107. net::buffer_copy(net::buffer(
  108. body_.data, body_.size), buffers);
  109. body_.data = static_cast<char*>(
  110. body_.data) + bytes_transferred;
  111. body_.size -= bytes_transferred;
  112. if(bytes_transferred == buffer_bytes(buffers))
  113. ec = {};
  114. else
  115. {
  116. BOOST_BEAST_ASSIGN_EC(ec, error::need_buffer);
  117. }
  118. return bytes_transferred;
  119. }
  120. void
  121. finish(error_code& ec)
  122. {
  123. ec = {};
  124. }
  125. };
  126. #endif
  127. /** The algorithm for serializing the body
  128. Meets the requirements of <em>BodyWriter</em>.
  129. */
  130. #if BOOST_BEAST_DOXYGEN
  131. using writer = __implementation_defined__;
  132. #else
  133. class writer
  134. {
  135. bool toggle_ = false;
  136. value_type const& body_;
  137. public:
  138. using const_buffers_type =
  139. net::const_buffer;
  140. template<bool isRequest, class Fields>
  141. explicit
  142. writer(header<isRequest, Fields> const&, value_type const& b)
  143. : body_(b)
  144. {
  145. }
  146. void
  147. init(error_code& ec)
  148. {
  149. ec = {};
  150. }
  151. boost::optional<
  152. std::pair<const_buffers_type, bool>>
  153. get(error_code& ec)
  154. {
  155. if(toggle_)
  156. {
  157. if(body_.more)
  158. {
  159. toggle_ = false;
  160. BOOST_BEAST_ASSIGN_EC(ec, error::need_buffer);
  161. }
  162. else
  163. {
  164. ec = {};
  165. }
  166. return boost::none;
  167. }
  168. if(body_.data)
  169. {
  170. ec = {};
  171. toggle_ = true;
  172. return {{const_buffers_type{
  173. body_.data, body_.size}, body_.more}};
  174. }
  175. if(body_.more)
  176. {
  177. BOOST_BEAST_ASSIGN_EC(ec, error::need_buffer);
  178. }
  179. else
  180. ec = {};
  181. return boost::none;
  182. }
  183. };
  184. #endif
  185. };
  186. #if ! BOOST_BEAST_DOXYGEN
  187. // operator<< is not supported for buffer_body
  188. template<bool isRequest, class Fields>
  189. std::ostream&
  190. operator<<(std::ostream& os, message<isRequest,
  191. buffer_body, Fields> const& msg) = delete;
  192. #endif
  193. } // http
  194. } // beast
  195. } // boost
  196. #endif