123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980 |
- //
- // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- // Official repository: https://github.com/boostorg/beast
- //
- #ifndef BOOST_BEAST_HTTP_IMPL_WRITE_HPP
- #define BOOST_BEAST_HTTP_IMPL_WRITE_HPP
- #include <boost/beast/http/type_traits.hpp>
- #include <boost/beast/core/async_base.hpp>
- #include <boost/beast/core/bind_handler.hpp>
- #include <boost/beast/core/buffers_range.hpp>
- #include <boost/beast/core/make_printable.hpp>
- #include <boost/beast/core/stream_traits.hpp>
- #include <boost/beast/core/detail/is_invocable.hpp>
- #include <boost/asio/coroutine.hpp>
- #include <boost/asio/post.hpp>
- #include <boost/asio/write.hpp>
- #include <boost/optional.hpp>
- #include <boost/throw_exception.hpp>
- #include <ostream>
- #include <sstream>
- namespace boost {
- namespace beast {
- namespace http {
- namespace detail {
- template<
- class Handler,
- class Stream,
- bool isRequest, class Body, class Fields>
- class write_some_op
- : public beast::async_base<
- Handler, beast::executor_type<Stream>>
- {
- Stream& s_;
- serializer<isRequest,Body, Fields>& sr_;
- class lambda
- {
- write_some_op& op_;
- public:
- bool invoked = false;
- explicit
- lambda(write_some_op& op)
- : op_(op)
- {
- }
- template<class ConstBufferSequence>
- void
- operator()(
- error_code& ec,
- ConstBufferSequence const& buffers)
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "http::async_write_some"));
- invoked = true;
- ec = {};
- op_.s_.async_write_some(
- buffers, std::move(op_));
- }
- };
- public:
- template<class Handler_>
- write_some_op(
- Handler_&& h,
- Stream& s,
- serializer<isRequest, Body, Fields>& sr)
- : async_base<
- Handler, beast::executor_type<Stream>>(
- std::forward<Handler_>(h), s.get_executor())
- , s_(s)
- , sr_(sr)
- {
- (*this)();
- }
- void
- operator()()
- {
- error_code ec;
- if(! sr_.is_done())
- {
- lambda f{*this};
- sr_.next(ec, f);
- if(ec)
- {
- BOOST_ASSERT(! f.invoked);
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "http::async_write_some"));
- return net::post(
- s_.get_executor(),
- beast::bind_front_handler(
- std::move(*this), ec, 0));
- }
- if(f.invoked)
- {
- // *this is now moved-from,
- return;
- }
- // What else could it be?
- BOOST_ASSERT(sr_.is_done());
- }
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "http::async_write_some"));
- return net::post(
- s_.get_executor(),
- beast::bind_front_handler(
- std::move(*this), ec, 0));
- }
- void
- operator()(
- error_code ec,
- std::size_t bytes_transferred)
- {
- if(! ec)
- sr_.consume(bytes_transferred);
- this->complete_now(ec, bytes_transferred);
- }
- };
- //------------------------------------------------------------------------------
- struct serializer_is_header_done
- {
- template<
- bool isRequest, class Body, class Fields>
- bool
- operator()(
- serializer<isRequest, Body, Fields>& sr) const
- {
- return sr.is_header_done();
- }
- };
- struct serializer_is_done
- {
- template<
- bool isRequest, class Body, class Fields>
- bool
- operator()(
- serializer<isRequest, Body, Fields>& sr) const
- {
- return sr.is_done();
- }
- };
- //------------------------------------------------------------------------------
- template<
- class Handler,
- class Stream,
- class Predicate,
- bool isRequest, class Body, class Fields>
- class write_op
- : public beast::async_base<
- Handler, beast::executor_type<Stream>>
- , public asio::coroutine
- {
- Stream& s_;
- serializer<isRequest, Body, Fields>& sr_;
- std::size_t bytes_transferred_ = 0;
- public:
- template<class Handler_>
- write_op(
- Handler_&& h,
- Stream& s,
- serializer<isRequest, Body, Fields>& sr)
- : async_base<
- Handler, beast::executor_type<Stream>>(
- std::forward<Handler_>(h), s.get_executor())
- , s_(s)
- , sr_(sr)
- {
- (*this)();
- }
- void
- operator()(
- error_code ec = {},
- std::size_t bytes_transferred = 0)
- {
- BOOST_ASIO_CORO_REENTER(*this)
- {
- if(Predicate{}(sr_))
- {
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "http::async_write"));
- net::post(
- s_.get_executor(),
- std::move(*this));
- }
- goto upcall;
- }
- for(;;)
- {
- BOOST_ASIO_CORO_YIELD
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "http::async_write"));
- beast::http::async_write_some(
- s_, sr_, std::move(*this));
- }
- bytes_transferred_ += bytes_transferred;
- if(ec)
- goto upcall;
- if(Predicate{}(sr_))
- break;
- }
- upcall:
- this->complete_now(ec, bytes_transferred_);
- }
- }
- };
- //------------------------------------------------------------------------------
- template<
- class Handler,
- class Stream,
- bool isRequest, class Body, class Fields>
- class write_msg_op
- : public beast::stable_async_base<
- Handler, beast::executor_type<Stream>>
- {
- Stream& s_;
- serializer<isRequest, Body, Fields>& sr_;
- public:
- template<
- class Handler_,
- class... Args>
- write_msg_op(
- Handler_&& h,
- Stream& s,
- Args&&... args)
- : stable_async_base<
- Handler, beast::executor_type<Stream>>(
- std::forward<Handler_>(h), s.get_executor())
- , s_(s)
- , sr_(beast::allocate_stable<
- serializer<isRequest, Body, Fields>>(
- *this, std::forward<Args>(args)...))
- {
- (*this)();
- }
- void
- operator()()
- {
- BOOST_ASIO_HANDLER_LOCATION((
- __FILE__, __LINE__,
- "http::async_write(msg)"));
- async_write(s_, sr_, std::move(*this));
- }
- void
- operator()(
- error_code ec, std::size_t bytes_transferred)
- {
- this->complete_now(ec, bytes_transferred);
- }
- };
- struct run_write_some_op
- {
- template<
- class WriteHandler,
- class Stream,
- bool isRequest, class Body, class Fields>
- void
- operator()(
- WriteHandler&& h,
- Stream* s,
- serializer<isRequest, Body, Fields>* sr)
- {
- // If you get an error on the following line it means
- // that your handler does not meet the documented type
- // requirements for the handler.
- static_assert(
- beast::detail::is_invocable<WriteHandler,
- void(error_code, std::size_t)>::value,
- "WriteHandler type requirements not met");
- write_some_op<
- typename std::decay<WriteHandler>::type,
- Stream,
- isRequest, Body, Fields>(
- std::forward<WriteHandler>(h), *s, *sr);
- }
- };
- struct run_write_op
- {
- template<
- class WriteHandler,
- class Stream,
- class Predicate,
- bool isRequest, class Body, class Fields>
- void
- operator()(
- WriteHandler&& h,
- Stream* s,
- Predicate const&,
- serializer<isRequest, Body, Fields>* sr)
- {
- // If you get an error on the following line it means
- // that your handler does not meet the documented type
- // requirements for the handler.
- static_assert(
- beast::detail::is_invocable<WriteHandler,
- void(error_code, std::size_t)>::value,
- "WriteHandler type requirements not met");
- write_op<
- typename std::decay<WriteHandler>::type,
- Stream,
- Predicate,
- isRequest, Body, Fields>(
- std::forward<WriteHandler>(h), *s, *sr);
- }
- };
- struct run_write_msg_op
- {
- template<
- class WriteHandler,
- class Stream,
- bool isRequest, class Body, class Fields,
- class... Args>
- void
- operator()(
- WriteHandler&& h,
- Stream* s,
- message<isRequest, Body, Fields>* m,
- std::false_type,
- Args&&... args)
- {
- // If you get an error on the following line it means
- // that your handler does not meet the documented type
- // requirements for the handler.
- static_assert(
- beast::detail::is_invocable<WriteHandler,
- void(error_code, std::size_t)>::value,
- "WriteHandler type requirements not met");
- write_msg_op<
- typename std::decay<WriteHandler>::type,
- Stream,
- isRequest, Body, Fields>(
- std::forward<WriteHandler>(h), *s, *m,
- std::forward<Args>(args)...);
- }
- template<
- class WriteHandler,
- class Stream,
- bool isRequest, class Body, class Fields,
- class... Args>
- void
- operator()(
- WriteHandler&& h,
- Stream* s,
- message<isRequest, Body, Fields> const* m,
- std::true_type,
- Args&&... args)
- {
- // If you get an error on the following line it means
- // that your handler does not meet the documented type
- // requirements for the handler.
- static_assert(
- beast::detail::is_invocable<WriteHandler,
- void(error_code, std::size_t)>::value,
- "WriteHandler type requirements not met");
- write_msg_op<
- typename std::decay<WriteHandler>::type,
- Stream,
- isRequest, Body, Fields>(
- std::forward<WriteHandler>(h), *s, *m,
- std::forward<Args>(args)...);
- }
- };
- //------------------------------------------------------------------------------
- template<class Stream>
- class write_some_lambda
- {
- Stream& stream_;
- public:
- bool invoked = false;
- std::size_t bytes_transferred = 0;
- explicit
- write_some_lambda(Stream& stream)
- : stream_(stream)
- {
- }
- template<class ConstBufferSequence>
- void
- operator()(error_code& ec,
- ConstBufferSequence const& buffers)
- {
- invoked = true;
- bytes_transferred =
- stream_.write_some(buffers, ec);
- }
- };
- template<class Stream>
- class write_lambda
- {
- Stream& stream_;
- public:
- bool invoked = false;
- std::size_t bytes_transferred = 0;
- explicit
- write_lambda(Stream& stream)
- : stream_(stream)
- {
- }
- template<class ConstBufferSequence>
- void
- operator()(error_code& ec,
- ConstBufferSequence const& buffers)
- {
- invoked = true;
- bytes_transferred = net::write(
- stream_, buffers, ec);
- }
- };
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- std::size_t
- write_some_impl(
- SyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr,
- error_code& ec)
- {
- if(! sr.is_done())
- {
- write_some_lambda<SyncWriteStream> f{stream};
- sr.next(ec, f);
- if(ec)
- return f.bytes_transferred;
- if(f.invoked)
- sr.consume(f.bytes_transferred);
- return f.bytes_transferred;
- }
- ec = {};
- return 0;
- }
- template<
- class AsyncWriteStream,
- bool isRequest, class Body, class Fields,
- BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
- BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
- async_write_some_impl(
- AsyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr,
- WriteHandler&& handler)
- {
- return net::async_initiate<
- WriteHandler,
- void(error_code, std::size_t)>(
- run_write_some_op{},
- handler,
- &stream,
- &sr);
- }
- } // detail
- //------------------------------------------------------------------------------
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- std::size_t
- write_some(
- SyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr)
- {
- static_assert(is_sync_write_stream<SyncWriteStream>::value,
- "SyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- error_code ec;
- auto const bytes_transferred =
- write_some(stream, sr, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- return bytes_transferred;
- }
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- std::size_t
- write_some(
- SyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr,
- error_code& ec)
- {
- static_assert(is_sync_write_stream<SyncWriteStream>::value,
- "SyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- return detail::write_some_impl(stream, sr, ec);
- }
- template<
- class AsyncWriteStream,
- bool isRequest, class Body, class Fields,
- BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
- BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
- async_write_some(
- AsyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr,
- WriteHandler&& handler)
- {
- static_assert(is_async_write_stream<
- AsyncWriteStream>::value,
- "AsyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- return detail::async_write_some_impl(stream, sr,
- std::forward<WriteHandler>(handler));
- }
- //------------------------------------------------------------------------------
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- std::size_t
- write_header(SyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr)
- {
- static_assert(is_sync_write_stream<SyncWriteStream>::value,
- "SyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- error_code ec;
- auto const bytes_transferred =
- write_header(stream, sr, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- return bytes_transferred;
- }
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- std::size_t
- write_header(
- SyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr,
- error_code& ec)
- {
- static_assert(is_sync_write_stream<SyncWriteStream>::value,
- "SyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- sr.split(true);
- std::size_t bytes_transferred = 0;
- if(! sr.is_header_done())
- {
- detail::write_lambda<SyncWriteStream> f{stream};
- do
- {
- sr.next(ec, f);
- bytes_transferred += f.bytes_transferred;
- if(ec)
- return bytes_transferred;
- BOOST_ASSERT(f.invoked);
- sr.consume(f.bytes_transferred);
- }
- while(! sr.is_header_done());
- }
- else
- {
- ec = {};
- }
- return bytes_transferred;
- }
- template<
- class AsyncWriteStream,
- bool isRequest, class Body, class Fields,
- BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
- BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
- async_write_header(
- AsyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr,
- WriteHandler&& handler)
- {
- static_assert(is_async_write_stream<
- AsyncWriteStream>::value,
- "AsyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- sr.split(true);
- return net::async_initiate<
- WriteHandler,
- void(error_code, std::size_t)>(
- detail::run_write_op{},
- handler,
- &stream,
- detail::serializer_is_header_done{},
- &sr);
- }
- //------------------------------------------------------------------------------
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- std::size_t
- write(
- SyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr)
- {
- static_assert(is_sync_write_stream<SyncWriteStream>::value,
- "SyncWriteStream type requirements not met");
- error_code ec;
- auto const bytes_transferred =
- write(stream, sr, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- return bytes_transferred;
- }
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- std::size_t
- write(
- SyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr,
- error_code& ec)
- {
- static_assert(is_sync_write_stream<SyncWriteStream>::value,
- "SyncWriteStream type requirements not met");
- std::size_t bytes_transferred = 0;
- sr.split(false);
- for(;;)
- {
- bytes_transferred +=
- write_some(stream, sr, ec);
- if(ec)
- return bytes_transferred;
- if(sr.is_done())
- break;
- }
- return bytes_transferred;
- }
- template<
- class AsyncWriteStream,
- bool isRequest, class Body, class Fields,
- BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
- BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
- async_write(
- AsyncWriteStream& stream,
- serializer<isRequest, Body, Fields>& sr,
- WriteHandler&& handler)
- {
- static_assert(is_async_write_stream<
- AsyncWriteStream>::value,
- "AsyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- sr.split(false);
- return net::async_initiate<
- WriteHandler,
- void(error_code, std::size_t)>(
- detail::run_write_op{},
- handler,
- &stream,
- detail::serializer_is_done{},
- &sr);
- }
- //------------------------------------------------------------------------------
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- typename std::enable_if<
- is_mutable_body_writer<Body>::value,
- std::size_t>::type
- write(
- SyncWriteStream& stream,
- message<isRequest, Body, Fields>& msg)
- {
- static_assert(is_sync_write_stream<SyncWriteStream>::value,
- "SyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- error_code ec;
- auto const bytes_transferred =
- write(stream, msg, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- return bytes_transferred;
- }
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- typename std::enable_if<
- ! is_mutable_body_writer<Body>::value,
- std::size_t>::type
- write(
- SyncWriteStream& stream,
- message<isRequest, Body, Fields> const& msg)
- {
- static_assert(is_sync_write_stream<SyncWriteStream>::value,
- "SyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- error_code ec;
- auto const bytes_transferred =
- write(stream, msg, ec);
- if(ec)
- BOOST_THROW_EXCEPTION(system_error{ec});
- return bytes_transferred;
- }
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- typename std::enable_if<
- is_mutable_body_writer<Body>::value,
- std::size_t>::type
- write(
- SyncWriteStream& stream,
- message<isRequest, Body, Fields>& msg,
- error_code& ec)
- {
- static_assert(is_sync_write_stream<SyncWriteStream>::value,
- "SyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- serializer<isRequest, Body, Fields> sr{msg};
- return write(stream, sr, ec);
- }
- template<
- class SyncWriteStream,
- bool isRequest, class Body, class Fields>
- typename std::enable_if<
- ! is_mutable_body_writer<Body>::value,
- std::size_t>::type
- write(
- SyncWriteStream& stream,
- message<isRequest, Body, Fields> const& msg,
- error_code& ec)
- {
- static_assert(is_sync_write_stream<SyncWriteStream>::value,
- "SyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- serializer<isRequest, Body, Fields> sr{msg};
- return write(stream, sr, ec);
- }
- template<
- class AsyncWriteStream,
- bool isRequest, class Body, class Fields,
- BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
- BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
- async_write(
- AsyncWriteStream& stream,
- message<isRequest, Body, Fields>& msg,
- WriteHandler&& handler,
- typename std::enable_if<
- is_mutable_body_writer<Body>::value>::type*)
- {
- static_assert(
- is_async_write_stream<AsyncWriteStream>::value,
- "AsyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- return net::async_initiate<
- WriteHandler,
- void(error_code, std::size_t)>(
- detail::run_write_msg_op{},
- handler,
- &stream,
- &msg,
- std::false_type{});
- }
- template<
- class AsyncWriteStream,
- bool isRequest, class Body, class Fields,
- BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
- BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
- async_write(
- AsyncWriteStream& stream,
- message<isRequest, Body, Fields> const& msg,
- WriteHandler&& handler,
- typename std::enable_if<
- ! is_mutable_body_writer<Body>::value>::type*)
- {
- static_assert(
- is_async_write_stream<AsyncWriteStream>::value,
- "AsyncWriteStream type requirements not met");
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- return net::async_initiate<
- WriteHandler,
- void(error_code, std::size_t)>(
- detail::run_write_msg_op{},
- handler,
- &stream,
- &msg,
- std::true_type{});
- }
- //------------------------------------------------------------------------------
- namespace detail {
- template<class Serializer>
- class write_ostream_lambda
- {
- std::ostream& os_;
- Serializer& sr_;
- public:
- write_ostream_lambda(std::ostream& os,
- Serializer& sr)
- : os_(os)
- , sr_(sr)
- {
- }
- template<class ConstBufferSequence>
- void
- operator()(error_code& ec,
- ConstBufferSequence const& buffers) const
- {
- ec = {};
- if(os_.fail())
- return;
- std::size_t bytes_transferred = 0;
- for(auto b : beast::buffers_range_ref(buffers))
- {
- os_.write(static_cast<char const*>(
- b.data()), b.size());
- if(os_.fail())
- return;
- bytes_transferred += b.size();
- }
- sr_.consume(bytes_transferred);
- }
- };
- } // detail
- template<class Fields>
- std::ostream&
- operator<<(std::ostream& os,
- header<true, Fields> const& h)
- {
- typename Fields::writer fr{
- h, h.version(), h.method()};
- return os << beast::make_printable(fr.get());
- }
- template<class Fields>
- std::ostream&
- operator<<(std::ostream& os,
- header<false, Fields> const& h)
- {
- typename Fields::writer fr{
- h, h.version(), h.result_int()};
- return os << beast::make_printable(fr.get());
- }
- template<bool isRequest, class Body, class Fields>
- std::ostream&
- operator<<(std::ostream& os,
- message<isRequest, Body, Fields> const& msg)
- {
- static_assert(is_body<Body>::value,
- "Body type requirements not met");
- static_assert(is_body_writer<Body>::value,
- "BodyWriter type requirements not met");
- serializer<isRequest, Body, Fields> sr{msg};
- error_code ec;
- detail::write_ostream_lambda<decltype(sr)> f{os, sr};
- do
- {
- sr.next(ec, f);
- if(os.fail())
- break;
- if(ec)
- {
- os.setstate(std::ios::failbit);
- break;
- }
- }
- while(! sr.is_done());
- return os;
- }
- } // http
- } // beast
- } // boost
- #endif
|