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

76 lines
1.8 KiB
C++
Raw Normal View History

2017-06-12 18:39:04 -07:00
//
// Copyright (c) 2013-2017 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_STRING_VIEW_BODY
#define BEAST_HTTP_STRING_VIEW_BODY
#include <beast/config.hpp>
2017-06-16 09:38:53 -07:00
#include <beast/core/string.hpp>
2017-06-12 18:39:04 -07:00
#include <beast/http/message.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/optional.hpp>
#include <utility>
namespace beast {
namespace http {
/** A readable HTTP message body represented by a @ref string_view.
The application must ensure that the memory pointed to
by the string view remains valid for the lifetime of
any attempted operations.
Meets the requirements of @b Body.
*/
struct string_view_body
{
/// The type of the body member when used in a message.
using value_type = string_view;
/// Returns the content length of this body in a message.
static
std::uint64_t
2017-06-15 19:30:43 -07:00
size(value_type const& v)
2017-06-12 18:39:04 -07:00
{
2017-06-15 19:30:43 -07:00
return v.size();
2017-06-12 18:39:04 -07:00
}
#if BEAST_DOXYGEN
/// The algorithm to obtain buffers representing the body
using reader = implementation_defined;
#else
class reader
{
string_view body_;
public:
using const_buffers_type =
boost::asio::const_buffers_1;
template<bool isRequest, class Fields>
explicit
reader(message<isRequest, string_view_body,
Fields> const& m, error_code& ec)
2017-06-12 18:39:04 -07:00
: body_(m.body)
{
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
2017-06-12 18:39:04 -07:00
}
boost::optional<std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
2017-06-12 18:39:04 -07:00
return {{{body_.data(), body_.size()}, false}};
}
};
#endif
};
} // http
} // beast
#endif