2017-07-20 08:01:46 -07:00
|
|
|
//
|
2017-07-24 09:42:36 -07:00
|
|
|
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2017-07-20 08:01:46 -07:00
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
//
|
2017-07-20 13:40:34 -07:00
|
|
|
// Official repository: https://github.com/boostorg/beast
|
|
|
|
//
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#ifndef BOOST_BEAST_HTTP_IMPL_WRITE_IPP
|
|
|
|
#define BOOST_BEAST_HTTP_IMPL_WRITE_IPP
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/http/type_traits.hpp>
|
|
|
|
#include <boost/beast/core/bind_handler.hpp>
|
|
|
|
#include <boost/beast/core/ostream.hpp>
|
|
|
|
#include <boost/beast/core/handler_ptr.hpp>
|
|
|
|
#include <boost/beast/core/type_traits.hpp>
|
|
|
|
#include <boost/beast/core/detail/config.hpp>
|
2017-05-14 09:25:43 -07:00
|
|
|
#include <boost/asio/handler_alloc_hook.hpp>
|
|
|
|
#include <boost/asio/handler_continuation_hook.hpp>
|
|
|
|
#include <boost/asio/handler_invoke_hook.hpp>
|
2017-05-31 08:01:55 -07:00
|
|
|
#include <boost/asio/write.hpp>
|
2017-05-28 17:04:39 -07:00
|
|
|
#include <boost/optional.hpp>
|
2017-05-22 15:30:12 -07:00
|
|
|
#include <boost/throw_exception.hpp>
|
2016-04-29 06:04:40 -04:00
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2017-07-20 08:01:46 -07:00
|
|
|
namespace beast {
|
|
|
|
namespace http {
|
|
|
|
namespace detail {
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class Stream, class Handler,
|
|
|
|
bool isRequest, class Body, class Fields>
|
2017-05-28 17:04:39 -07:00
|
|
|
class write_some_op
|
2016-05-01 11:14:10 -04:00
|
|
|
{
|
2017-05-28 17:04:39 -07:00
|
|
|
Stream& s_;
|
2017-07-09 20:09:30 -07:00
|
|
|
serializer<isRequest,Body, Fields>& sr_;
|
2017-05-28 17:04:39 -07:00
|
|
|
Handler h_;
|
2016-05-01 11:14:10 -04:00
|
|
|
|
2017-05-28 17:04:39 -07:00
|
|
|
class lambda
|
2016-05-01 11:14:10 -04:00
|
|
|
{
|
2017-05-28 17:04:39 -07:00
|
|
|
write_some_op& op_;
|
2016-05-01 11:14:10 -04:00
|
|
|
|
2017-05-28 17:04:39 -07:00
|
|
|
public:
|
2017-05-31 08:01:55 -07:00
|
|
|
bool invoked = false;
|
2016-11-07 13:51:10 -05:00
|
|
|
|
2017-05-28 17:04:39 -07:00
|
|
|
explicit
|
|
|
|
lambda(write_some_op& op)
|
|
|
|
: op_(op)
|
|
|
|
{
|
|
|
|
}
|
2016-11-07 13:51:10 -05:00
|
|
|
|
2017-05-28 17:04:39 -07:00
|
|
|
template<class ConstBufferSequence>
|
|
|
|
void
|
|
|
|
operator()(error_code& ec,
|
2017-07-06 20:58:07 -07:00
|
|
|
ConstBufferSequence const& buffers)
|
2017-05-28 17:04:39 -07:00
|
|
|
{
|
2017-05-31 08:01:55 -07:00
|
|
|
invoked = true;
|
2017-07-07 07:07:50 -07:00
|
|
|
ec.assign(0, ec.category());
|
2017-05-28 17:04:39 -07:00
|
|
|
return op_.s_.async_write_some(
|
2017-07-06 20:58:07 -07:00
|
|
|
buffers, std::move(op_));
|
2017-05-28 17:04:39 -07:00
|
|
|
}
|
|
|
|
};
|
2016-11-07 13:51:10 -05:00
|
|
|
|
|
|
|
public:
|
2017-05-28 17:04:39 -07:00
|
|
|
write_some_op(write_some_op&&) = default;
|
|
|
|
write_some_op(write_some_op const&) = default;
|
2017-05-08 12:41:45 -07:00
|
|
|
|
2017-05-28 17:04:39 -07:00
|
|
|
template<class DeducedHandler>
|
2017-07-09 20:09:30 -07:00
|
|
|
write_some_op(DeducedHandler&& h, Stream& s,
|
|
|
|
serializer<isRequest, Body, Fields>& sr)
|
2017-05-28 17:04:39 -07:00
|
|
|
: s_(s)
|
|
|
|
, sr_(sr)
|
|
|
|
, h_(std::forward<DeducedHandler>(h))
|
2017-05-08 12:41:45 -07:00
|
|
|
{
|
|
|
|
}
|
2016-11-07 13:51:10 -05:00
|
|
|
|
2017-05-28 17:04:39 -07:00
|
|
|
void
|
|
|
|
operator()();
|
2016-11-07 13:51:10 -05:00
|
|
|
|
|
|
|
void
|
2017-09-03 18:06:09 -07:00
|
|
|
operator()(
|
|
|
|
error_code ec,
|
2017-05-28 17:04:39 -07:00
|
|
|
std::size_t bytes_transferred);
|
2016-11-07 13:51:10 -05:00
|
|
|
|
|
|
|
friend
|
|
|
|
void* asio_handler_allocate(
|
2017-05-28 17:04:39 -07:00
|
|
|
std::size_t size, write_some_op* op)
|
2016-11-07 13:51:10 -05:00
|
|
|
{
|
2017-05-14 09:25:43 -07:00
|
|
|
using boost::asio::asio_handler_allocate;
|
|
|
|
return asio_handler_allocate(
|
2017-05-08 12:41:45 -07:00
|
|
|
size, std::addressof(op->h_));
|
2016-11-07 13:51:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
void asio_handler_deallocate(
|
2017-05-28 17:04:39 -07:00
|
|
|
void* p, std::size_t size, write_some_op* op)
|
2016-11-07 13:51:10 -05:00
|
|
|
{
|
2017-05-14 09:25:43 -07:00
|
|
|
using boost::asio::asio_handler_deallocate;
|
|
|
|
asio_handler_deallocate(
|
2017-05-08 12:41:45 -07:00
|
|
|
p, size, std::addressof(op->h_));
|
2016-11-07 13:51:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
2017-05-28 17:04:39 -07:00
|
|
|
bool asio_handler_is_continuation(write_some_op* op)
|
2016-11-07 13:51:10 -05:00
|
|
|
{
|
2017-05-28 17:04:39 -07:00
|
|
|
using boost::asio::asio_handler_is_continuation;
|
|
|
|
return asio_handler_is_continuation(
|
|
|
|
std::addressof(op->h_));
|
2016-11-07 13:51:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class Function>
|
|
|
|
friend
|
2017-05-28 17:04:39 -07:00
|
|
|
void asio_handler_invoke(Function&& f, write_some_op* op)
|
2016-11-07 13:51:10 -05:00
|
|
|
{
|
2017-05-14 09:25:43 -07:00
|
|
|
using boost::asio::asio_handler_invoke;
|
2017-05-28 17:04:39 -07:00
|
|
|
asio_handler_invoke(f, std::addressof(op->h_));
|
2016-11-07 13:51:10 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class Stream, class Handler,
|
|
|
|
bool isRequest, class Body, class Fields>
|
2016-11-07 13:51:10 -05:00
|
|
|
void
|
2017-07-09 20:09:30 -07:00
|
|
|
write_some_op<
|
|
|
|
Stream, Handler, isRequest, Body, Fields>::
|
2017-05-28 17:04:39 -07:00
|
|
|
operator()()
|
2016-11-07 13:51:10 -05:00
|
|
|
{
|
|
|
|
error_code ec;
|
2017-07-07 07:07:50 -07:00
|
|
|
if(! sr_.is_done())
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
2017-07-07 07:07:50 -07:00
|
|
|
lambda f{*this};
|
|
|
|
sr_.next(ec, f);
|
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
BOOST_ASSERT(! f.invoked);
|
|
|
|
return s_.get_io_service().post(
|
|
|
|
bind_handler(std::move(*this), ec, 0));
|
|
|
|
}
|
|
|
|
if(f.invoked)
|
|
|
|
{
|
|
|
|
// *this has been moved from,
|
|
|
|
// cannot access members here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// What else could it be?
|
|
|
|
BOOST_ASSERT(sr_.is_done());
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
2017-05-28 17:04:39 -07:00
|
|
|
return s_.get_io_service().post(
|
|
|
|
bind_handler(std::move(*this), ec, 0));
|
2016-11-07 13:51:10 -05:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class Stream, class Handler,
|
|
|
|
bool isRequest, class Body, class Fields>
|
2016-11-07 13:51:10 -05:00
|
|
|
void
|
2017-07-09 20:09:30 -07:00
|
|
|
write_some_op<
|
|
|
|
Stream, Handler, isRequest, Body, Fields>::
|
2017-07-07 07:07:50 -07:00
|
|
|
operator()(
|
|
|
|
error_code ec, std::size_t bytes_transferred)
|
2016-11-07 13:51:10 -05:00
|
|
|
{
|
2017-05-28 17:04:39 -07:00
|
|
|
if(! ec)
|
2017-07-07 07:07:50 -07:00
|
|
|
{
|
2017-05-28 17:04:39 -07:00
|
|
|
sr_.consume(bytes_transferred);
|
2017-07-07 07:07:50 -07:00
|
|
|
if(sr_.is_done())
|
2017-07-07 23:05:11 -07:00
|
|
|
if(! sr_.keep_alive())
|
2017-07-07 07:07:50 -07:00
|
|
|
ec = error::end_of_stream;
|
|
|
|
}
|
2017-09-03 18:06:09 -07:00
|
|
|
h_(ec, bytes_transferred);
|
2017-05-08 12:41:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-05-31 08:01:55 -07:00
|
|
|
struct serializer_is_header_done
|
|
|
|
{
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
bool isRequest, class Body, class Fields>
|
2017-05-31 08:01:55 -07:00
|
|
|
bool
|
2017-07-09 20:09:30 -07:00
|
|
|
operator()(
|
|
|
|
serializer<isRequest, Body, Fields>& sr) const
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
|
|
|
return sr.is_header_done();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct serializer_is_done
|
|
|
|
{
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
bool isRequest, class Body, class Fields>
|
2017-05-31 08:01:55 -07:00
|
|
|
bool
|
2017-07-09 20:09:30 -07:00
|
|
|
operator()(
|
|
|
|
serializer<isRequest, Body, Fields>& sr) const
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
|
|
|
return sr.is_done();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-07 07:07:50 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template<
|
|
|
|
class Stream, class Handler, class Predicate,
|
2017-07-09 20:09:30 -07:00
|
|
|
bool isRequest, class Body, class Fields>
|
2017-05-31 08:01:55 -07:00
|
|
|
class write_op
|
|
|
|
{
|
|
|
|
int state_ = 0;
|
|
|
|
Stream& s_;
|
2017-07-09 20:09:30 -07:00
|
|
|
serializer<isRequest, Body, Fields>& sr_;
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t bytes_transferred_ = 0;
|
2017-05-31 08:01:55 -07:00
|
|
|
Handler h_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
write_op(write_op&&) = default;
|
|
|
|
write_op(write_op const&) = default;
|
|
|
|
|
|
|
|
template<class DeducedHandler>
|
|
|
|
write_op(DeducedHandler&& h, Stream& s,
|
2017-07-09 20:09:30 -07:00
|
|
|
serializer<isRequest, Body, Fields>& sr)
|
2017-05-31 08:01:55 -07:00
|
|
|
: s_(s)
|
|
|
|
, sr_(sr)
|
|
|
|
, h_(std::forward<DeducedHandler>(h))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-03 18:06:09 -07:00
|
|
|
operator()(
|
|
|
|
error_code ec = {},
|
|
|
|
std::size_t bytes_transferred = 0);
|
2017-05-31 08:01:55 -07:00
|
|
|
|
|
|
|
friend
|
|
|
|
void* asio_handler_allocate(
|
|
|
|
std::size_t size, write_op* op)
|
|
|
|
{
|
|
|
|
using boost::asio::asio_handler_allocate;
|
|
|
|
return asio_handler_allocate(
|
|
|
|
size, std::addressof(op->h_));
|
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
void asio_handler_deallocate(
|
|
|
|
void* p, std::size_t size, write_op* op)
|
|
|
|
{
|
|
|
|
using boost::asio::asio_handler_deallocate;
|
|
|
|
asio_handler_deallocate(
|
|
|
|
p, size, std::addressof(op->h_));
|
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
bool asio_handler_is_continuation(write_op* op)
|
|
|
|
{
|
|
|
|
using boost::asio::asio_handler_is_continuation;
|
|
|
|
return op->state_ >= 3 ||
|
|
|
|
asio_handler_is_continuation(
|
|
|
|
std::addressof(op->h_));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Function>
|
|
|
|
friend
|
|
|
|
void asio_handler_invoke(Function&& f, write_op* op)
|
|
|
|
{
|
|
|
|
using boost::asio::asio_handler_invoke;
|
|
|
|
asio_handler_invoke(
|
|
|
|
f, std::addressof(op->h_));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-07 07:07:50 -07:00
|
|
|
template<
|
|
|
|
class Stream, class Handler, class Predicate,
|
2017-07-09 20:09:30 -07:00
|
|
|
bool isRequest, class Body, class Fields>
|
2017-05-31 08:01:55 -07:00
|
|
|
void
|
2017-07-07 07:07:50 -07:00
|
|
|
write_op<Stream, Handler, Predicate,
|
2017-07-09 20:09:30 -07:00
|
|
|
isRequest, Body, Fields>::
|
2017-09-03 18:06:09 -07:00
|
|
|
operator()(
|
|
|
|
error_code ec, std::size_t bytes_transferred)
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
|
|
|
if(ec)
|
|
|
|
goto upcall;
|
|
|
|
switch(state_)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
if(Predicate{}(sr_))
|
|
|
|
{
|
|
|
|
state_ = 1;
|
|
|
|
return s_.get_io_service().post(
|
2017-09-03 18:06:09 -07:00
|
|
|
bind_handler(std::move(*this), ec, 0));
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
|
|
|
state_ = 2;
|
2017-07-07 09:03:06 -07:00
|
|
|
return beast::http::async_write_some(
|
|
|
|
s_, sr_, std::move(*this));
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
goto upcall;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
state_ = 3;
|
2017-07-20 13:40:34 -07:00
|
|
|
BOOST_BEAST_FALLTHROUGH;
|
2017-05-31 08:01:55 -07:00
|
|
|
|
|
|
|
case 3:
|
|
|
|
{
|
2017-09-03 18:06:09 -07:00
|
|
|
bytes_transferred_ += bytes_transferred;
|
2017-05-31 08:01:55 -07:00
|
|
|
if(Predicate{}(sr_))
|
|
|
|
goto upcall;
|
2017-07-07 09:03:06 -07:00
|
|
|
return beast::http::async_write_some(
|
|
|
|
s_, sr_, std::move(*this));
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
upcall:
|
2017-09-03 18:06:09 -07:00
|
|
|
h_(ec, bytes_transferred_);
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2017-05-08 12:41:45 -07:00
|
|
|
template<class Stream, class Handler,
|
|
|
|
bool isRequest, class Body, class Fields>
|
2017-05-31 08:01:55 -07:00
|
|
|
class write_msg_op
|
2017-05-08 12:41:45 -07:00
|
|
|
{
|
|
|
|
struct data
|
|
|
|
{
|
|
|
|
Stream& s;
|
2017-07-09 20:09:30 -07:00
|
|
|
serializer<isRequest, Body, Fields> sr;
|
2017-05-08 12:41:45 -07:00
|
|
|
|
2017-06-22 10:27:45 -07:00
|
|
|
data(Handler&, Stream& s_, message<
|
2017-06-21 11:50:50 -07:00
|
|
|
isRequest, Body, Fields>& m_)
|
2017-05-08 12:41:45 -07:00
|
|
|
: s(s_)
|
2017-07-09 20:09:30 -07:00
|
|
|
, sr(m_)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-02 13:29:48 -05:00
|
|
|
handler_ptr<data, Handler> d_;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
public:
|
2017-05-31 08:01:55 -07:00
|
|
|
write_msg_op(write_msg_op&&) = default;
|
|
|
|
write_msg_op(write_msg_op const&) = default;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
template<class DeducedHandler, class... Args>
|
2017-05-31 08:01:55 -07:00
|
|
|
write_msg_op(DeducedHandler&& h, Stream& s, Args&&... args)
|
2017-01-29 19:46:17 -05:00
|
|
|
: d_(std::forward<DeducedHandler>(h),
|
|
|
|
s, std::forward<Args>(args)...)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-05-31 08:01:55 -07:00
|
|
|
void
|
|
|
|
operator()();
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
void
|
2017-09-03 18:06:09 -07:00
|
|
|
operator()(
|
|
|
|
error_code ec, std::size_t bytes_transferred);
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
friend
|
|
|
|
void* asio_handler_allocate(
|
2017-05-31 08:01:55 -07:00
|
|
|
std::size_t size, write_msg_op* op)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-05-14 09:25:43 -07:00
|
|
|
using boost::asio::asio_handler_allocate;
|
|
|
|
return asio_handler_allocate(
|
|
|
|
size, std::addressof(op->d_.handler()));
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
void asio_handler_deallocate(
|
2017-05-31 08:01:55 -07:00
|
|
|
void* p, std::size_t size, write_msg_op* op)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-05-14 09:25:43 -07:00
|
|
|
using boost::asio::asio_handler_deallocate;
|
|
|
|
asio_handler_deallocate(
|
|
|
|
p, size, std::addressof(op->d_.handler()));
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
2017-05-31 08:01:55 -07:00
|
|
|
bool asio_handler_is_continuation(write_msg_op* op)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-05-08 12:41:45 -07:00
|
|
|
using boost::asio::asio_handler_is_continuation;
|
2017-05-31 08:01:55 -07:00
|
|
|
return asio_handler_is_continuation(
|
|
|
|
std::addressof(op->d_.handler()));
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
2016-08-26 08:01:44 -04:00
|
|
|
template<class Function>
|
2017-07-20 08:01:46 -07:00
|
|
|
friend
|
2017-05-31 08:01:55 -07:00
|
|
|
void asio_handler_invoke(Function&& f, write_msg_op* op)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-05-14 09:25:43 -07:00
|
|
|
using boost::asio::asio_handler_invoke;
|
|
|
|
asio_handler_invoke(
|
|
|
|
f, std::addressof(op->d_.handler()));
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Stream, class Handler,
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Body, class Fields>
|
2017-07-20 08:01:46 -07:00
|
|
|
void
|
2017-07-07 07:07:50 -07:00
|
|
|
write_msg_op<
|
|
|
|
Stream, Handler, isRequest, Body, Fields>::
|
2017-05-31 08:01:55 -07:00
|
|
|
operator()()
|
|
|
|
{
|
|
|
|
auto& d = *d_;
|
|
|
|
return async_write(d.s, d.sr, std::move(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Stream, class Handler,
|
|
|
|
bool isRequest, class Body, class Fields>
|
|
|
|
void
|
2017-07-07 07:07:50 -07:00
|
|
|
write_msg_op<
|
|
|
|
Stream, Handler, isRequest, Body, Fields>::
|
2017-09-03 18:06:09 -07:00
|
|
|
operator()(error_code ec, std::size_t bytes_transferred)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-09-03 18:06:09 -07:00
|
|
|
d_.invoke(ec, bytes_transferred);
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
2017-05-28 17:04:39 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template<class Stream>
|
|
|
|
class write_some_lambda
|
|
|
|
{
|
|
|
|
Stream& stream_;
|
|
|
|
|
|
|
|
public:
|
2017-05-31 08:01:55 -07:00
|
|
|
bool invoked = false;
|
|
|
|
std::size_t bytes_transferred = 0;
|
2017-05-28 17:04:39 -07:00
|
|
|
|
|
|
|
explicit
|
|
|
|
write_some_lambda(Stream& stream)
|
|
|
|
: stream_(stream)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ConstBufferSequence>
|
|
|
|
void
|
|
|
|
operator()(error_code& ec,
|
2017-07-06 20:58:07 -07:00
|
|
|
ConstBufferSequence const& buffers)
|
2017-05-28 17:04:39 -07:00
|
|
|
{
|
2017-05-31 08:01:55 -07:00
|
|
|
invoked = true;
|
2017-05-28 17:04:39 -07:00
|
|
|
bytes_transferred =
|
2017-07-06 20:58:07 -07:00
|
|
|
stream_.write_some(buffers, ec);
|
2017-05-28 17:04:39 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-31 08:01:55 -07:00
|
|
|
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,
|
2017-07-06 20:58:07 -07:00
|
|
|
ConstBufferSequence const& buffers)
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
|
|
|
invoked = true;
|
|
|
|
bytes_transferred = boost::asio::write(
|
2017-07-06 20:58:07 -07:00
|
|
|
stream_, buffers, ec);
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-07 07:07:50 -07:00
|
|
|
template<
|
|
|
|
class SyncWriteStream,
|
2017-07-09 20:09:30 -07:00
|
|
|
bool isRequest, class Body, class Fields>
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t
|
2017-07-07 07:07:50 -07:00
|
|
|
write_some(
|
2017-07-09 20:09:30 -07:00
|
|
|
SyncWriteStream& stream,
|
|
|
|
serializer<isRequest, Body, Fields>& sr,
|
2017-07-07 07:07:50 -07:00
|
|
|
error_code& ec)
|
|
|
|
{
|
|
|
|
if(! sr.is_done())
|
|
|
|
{
|
|
|
|
write_some_lambda<SyncWriteStream> f{stream};
|
|
|
|
sr.next(ec, f);
|
|
|
|
if(ec)
|
2017-09-03 18:06:09 -07:00
|
|
|
return f.bytes_transferred;
|
2017-07-07 07:07:50 -07:00
|
|
|
if(f.invoked)
|
|
|
|
sr.consume(f.bytes_transferred);
|
|
|
|
if(sr.is_done())
|
2017-07-07 23:05:11 -07:00
|
|
|
if(! sr.keep_alive())
|
2017-07-07 07:07:50 -07:00
|
|
|
ec = error::end_of_stream;
|
2017-09-03 18:06:09 -07:00
|
|
|
return f.bytes_transferred;
|
2017-07-07 07:07:50 -07:00
|
|
|
}
|
2017-07-07 23:05:11 -07:00
|
|
|
if(! sr.keep_alive())
|
2017-07-07 07:07:50 -07:00
|
|
|
ec = error::end_of_stream;
|
|
|
|
else
|
|
|
|
ec.assign(0, ec.category());
|
2017-09-03 18:06:09 -07:00
|
|
|
return 0;
|
2017-07-07 07:07:50 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class AsyncWriteStream,
|
2017-07-07 09:03:06 -07:00
|
|
|
bool isRequest, class Body, class Fields,
|
2017-07-09 20:09:30 -07:00
|
|
|
class WriteHandler>
|
2017-09-03 18:06:09 -07:00
|
|
|
async_return_type<
|
|
|
|
WriteHandler,
|
|
|
|
void(error_code, std::size_t)>
|
2017-07-09 20:09:30 -07:00
|
|
|
async_write_some(
|
|
|
|
AsyncWriteStream& stream,
|
|
|
|
serializer<isRequest, Body, Fields>& sr,
|
|
|
|
WriteHandler&& handler)
|
2017-07-07 09:03:06 -07:00
|
|
|
{
|
2017-09-03 18:06:09 -07:00
|
|
|
async_completion<
|
|
|
|
WriteHandler,
|
|
|
|
void(error_code, std::size_t)> init{handler};
|
|
|
|
detail::write_some_op<
|
|
|
|
AsyncWriteStream,
|
|
|
|
handler_type<WriteHandler,
|
|
|
|
void(error_code, std::size_t)>,
|
|
|
|
isRequest, Body, Fields>{
|
|
|
|
init.completion_handler, stream, sr}();
|
2017-07-07 09:03:06 -07:00
|
|
|
return init.result.get();
|
|
|
|
}
|
|
|
|
|
2017-07-07 07:07:50 -07:00
|
|
|
} // detail
|
|
|
|
|
2017-09-03 18:06:09 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class SyncWriteStream,
|
|
|
|
bool isRequest, class Body, class Fields>
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t
|
2017-07-09 20:09:30 -07:00
|
|
|
write_some(
|
|
|
|
SyncWriteStream& stream,
|
|
|
|
serializer<isRequest, Body, Fields>& sr)
|
2017-05-28 17:04:39 -07:00
|
|
|
{
|
|
|
|
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
|
|
|
"SyncWriteStream requirements not met");
|
2017-05-31 08:01:55 -07:00
|
|
|
static_assert(is_body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
2017-05-28 17:04:39 -07:00
|
|
|
error_code ec;
|
2017-09-03 18:06:09 -07:00
|
|
|
auto const bytes_transferred =
|
|
|
|
write_some(stream, sr, ec);
|
2017-05-28 17:04:39 -07:00
|
|
|
if(ec)
|
|
|
|
BOOST_THROW_EXCEPTION(system_error{ec});
|
2017-09-03 18:06:09 -07:00
|
|
|
return bytes_transferred;
|
2017-05-28 17:04:39 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class SyncWriteStream,
|
|
|
|
bool isRequest, class Body, class Fields>
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t
|
2017-07-09 20:09:30 -07:00
|
|
|
write_some(
|
|
|
|
SyncWriteStream& stream,
|
|
|
|
serializer<isRequest, Body, Fields>& sr,
|
|
|
|
error_code& ec)
|
2017-05-28 17:04:39 -07:00
|
|
|
{
|
|
|
|
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
|
|
|
"SyncWriteStream requirements not met");
|
2017-05-31 08:01:55 -07:00
|
|
|
static_assert(is_body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
2017-09-03 18:06:09 -07:00
|
|
|
return detail::write_some(stream, sr, ec);
|
2017-05-28 17:04:39 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class AsyncWriteStream,
|
2017-05-28 17:04:39 -07:00
|
|
|
bool isRequest, class Body, class Fields,
|
2017-07-09 20:09:30 -07:00
|
|
|
class WriteHandler>
|
2017-09-03 18:06:09 -07:00
|
|
|
async_return_type<
|
|
|
|
WriteHandler,
|
|
|
|
void(error_code, std::size_t)>
|
2017-07-09 20:09:30 -07:00
|
|
|
async_write_some(
|
|
|
|
AsyncWriteStream& stream,
|
|
|
|
serializer<isRequest, Body, Fields>& sr,
|
|
|
|
WriteHandler&& handler)
|
2017-05-28 17:04:39 -07:00
|
|
|
{
|
|
|
|
static_assert(is_async_write_stream<
|
|
|
|
AsyncWriteStream>::value,
|
|
|
|
"AsyncWriteStream requirements not met");
|
|
|
|
static_assert(is_body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
2017-07-07 09:03:06 -07:00
|
|
|
return detail::async_write_some(stream, sr,
|
|
|
|
std::forward<WriteHandler>(handler));
|
2017-05-28 17:04:39 -07:00
|
|
|
}
|
|
|
|
|
2017-05-31 08:01:55 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class SyncWriteStream,
|
|
|
|
bool isRequest, class Body, class Fields>
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t
|
2017-07-09 20:09:30 -07:00
|
|
|
write_header(SyncWriteStream& stream,
|
|
|
|
serializer<isRequest, Body, Fields>& sr)
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
|
|
|
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
|
|
|
"SyncWriteStream requirements not met");
|
|
|
|
static_assert(is_body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
|
|
|
error_code ec;
|
2017-09-03 18:06:09 -07:00
|
|
|
auto const bytes_transferred =
|
|
|
|
write_header(stream, sr, ec);
|
2017-05-31 08:01:55 -07:00
|
|
|
if(ec)
|
|
|
|
BOOST_THROW_EXCEPTION(system_error{ec});
|
2017-09-03 18:06:09 -07:00
|
|
|
return bytes_transferred;
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class SyncWriteStream,
|
|
|
|
bool isRequest, class Body, class Fields>
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t
|
2017-07-09 20:09:30 -07:00
|
|
|
write_header(
|
|
|
|
SyncWriteStream& stream,
|
|
|
|
serializer<isRequest, Body, Fields>& sr,
|
|
|
|
error_code& ec)
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
|
|
|
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
|
|
|
"SyncWriteStream requirements not met");
|
|
|
|
static_assert(is_body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
|
|
|
sr.split(true);
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t bytes_transferred = 0;
|
2017-07-07 07:07:50 -07:00
|
|
|
if(! sr.is_header_done())
|
2017-06-13 07:08:52 -07:00
|
|
|
{
|
2017-07-07 07:07:50 -07:00
|
|
|
detail::write_lambda<SyncWriteStream> f{stream};
|
|
|
|
do
|
|
|
|
{
|
|
|
|
sr.next(ec, f);
|
2017-09-03 18:06:09 -07:00
|
|
|
bytes_transferred += f.bytes_transferred;
|
2017-07-07 07:07:50 -07:00
|
|
|
if(ec)
|
2017-09-03 18:06:09 -07:00
|
|
|
return bytes_transferred;
|
2017-07-07 07:07:50 -07:00
|
|
|
BOOST_ASSERT(f.invoked);
|
|
|
|
sr.consume(f.bytes_transferred);
|
|
|
|
}
|
|
|
|
while(! sr.is_header_done());
|
2017-06-13 07:08:52 -07:00
|
|
|
}
|
2017-07-07 07:07:50 -07:00
|
|
|
else
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
2017-07-07 07:07:50 -07:00
|
|
|
ec.assign(0, ec.category());
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
2017-09-03 18:06:09 -07:00
|
|
|
return bytes_transferred;
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class AsyncWriteStream,
|
2017-05-31 08:01:55 -07:00
|
|
|
bool isRequest, class Body, class Fields,
|
2017-07-09 20:09:30 -07:00
|
|
|
class WriteHandler>
|
2017-09-03 18:06:09 -07:00
|
|
|
async_return_type<
|
|
|
|
WriteHandler,
|
|
|
|
void(error_code, std::size_t)>
|
2017-07-09 20:09:30 -07:00
|
|
|
async_write_header(
|
|
|
|
AsyncWriteStream& stream,
|
|
|
|
serializer<isRequest, Body, Fields>& sr,
|
|
|
|
WriteHandler&& handler)
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
|
|
|
static_assert(is_async_write_stream<
|
|
|
|
AsyncWriteStream>::value,
|
|
|
|
"AsyncWriteStream requirements not met");
|
|
|
|
static_assert(is_body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
|
|
|
sr.split(true);
|
2017-09-03 18:06:09 -07:00
|
|
|
async_completion<
|
|
|
|
WriteHandler,
|
|
|
|
void(error_code, std::size_t)> init{handler};
|
|
|
|
detail::write_op<
|
|
|
|
AsyncWriteStream,
|
|
|
|
handler_type<WriteHandler,
|
|
|
|
void(error_code, std::size_t)>,
|
|
|
|
detail::serializer_is_header_done,
|
|
|
|
isRequest, Body, Fields>{
|
|
|
|
init.completion_handler, stream, sr}();
|
2017-05-31 08:01:55 -07:00
|
|
|
return init.result.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2017-07-07 07:07:50 -07:00
|
|
|
template<
|
|
|
|
class SyncWriteStream,
|
2017-07-09 20:09:30 -07:00
|
|
|
bool isRequest, class Body, class Fields>
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t
|
2017-07-07 07:07:50 -07:00
|
|
|
write(
|
|
|
|
SyncWriteStream& stream,
|
2017-07-09 20:09:30 -07:00
|
|
|
serializer<isRequest, Body, Fields>& sr)
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
|
|
|
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
|
|
|
"SyncWriteStream requirements not met");
|
|
|
|
error_code ec;
|
2017-09-03 18:06:09 -07:00
|
|
|
auto const bytes_transferred =
|
|
|
|
write(stream, sr, ec);
|
2017-05-31 08:01:55 -07:00
|
|
|
if(ec)
|
|
|
|
BOOST_THROW_EXCEPTION(system_error{ec});
|
2017-09-03 18:06:09 -07:00
|
|
|
return bytes_transferred;
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
|
|
|
|
2017-07-07 07:07:50 -07:00
|
|
|
template<
|
|
|
|
class SyncWriteStream,
|
2017-07-09 20:09:30 -07:00
|
|
|
bool isRequest, class Body, class Fields>
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t
|
2017-07-07 07:07:50 -07:00
|
|
|
write(
|
|
|
|
SyncWriteStream& stream,
|
2017-07-09 20:09:30 -07:00
|
|
|
serializer<isRequest, Body, Fields>& sr,
|
2017-07-07 07:07:50 -07:00
|
|
|
error_code& ec)
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
|
|
|
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
|
|
|
"SyncWriteStream requirements not met");
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t bytes_transferred = 0;
|
2017-05-31 08:01:55 -07:00
|
|
|
sr.split(false);
|
2017-07-07 07:07:50 -07:00
|
|
|
for(;;)
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
2017-09-03 18:06:09 -07:00
|
|
|
bytes_transferred +=
|
|
|
|
write_some(stream, sr, ec);
|
2017-05-31 08:01:55 -07:00
|
|
|
if(ec)
|
2017-09-03 18:06:09 -07:00
|
|
|
return bytes_transferred;
|
2017-07-07 07:07:50 -07:00
|
|
|
if(sr.is_done())
|
|
|
|
break;
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
2017-09-03 18:06:09 -07:00
|
|
|
return bytes_transferred;
|
2017-05-31 08:01:55 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class AsyncWriteStream,
|
2017-05-31 08:01:55 -07:00
|
|
|
bool isRequest, class Body, class Fields,
|
2017-07-09 20:09:30 -07:00
|
|
|
class WriteHandler>
|
2017-09-03 18:06:09 -07:00
|
|
|
async_return_type<
|
|
|
|
WriteHandler,
|
|
|
|
void(error_code, std::size_t)>
|
2017-07-09 20:09:30 -07:00
|
|
|
async_write(
|
|
|
|
AsyncWriteStream& stream,
|
|
|
|
serializer<isRequest, Body, Fields>& sr,
|
|
|
|
WriteHandler&& handler)
|
2017-05-31 08:01:55 -07:00
|
|
|
{
|
|
|
|
static_assert(is_async_write_stream<
|
|
|
|
AsyncWriteStream>::value,
|
|
|
|
"AsyncWriteStream requirements not met");
|
|
|
|
static_assert(is_body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
|
|
|
sr.split(false);
|
2017-09-03 18:06:09 -07:00
|
|
|
async_completion<
|
|
|
|
WriteHandler,
|
|
|
|
void(error_code, std::size_t)> init{handler};
|
|
|
|
detail::write_op<
|
|
|
|
AsyncWriteStream,
|
|
|
|
handler_type<WriteHandler,
|
|
|
|
void(error_code, std::size_t)>,
|
|
|
|
detail::serializer_is_done,
|
|
|
|
isRequest, Body, Fields>{
|
|
|
|
init.completion_handler, stream, sr}();
|
2017-05-31 08:01:55 -07:00
|
|
|
return init.result.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class SyncWriteStream,
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Body, class Fields>
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t
|
2017-07-09 20:09:30 -07:00
|
|
|
write(
|
|
|
|
SyncWriteStream& stream,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields> const& msg)
|
2016-05-01 11:14:10 -04:00
|
|
|
{
|
2017-05-10 12:03:00 -07:00
|
|
|
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
2016-05-01 11:14:10 -04:00
|
|
|
"SyncWriteStream requirements not met");
|
2017-05-28 09:05:29 -07:00
|
|
|
static_assert(is_body<Body>::value,
|
2016-10-15 13:40:17 -04:00
|
|
|
"Body requirements not met");
|
2017-05-28 09:05:29 -07:00
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
2016-05-01 11:14:10 -04:00
|
|
|
error_code ec;
|
2017-09-03 18:06:09 -07:00
|
|
|
auto const bytes_transferred =
|
|
|
|
write(stream, msg, ec);
|
2016-05-01 11:14:10 -04:00
|
|
|
if(ec)
|
2017-05-22 15:30:12 -07:00
|
|
|
BOOST_THROW_EXCEPTION(system_error{ec});
|
2017-09-03 18:06:09 -07:00
|
|
|
return bytes_transferred;
|
2016-05-01 11:14:10 -04:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class SyncWriteStream,
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Body, class Fields>
|
2017-09-03 18:06:09 -07:00
|
|
|
std::size_t
|
2017-07-09 20:09:30 -07:00
|
|
|
write(
|
|
|
|
SyncWriteStream& stream,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields> const& msg,
|
2017-07-09 20:09:30 -07:00
|
|
|
error_code& ec)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-05-10 12:03:00 -07:00
|
|
|
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
2016-04-29 06:04:40 -04:00
|
|
|
"SyncWriteStream requirements not met");
|
2017-05-28 09:05:29 -07:00
|
|
|
static_assert(is_body<Body>::value,
|
2016-10-15 13:40:17 -04:00
|
|
|
"Body requirements not met");
|
2017-05-28 09:05:29 -07:00
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
2017-06-22 07:48:05 -07:00
|
|
|
serializer<isRequest, Body, Fields> sr{msg};
|
2017-09-03 18:06:09 -07:00
|
|
|
return write(stream, sr, ec);
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
2017-07-09 20:09:30 -07:00
|
|
|
template<
|
|
|
|
class AsyncWriteStream,
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Body, class Fields,
|
2017-07-09 20:09:30 -07:00
|
|
|
class WriteHandler>
|
2017-05-12 17:13:03 -07:00
|
|
|
async_return_type<
|
2017-09-03 18:06:09 -07:00
|
|
|
WriteHandler,
|
|
|
|
void(error_code, std::size_t)>
|
2017-07-09 20:09:30 -07:00
|
|
|
async_write(
|
|
|
|
AsyncWriteStream& stream,
|
2017-06-21 11:50:50 -07:00
|
|
|
message<isRequest, Body, Fields>& msg,
|
2017-07-09 20:09:30 -07:00
|
|
|
WriteHandler&& handler)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-05-08 12:41:45 -07:00
|
|
|
static_assert(
|
|
|
|
is_async_write_stream<AsyncWriteStream>::value,
|
2016-05-28 09:23:54 -04:00
|
|
|
"AsyncWriteStream requirements not met");
|
2017-05-28 09:05:29 -07:00
|
|
|
static_assert(is_body<Body>::value,
|
2016-10-15 13:40:17 -04:00
|
|
|
"Body requirements not met");
|
2017-05-28 09:05:29 -07:00
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
2017-09-03 18:06:09 -07:00
|
|
|
async_completion<
|
|
|
|
WriteHandler,
|
|
|
|
void(error_code, std::size_t)> init{handler};
|
|
|
|
detail::write_msg_op<
|
|
|
|
AsyncWriteStream,
|
|
|
|
handler_type<WriteHandler,
|
|
|
|
void(error_code, std::size_t)>,
|
|
|
|
isRequest, Body, Fields>{
|
|
|
|
init.completion_handler, stream, msg}();
|
2017-05-06 12:36:40 -07:00
|
|
|
return init.result.get();
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
2016-11-07 13:51:10 -05:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2017-06-03 17:28:19 -07:00
|
|
|
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
|
2017-06-13 07:08:52 -07:00
|
|
|
operator()(error_code& ec,
|
2017-06-03 17:28:19 -07:00
|
|
|
ConstBufferSequence const& buffers) const
|
|
|
|
{
|
2017-06-19 16:57:12 -07:00
|
|
|
ec.assign(0, ec.category());
|
2017-06-13 07:08:52 -07:00
|
|
|
if(os_.fail())
|
|
|
|
return;
|
2017-06-03 17:28:19 -07:00
|
|
|
std::size_t bytes_transferred = 0;
|
|
|
|
using boost::asio::buffer_cast;
|
|
|
|
using boost::asio::buffer_size;
|
|
|
|
for(auto it = buffers.begin();
|
|
|
|
it != buffers.end(); ++it)
|
|
|
|
{
|
|
|
|
boost::asio::const_buffer b = *it;
|
|
|
|
auto const n = buffer_size(b);
|
|
|
|
os_.write(buffer_cast<char const*>(b), n);
|
|
|
|
if(os_.fail())
|
|
|
|
return;
|
|
|
|
bytes_transferred += n;
|
|
|
|
}
|
|
|
|
sr_.consume(bytes_transferred);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
|
2017-06-07 16:43:09 -07:00
|
|
|
template<class Fields>
|
2016-11-07 13:51:10 -05:00
|
|
|
std::ostream&
|
|
|
|
operator<<(std::ostream& os,
|
2017-06-07 16:43:09 -07:00
|
|
|
header<true, Fields> const& h)
|
2016-11-07 13:51:10 -05:00
|
|
|
{
|
2017-06-07 16:43:09 -07:00
|
|
|
typename Fields::reader fr{
|
|
|
|
h, h.version, h.method()};
|
|
|
|
return os << buffers(fr.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Fields>
|
|
|
|
std::ostream&
|
|
|
|
operator<<(std::ostream& os,
|
|
|
|
header<false, Fields> const& h)
|
|
|
|
{
|
|
|
|
typename Fields::reader fr{
|
|
|
|
h, h.version, h.result_int()};
|
|
|
|
return os << buffers(fr.get());
|
2016-11-07 13:51:10 -05:00
|
|
|
}
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Body, class Fields>
|
2016-04-29 06:04:40 -04:00
|
|
|
std::ostream&
|
|
|
|
operator<<(std::ostream& os,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields> const& msg)
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
2017-05-28 09:05:29 -07:00
|
|
|
static_assert(is_body<Body>::value,
|
2016-10-15 13:40:17 -04:00
|
|
|
"Body requirements not met");
|
2017-05-28 09:05:29 -07:00
|
|
|
static_assert(is_body_reader<Body>::value,
|
|
|
|
"BodyReader requirements not met");
|
2017-06-03 17:28:19 -07:00
|
|
|
serializer<isRequest, Body, Fields> sr{msg};
|
2016-04-29 06:04:40 -04:00
|
|
|
error_code ec;
|
2017-06-03 17:28:19 -07:00
|
|
|
detail::write_ostream_lambda<decltype(sr)> f{os, sr};
|
|
|
|
do
|
|
|
|
{
|
2017-07-04 01:17:56 -07:00
|
|
|
sr.next(ec, f);
|
2017-06-03 17:28:19 -07:00
|
|
|
if(os.fail())
|
|
|
|
break;
|
|
|
|
if(ec == error::end_of_stream)
|
2017-06-19 16:57:12 -07:00
|
|
|
ec.assign(0, ec.category());
|
2017-06-03 17:28:19 -07:00
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
os.setstate(std::ios::failbit);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(! sr.is_done());
|
2016-04-29 06:04:40 -04:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
} // http
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
#endif
|