]> git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/include/beast/http/string_body.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / Beast / include / beast / http / string_body.hpp
1 //
2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7
8 #ifndef BEAST_HTTP_STRING_BODY_HPP
9 #define BEAST_HTTP_STRING_BODY_HPP
10
11 #include <beast/config.hpp>
12 #include <beast/core/error.hpp>
13 #include <beast/http/message.hpp>
14 #include <beast/core/detail/type_traits.hpp>
15 #include <boost/asio/buffer.hpp>
16 #include <memory>
17 #include <string>
18
19 namespace beast {
20 namespace http {
21
22 /** A Body represented by a std::string.
23
24 Meets the requirements of @b `Body`.
25 */
26 struct string_body
27 {
28 /// The type of the `message::body` member
29 using value_type = std::string;
30
31 #if BEAST_DOXYGEN
32 private:
33 #endif
34
35 class reader
36 {
37 value_type& body_;
38 std::size_t len_ = 0;
39
40 public:
41 static bool constexpr is_direct = true;
42
43 using mutable_buffers_type =
44 boost::asio::mutable_buffers_1;
45
46 template<bool isRequest, class Fields>
47 explicit
48 reader(message<isRequest,
49 string_body, Fields>& m)
50 : body_(m.body)
51 {
52 }
53
54 void
55 init()
56 {
57 }
58
59 void
60 init(std::uint64_t content_length)
61 {
62 if(content_length >
63 (std::numeric_limits<std::size_t>::max)())
64 throw std::length_error{
65 "Content-Length overflow"};
66 body_.reserve(static_cast<
67 std::size_t>(content_length));
68 }
69
70 mutable_buffers_type
71 prepare(std::size_t n)
72 {
73 body_.resize(len_ + n);
74 return {&body_[len_], n};
75 }
76
77 void
78 commit(std::size_t n)
79 {
80 if(body_.size() > len_ + n)
81 body_.resize(len_ + n);
82 len_ = body_.size();
83 }
84
85 void
86 finish()
87 {
88 body_.resize(len_);
89 }
90 };
91
92 class writer
93 {
94 value_type const& body_;
95
96 public:
97 template<bool isRequest, class Fields>
98 explicit
99 writer(message<
100 isRequest, string_body, Fields> const& msg) noexcept
101 : body_(msg.body)
102 {
103 }
104
105 void
106 init(error_code& ec) noexcept
107 {
108 beast::detail::ignore_unused(ec);
109 }
110
111 std::uint64_t
112 content_length() const noexcept
113 {
114 return body_.size();
115 }
116
117 template<class WriteFunction>
118 bool
119 write(error_code&, WriteFunction&& wf) noexcept
120 {
121 wf(boost::asio::buffer(body_));
122 return true;
123 }
124 };
125 };
126
127 } // http
128 } // beast
129
130 #endif