vector_body.hpp 4.1 KB

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