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

102 lines
2.0 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;
#if GENERATING_DOCS
private:
#endif
class reader
{
value_type& s_;
public:
template<bool isRequest, class Fields>
2017-07-20 08:01:46 -07:00
explicit
reader(message<isRequest,
string_body, Fields>& m) noexcept
2017-07-20 08:01:46 -07:00
: s_(m.body)
{
}
void
init(error_code&) noexcept
{
}
2017-07-20 08:01:46 -07:00
void
write(void const* data,
std::size_t size, error_code&) noexcept
{
auto const n = s_.size();
s_.resize(n + size);
std::memcpy(&s_[n], data, size);
}
};
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
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
content_length() const noexcept
2017-07-20 08:01:46 -07:00
{
return body_.size();
}
template<class WriteFunction>
bool
write(error_code&, WriteFunction&& wf) noexcept
2017-07-20 08:01:46 -07:00
{
wf(boost::asio::buffer(body_));
2017-07-20 08:01:46 -07:00
return true;
}
};
};
} // http
} // beast
#endif