Files
boost_beast/include/beast/http/impl/write.ipp
T

451 lines
12 KiB
C++
Raw Normal View History

2017-07-20 08:01:46 -07:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-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)
//
#ifndef BEAST_HTTP_IMPL_WRITE_IPP
#define BEAST_HTTP_IMPL_WRITE_IPP
2017-05-28 09:05:29 -07:00
#include <beast/http/type_traits.hpp>
2016-05-07 14:57:15 -04:00
#include <beast/core/bind_handler.hpp>
#include <beast/core/ostream.hpp>
2017-05-08 12:41:45 -07:00
#include <beast/core/handler_alloc.hpp>
#include <beast/core/handler_ptr.hpp>
#include <beast/core/type_traits.hpp>
2016-11-07 06:33:03 -05:00
#include <beast/core/detail/sync_ostream.hpp>
#include <boost/asio/handler_alloc_hook.hpp>
#include <boost/asio/handler_continuation_hook.hpp>
#include <boost/asio/handler_invoke_hook.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>
#include <ostream>
#include <sstream>
2017-07-20 08:01:46 -07:00
namespace beast {
namespace http {
namespace detail {
2017-05-28 17:04:39 -07:00
template<
class Stream, class Serializer, class Handler>
class write_some_op
{
2017-05-08 12:41:45 -07:00
Stream& s_;
2017-05-28 17:04:39 -07:00
Serializer& sr_;
2017-05-08 12:41:45 -07:00
Handler h_;
2017-05-28 17:04:39 -07:00
class lambda
{
write_some_op& op_;
public:
bool empty = true;
explicit
lambda(write_some_op& op)
: op_(op)
{
}
template<class ConstBufferSequence>
void
operator()(error_code& ec,
ConstBufferSequence const& buffer)
{
empty = false;
return op_.s_.async_write_some(
buffer, std::move(op_));
}
};
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-28 17:04:39 -07:00
template<class DeducedHandler>
write_some_op(DeducedHandler&& h,
Stream& s, Serializer& sr)
: s_(s)
, sr_(sr)
, h_(std::forward<DeducedHandler>(h))
{
}
void
2017-05-28 17:04:39 -07:00
operator()();
void
operator()(error_code ec,
std::size_t bytes_transferred);
friend
void* asio_handler_allocate(
2017-05-28 17:04:39 -07:00
std::size_t size, write_some_op* op)
{
using boost::asio::asio_handler_allocate;
return asio_handler_allocate(
2017-05-08 12:41:45 -07:00
size, std::addressof(op->h_));
}
friend
void asio_handler_deallocate(
2017-05-28 17:04:39 -07:00
void* p, std::size_t size, write_some_op* op)
{
using boost::asio::asio_handler_deallocate;
asio_handler_deallocate(
2017-05-08 12:41:45 -07:00
p, size, std::addressof(op->h_));
}
friend
2017-05-28 17:04:39 -07:00
bool asio_handler_is_continuation(write_some_op* op)
{
2017-05-28 17:04:39 -07:00
using boost::asio::asio_handler_is_continuation;
return asio_handler_is_continuation(
std::addressof(op->h_));
}
template<class Function>
friend
2017-05-28 17:04:39 -07:00
void asio_handler_invoke(Function&& f, write_some_op* op)
{
using boost::asio::asio_handler_invoke;
2017-05-28 17:04:39 -07:00
asio_handler_invoke(f, std::addressof(op->h_));
}
};
2017-05-28 17:04:39 -07:00
template<
class Stream, class Serializer, class Handler>
void
2017-05-28 17:04:39 -07:00
write_some_op<Stream, Serializer, Handler>::
operator()()
{
2017-05-28 17:04:39 -07:00
lambda f{*this};
error_code ec;
sr_.get(ec, f);
if(! ec && ! f.empty)
return;
return s_.get_io_service().post(
bind_handler(std::move(*this), ec, 0));
}
2017-05-08 12:41:45 -07:00
2017-05-28 17:04:39 -07:00
template<
class Stream, class Serializer, class Handler>
void
write_some_op<Stream, Serializer, Handler>::
operator()(error_code ec, std::size_t bytes_transferred)
{
if(! ec)
sr_.consume(bytes_transferred);
2017-05-08 12:41:45 -07:00
h_(ec);
}
2017-05-08 12:41:45 -07:00
//------------------------------------------------------------------------------
2017-07-20 08:01:46 -07:00
template<class Stream, class Handler,
bool isRequest, class Body, class Fields>
2017-07-20 08:01:46 -07:00
class write_op
{
struct data
{
int state = 0;
2017-05-08 12:41:45 -07:00
Stream& s;
serializer<isRequest, Body, Fields,
2017-05-28 17:04:39 -07:00
empty_decorator, handler_alloc<char, Handler>> sr;
2017-07-20 08:01:46 -07:00
2017-05-08 12:41:45 -07:00
data(Handler& h, Stream& s_, message<
isRequest, Body, Fields> const& m_)
: s(s_)
2017-05-28 17:04:39 -07:00
, sr(m_, empty_decorator{},
2017-05-08 12:41:45 -07:00
handler_alloc<char, Handler>{h})
2017-07-20 08:01:46 -07:00
{
}
};
handler_ptr<data, Handler> d_;
2017-07-20 08:01:46 -07:00
public:
write_op(write_op&&) = default;
write_op(write_op const&) = default;
template<class DeducedHandler, class... Args>
write_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
{
}
void
2017-05-08 12:41:45 -07:00
operator()(error_code ec);
2017-07-20 08:01:46 -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->d_.handler()));
2017-07-20 08:01:46 -07:00
}
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->d_.handler()));
2017-07-20 08:01:46 -07:00
}
friend
bool asio_handler_is_continuation(write_op* op)
{
2017-05-08 12:41:45 -07:00
using boost::asio::asio_handler_is_continuation;
return op->d_->state == 2 ||
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
void asio_handler_invoke(Function&& f, write_op* op)
{
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,
bool isRequest, class Body, class Fields>
2017-07-20 08:01:46 -07:00
void
write_op<Stream, Handler, isRequest, Body, Fields>::
2017-05-08 12:41:45 -07:00
operator()(error_code ec)
2017-07-20 08:01:46 -07:00
{
auto& d = *d_;
2017-05-08 12:41:45 -07:00
if(ec)
goto upcall;
switch(d.state)
2017-07-20 08:01:46 -07:00
{
2017-05-08 12:41:45 -07:00
case 0:
d.state = 1;
2017-05-28 17:04:39 -07:00
write_some_op<Stream, decltype(d.sr),
write_op>{std::move(*this), d.s, d.sr}();
return;
2017-05-08 12:41:45 -07:00
case 1:
d.state = 2;
2017-05-28 17:04:39 -07:00
// [[fallthrough]]
2017-05-08 12:41:45 -07:00
case 2:
2017-05-28 17:04:39 -07:00
if(d.sr.is_done())
{
if(d.sr.needs_close())
// VFALCO Choose an error code
ec = boost::asio::error::eof;
2017-07-20 08:01:46 -07:00
break;
2017-05-28 17:04:39 -07:00
}
write_some_op<Stream, decltype(d.sr),
write_op>{std::move(*this), d.s, d.sr}();
return;
2017-07-20 08:01:46 -07:00
}
2017-05-08 12:41:45 -07:00
upcall:
d_.invoke(ec);
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:
boost::optional<std::size_t> bytes_transferred;
explicit
write_some_lambda(Stream& stream)
: stream_(stream)
{
}
template<class ConstBufferSequence>
void
operator()(error_code& ec,
ConstBufferSequence const& buffer)
{
bytes_transferred =
stream_.write_some(buffer, ec);
}
};
2017-07-20 08:01:46 -07:00
} // detail
2017-05-28 17:04:39 -07:00
//------------------------------------------------------------------------------
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class Allocator>
void
write_some(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator, Allocator>& sr)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
error_code ec;
write_some(stream, sr, ec);
if(ec)
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class Allocator>
void
write_some(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator, Allocator>& sr,
error_code& ec)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
detail::write_some_lambda<SyncWriteStream> f{stream};
sr.get(ec, f);
if(! ec)
{
if(f.bytes_transferred)
sr.consume(*f.bytes_transferred);
if(sr.is_done() && sr.needs_close())
// VFALCO decide on an error code
ec = boost::asio::error::eof;
}
}
template<class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class Allocator, class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write_some(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator, Allocator>& sr,
WriteHandler&& handler)
{
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");
async_completion<WriteHandler,
void(error_code)> init{handler};
detail::write_some_op<AsyncWriteStream, decltype(sr),
handler_type<WriteHandler, void(error_code)>>{
init.completion_handler, stream, sr}();
return init.result.get();
}
2017-07-20 08:01:46 -07:00
template<class SyncWriteStream,
bool isRequest, class Body, class Fields>
2017-07-20 08:01:46 -07:00
void
write(SyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"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");
error_code ec;
write(stream, msg, ec);
if(ec)
2017-05-22 15:30:12 -07:00
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg,
2016-10-04 18:00:11 -04:00
error_code& ec)
2017-07-20 08:01:46 -07:00
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"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-05-28 17:04:39 -07:00
auto sr = make_serializer(msg);
2017-05-08 12:41:45 -07:00
for(;;)
2016-05-07 17:06:46 -04:00
{
2017-05-28 17:04:39 -07:00
#if 0
sr.write_some(stream, ec);
#else
write_some(stream, sr, ec);
#endif
2017-07-20 08:01:46 -07:00
if(ec)
return;
2017-05-28 17:04:39 -07:00
if(sr.is_done())
2017-05-08 12:41:45 -07:00
break;
2017-07-20 08:01:46 -07:00
}
}
template<class AsyncWriteStream,
bool isRequest, class Body, class Fields,
2017-07-20 08:01:46 -07:00
class WriteHandler>
async_return_type<
WriteHandler, void(error_code)>
2017-07-20 08:01:46 -07:00
async_write(AsyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg,
2017-07-20 08:01:46 -07:00
WriteHandler&& handler)
{
2017-05-08 12:41:45 -07:00
static_assert(
is_async_write_stream<AsyncWriteStream>::value,
"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");
async_completion<WriteHandler,
void(error_code)> init{handler};
detail::write_op<AsyncWriteStream, handler_type<
2017-05-08 12:41:45 -07:00
WriteHandler, void(error_code)>,
isRequest, Body, Fields>{
init.completion_handler, stream, msg}(
error_code{});
return init.result.get();
2017-07-20 08:01:46 -07:00
}
//------------------------------------------------------------------------------
template<bool isRequest, class Fields>
std::ostream&
operator<<(std::ostream& os,
header<isRequest, Fields> const& msg)
{
2017-05-08 12:41:45 -07:00
detail::write_start_line(os, msg);
detail::write_fields(os, msg.fields);
os << "\r\n";
return os;
}
template<bool isRequest, class Body, class Fields>
std::ostream&
operator<<(std::ostream& os,
message<isRequest, Body, Fields> const& msg)
{
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-11-07 06:33:03 -05:00
beast::detail::sync_ostream oss{os};
error_code ec;
write(oss, msg, ec);
if(ec && ec != boost::asio::error::eof)
2017-05-22 15:30:12 -07:00
BOOST_THROW_EXCEPTION(system_error{ec});
return os;
}
2017-07-20 08:01:46 -07:00
} // http
} // beast
#endif