buffer_body.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. 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. ec = error::need_buffer;
  116. return bytes_transferred;
  117. }
  118. void
  119. finish(error_code& ec)
  120. {
  121. ec = {};
  122. }
  123. };
  124. #endif
  125. /** The algorithm for serializing the body
  126. Meets the requirements of <em>BodyWriter</em>.
  127. */
  128. #if BOOST_BEAST_DOXYGEN
  129. using writer = __implementation_defined__;
  130. #else
  131. class writer
  132. {
  133. bool toggle_ = false;
  134. value_type const& body_;
  135. public:
  136. using const_buffers_type =
  137. net::const_buffer;
  138. template<bool isRequest, class Fields>
  139. explicit
  140. writer(header<isRequest, Fields> const&, value_type const& b)
  141. : body_(b)
  142. {
  143. }
  144. void
  145. init(error_code& ec)
  146. {
  147. ec = {};
  148. }
  149. boost::optional<
  150. std::pair<const_buffers_type, bool>>
  151. get(error_code& ec)
  152. {
  153. if(toggle_)
  154. {
  155. if(body_.more)
  156. {
  157. toggle_ = false;
  158. ec = error::need_buffer;
  159. }
  160. else
  161. {
  162. ec = {};
  163. }
  164. return boost::none;
  165. }
  166. if(body_.data)
  167. {
  168. ec = {};
  169. toggle_ = true;
  170. return {{const_buffers_type{
  171. body_.data, body_.size}, body_.more}};
  172. }
  173. if(body_.more)
  174. ec = error::need_buffer;
  175. else
  176. ec = {};
  177. return boost::none;
  178. }
  179. };
  180. #endif
  181. };
  182. #if ! BOOST_BEAST_DOXYGEN
  183. // operator<< is not supported for buffer_body
  184. template<bool isRequest, class Fields>
  185. std::ostream&
  186. operator<<(std::ostream& os, message<isRequest,
  187. buffer_body, Fields> const& msg) = delete;
  188. #endif
  189. } // http
  190. } // beast
  191. } // boost
  192. #endif