empty_body.hpp 3.0 KB

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