empty_body.hpp 2.9 KB

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