empty_body.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_EMPTY_BODY_HPP
  10. #define BOOST_BEAST_HTTP_EMPTY_BODY_HPP
  11. #include <boost/beast/http/empty_body_fwd.hpp>
  12. #include <boost/beast/core/detail/config.hpp>
  13. #include <boost/beast/http/error.hpp>
  14. #include <boost/beast/http/message.hpp>
  15. #include <boost/optional.hpp>
  16. #include <cstdint>
  17. namespace boost {
  18. namespace beast {
  19. namespace http {
  20. /** An empty <em>Body</em>
  21. This body is used to represent messages which do not have a
  22. message body. If this body is used with a parser, and the
  23. parser encounters octets corresponding to a message body,
  24. the parser will fail with the error @ref http::unexpected_body.
  25. The Content-Length of this body is always 0.
  26. */
  27. struct empty_body
  28. {
  29. /** The type of container used for the body
  30. This determines the type of @ref message::body
  31. when this body type is used with a message container.
  32. */
  33. struct value_type
  34. {
  35. };
  36. /** Returns the payload size of the body
  37. When this body is used with @ref message::prepare_payload,
  38. the Content-Length will be set to the payload size, and
  39. any chunked Transfer-Encoding will be removed.
  40. */
  41. static
  42. std::uint64_t
  43. size(value_type)
  44. {
  45. return 0;
  46. }
  47. /** The algorithm for parsing the body
  48. Meets the requirements of <em>BodyReader</em>.
  49. */
  50. #if BOOST_BEAST_DOXYGEN
  51. using reader = __implementation_defined__;
  52. #else
  53. struct reader
  54. {
  55. template<bool isRequest, class Fields>
  56. explicit
  57. reader(header<isRequest, Fields>&, value_type&)
  58. {
  59. }
  60. void
  61. init(boost::optional<std::uint64_t> const&, error_code& ec)
  62. {
  63. ec = {};
  64. }
  65. template<class ConstBufferSequence>
  66. std::size_t
  67. put(ConstBufferSequence const&,
  68. error_code& ec)
  69. {
  70. BOOST_BEAST_ASSIGN_EC(ec, error::unexpected_body);
  71. return 0;
  72. }
  73. void
  74. finish(error_code& ec)
  75. {
  76. ec = {};
  77. }
  78. };
  79. #endif
  80. /** The algorithm for serializing the body
  81. Meets the requirements of <em>BodyWriter</em>.
  82. */
  83. #if BOOST_BEAST_DOXYGEN
  84. using writer = __implementation_defined__;
  85. #else
  86. struct writer
  87. {
  88. using const_buffers_type =
  89. net::const_buffer;
  90. template<bool isRequest, class Fields>
  91. explicit
  92. writer(header<isRequest, Fields> const&, value_type const&)
  93. {
  94. }
  95. void
  96. init(error_code& ec)
  97. {
  98. ec = {};
  99. }
  100. boost::optional<std::pair<const_buffers_type, bool>>
  101. get(error_code& ec)
  102. {
  103. ec = {};
  104. return boost::none;
  105. }
  106. };
  107. #endif
  108. };
  109. } // http
  110. } // beast
  111. } // boost
  112. #endif