Truncate history, version 1.0.0-b2

This commit is contained in:
Vinnie Falco committed 2017-07-20 08:01:46 -07:00
commit 9a3a42a644
204 files changed
+46608

No files matched your search

+299
View File
@@ -0,0 +1,299 @@
//
// Copyright (c) 2013-2016 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)
//
#ifndef BEAST_HTTP_IMPL_BASIC_HEADERS_IPP
#define BEAST_HTTP_IMPL_BASIC_HEADERS_IPP
#include <beast/type_check.hpp>
namespace beast {
namespace http {
namespace detail {
inline
auto
basic_headers_base::begin() const ->
const_iterator
{
return list_.cbegin();
}
inline
auto
basic_headers_base::end() const ->
const_iterator
{
return list_.cend();
}
inline
auto
basic_headers_base::cbegin() const ->
const_iterator
{
return list_.cbegin();
}
inline
auto
basic_headers_base::cend() const ->
const_iterator
{
return list_.cend();
}
} // detail
//------------------------------------------------------------------------------
template<class Allocator>
void
basic_headers<Allocator>::
delete_all()
{
for(auto it = list_.begin(); it != list_.end();)
{
auto& e = *it++;
e.~element();
alloc_traits::deallocate(
this->member(), &e, 1);
}
}
template<class Allocator>
inline
void
basic_headers<Allocator>::
move_assign(basic_headers& other, std::false_type)
{
if(this->member() != other.member())
{
copy_from(other);
other.clear();
}
else
{
set_ = std::move(other.set_);
list_ = std::move(other.list_);
}
}
template<class Allocator>
inline
void
basic_headers<Allocator>::
move_assign(basic_headers& other, std::true_type)
{
this->member() = std::move(other.member());
set_ = std::move(other.set_);
list_ = std::move(other.list_);
}
template<class Allocator>
inline
void
basic_headers<Allocator>::
copy_assign(basic_headers const& other, std::false_type)
{
copy_from(other);
}
template<class Allocator>
inline
void
basic_headers<Allocator>::
copy_assign(basic_headers const& other, std::true_type)
{
this->member() = other.member();
copy_from(other);
}
//------------------------------------------------------------------------------
template<class Allocator>
basic_headers<Allocator>::
~basic_headers()
{
delete_all();
}
template<class Allocator>
basic_headers<Allocator>::
basic_headers(Allocator const& alloc)
: beast::detail::empty_base_optimization<
alloc_type>(alloc)
{
}
template<class Allocator>
basic_headers<Allocator>::
basic_headers(basic_headers&& other)
: beast::detail::empty_base_optimization<alloc_type>(
std::move(other.member()))
, detail::basic_headers_base(
std::move(other.set_), std::move(other.list_))
{
other.list_.clear();
other.set_.clear();
}
template<class Allocator>
auto
basic_headers<Allocator>::
operator=(basic_headers&& other) ->
basic_headers&
{
if(this == &other)
return *this;
clear();
move_assign(other, std::integral_constant<bool,
alloc_traits::propagate_on_container_move_assignment::value>{});
return *this;
}
template<class Allocator>
basic_headers<Allocator>::
basic_headers(basic_headers const& other)
: basic_headers(alloc_traits::
select_on_container_copy_construction(other.member()))
{
copy_from(other);
}
template<class Allocator>
auto
basic_headers<Allocator>::
operator=(basic_headers const& other) ->
basic_headers&
{
clear();
copy_assign(other, std::integral_constant<bool,
alloc_traits::propagate_on_container_copy_assignment::value>{});
return *this;
}
template<class Allocator>
template<class OtherAlloc>
basic_headers<Allocator>::
basic_headers(basic_headers<OtherAlloc> const& other)
{
copy_from(other);
}
template<class Allocator>
template<class OtherAlloc>
auto
basic_headers<Allocator>::
operator=(basic_headers<OtherAlloc> const& other) ->
basic_headers&
{
clear();
copy_from(other);
return *this;
}
template<class Allocator>
template<class FwdIt>
basic_headers<Allocator>::
basic_headers(FwdIt first, FwdIt last)
{
for(;first != last; ++first)
insert(first->name(), first->value());
}
template<class Allocator>
auto
basic_headers<Allocator>::
find(boost::string_ref const& name) const ->
iterator
{
auto const it = set_.find(name, less{});
if(it == set_.end())
return list_.end();
return list_.iterator_to(*it);
}
template<class Allocator>
boost::string_ref
basic_headers<Allocator>::
operator[](boost::string_ref const& name) const
{
// VFALCO This none object looks sketchy
static boost::string_ref const none;
auto const it = find(name);
if(it == end())
return none;
return it->second;
}
template<class Allocator>
void
basic_headers<Allocator>::
clear() noexcept
{
delete_all();
list_.clear();
set_.clear();
}
template<class Allocator>
std::size_t
basic_headers<Allocator>::
erase(boost::string_ref const& name)
{
auto const it = set_.find(name, less{});
if(it == set_.end())
return 0;
auto& e = *it;
set_.erase(set_.iterator_to(e));
list_.erase(list_.iterator_to(e));
alloc_traits::deallocate(this->member(), &e, 1);
return 1;
}
template<class Allocator>
void
basic_headers<Allocator>::
insert(boost::string_ref const& name,
boost::string_ref const& value)
{
typename set_t::insert_commit_data d;
auto const result =
set_.insert_check(name, less{}, d);
if (result.second)
{
auto const p = alloc_traits::allocate(
this->member(), 1);
alloc_traits::construct(
this->member(), p, name, value);
list_.push_back(*p);
set_.insert_commit(*p, d);
return;
}
// If field already exists, insert comma
// separated value as per RFC2616 section 4.2
auto& cur = result.first->data.second;
cur.reserve(cur.size() + 1 + value.size());
cur.append(1, ',');
cur.append(value.data(), value.size());
}
template<class Allocator>
void
basic_headers<Allocator>::
replace(boost::string_ref const& name,
boost::string_ref const& value)
{
erase(name);
insert(name, value);
}
} // http
} // beast
#endif
File diff suppressed because it is too large. Load diff
+318
View File
@@ -0,0 +1,318 @@
//
// Copyright (c) 2013-2016 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)
//
#ifndef BEAST_HTTP_IMPL_MESSAGE_IPP
#define BEAST_HTTP_IMPL_MESSAGE_IPP
#include <beast/http/chunk_encode.hpp>
#include <beast/http/resume_context.hpp>
#include <beast/http/rfc2616.hpp>
#include <beast/write_streambuf.hpp>
#include <beast/type_check.hpp>
#include <beast/http/detail/write_preparation.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/logic/tribool.hpp>
#include <condition_variable>
#include <mutex>
namespace beast {
namespace http {
template<bool isRequest, class Body, class Headers>
message<isRequest, Body, Headers>::
message()
{
}
template<bool isRequest, class Body, class Headers>
message<isRequest, Body, Headers>::
message(request_params params)
{
static_assert(isRequest, "message is not a request");
this->method = params.method;
this->url = std::move(params.url);
version = params.version;
}
template<bool isRequest, class Body, class Headers>
message<isRequest, Body, Headers>::
message(response_params params)
{
static_assert(! isRequest, "message is not a response");
this->status = params.status;
this->reason = std::move(params.reason);
version = params.version;
}
template<bool isRequest, class Body, class Headers>
template<class Streambuf>
void
message<isRequest, Body, Headers>::
write_firstline(Streambuf& streambuf,
std::true_type) const
{
write(streambuf, to_string(this->method));
write(streambuf, " ");
write(streambuf, this->url);
switch(version)
{
case 10:
write(streambuf, " HTTP/1.0\r\n");
break;
case 11:
write(streambuf, " HTTP/1.1\r\n");
break;
default:
write(streambuf, " HTTP/");
write(streambuf, version / 10);
write(streambuf, ".");
write(streambuf, version % 10);
write(streambuf, "\r\n");
break;
}
}
template<bool isRequest, class Body, class Headers>
template<class Streambuf>
void
message<isRequest, Body, Headers>::
write_firstline(Streambuf& streambuf,
std::false_type) const
{
switch(version)
{
case 10:
write(streambuf, "HTTP/1.0 ");
break;
case 11:
write(streambuf, "HTTP/1.1 ");
break;
default:
write(streambuf, " HTTP/");
write(streambuf, version / 10);
write(streambuf, ".");
write(streambuf, version % 10);
write(streambuf, " ");
break;
}
write(streambuf, this->status);
write(streambuf, " ");
write(streambuf, this->reason);
write(streambuf, "\r\n");
}
namespace detail {
template<class ConstBufferSequence>
std::string
buffers_to_string(ConstBufferSequence const& buffers)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(buffers));
for(auto const& b : buffers)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
}
class writef_ostream
{
std::ostream& os_;
bool chunked_;
public:
writef_ostream(std::ostream& os, bool chunked)
: os_(os)
, chunked_(chunked)
{
}
template<class ConstBufferSequence>
void
operator()(ConstBufferSequence const& buffers)
{
if(chunked_)
os_ << buffers_to_string(
chunk_encode(buffers));
else
os_ << buffers_to_string(
buffers);
}
};
} // detail
// Diagnostic output only
template<bool isRequest, class Body, class Headers>
std::ostream&
operator<<(std::ostream& os,
message<isRequest, Body, Headers> const& msg)
{
error_code ec;
detail::write_preparation<isRequest, Body, Headers> wp(msg);
wp.init(ec);
if(ec)
return os;
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;
os << detail::buffers_to_string(wp.sb.data());
wp.sb.consume(wp.sb.size());
detail::writef_ostream writef(os, wp.chunked);
for(;;)
{
{
auto result = wp.w(std::move(copy), ec, writef);
if(ec)
return os;
if(result)
break;
if(boost::indeterminate(result))
{
copy = resume;
std::unique_lock<std::mutex> lock(m);
cv.wait(lock, [&]{ return ready; });
ready = false;
}
}
wp.sb.consume(wp.sb.size());
for(;;)
{
auto result = wp.w(std::move(copy), ec, writef);
if(ec)
return os;
if(result)
break;
if(boost::indeterminate(result))
{
copy = resume;
std::unique_lock<std::mutex> lock(m);
cv.wait(lock, [&]{ return ready; });
ready = false;
}
}
}
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
os << detail::buffers_to_string(chunk_encode_final());
if(ec)
return os;
}
os << std::endl;
return os;
}
//------------------------------------------------------------------------------
template<bool isRequest, class Body, class Headers>
void
set_connection(bool keep_alive,
message<isRequest, Body, Headers>& req)
{
if(req.version >= 11)
{
if(! keep_alive)
req.headers.replace("Connection", "close");
else
req.headers.erase("Connection");
}
else
{
if(keep_alive)
req.headers.replace("Connection", "keep-alive");
else
req.headers.erase("Connection");
}
}
template<class Body, class Headers,
class OtherBody, class OtherAllocator>
void
set_connection(bool keep_alive,
message<false, Body, Headers>& resp,
message<true, OtherBody, OtherAllocator> const& req)
{
if(req.version >= 11)
{
if(rfc2616::token_in_list(req["Connection"], "close"))
keep_alive = false;
}
else
{
if(! rfc2616::token_in_list(req["Connection"], "keep-alive"))
keep_alive = false;
}
set_connection(keep_alive, resp);
}
template<class Streambuf, class FieldSequence>
void
write_fields(Streambuf& streambuf, FieldSequence const& fields)
{
static_assert(is_Streambuf<Streambuf>::value,
"Streambuf requirements not met");
//static_assert(is_FieldSequence<FieldSequence>::value,
// "FieldSequence requirements not met");
for(auto const& field : fields)
{
write(streambuf, field.name());
write(streambuf, ": ");
write(streambuf, field.value());
write(streambuf, "\r\n");
}
}
template<bool isRequest, class Body, class Headers>
bool
is_keep_alive(message<isRequest, Body, Headers> const& msg)
{
if(msg.version >= 11)
{
if(rfc2616::token_in_list(
msg.headers["Connection"], "close"))
return false;
return true;
}
if(rfc2616::token_in_list(
msg.headers["Connection"], "keep-alive"))
return true;
return false;
}
template<bool isRequest, class Body, class Headers>
bool
is_upgrade(message<isRequest, Body, Headers> const& msg)
{
if(msg.version < 11)
return false;
if(rfc2616::token_in_list(
msg.headers["Connection"], "upgrade"))
return true;
return false;
}
} // http
} // beast
#include <beast/http/impl/message.ipp>
#endif
+286
View File
@@ -0,0 +1,286 @@
//
// Copyright (c) 2013-2016 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)
//
#ifndef BEAST_HTTP_IMPL_READ_IPP_HPP
#define BEAST_HTTP_IMPL_READ_IPP_HPP
#include <beast/bind_handler.hpp>
#include <beast/handler_alloc.hpp>
#include <cassert>
namespace beast {
namespace http {
namespace detail {
template<class Stream, class Streambuf,
bool isRequest, class Body, class Headers,
class Handler>
class read_op
{
using alloc_type =
handler_alloc<char, Handler>;
using parser_type =
parser<isRequest, Body, Headers>;
using message_type =
message<isRequest, Body, Headers>;
struct data
{
Stream& s;
Streambuf& sb;
message_type& m;
parser_type p;
Handler h;
bool started = false;
bool cont;
int state = 0;
template<class DeducedHandler>
data(DeducedHandler&& h_, Stream& s_,
Streambuf& sb_, message_type& m_)
: s(s_)
, sb(sb_)
, m(m_)
, h(std::forward<DeducedHandler>(h_))
, cont(boost_asio_handler_cont_helpers::
is_continuation(h))
{
}
};
std::shared_ptr<data> d_;
public:
read_op(read_op&&) = default;
read_op(read_op const&) = default;
template<class DeducedHandler, class... Args>
read_op(DeducedHandler&& h, Stream&s, Args&&... args)
: d_(std::allocate_shared<data>(alloc_type{h},
std::forward<DeducedHandler>(h), s,
std::forward<Args>(args)...))
{
(*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, read_op* op)
{
return boost_asio_handler_alloc_helpers::
allocate(size, op->d_->h);
}
friend
void asio_handler_deallocate(
void* p, std::size_t size, read_op* op)
{
return boost_asio_handler_alloc_helpers::
deallocate(p, size, op->d_->h);
}
friend
bool asio_handler_is_continuation(read_op* op)
{
return op->d_->cont;
}
template <class Function>
friend
void asio_handler_invoke(Function&& f, read_op* op)
{
return boost_asio_handler_invoke_helpers::
invoke(f, op->d_->h);
}
};
template<class Stream, class Streambuf,
bool isRequest, class Body, class Headers,
class Handler>
void
read_op<Stream, Streambuf, isRequest, Body, Headers, Handler>::
operator()(error_code ec, std::size_t bytes_transferred, bool again)
{
auto& d = *d_;
d.cont = d.cont || again;
while(d.state != 99)
{
switch(d.state)
{
case 0:
{
auto const used =
d.p.write(d.sb.data(), ec);
if(ec)
{
// call handler
d.state = 99;
d.s.get_io_service().post(
bind_handler(std::move(*this), ec, 0));
return;
}
if(used > 0)
d.started = true;
d.sb.consume(used);
if(d.p.complete())
{
// call handler
d.state = 99;
d.m = d.p.release();
d.s.get_io_service().post(
bind_handler(std::move(*this), ec, 0));
return;
}
d.state = 1;
break;
}
case 1:
// read
d.state = 2;
d.s.async_read_some(d.sb.prepare(
read_size_helper(d.sb, 65536)),
std::move(*this));
return;
// got data
case 2:
{
if(ec == boost::asio::error::eof)
{
if(! d.started)
{
// call handler
d.state = 99;
break;
}
// Caller will see eof on next read.
ec = {};
d.p.write_eof(ec);
if(! ec)
{
assert(d.p.complete());
d.m = d.p.release();
}
// call handler
d.state = 99;
break;
}
if(ec)
{
// call handler
d.state = 99;
break;
}
d.sb.commit(bytes_transferred);
d.sb.consume(d.p.write(d.sb.data(), ec));
if(ec)
{
// call handler
d.state = 99;
break;
}
if(d.p.complete())
{
// call handler
d.state = 99;
d.m = d.p.release();
break;
}
d.state = 1;
break;
}
}
}
d.h(ec);
}
} // detail
//------------------------------------------------------------------------------
template<class SyncReadStream, class Streambuf,
bool isRequest, class Body, class Headers>
void
read(SyncReadStream& stream, Streambuf& streambuf,
message<isRequest, Body, Headers>& m,
error_code& ec)
{
static_assert(is_SyncReadStream<SyncReadStream>::value,
"SyncReadStream requirements not met");
static_assert(is_Streambuf<Streambuf>::value,
"Streambuf requirements not met");
parser<isRequest, Body, Headers> p;
bool started = false;
for(;;)
{
auto used =
p.write(streambuf.data(), ec);
if(ec)
return;
streambuf.consume(used);
if(used > 0)
started = true;
if(p.complete())
{
m = p.release();
break;
}
streambuf.commit(stream.read_some(
streambuf.prepare(read_size_helper(
streambuf, 65536)), ec));
if(ec && ec != boost::asio::error::eof)
return;
if(ec == boost::asio::error::eof)
{
if(! started)
return;
// Caller will see eof on next read.
ec = {};
p.write_eof(ec);
if(ec)
return;
assert(p.complete());
m = p.release();
break;
}
}
}
template<class AsyncReadStream, class Streambuf,
bool isRequest, class Body, class Headers,
class ReadHandler>
typename async_completion<
ReadHandler, void(error_code)>::result_type
async_read(AsyncReadStream& stream, Streambuf& streambuf,
message<isRequest, Body, Headers>& m,
ReadHandler&& handler)
{
static_assert(is_AsyncReadStream<AsyncReadStream>::value,
"AsyncReadStream requirements not met");
static_assert(is_Streambuf<Streambuf>::value,
"Streambuf requirements not met");
beast::async_completion<ReadHandler,
void(error_code)> completion(handler);
detail::read_op<AsyncReadStream, Streambuf,
isRequest, Body, Headers, decltype(
completion.handler)>{completion.handler,
stream, streambuf, m};
return completion.result.get();
}
} // http
} // beast
#endif
+456
View File
@@ -0,0 +1,456 @@
//
// Copyright (c) 2013-2016 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)
//
#ifndef BEAST_HTTP_IMPL_WRITE_IPP
#define BEAST_HTTP_IMPL_WRITE_IPP
#include <beast/http/chunk_encode.hpp>
#include <beast/http/resume_context.hpp>
#include <beast/http/detail/write_preparation.hpp>
#include <beast/buffer_cat.hpp>
#include <beast/bind_handler.hpp>
#include <beast/handler_alloc.hpp>
#include <beast/streambuf.hpp>
#include <beast/type_check.hpp>
#include <boost/asio/write.hpp>
#include <boost/logic/tribool.hpp>
#include <condition_variable>
#include <mutex>
#include <type_traits>
namespace beast {
namespace http {
namespace detail {
template<class Stream, class Handler,
bool isRequest, class Body, class Headers>
class write_op
{
using alloc_type =
handler_alloc<char, Handler>;
struct data
{
Stream& s;
// VFALCO How do we use handler_alloc in write_preparation?
write_preparation<
isRequest, Body, Headers> wp;
Handler h;
resume_context resume;
resume_context copy;
bool cont;
int state = 0;
template<class DeducedHandler>
data(DeducedHandler&& h_, Stream& s_,
message<isRequest, Body, Headers> const& m_)
: s(s_)
, wp(m_)
, h(std::forward<DeducedHandler>(h_))
, cont(boost_asio_handler_cont_helpers::
is_continuation(h))
{
}
};
class writef0
{
write_op& self_;
public:
explicit
writef0(write_op& self)
: self_(self)
{
}
template<class ConstBufferSequence>
void operator()(ConstBufferSequence const& buffers)
{
auto& d = *self_.d_;
// write headers and body
if(d.wp.chunked)
boost::asio::async_write(d.s,
buffer_cat(d.wp.sb.data(),
chunk_encode(buffers)),
std::move(self_));
else
boost::asio::async_write(d.s,
buffer_cat(d.wp.sb.data(),
buffers), std::move(self_));
}
};
class writef
{
write_op& self_;
public:
explicit
writef(write_op& self)
: self_(self)
{
}
template<class ConstBufferSequence>
void operator()(ConstBufferSequence const& buffers)
{
auto& d = *self_.d_;
// write body
if(d.wp.chunked)
boost::asio::async_write(d.s,
chunk_encode(buffers),
std::move(self_));
else
boost::asio::async_write(d.s,
buffers, std::move(self_));
}
};
std::shared_ptr<data> d_;
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)
: d_(std::allocate_shared<data>(alloc_type{h},
std::forward<DeducedHandler>(h), s,
std::forward<Args>(args)...))
{
auto& d = *d_;
auto sp = d_;
d.resume = {
[sp]() mutable
{
write_op self(std::move(sp));
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
write_op(std::shared_ptr<data> d)
: 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)
{
return boost_asio_handler_alloc_helpers::
allocate(size, op->d_->h);
}
friend
void asio_handler_deallocate(
void* p, std::size_t size, write_op* op)
{
return boost_asio_handler_alloc_helpers::
deallocate(p, size, op->d_->h);
}
friend
bool asio_handler_is_continuation(write_op* op)
{
return op->d_->cont;
}
template <class Function>
friend
void asio_handler_invoke(Function&& f, write_op* op)
{
return boost_asio_handler_invoke_helpers::
invoke(f, op->d_->h);
}
};
template<class Stream, class Handler,
bool isRequest, class Body, class Headers>
void
write_op<Stream, Handler, isRequest, Body, Headers>::
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:
{
auto const result = d.wp.w(
std::move(d.copy), ec, writef0{*this});
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;
}
// sent headers and body
case 2:
d.wp.sb.consume(d.wp.sb.size());
d.state = 3;
break;
case 3:
{
auto const result = d.wp.w(
std::move(d.copy), ec, writef{*this});
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,
chunk_encode_final(), std::move(*this));
return;
case 5:
if(d.wp.close)
{
// VFALCO TODO Decide on an error code
ec = boost::asio::error::eof;
}
d.state = 99;
break;
}
}
d.h(ec);
d.resume = {};
d.copy = {};
}
template<class SyncWriteStream, class Streambuf>
class writef0_write
{
Streambuf const& sb_;
SyncWriteStream& stream_;
bool chunked_;
error_code& ec_;
public:
writef0_write(SyncWriteStream& stream,
Streambuf const& sb, bool chunked, error_code& ec)
: sb_(sb)
, stream_(stream)
, chunked_(chunked)
, ec_(ec)
{
}
template<class ConstBufferSequence>
void operator()(ConstBufferSequence const& buffers)
{
// write headers and body
if(chunked_)
boost::asio::write(stream_, buffer_cat(
sb_.data(), chunk_encode(buffers)), ec_);
else
boost::asio::write(stream_, buffer_cat(
sb_.data(), buffers), ec_);
}
};
template<class SyncWriteStream>
class writef_write
{
SyncWriteStream& stream_;
bool chunked_;
error_code& ec_;
public:
writef_write(SyncWriteStream& stream,
bool chunked, error_code& ec)
: stream_(stream)
, chunked_(chunked)
, ec_(ec)
{
}
template<class ConstBufferSequence>
void operator()(ConstBufferSequence const& buffers)
{
// write body
if(chunked_)
boost::asio::write(stream_,
chunk_encode(buffers), ec_);
else
boost::asio::write(stream_, buffers, ec_);
}
};
} // detail
//------------------------------------------------------------------------------
template<class SyncWriteStream,
bool isRequest, class Body, class Headers>
void
write(SyncWriteStream& stream,
message<isRequest, Body, Headers> const& msg,
boost::system::error_code& ec)
{
detail::write_preparation<isRequest, Body, Headers> wp(msg);
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;
for(;;)
{
{
auto result = wp.w(std::move(copy), ec,
detail::writef0_write<SyncWriteStream, decltype(wp.sb)>{
stream, wp.sb, wp.chunked, ec});
if(ec)
return;
if(result)
break;
if(boost::indeterminate(result))
{
boost::asio::write(stream, wp.sb.data(), ec);
if(ec)
return;
wp.sb.consume(wp.sb.size());
copy = resume;
std::unique_lock<std::mutex> lock(m);
cv.wait(lock, [&]{ return ready; });
ready = false;
}
}
wp.sb.consume(wp.sb.size());
for(;;)
{
auto result = wp.w(std::move(copy), ec,
detail::writef_write<SyncWriteStream>{
stream, wp.chunked, ec});
if(ec)
return;
if(result)
break;
if(boost::indeterminate(result))
{
copy = resume;
std::unique_lock<std::mutex> lock(m);
cv.wait(lock, [&]{ return ready; });
ready = false;
}
}
}
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
boost::asio::write(stream, chunk_encode_final(), ec);
if(ec)
return;
}
if(wp.close)
{
// VFALCO TODO Decide on an error code
ec = boost::asio::error::eof;
}
}
template<class AsyncWriteStream,
bool isRequest, class Body, class Headers,
class WriteHandler>
typename async_completion<
WriteHandler, void(error_code)>::result_type
async_write(AsyncWriteStream& stream,
message<isRequest, Body, Headers> const& msg,
WriteHandler&& handler)
{
static_assert(
is_AsyncWriteStream<AsyncWriteStream>::value,
"AsyncWriteStream requirements not met");
beast::async_completion<WriteHandler,
void(error_code)> completion(handler);
detail::write_op<AsyncWriteStream, decltype(completion.handler),
isRequest, Body, Headers>{completion.handler, stream, msg};
return completion.result.get();
}
} // http
} // beast
#endif