123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #ifndef BOOST_BEAST_HTTP_BASIC_DYNAMIC_BODY_HPP
- #define BOOST_BEAST_HTTP_BASIC_DYNAMIC_BODY_HPP
- #include <boost/beast/core/detail/config.hpp>
- #include <boost/beast/core/buffer_traits.hpp>
- #include <boost/beast/core/detail/buffer.hpp>
- #include <boost/beast/core/detail/clamp.hpp>
- #include <boost/beast/http/error.hpp>
- #include <boost/beast/http/message.hpp>
- #include <boost/optional.hpp>
- #include <algorithm>
- #include <cstdint>
- #include <utility>
- namespace boost {
- namespace beast {
- namespace http {
- template<class DynamicBuffer>
- struct basic_dynamic_body
- {
- static_assert(
- net::is_dynamic_buffer<DynamicBuffer>::value,
- "DynamicBuffer type requirements not met");
-
- using value_type = DynamicBuffer;
-
- static
- std::uint64_t
- size(value_type const& v)
- {
- return v.size();
- }
-
- #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)
- {
- auto const n = buffer_bytes(buffers);
- if(beast::detail::sum_exceeds(body_.size(), n, body_.max_size()))
- {
- BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
- return 0;
- }
- auto const mb =
- beast::detail::dynamic_buffer_prepare(
- body_, (std::min)(n,
- body_.max_size() - body_.size()),
- ec, error::buffer_overflow);
- if(ec)
- return 0;
- auto const bytes_transferred =
- net::buffer_copy(*mb, buffers);
- body_.commit(bytes_transferred);
- return bytes_transferred;
- }
- void
- finish(error_code& ec)
- {
- ec = {};
- }
- };
- #endif
-
- #if BOOST_BEAST_DOXYGEN
- using writer = __implementation_defined__;
- #else
- class writer
- {
- DynamicBuffer const& body_;
- public:
- using const_buffers_type =
- typename DynamicBuffer::const_buffers_type;
- 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)
- {
- ec = {};
- return {{body_.data(), false}};
- }
- };
- #endif
- };
- }
- }
- }
- #endif
|