ゴミ箱
string_body.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-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 // Official repository: https://github.com/boostorg/beast
8 //
9 
10 #ifndef BOOST_BEAST_HTTP_STRING_BODY_HPP
11 #define BOOST_BEAST_HTTP_STRING_BODY_HPP
12 
13 #include <boost/beast/config.hpp>
17 #include <boost/asio/buffer.hpp>
18 #include <boost/optional.hpp>
19 #include <cstdint>
20 #include <limits>
21 #include <memory>
22 #include <stdexcept>
23 #include <string>
24 #include <utility>
25 
26 namespace boost {
27 namespace beast {
28 namespace http {
29 
36 template<
37  class CharT,
38  class Traits = std::char_traits<CharT>,
39  class Allocator = std::allocator<CharT>>
41 {
42 private:
43  static_assert(
44  std::is_integral<CharT>::value &&
45  sizeof(CharT) == 1,
46  "CharT requirements not met");
47 
48 public:
54  using value_type =
55  std::basic_string<CharT, Traits, Allocator>;
56 
63  static
64  std::uint64_t
66  {
67  return body.size();
68  }
69 
74 #if BOOST_BEAST_DOXYGEN
75  using reader = implementation_defined;
76 #else
77  class reader
78  {
79  value_type const& body_;
80 
81  public:
82  using const_buffers_type =
83  boost::asio::const_buffers_1;
84 
85  template<bool isRequest, class Fields>
86  explicit
87  reader(message<isRequest,
88  basic_string_body, Fields> const& msg)
89  : body_(msg.body)
90  {
91  }
92 
93  void
95  {
96  ec.assign(0, ec.category());
97  }
98 
99  boost::optional<std::pair<const_buffers_type, bool>>
100  get(error_code& ec)
101  {
102  ec.assign(0, ec.category());
103  return {{const_buffers_type{
104  body_.data(), body_.size()}, false}};
105  }
106  };
107 #endif
108 
113 #if BOOST_BEAST_DOXYGEN
114  using writer = implementation_defined;
115 #else
116  class writer
117  {
118  value_type& body_;
119 
120  public:
121  template<bool isRequest, class Fields>
122  explicit
123  writer(message<isRequest,
124  basic_string_body, Fields>& m)
125  : body_(m.body)
126  {
127  }
128 
129  void
131  std::uint64_t> const& length, error_code& ec)
132  {
133  if(length)
134  {
135  if(*length > (
136  std::numeric_limits<std::size_t>::max)())
137  {
139  return;
140  }
141  try
142  {
143  body_.reserve(
144  static_cast<std::size_t>(*length));
145  }
146  catch(std::exception const&)
147  {
149  return;
150  }
151  }
152  ec.assign(0, ec.category());
153  }
154 
155  template<class ConstBufferSequence>
156  std::size_t
158  error_code& ec)
159  {
160  using boost::asio::buffer_size;
161  using boost::asio::buffer_copy;
162  auto const extra = buffer_size(buffers);
163  auto const size = body_.size();
164  try
165  {
166  body_.resize(size + extra);
167  }
168  catch(std::exception const&)
169  {
171  return 0;
172  }
173  ec.assign(0, ec.category());
174  CharT* dest = &body_[size];
175  for(boost::asio::const_buffer b : buffers)
176  {
177  using boost::asio::buffer_cast;
178  auto const len = boost::asio::buffer_size(b);
179  Traits::copy(
180  dest, buffer_cast<CharT const*>(b), len);
181  dest += len;
182  }
183  return extra;
184  }
185 
186  void
188  {
189  ec.assign(0, ec.category());
190  }
191  };
192 #endif
193 };
194 
197 
198 } // http
199 } // beast
200 } // boost
201 
202 #endif
BufferSequence< boost::asio::const_buffer > ConstBufferSequence
Definition: type_traits.hpp:280
reader(message< isRequest, basic_string_body, Fields > const &msg)
Definition: string_body.hpp:87
Definition: async_result.hpp:20
Definition: type_traits.hpp:25
detail::buffers_helper< ConstBufferSequence > buffers(ConstBufferSequence const &b)
Definition: ostream.hpp:50
Definition: string_body.hpp:116
writer(message< isRequest, basic_string_body, Fields > &m)
Definition: string_body.hpp:123
std::size_t put(ConstBufferSequence const &buffers, error_code &ec)
Definition: string_body.hpp:157
Definition: beast_common.hpp:6
boost::system::error_code error_code
The type of error code used by the library.
Definition: error.hpp:21
std::basic_string< CharT, Traits, Allocator > value_type
Definition: string_body.hpp:55
Definition: string_body.hpp:77
void finish(error_code &ec)
Definition: string_body.hpp:187
Definition: string_body.hpp:40
boost::asio::const_buffers_1 const_buffers_type
Definition: string_body.hpp:83
void init(error_code &ec)
Definition: string_body.hpp:94
static std::uint64_t size(value_type const &body)
Definition: string_body.hpp:65
void init(boost::optional< std::uint64_t > const &length, error_code &ec)
Definition: string_body.hpp:130