123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- #ifndef BOOST_BEAST_HTTP_BUFFER_BODY_HPP
- #define BOOST_BEAST_HTTP_BUFFER_BODY_HPP
- #include <boost/beast/core/detail/config.hpp>
- #include <boost/beast/core/buffer_traits.hpp>
- #include <boost/beast/http/error.hpp>
- #include <boost/beast/http/message.hpp>
- #include <boost/beast/http/type_traits.hpp>
- #include <boost/optional.hpp>
- #include <type_traits>
- #include <utility>
- namespace boost {
- namespace beast {
- namespace http {
- struct buffer_body
- {
-
- struct value_type
- {
-
- void* data = nullptr;
-
- std::size_t size = 0;
-
- bool more = true;
- };
-
- #if BOOST_BEAST_DOXYGEN
- using reader = __implementation_defined__;
- #else
- class reader
- {
- value_type& body_;
- public:
- template<bool isRequest, class Fields>
- explicit
- reader(header<isRequest, Fields>&, value_type& b)
- : body_(b)
- {
- }
- void
- init(boost::optional<std::uint64_t> const&, error_code& ec)
- {
- ec = {};
- }
- template<class ConstBufferSequence>
- std::size_t
- put(ConstBufferSequence const& buffers,
- error_code& ec)
- {
- if(! body_.data)
- {
- ec = error::need_buffer;
- return 0;
- }
- auto const bytes_transferred =
- net::buffer_copy(net::buffer(
- body_.data, body_.size), buffers);
- body_.data = static_cast<char*>(
- body_.data) + bytes_transferred;
- body_.size -= bytes_transferred;
- if(bytes_transferred == buffer_bytes(buffers))
- ec = {};
- else
- ec = error::need_buffer;
- return bytes_transferred;
- }
- void
- finish(error_code& ec)
- {
- ec = {};
- }
- };
- #endif
-
- #if BOOST_BEAST_DOXYGEN
- using writer = __implementation_defined__;
- #else
- class writer
- {
- bool toggle_ = false;
- value_type const& body_;
- public:
- using const_buffers_type =
- net::const_buffer;
- template<bool isRequest, class Fields>
- explicit
- writer(header<isRequest, Fields> const&, value_type const& b)
- : body_(b)
- {
- }
- void
- init(error_code& ec)
- {
- ec = {};
- }
- boost::optional<
- std::pair<const_buffers_type, bool>>
- get(error_code& ec)
- {
- if(toggle_)
- {
- if(body_.more)
- {
- toggle_ = false;
- ec = error::need_buffer;
- }
- else
- {
- ec = {};
- }
- return boost::none;
- }
- if(body_.data)
- {
- ec = {};
- toggle_ = true;
- return {{const_buffers_type{
- body_.data, body_.size}, body_.more}};
- }
- if(body_.more)
- ec = error::need_buffer;
- else
- ec = {};
- return boost::none;
- }
- };
- #endif
- };
- #if ! BOOST_BEAST_DOXYGEN
- template<bool isRequest, class Fields>
- std::ostream&
- operator<<(std::ostream& os, message<isRequest,
- buffer_body, Fields> const& msg) = delete;
- #endif
- }
- }
- }
- #endif
|