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

142 lines
3.1 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-05-08 12:41:45 -07:00
#include <boost/optional.hpp>
2017-07-20 08:01:46 -07:00
#include <memory>
#include <string>
2017-05-08 12:41:45 -07:00
#include <utility>
2017-07-20 08:01:46 -07:00
namespace beast {
namespace http {
2017-05-08 12:41:45 -07:00
/** An HTTP message body represented by a `std::string`.
Meets the requirements of @b Body.
2017-07-20 08:01:46 -07:00
*/
struct string_body
{
2017-05-08 12:41:45 -07:00
/// The type of the body member when used in a message.
2017-07-20 08:01:46 -07:00
using value_type = std::string;
2017-04-22 14:56:09 -07:00
#if BEAST_DOXYGEN
2017-05-28 09:05:29 -07:00
/// The algorithm used store buffers in this body
using writer = implementation_defined;
2017-05-08 12:41:45 -07:00
#else
2017-05-28 09:05:29 -07:00
class writer
2017-07-20 08:01:46 -07:00
{
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
2017-05-28 09:05:29 -07:00
writer(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)())
2017-05-22 15:30:12 -07:00
BOOST_THROW_EXCEPTION(std::length_error{
"Content-Length overflow"});
2016-11-20 07:32:41 -05:00
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
}
};
2017-05-08 12:41:45 -07:00
#endif
2017-07-20 08:01:46 -07:00
2017-05-08 12:41:45 -07:00
#if BEAST_DOXYGEN
2017-05-28 09:05:29 -07:00
/// The algorithm to obtain buffers representing the body
using reader = implementation_defined;
2017-05-08 12:41:45 -07:00
#else
2017-05-28 09:05:29 -07:00
class reader
2017-07-20 08:01:46 -07:00
{
value_type const& body_;
public:
2017-05-08 12:41:45 -07:00
using is_deferred = std::false_type;
using const_buffers_type =
boost::asio::const_buffers_1;
template<bool isRequest, class Fields>
2017-07-20 08:01:46 -07:00
explicit
2017-05-28 09:05:29 -07:00
reader(message<
2017-05-08 12:41:45 -07:00
isRequest, string_body, Fields> const& msg)
2017-07-20 08:01:46 -07:00
: body_(msg.body)
{
}
void
2017-05-08 12:41:45 -07:00
init(error_code& ec)
2017-07-20 08:01:46 -07:00
{
beast::detail::ignore_unused(ec);
2017-07-20 08:01:46 -07:00
}
std::uint64_t
2017-05-08 12:41:45 -07:00
content_length() const
2017-07-20 08:01:46 -07:00
{
return body_.size();
}
2017-05-08 12:41:45 -07:00
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
2017-07-20 08:01:46 -07:00
{
2017-05-08 12:41:45 -07:00
return {{const_buffers_type{
body_.data(), body_.size()}, false}};
2017-07-20 08:01:46 -07:00
}
};
2017-05-08 12:41:45 -07:00
#endif
2017-07-20 08:01:46 -07:00
};
} // http
} // beast
#endif