forked from boostorg/beast
Return std::size_t from Body::writer::put (API Change)
`put` returns the number of bytes actually transferred from the input buffers. Actions Required: * Return the number of bytes actually transferred from the input buffers in user defined `Body::writer::put` functions.
This commit is contained in:
1 parent
f3afc834b1
commit
c149321013
19 files changed
+179
-55
No files matched your search
@@ -161,25 +161,28 @@ struct buffer_body
|
||||
}
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
void
|
||||
std::size_t
|
||||
put(ConstBufferSequence const& buffers,
|
||||
error_code& ec)
|
||||
{
|
||||
using boost::asio::buffer_size;
|
||||
using boost::asio::buffer_copy;
|
||||
auto const n = buffer_size(buffers);
|
||||
if(! body_.data || n > body_.size)
|
||||
if(! body_.data)
|
||||
{
|
||||
ec = error::need_buffer;
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
auto const bytes_transferred =
|
||||
buffer_copy(boost::asio::buffer(
|
||||
body_.data, body_.size), buffers);
|
||||
body_.data = reinterpret_cast<char*>(
|
||||
body_.data) + bytes_transferred;
|
||||
body_.size -= bytes_transferred;
|
||||
if(bytes_transferred == buffer_size(buffers))
|
||||
ec.assign(0, ec.category());
|
||||
else
|
||||
ec = error::need_buffer;
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <beast/http/error.hpp>
|
||||
#include <beast/http/message.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace beast {
|
||||
@@ -86,26 +87,35 @@ struct basic_dynamic_body
|
||||
}
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
void
|
||||
std::size_t
|
||||
put(ConstBufferSequence const& buffers,
|
||||
error_code& ec)
|
||||
{
|
||||
using boost::asio::buffer_copy;
|
||||
using boost::asio::buffer_size;
|
||||
auto const n = buffer_size(buffers);
|
||||
if(body_.size() > body_.max_size() - n)
|
||||
{
|
||||
ec = error::buffer_overflow;
|
||||
return 0;
|
||||
}
|
||||
boost::optional<typename
|
||||
DynamicBuffer::mutable_buffers_type> b;
|
||||
try
|
||||
{
|
||||
b.emplace(body_.prepare(
|
||||
buffer_size(buffers)));
|
||||
b.emplace(body_.prepare((std::min)(n,
|
||||
body_.max_size() - body_.size())));
|
||||
}
|
||||
catch(std::length_error const&)
|
||||
{
|
||||
ec = error::buffer_overflow;
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
body_.commit(buffer_copy(*b, buffers));
|
||||
auto const bytes_transferred =
|
||||
buffer_copy(*b, buffers);
|
||||
body_.commit(bytes_transferred);
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -85,11 +85,12 @@ struct empty_body
|
||||
}
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
void
|
||||
std::size_t
|
||||
put(ConstBufferSequence const&,
|
||||
error_code& ec)
|
||||
{
|
||||
ec = error::unexpected_body;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -523,12 +523,12 @@ basic_parser<isRequest, Derived>::
|
||||
parse_body(char const*& p,
|
||||
std::size_t n, error_code& ec)
|
||||
{
|
||||
n = beast::detail::clamp(len_, n);
|
||||
impl().on_data(string_view{p, n}, ec);
|
||||
if(ec)
|
||||
return;
|
||||
n = impl().on_data(string_view{p,
|
||||
beast::detail::clamp(len_, n)}, ec);
|
||||
p += n;
|
||||
len_ -= n;
|
||||
if(ec)
|
||||
return;
|
||||
if(len_ > 0)
|
||||
return;
|
||||
impl().on_complete(ec);
|
||||
@@ -550,10 +550,10 @@ parse_body_to_eof(char const*& p,
|
||||
return;
|
||||
}
|
||||
body_limit_ = body_limit_ - n;
|
||||
impl().on_data(string_view{p, n}, ec);
|
||||
n = impl().on_data(string_view{p, n}, ec);
|
||||
p += n;
|
||||
if(ec)
|
||||
return;
|
||||
p += n;
|
||||
}
|
||||
|
||||
template<bool isRequest, class Derived>
|
||||
@@ -701,12 +701,12 @@ basic_parser<isRequest, Derived>::
|
||||
parse_chunk_body(char const*& p,
|
||||
std::size_t n, error_code& ec)
|
||||
{
|
||||
n = beast::detail::clamp(len_, n);
|
||||
impl().on_data(string_view{p, n}, ec);
|
||||
if(ec)
|
||||
return;
|
||||
n = impl().on_data(string_view{p,
|
||||
beast::detail::clamp(len_, n)}, ec);
|
||||
p += n;
|
||||
len_ -= n;
|
||||
if(ec)
|
||||
return;
|
||||
if(len_ > 0)
|
||||
return;
|
||||
state_ = state::chunk_header;
|
||||
|
||||
@@ -288,10 +288,10 @@ private:
|
||||
wr_.emplace(m_, content_length, ec);
|
||||
}
|
||||
|
||||
void
|
||||
std::size_t
|
||||
on_data(string_view s, error_code& ec)
|
||||
{
|
||||
wr_->put(boost::asio::buffer(
|
||||
return wr_->put(boost::asio::buffer(
|
||||
s.data(), s.size()), ec);
|
||||
}
|
||||
|
||||
|
||||
@@ -105,7 +105,7 @@ struct string_body
|
||||
}
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
void
|
||||
std::size_t
|
||||
put(ConstBufferSequence const& buffers,
|
||||
error_code& ec)
|
||||
{
|
||||
@@ -120,10 +120,10 @@ struct string_body
|
||||
catch(std::length_error const&)
|
||||
{
|
||||
ec = error::buffer_overflow;
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
ec.assign(0, ec.category());
|
||||
buffer_copy(boost::asio::buffer(
|
||||
return buffer_copy(boost::asio::buffer(
|
||||
&body_[0] + len, n), buffers);
|
||||
}
|
||||
|
||||
|
||||
@@ -122,9 +122,10 @@ struct is_body_writer : std::false_type {};
|
||||
|
||||
template<class T>
|
||||
struct is_body_writer<T, beast::detail::void_t<decltype(
|
||||
std::declval<typename T::writer&>().put(
|
||||
std::declval<boost::asio::const_buffers_1>(),
|
||||
std::declval<error_code&>()),
|
||||
std::declval<std::size_t&>() =
|
||||
std::declval<typename T::writer&>().put(
|
||||
std::declval<boost::asio::const_buffers_1>(),
|
||||
std::declval<error_code&>()),
|
||||
std::declval<typename T::writer&>().finish(
|
||||
std::declval<error_code&>()),
|
||||
(void)0)>> : std::integral_constant<bool,
|
||||
|
||||
Reference in new issue
Block a user