Files
boost_beast/include/beast/http/string_body.hpp
T

131 lines
2.7 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_STRING_BODY_HPP
#define BEAST_HTTP_STRING_BODY_HPP
#include <beast/config.hpp>
#include <beast/core/error.hpp>
#include <beast/http/message.hpp>
#include <beast/core/detail/type_traits.hpp>
#include <boost/asio/buffer.hpp>
2017-07-20 08:01:46 -07:00
#include <memory>
#include <string>
namespace beast {
namespace http {
/** A Body represented by a std::string.
Meets the requirements of @b `Body`.
2017-07-20 08:01:46 -07:00
*/
struct string_body
{
/// The type of the `message::body` member
using value_type = std::string;
2017-04-22 14:56:09 -07:00
#if BEAST_DOXYGEN
2017-07-20 08:01:46 -07:00
private:
#endif
class reader
{
2016-11-20 07:32:41 -05:00
value_type& body_;
std::size_t len_ = 0;
2017-07-20 08:01:46 -07:00
public:
2016-11-20 07:32:41 -05:00
static bool constexpr is_direct = true;
using mutable_buffers_type =
boost::asio::mutable_buffers_1;
template<bool isRequest, class Fields>
2017-07-20 08:01:46 -07:00
explicit
reader(message<isRequest,
2016-11-20 07:32:41 -05:00
string_body, Fields>& m)
: body_(m.body)
2017-07-20 08:01:46 -07:00
{
}
2016-10-15 09:29:14 -04:00
void
2016-11-20 07:32:41 -05:00
init()
2016-10-15 09:29:14 -04:00
{
}
2017-07-20 08:01:46 -07:00
void
2016-11-20 07:32:41 -05:00
init(std::uint64_t content_length)
2017-07-20 08:01:46 -07:00
{
2016-11-20 07:32:41 -05:00
if(content_length >
(std::numeric_limits<std::size_t>::max)())
throw std::length_error{
"Content-Length overflow"};
body_.reserve(static_cast<
std::size_t>(content_length));
}
mutable_buffers_type
prepare(std::size_t n)
{
body_.resize(len_ + n);
return {&body_[len_], n};
}
void
commit(std::size_t n)
{
if(body_.size() > len_ + n)
body_.resize(len_ + n);
len_ = body_.size();
}
void
finish()
{
body_.resize(len_);
2017-07-20 08:01:46 -07:00
}
};
class writer
{
value_type const& body_;
public:
template<bool isRequest, class Fields>
2017-07-20 08:01:46 -07:00
explicit
writer(message<
isRequest, string_body, Fields> const& msg) noexcept
2017-07-20 08:01:46 -07:00
: body_(msg.body)
{
}
void
2016-10-15 13:40:17 -04:00
init(error_code& ec) noexcept
2017-07-20 08:01:46 -07:00
{
beast::detail::ignore_unused(ec);
2017-07-20 08:01:46 -07:00
}
std::uint64_t
2016-10-15 13:40:17 -04:00
content_length() const noexcept
2017-07-20 08:01:46 -07:00
{
return body_.size();
}
2016-10-15 13:40:17 -04:00
template<class WriteFunction>
bool
write(error_code&, WriteFunction&& wf) noexcept
2017-07-20 08:01:46 -07:00
{
2016-10-15 13:40:17 -04:00
wf(boost::asio::buffer(body_));
2017-07-20 08:01:46 -07:00
return true;
}
};
};
} // http
} // beast
#endif