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

144 lines
3.3 KiB
C++
Raw Normal View History

2016-04-19 20:29:16 -04:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-04-19 20:29:16 -04: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)
//
2016-03-11 06:40:37 -05:00
2016-04-19 20:29:16 -04:00
#ifndef BEAST_HTTP_STRING_BODY_HPP
#define BEAST_HTTP_STRING_BODY_HPP
2016-03-11 06:40:37 -05:00
#include <beast/config.hpp>
#include <beast/http/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>
2016-03-11 06:40:37 -05:00
#include <memory>
#include <string>
2017-05-08 12:41:45 -07:00
#include <utility>
2016-03-11 06:40:37 -05: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.
2016-03-11 06:40:37 -05:00
*/
struct string_body
{
2017-05-08 12:41:45 -07:00
/// The type of the body member when used in a message.
2016-03-11 06:40:37 -05:00
using value_type = std::string;
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
2016-03-11 06:40:37 -05: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>
2016-03-11 06:40:37 -05: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)
2016-03-11 06:40:37 -05:00
: body_(msg.body)
{
}
void
2017-05-08 12:41:45 -07:00
init(error_code& ec)
2016-03-11 06:40:37 -05:00
{
beast::detail::ignore_unused(ec);
2016-03-11 06:40:37 -05:00
}
std::uint64_t
2017-05-08 12:41:45 -07:00
content_length() const
2016-03-11 06:40:37 -05:00
{
return body_.size();
}
2017-05-08 12:41:45 -07:00
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
2016-03-11 06:40:37 -05:00
{
2017-05-08 12:41:45 -07:00
return {{const_buffers_type{
body_.data(), body_.size()}, false}};
2016-03-11 06:40:37 -05:00
}
};
2017-05-08 12:41:45 -07:00
#endif
#if BEAST_DOXYGEN
/// The algorithm used store buffers in this body
using writer = implementation_defined;
#else
class writer
{
value_type& body_;
public:
template<bool isRequest, class Fields>
explicit
writer(message<isRequest, string_body, Fields>& m)
: body_(m.body)
{
}
void
init(boost::optional<
std::uint64_t> content_length, error_code& ec)
{
if(content_length)
{
if(*content_length > (std::numeric_limits<
std::size_t>::max)())
{
ec = make_error_code(
errc::not_enough_memory);
return;
}
body_.reserve(static_cast<
std::size_t>(*content_length));
}
}
template<class ConstBufferSequence>
void
put(ConstBufferSequence const& buffers,
error_code& ec)
{
using boost::asio::buffer_size;
using boost::asio::buffer_copy;
auto const n = buffer_size(buffers);
auto const len = body_.size();
try
{
body_.resize(len + n);
}
catch(std::length_error const&)
{
ec = error::buffer_overflow;
return;
}
buffer_copy(boost::asio::buffer(
&body_[0] + len, n), buffers);
}
void
finish(error_code&)
{
}
};
#endif
2016-03-11 06:40:37 -05:00
};
} // http
} // beast
#endif