span_body.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_SPAN_BODY_HPP
  10. #define BOOST_BEAST_HTTP_SPAN_BODY_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/buffer_traits.hpp>
  13. #include <boost/beast/core/span.hpp>
  14. #include <boost/beast/http/error.hpp>
  15. #include <boost/beast/http/message.hpp>
  16. #include <boost/optional.hpp>
  17. namespace boost {
  18. namespace beast {
  19. namespace http {
  20. /** A <em>Body</em> using @ref span
  21. This body uses @ref span as a memory-based container for
  22. holding message payloads. The container represents a
  23. non-owning reference to a contiguous area of memory.
  24. Messages using this body type may be serialized and
  25. parsed.
  26. Unlike @ref buffer_body, only one buffer may be provided
  27. during a parse or serialize operation.
  28. */
  29. template<class T>
  30. struct span_body
  31. {
  32. private:
  33. static_assert(std::is_pod<T>::value,
  34. "POD requirements not met");
  35. public:
  36. /** The type of container used for the body
  37. This determines the type of @ref message::body
  38. when this body type is used with a message container.
  39. */
  40. using value_type = span<T>;
  41. /** Returns the payload size of the body
  42. When this body is used with @ref message::prepare_payload,
  43. the Content-Length will be set to the payload size, and
  44. any chunked Transfer-Encoding will be removed.
  45. */
  46. static
  47. std::uint64_t
  48. size(value_type const& body)
  49. {
  50. return body.size();
  51. }
  52. /** The algorithm for parsing the body
  53. Meets the requirements of <em>BodyReader</em>.
  54. */
  55. #if BOOST_BEAST_DOXYGEN
  56. using reader = __implementation_defined__;
  57. #else
  58. class reader
  59. {
  60. value_type& body_;
  61. public:
  62. template<bool isRequest, class Fields>
  63. explicit
  64. reader(header<isRequest, Fields>&, value_type& b)
  65. : body_(b)
  66. {
  67. }
  68. void
  69. init(boost::optional<
  70. std::uint64_t> const& length, error_code& ec)
  71. {
  72. if(length && *length > body_.size())
  73. {
  74. ec = error::buffer_overflow;
  75. return;
  76. }
  77. ec = {};
  78. }
  79. template<class ConstBufferSequence>
  80. std::size_t
  81. put(ConstBufferSequence const& buffers,
  82. error_code& ec)
  83. {
  84. auto const n = buffer_bytes(buffers);
  85. auto const len = body_.size();
  86. if(n > len)
  87. {
  88. ec = error::buffer_overflow;
  89. return 0;
  90. }
  91. ec = {};
  92. net::buffer_copy(net::buffer(
  93. body_.data(), n), buffers);
  94. body_ = value_type{
  95. body_.data() + n, body_.size() - n};
  96. return n;
  97. }
  98. void
  99. finish(error_code& ec)
  100. {
  101. ec = {};
  102. }
  103. };
  104. #endif
  105. /** The algorithm for serializing the body
  106. Meets the requirements of <em>BodyWriter</em>.
  107. */
  108. #if BOOST_BEAST_DOXYGEN
  109. using writer = __implementation_defined__;
  110. #else
  111. class writer
  112. {
  113. value_type const& body_;
  114. public:
  115. using const_buffers_type =
  116. net::const_buffer;
  117. template<bool isRequest, class Fields>
  118. explicit
  119. writer(header<isRequest, Fields> const&, value_type const& b)
  120. : body_(b)
  121. {
  122. }
  123. void
  124. init(error_code& ec)
  125. {
  126. ec = {};
  127. }
  128. boost::optional<std::pair<const_buffers_type, bool>>
  129. get(error_code& ec)
  130. {
  131. ec = {};
  132. return {{
  133. { body_.data(),
  134. body_.size() * sizeof(typename
  135. value_type::value_type)},
  136. false}};
  137. }
  138. };
  139. #endif
  140. };
  141. } // http
  142. } // beast
  143. } // boost
  144. #endif