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

104 lines
2.1 KiB
C++
Raw Normal View History

2016-04-19 20:29:16 -04:00
//
// Copyright (c) 2013-2016 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)
//
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/core/error.hpp>
#include <beast/http/message.hpp>
#include <beast/http/resume_context.hpp>
#include <beast/core/detail/type_traits.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/logic/tribool.hpp>
2016-03-11 06:40:37 -05:00
#include <memory>
#include <string>
namespace beast {
namespace http {
/** A Body represented by a std::string.
Meets the requirements of @b `Body`.
2016-03-11 06:40:37 -05:00
*/
struct string_body
{
2016-04-22 19:02:07 -04:00
/// The type of the `message::body` member
2016-03-11 06:40:37 -05:00
using value_type = std::string;
2016-04-22 19:02:07 -04:00
#if GENERATING_DOCS
private:
#endif
2016-03-11 06:40:37 -05:00
class reader
{
value_type& s_;
public:
template<bool isRequest, class Fields>
2016-03-11 06:40:37 -05:00
explicit
reader(message<isRequest,
string_body, Fields>& m) noexcept
2016-03-11 06:40:37 -05:00
: s_(m.body)
{
}
2016-10-15 09:29:14 -04:00
void
init(error_code&) noexcept
{
}
2016-03-11 06:40:37 -05: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>
2016-03-11 06:40:37 -05:00
explicit
writer(message<
isRequest, string_body, Fields> const& msg) noexcept
2016-03-11 06:40:37 -05:00
: body_(msg.body)
{
}
void
2016-10-15 13:40:17 -04:00
init(error_code& ec) noexcept
2016-03-11 06:40:37 -05:00
{
beast::detail::ignore_unused(ec);
2016-03-11 06:40:37 -05:00
}
std::uint64_t
2016-10-15 13:40:17 -04:00
content_length() const noexcept
2016-03-11 06:40:37 -05:00
{
return body_.size();
}
2016-10-15 13:40:17 -04:00
template<class WriteFunction>
2016-03-11 06:40:37 -05:00
boost::tribool
2016-10-15 13:40:17 -04:00
write(resume_context&&, error_code&,
WriteFunction&& wf) noexcept
2016-03-11 06:40:37 -05:00
{
2016-10-15 13:40:17 -04:00
wf(boost::asio::buffer(body_));
2016-03-11 06:40:37 -05:00
return true;
}
};
};
} // http
} // beast
#endif