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
|
|
|
|
|
2016-05-18 12:30:15 -04:00
|
|
|
#include <beast/http/concepts.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <beast/http/resume_context.hpp>
|
2016-11-07 16:57:41 -05:00
|
|
|
#include <beast/http/chunk_encode.hpp>
|
2016-05-07 14:57:15 -04:00
|
|
|
#include <beast/core/buffer_cat.hpp>
|
|
|
|
#include <beast/core/bind_handler.hpp>
|
|
|
|
#include <beast/core/buffer_concepts.hpp>
|
2017-01-05 09:07:18 -05:00
|
|
|
#include <beast/core/handler_helpers.hpp>
|
2017-01-02 13:29:48 -05:00
|
|
|
#include <beast/core/handler_ptr.hpp>
|
2016-05-07 14:57:15 -04:00
|
|
|
#include <beast/core/stream_concepts.hpp>
|
|
|
|
#include <beast/core/streambuf.hpp>
|
2016-05-28 09:23:54 -04:00
|
|
|
#include <beast/core/write_dynabuf.hpp>
|
2016-11-07 06:33:03 -05:00
|
|
|
#include <beast/core/detail/sync_ostream.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <boost/asio/write.hpp>
|
|
|
|
#include <boost/logic/tribool.hpp>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2016-04-29 06:04:40 -04:00
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace beast {
|
|
|
|
namespace http {
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<class DynamicBuffer, class Fields>
|
2016-05-01 11:14:10 -04:00
|
|
|
void
|
2016-11-09 16:55:16 -05:00
|
|
|
write_start_line(DynamicBuffer& dynabuf,
|
2016-11-10 05:34:49 -05:00
|
|
|
header<true, Fields> const& msg)
|
2016-05-01 11:14:10 -04:00
|
|
|
{
|
2016-11-07 13:51:10 -05:00
|
|
|
BOOST_ASSERT(msg.version == 10 || msg.version == 11);
|
2016-05-28 09:23:54 -04:00
|
|
|
write(dynabuf, msg.method);
|
|
|
|
write(dynabuf, " ");
|
|
|
|
write(dynabuf, msg.url);
|
2016-10-10 09:00:57 -04:00
|
|
|
switch(msg.version)
|
|
|
|
{
|
|
|
|
case 10:
|
|
|
|
write(dynabuf, " HTTP/1.0\r\n");
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
write(dynabuf, " HTTP/1.1\r\n");
|
|
|
|
break;
|
|
|
|
}
|
2016-05-01 11:14:10 -04:00
|
|
|
}
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<class DynamicBuffer, class Fields>
|
2016-05-01 11:14:10 -04:00
|
|
|
void
|
2016-11-09 16:55:16 -05:00
|
|
|
write_start_line(DynamicBuffer& dynabuf,
|
2016-11-10 05:34:49 -05:00
|
|
|
header<false, Fields> const& msg)
|
2016-05-01 11:14:10 -04:00
|
|
|
{
|
2016-11-07 13:51:10 -05:00
|
|
|
BOOST_ASSERT(msg.version == 10 || msg.version == 11);
|
2016-10-10 09:00:57 -04:00
|
|
|
switch(msg.version)
|
|
|
|
{
|
|
|
|
case 10:
|
|
|
|
write(dynabuf, "HTTP/1.0 ");
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
write(dynabuf, "HTTP/1.1 ");
|
|
|
|
break;
|
|
|
|
}
|
2016-05-28 09:23:54 -04:00
|
|
|
write(dynabuf, msg.status);
|
|
|
|
write(dynabuf, " ");
|
|
|
|
write(dynabuf, msg.reason);
|
|
|
|
write(dynabuf, "\r\n");
|
2016-05-01 11:14:10 -04:00
|
|
|
}
|
|
|
|
|
2016-05-28 09:23:54 -04:00
|
|
|
template<class DynamicBuffer, class FieldSequence>
|
2016-05-01 11:14:10 -04:00
|
|
|
void
|
2016-05-28 09:23:54 -04:00
|
|
|
write_fields(DynamicBuffer& dynabuf, FieldSequence const& fields)
|
2016-05-01 11:14:10 -04:00
|
|
|
{
|
2016-05-28 09:23:54 -04:00
|
|
|
static_assert(is_DynamicBuffer<DynamicBuffer>::value,
|
|
|
|
"DynamicBuffer requirements not met");
|
2016-05-01 11:14:10 -04:00
|
|
|
//static_assert(is_FieldSequence<FieldSequence>::value,
|
|
|
|
// "FieldSequence requirements not met");
|
|
|
|
for(auto const& field : fields)
|
|
|
|
{
|
2016-05-28 09:23:54 -04:00
|
|
|
write(dynabuf, field.name());
|
|
|
|
write(dynabuf, ": ");
|
|
|
|
write(dynabuf, field.value());
|
|
|
|
write(dynabuf, "\r\n");
|
2016-05-01 11:14:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:51:10 -05:00
|
|
|
} // detail
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template<class Stream, class Handler>
|
|
|
|
class write_streambuf_op
|
|
|
|
{
|
|
|
|
struct data
|
|
|
|
{
|
2017-01-02 13:29:48 -05:00
|
|
|
bool cont;
|
2016-11-07 13:51:10 -05:00
|
|
|
Stream& s;
|
|
|
|
streambuf sb;
|
|
|
|
int state = 0;
|
|
|
|
|
2017-01-02 13:29:48 -05:00
|
|
|
data(Handler& handler, Stream& s_,
|
2016-11-07 13:51:10 -05:00
|
|
|
streambuf&& sb_)
|
2017-01-05 09:07:18 -05:00
|
|
|
: cont(beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
is_continuation(handler))
|
|
|
|
, s(s_)
|
2016-11-07 13:51:10 -05:00
|
|
|
, sb(std::move(sb_))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-02 13:29:48 -05:00
|
|
|
handler_ptr<data, Handler> d_;
|
2016-11-07 13:51:10 -05:00
|
|
|
|
|
|
|
public:
|
|
|
|
write_streambuf_op(write_streambuf_op&&) = default;
|
|
|
|
write_streambuf_op(write_streambuf_op const&) = default;
|
|
|
|
|
|
|
|
template<class DeducedHandler, class... Args>
|
|
|
|
write_streambuf_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)...)
|
2016-11-07 13:51:10 -05:00
|
|
|
{
|
|
|
|
(*this)(error_code{}, 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
operator()(error_code ec,
|
|
|
|
std::size_t bytes_transferred, bool again = true);
|
|
|
|
|
|
|
|
friend
|
|
|
|
void* asio_handler_allocate(
|
|
|
|
std::size_t size, write_streambuf_op* op)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
allocate(size, op->d_.handler());
|
2016-11-07 13:51:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
void asio_handler_deallocate(
|
|
|
|
void* p, std::size_t size, write_streambuf_op* op)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
deallocate(p, size, op->d_.handler());
|
2016-11-07 13:51:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
bool asio_handler_is_continuation(write_streambuf_op* op)
|
|
|
|
{
|
|
|
|
return op->d_->cont;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Function>
|
|
|
|
friend
|
|
|
|
void asio_handler_invoke(Function&& f, write_streambuf_op* op)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
invoke(f, op->d_.handler());
|
2016-11-07 13:51:10 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Stream, class Handler>
|
|
|
|
void
|
|
|
|
write_streambuf_op<Stream, Handler>::
|
|
|
|
operator()(error_code ec, std::size_t, bool again)
|
|
|
|
{
|
|
|
|
auto& d = *d_;
|
|
|
|
d.cont = d.cont || again;
|
|
|
|
while(! ec && d.state != 99)
|
|
|
|
{
|
|
|
|
switch(d.state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
d.state = 99;
|
|
|
|
boost::asio::async_write(d.s,
|
|
|
|
d.sb.data(), std::move(*this));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-02 13:29:48 -05:00
|
|
|
d_.invoke(ec);
|
2016-11-07 13:51:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
template<class SyncWriteStream,
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Fields>
|
2016-11-07 13:51:10 -05:00
|
|
|
void
|
|
|
|
write(SyncWriteStream& stream,
|
2016-11-10 05:34:49 -05:00
|
|
|
header<isRequest, Fields> const& msg)
|
2016-11-07 13:51:10 -05:00
|
|
|
{
|
|
|
|
static_assert(is_SyncWriteStream<SyncWriteStream>::value,
|
|
|
|
"SyncWriteStream requirements not met");
|
|
|
|
error_code ec;
|
|
|
|
write(stream, msg, ec);
|
|
|
|
if(ec)
|
|
|
|
throw system_error{ec};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class SyncWriteStream,
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Fields>
|
2016-11-07 13:51:10 -05:00
|
|
|
void
|
|
|
|
write(SyncWriteStream& stream,
|
2016-11-10 05:34:49 -05:00
|
|
|
header<isRequest, Fields> const& msg,
|
2016-11-07 13:51:10 -05:00
|
|
|
error_code& ec)
|
|
|
|
{
|
|
|
|
static_assert(is_SyncWriteStream<SyncWriteStream>::value,
|
|
|
|
"SyncWriteStream requirements not met");
|
|
|
|
streambuf sb;
|
2016-11-09 16:55:16 -05:00
|
|
|
detail::write_start_line(sb, msg);
|
2016-11-10 05:34:49 -05:00
|
|
|
detail::write_fields(sb, msg.fields);
|
2016-11-07 13:51:10 -05:00
|
|
|
beast::write(sb, "\r\n");
|
|
|
|
boost::asio::write(stream, sb.data(), ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class AsyncWriteStream,
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Fields,
|
2016-11-07 13:51:10 -05:00
|
|
|
class WriteHandler>
|
|
|
|
typename async_completion<
|
|
|
|
WriteHandler, void(error_code)>::result_type
|
|
|
|
async_write(AsyncWriteStream& stream,
|
2016-11-10 05:34:49 -05:00
|
|
|
header<isRequest, Fields> const& msg,
|
2016-11-07 13:51:10 -05:00
|
|
|
WriteHandler&& handler)
|
|
|
|
{
|
|
|
|
static_assert(is_AsyncWriteStream<AsyncWriteStream>::value,
|
|
|
|
"AsyncWriteStream requirements not met");
|
|
|
|
beast::async_completion<WriteHandler,
|
2017-01-09 11:25:34 -05:00
|
|
|
void(error_code)> completion{handler};
|
2016-11-07 13:51:10 -05:00
|
|
|
streambuf sb;
|
2016-11-09 16:55:16 -05:00
|
|
|
detail::write_start_line(sb, msg);
|
2016-11-10 05:34:49 -05:00
|
|
|
detail::write_fields(sb, msg.fields);
|
2016-11-07 13:51:10 -05:00
|
|
|
beast::write(sb, "\r\n");
|
|
|
|
detail::write_streambuf_op<AsyncWriteStream,
|
|
|
|
decltype(completion.handler)>{
|
|
|
|
completion.handler, stream, std::move(sb)};
|
|
|
|
return completion.result.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Body, class Fields>
|
2016-05-01 11:14:10 -04:00
|
|
|
struct write_preparation
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields> const& msg;
|
2016-05-01 11:14:10 -04:00
|
|
|
typename Body::writer w;
|
|
|
|
streambuf sb;
|
|
|
|
bool chunked;
|
|
|
|
bool close;
|
|
|
|
|
|
|
|
explicit
|
|
|
|
write_preparation(
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields> const& msg_)
|
2016-05-01 11:14:10 -04:00
|
|
|
: msg(msg_)
|
|
|
|
, w(msg)
|
2016-05-24 06:17:04 -04:00
|
|
|
, chunked(token_list{
|
2016-11-10 05:34:49 -05:00
|
|
|
msg.fields["Transfer-Encoding"]}.exists("chunked"))
|
2016-05-24 06:17:04 -04:00
|
|
|
, close(token_list{
|
2016-11-10 05:34:49 -05:00
|
|
|
msg.fields["Connection"]}.exists("close") ||
|
|
|
|
(msg.version < 11 && ! msg.fields.exists(
|
2016-05-01 11:14:10 -04:00
|
|
|
"Content-Length")))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init(error_code& ec)
|
|
|
|
{
|
|
|
|
w.init(ec);
|
|
|
|
if(ec)
|
|
|
|
return;
|
2017-02-06 20:07:03 -05:00
|
|
|
|
2016-11-09 16:55:16 -05:00
|
|
|
write_start_line(sb, msg);
|
2016-11-10 05:34:49 -05:00
|
|
|
write_fields(sb, msg.fields);
|
2016-05-01 11:14:10 -04:00
|
|
|
beast::write(sb, "\r\n");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
class write_op
|
|
|
|
{
|
|
|
|
struct data
|
|
|
|
{
|
2017-01-02 13:29:48 -05:00
|
|
|
bool cont;
|
2017-07-20 08:01:46 -07:00
|
|
|
Stream& s;
|
|
|
|
// VFALCO How do we use handler_alloc in write_preparation?
|
|
|
|
write_preparation<
|
2016-11-10 05:34:49 -05:00
|
|
|
isRequest, Body, Fields> wp;
|
2017-07-20 08:01:46 -07:00
|
|
|
resume_context resume;
|
|
|
|
resume_context copy;
|
|
|
|
int state = 0;
|
|
|
|
|
2017-01-02 13:29:48 -05:00
|
|
|
data(Handler& handler, Stream& s_,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields> const& m_)
|
2017-01-05 09:07:18 -05:00
|
|
|
: cont(beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
is_continuation(handler))
|
|
|
|
, s(s_)
|
2017-07-20 08:01:46 -07:00
|
|
|
, wp(m_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
class writef0_lambda
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
write_op& self_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit
|
2016-05-07 17:06:46 -04:00
|
|
|
writef0_lambda(write_op& self)
|
2017-07-20 08:01:46 -07:00
|
|
|
: self_(self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ConstBufferSequence>
|
2016-10-15 13:40:17 -04:00
|
|
|
void operator()(ConstBufferSequence const& buffers) const
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
auto& d = *self_.d_;
|
2016-11-10 05:34:49 -05:00
|
|
|
// write header and body
|
2017-07-20 08:01:46 -07:00
|
|
|
if(d.wp.chunked)
|
|
|
|
boost::asio::async_write(d.s,
|
|
|
|
buffer_cat(d.wp.sb.data(),
|
2016-11-07 16:57:41 -05:00
|
|
|
chunk_encode(false, buffers)),
|
2017-07-20 08:01:46 -07:00
|
|
|
std::move(self_));
|
|
|
|
else
|
|
|
|
boost::asio::async_write(d.s,
|
|
|
|
buffer_cat(d.wp.sb.data(),
|
|
|
|
buffers), std::move(self_));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
class writef_lambda
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
write_op& self_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit
|
2016-05-07 17:06:46 -04:00
|
|
|
writef_lambda(write_op& self)
|
2017-07-20 08:01:46 -07:00
|
|
|
: self_(self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ConstBufferSequence>
|
2016-10-15 13:40:17 -04:00
|
|
|
void operator()(ConstBufferSequence const& buffers) const
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
auto& d = *self_.d_;
|
|
|
|
// write body
|
|
|
|
if(d.wp.chunked)
|
|
|
|
boost::asio::async_write(d.s,
|
2016-11-07 16:57:41 -05:00
|
|
|
chunk_encode(false, buffers),
|
2017-07-20 08:01:46 -07:00
|
|
|
std::move(self_));
|
|
|
|
else
|
|
|
|
boost::asio::async_write(d.s,
|
|
|
|
buffers, std::move(self_));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-02 13:29:48 -05: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
|
|
|
{
|
|
|
|
auto& d = *d_;
|
|
|
|
auto sp = d_;
|
|
|
|
d.resume = {
|
|
|
|
[sp]() mutable
|
|
|
|
{
|
2016-11-07 16:57:41 -05:00
|
|
|
write_op self{std::move(sp)};
|
2017-07-20 08:01:46 -07:00
|
|
|
self.d_->cont = false;
|
|
|
|
auto& ios = self.d_->s.get_io_service();
|
|
|
|
ios.dispatch(bind_handler(std::move(self),
|
|
|
|
error_code{}, 0, false));
|
|
|
|
}};
|
|
|
|
d.copy = d.resume;
|
|
|
|
(*this)(error_code{}, 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit
|
2017-01-02 13:29:48 -05:00
|
|
|
write_op(handler_ptr<data, Handler> d)
|
2017-07-20 08:01:46 -07:00
|
|
|
: d_(std::move(d))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
operator()(error_code ec,
|
|
|
|
std::size_t bytes_transferred, bool again = true);
|
|
|
|
|
|
|
|
friend
|
|
|
|
void* asio_handler_allocate(
|
|
|
|
std::size_t size, write_op* op)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
allocate(size, 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)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
deallocate(p, size, op->d_.handler());
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
friend
|
|
|
|
bool asio_handler_is_continuation(write_op* op)
|
|
|
|
{
|
|
|
|
return op->d_->cont;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2017-01-05 09:07:18 -05:00
|
|
|
return beast_asio_helpers::
|
2017-01-02 13:29:48 -05:00
|
|
|
invoke(f, 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
|
2016-11-10 05:34:49 -05:00
|
|
|
write_op<Stream, Handler, isRequest, Body, Fields>::
|
2017-07-20 08:01:46 -07:00
|
|
|
operator()(error_code ec, std::size_t, bool again)
|
|
|
|
{
|
|
|
|
auto& d = *d_;
|
|
|
|
d.cont = d.cont || again;
|
|
|
|
while(! ec && d.state != 99)
|
|
|
|
{
|
|
|
|
switch(d.state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
d.wp.init(ec);
|
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
// call handler
|
|
|
|
d.state = 99;
|
|
|
|
d.s.get_io_service().post(bind_handler(
|
|
|
|
std::move(*this), ec, 0, false));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
d.state = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
2016-10-15 13:40:17 -04:00
|
|
|
boost::tribool const result = d.wp.w.write(
|
2016-05-07 17:06:46 -04:00
|
|
|
std::move(d.copy), ec, writef0_lambda{*this});
|
2017-07-20 08:01:46 -07:00
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
// call handler
|
|
|
|
d.state = 99;
|
|
|
|
d.s.get_io_service().post(bind_handler(
|
|
|
|
std::move(*this), ec, false));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(boost::indeterminate(result))
|
|
|
|
{
|
|
|
|
// suspend
|
|
|
|
d.copy = d.resume;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(result)
|
|
|
|
d.state = d.wp.chunked ? 4 : 5;
|
|
|
|
else
|
|
|
|
d.state = 2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
// sent header and body
|
2017-07-20 08:01:46 -07:00
|
|
|
case 2:
|
|
|
|
d.wp.sb.consume(d.wp.sb.size());
|
|
|
|
d.state = 3;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
{
|
2016-10-15 13:40:17 -04:00
|
|
|
boost::tribool result = d.wp.w.write(
|
2016-05-07 17:06:46 -04:00
|
|
|
std::move(d.copy), ec, writef_lambda{*this});
|
2017-07-20 08:01:46 -07:00
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
// call handler
|
|
|
|
d.state = 99;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(boost::indeterminate(result))
|
|
|
|
{
|
|
|
|
// suspend
|
|
|
|
d.copy = d.resume;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(result)
|
|
|
|
d.state = d.wp.chunked ? 4 : 5;
|
|
|
|
else
|
|
|
|
d.state = 2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
// VFALCO Unfortunately the current interface to the
|
|
|
|
// Writer concept prevents us from coalescing the
|
|
|
|
// final body chunk with the final chunk delimiter.
|
|
|
|
//
|
|
|
|
// write final chunk
|
|
|
|
d.state = 5;
|
|
|
|
boost::asio::async_write(d.s,
|
2016-11-07 16:57:41 -05:00
|
|
|
chunk_encode_final(), std::move(*this));
|
2017-07-20 08:01:46 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
if(d.wp.close)
|
|
|
|
{
|
|
|
|
// VFALCO TODO Decide on an error code
|
|
|
|
ec = boost::asio::error::eof;
|
|
|
|
}
|
|
|
|
d.state = 99;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.copy = {};
|
2017-01-02 13:29:48 -05:00
|
|
|
d.resume = {};
|
|
|
|
d_.invoke(ec);
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
2016-05-28 09:23:54 -04:00
|
|
|
template<class SyncWriteStream, class DynamicBuffer>
|
2016-05-07 17:06:46 -04:00
|
|
|
class writef0_lambda
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-05-28 09:23:54 -04:00
|
|
|
DynamicBuffer const& sb_;
|
2017-07-20 08:01:46 -07:00
|
|
|
SyncWriteStream& stream_;
|
|
|
|
bool chunked_;
|
|
|
|
error_code& ec_;
|
|
|
|
|
|
|
|
public:
|
2016-05-07 17:06:46 -04:00
|
|
|
writef0_lambda(SyncWriteStream& stream,
|
2016-05-28 09:23:54 -04:00
|
|
|
DynamicBuffer const& sb, bool chunked, error_code& ec)
|
2017-07-20 08:01:46 -07:00
|
|
|
: sb_(sb)
|
|
|
|
, stream_(stream)
|
|
|
|
, chunked_(chunked)
|
|
|
|
, ec_(ec)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ConstBufferSequence>
|
2016-10-15 13:40:17 -04:00
|
|
|
void operator()(ConstBufferSequence const& buffers) const
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
// write header and body
|
2017-07-20 08:01:46 -07:00
|
|
|
if(chunked_)
|
|
|
|
boost::asio::write(stream_, buffer_cat(
|
2016-11-07 16:57:41 -05:00
|
|
|
sb_.data(), chunk_encode(false, buffers)), ec_);
|
2017-07-20 08:01:46 -07:00
|
|
|
else
|
|
|
|
boost::asio::write(stream_, buffer_cat(
|
|
|
|
sb_.data(), buffers), ec_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class SyncWriteStream>
|
2016-05-07 17:06:46 -04:00
|
|
|
class writef_lambda
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
SyncWriteStream& stream_;
|
|
|
|
bool chunked_;
|
|
|
|
error_code& ec_;
|
|
|
|
|
|
|
|
public:
|
2016-05-07 17:06:46 -04:00
|
|
|
writef_lambda(SyncWriteStream& stream,
|
2017-07-20 08:01:46 -07:00
|
|
|
bool chunked, error_code& ec)
|
|
|
|
: stream_(stream)
|
|
|
|
, chunked_(chunked)
|
|
|
|
, ec_(ec)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ConstBufferSequence>
|
2016-10-15 13:40:17 -04:00
|
|
|
void operator()(ConstBufferSequence const& buffers) const
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
// write body
|
|
|
|
if(chunked_)
|
|
|
|
boost::asio::write(stream_,
|
2016-11-07 16:57:41 -05:00
|
|
|
chunk_encode(false, buffers), ec_);
|
2017-07-20 08:01:46 -07:00
|
|
|
else
|
|
|
|
boost::asio::write(stream_, buffers, ec_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
template<class SyncWriteStream,
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Body, class Fields>
|
2017-07-20 08:01:46 -07:00
|
|
|
void
|
|
|
|
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
|
|
|
{
|
|
|
|
static_assert(is_SyncWriteStream<SyncWriteStream>::value,
|
|
|
|
"SyncWriteStream requirements not met");
|
2016-10-15 13:40:17 -04:00
|
|
|
static_assert(is_Body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(has_writer<Body>::value,
|
|
|
|
"Body has no writer");
|
|
|
|
static_assert(is_Writer<typename Body::writer,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields>>::value,
|
2016-10-15 13:40:17 -04:00
|
|
|
"Writer requirements not met");
|
2016-05-01 11:14:10 -04:00
|
|
|
error_code ec;
|
|
|
|
write(stream, msg, ec);
|
|
|
|
if(ec)
|
2016-05-07 14:57:15 -04:00
|
|
|
throw system_error{ec};
|
2016-05-01 11:14:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class SyncWriteStream,
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Body, class Fields>
|
2016-05-01 11:14:10 -04:00
|
|
|
void
|
|
|
|
write(SyncWriteStream& stream,
|
2016-11-10 05:34:49 -05:00
|
|
|
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
|
|
|
{
|
2016-04-29 06:04:40 -04:00
|
|
|
static_assert(is_SyncWriteStream<SyncWriteStream>::value,
|
|
|
|
"SyncWriteStream requirements not met");
|
2016-10-15 13:40:17 -04:00
|
|
|
static_assert(is_Body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(has_writer<Body>::value,
|
|
|
|
"Body has no writer");
|
|
|
|
static_assert(is_Writer<typename Body::writer,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields>>::value,
|
2016-10-15 13:40:17 -04:00
|
|
|
"Writer requirements not met");
|
2016-11-10 05:34:49 -05:00
|
|
|
detail::write_preparation<isRequest, Body, Fields> wp(msg);
|
2017-07-20 08:01:46 -07:00
|
|
|
wp.init(ec);
|
|
|
|
if(ec)
|
|
|
|
return;
|
|
|
|
std::mutex m;
|
|
|
|
std::condition_variable cv;
|
|
|
|
bool ready = false;
|
|
|
|
resume_context resume{
|
|
|
|
[&]
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m);
|
|
|
|
ready = true;
|
|
|
|
cv.notify_one();
|
|
|
|
}};
|
|
|
|
auto copy = resume;
|
2016-10-15 13:40:17 -04:00
|
|
|
boost::tribool result =
|
|
|
|
wp.w.write(std::move(copy), ec,
|
|
|
|
detail::writef0_lambda<SyncWriteStream,
|
|
|
|
decltype(wp.sb)>{stream,
|
|
|
|
wp.sb, wp.chunked, ec});
|
2016-05-07 17:06:46 -04:00
|
|
|
if(ec)
|
|
|
|
return;
|
|
|
|
if(boost::indeterminate(result))
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
copy = resume;
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
std::unique_lock<std::mutex> lock(m);
|
|
|
|
cv.wait(lock, [&]{ return ready; });
|
|
|
|
ready = false;
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
2016-05-07 17:06:46 -04:00
|
|
|
boost::asio::write(stream, wp.sb.data(), ec);
|
|
|
|
if(ec)
|
|
|
|
return;
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
wp.sb.consume(wp.sb.size());
|
|
|
|
if(! result)
|
|
|
|
{
|
2016-10-15 13:40:17 -04:00
|
|
|
detail::writef_lambda<SyncWriteStream> wf{
|
|
|
|
stream, wp.chunked, ec};
|
2017-07-20 08:01:46 -07:00
|
|
|
for(;;)
|
|
|
|
{
|
2016-10-15 13:40:17 -04:00
|
|
|
result = wp.w.write(std::move(copy), ec, wf);
|
2017-07-20 08:01:46 -07:00
|
|
|
if(ec)
|
|
|
|
return;
|
|
|
|
if(result)
|
|
|
|
break;
|
2016-05-07 17:06:46 -04:00
|
|
|
if(! result)
|
|
|
|
continue;
|
|
|
|
copy = resume;
|
|
|
|
std::unique_lock<std::mutex> lock(m);
|
|
|
|
cv.wait(lock, [&]{ return ready; });
|
|
|
|
ready = false;
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(wp.chunked)
|
|
|
|
{
|
|
|
|
// VFALCO Unfortunately the current interface to the
|
|
|
|
// Writer concept prevents us from using coalescing the
|
|
|
|
// final body chunk with the final chunk delimiter.
|
|
|
|
//
|
|
|
|
// write final chunk
|
2016-11-07 16:57:41 -05:00
|
|
|
boost::asio::write(stream, chunk_encode_final(), ec);
|
2017-07-20 08:01:46 -07:00
|
|
|
if(ec)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(wp.close)
|
|
|
|
{
|
|
|
|
// VFALCO TODO Decide on an error code
|
|
|
|
ec = boost::asio::error::eof;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class AsyncWriteStream,
|
2016-11-10 05:34:49 -05:00
|
|
|
bool isRequest, class Body, class Fields,
|
2017-07-20 08:01:46 -07:00
|
|
|
class WriteHandler>
|
|
|
|
typename async_completion<
|
|
|
|
WriteHandler, void(error_code)>::result_type
|
|
|
|
async_write(AsyncWriteStream& stream,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields> const& msg,
|
2017-07-20 08:01:46 -07:00
|
|
|
WriteHandler&& handler)
|
|
|
|
{
|
2016-05-28 09:23:54 -04:00
|
|
|
static_assert(is_AsyncWriteStream<AsyncWriteStream>::value,
|
|
|
|
"AsyncWriteStream requirements not met");
|
2016-10-15 13:40:17 -04:00
|
|
|
static_assert(is_Body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(has_writer<Body>::value,
|
|
|
|
"Body has no writer");
|
|
|
|
static_assert(is_Writer<typename Body::writer,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields>>::value,
|
2016-10-15 13:40:17 -04:00
|
|
|
"Writer requirements not met");
|
2017-07-20 08:01:46 -07:00
|
|
|
beast::async_completion<WriteHandler,
|
2017-01-09 11:25:34 -05:00
|
|
|
void(error_code)> completion{handler};
|
2017-07-20 08:01:46 -07:00
|
|
|
detail::write_op<AsyncWriteStream, decltype(completion.handler),
|
2016-11-10 05:34:49 -05:00
|
|
|
isRequest, Body, Fields>{completion.handler, stream, msg};
|
2017-07-20 08:01:46 -07:00
|
|
|
return completion.result.get();
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:51:10 -05:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Fields>
|
2016-11-07 13:51:10 -05:00
|
|
|
std::ostream&
|
|
|
|
operator<<(std::ostream& os,
|
2016-11-10 05:34:49 -05:00
|
|
|
header<isRequest, Fields> const& msg)
|
2016-11-07 13:51:10 -05:00
|
|
|
{
|
|
|
|
beast::detail::sync_ostream oss{os};
|
|
|
|
error_code ec;
|
|
|
|
write(oss, msg, ec);
|
|
|
|
if(ec)
|
|
|
|
throw system_error{ec};
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-10-15 13:40:17 -04:00
|
|
|
static_assert(is_Body<Body>::value,
|
|
|
|
"Body requirements not met");
|
|
|
|
static_assert(has_writer<Body>::value,
|
|
|
|
"Body has no writer");
|
|
|
|
static_assert(is_Writer<typename Body::writer,
|
2016-11-10 05:34:49 -05:00
|
|
|
message<isRequest, Body, Fields>>::value,
|
2016-10-15 13:40:17 -04:00
|
|
|
"Writer requirements not met");
|
2016-11-07 06:33:03 -05:00
|
|
|
beast::detail::sync_ostream oss{os};
|
2016-04-29 06:04:40 -04:00
|
|
|
error_code ec;
|
|
|
|
write(oss, msg, ec);
|
|
|
|
if(ec && ec != boost::asio::error::eof)
|
2016-05-07 14:57:15 -04:00
|
|
|
throw system_error{ec};
|
2016-04-29 06:04:40 -04:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
} // http
|
|
|
|
} // beast
|
|
|
|
|
|
|
|
#endif
|