ゴミ箱
buffer_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_BUFFER_BODY_HPP
11 #define BOOST_BEAST_HTTP_BUFFER_BODY_HPP
12 
13 #include <boost/beast/config.hpp>
17 #include <boost/optional.hpp>
18 #include <type_traits>
19 #include <utility>
20 
21 namespace boost {
22 namespace beast {
23 namespace http {
24 
33 {
35  struct value_type
36  {
59  void* data = nullptr;
60 
78  std::size_t size = 0;
79 
91  bool more = true;
92  };
93 
98 #if BOOST_BEAST_DOXYGEN
99  using reader = implementation_defined;
100 #else
101  class reader
102  {
103  bool toggle_ = false;
104  value_type const& body_;
105 
106  public:
107  using const_buffers_type =
108  boost::asio::const_buffers_1;
109 
110  template<bool isRequest, class Fields>
111  explicit
112  reader(message<isRequest,
113  buffer_body, Fields> const& msg)
114  : body_(msg.body)
115  {
116  }
117 
118  void
120  {
121  ec.assign(0, ec.category());
122  }
123 
125  std::pair<const_buffers_type, bool>>
126  get(error_code& ec)
127  {
128  if(toggle_)
129  {
130  if(body_.more)
131  {
132  toggle_ = false;
133  ec = error::need_buffer;
134  }
135  else
136  {
137  ec.assign(0, ec.category());
138  }
139  return boost::none;
140  }
141  if(body_.data)
142  {
143  ec.assign(0, ec.category());
144  toggle_ = true;
145  return {{const_buffers_type{
146  body_.data, body_.size}, body_.more}};
147  }
148  if(body_.more)
149  ec = error::need_buffer;
150  else
151  ec.assign(0, ec.category());
152  return boost::none;
153  }
154  };
155 #endif
156 
161 #if BOOST_BEAST_DOXYGEN
162  using writer = implementation_defined;
163 #else
164  class writer
165  {
166  value_type& body_;
167 
168  public:
169  template<bool isRequest, class Fields>
170  explicit
172  : body_(m.body)
173  {
174  }
175 
176  void
177  init(boost::optional<std::uint64_t> const&, error_code& ec)
178  {
179  ec.assign(0, ec.category());
180  }
181 
182  template<class ConstBufferSequence>
183  std::size_t
185  error_code& ec)
186  {
187  using boost::asio::buffer_size;
188  using boost::asio::buffer_copy;
189  if(! body_.data)
190  {
191  ec = error::need_buffer;
192  return 0;
193  }
194  auto const bytes_transferred =
195  buffer_copy(boost::asio::buffer(
196  body_.data, body_.size), buffers);
197  body_.data = reinterpret_cast<char*>(
198  body_.data) + bytes_transferred;
199  body_.size -= bytes_transferred;
200  if(bytes_transferred == buffer_size(buffers))
201  ec.assign(0, ec.category());
202  else
203  ec = error::need_buffer;
204  return bytes_transferred;
205  }
206 
207  void
209  {
210  ec.assign(0, ec.category());
211  }
212  };
213 #endif
214 };
215 
216 #if ! BOOST_BEAST_DOXYGEN
217 // operator<< is not supported for buffer_body
218 template<bool isRequest, class Fields>
220 operator<<(std::ostream& os, message<isRequest,
221  buffer_body, Fields> const& msg) = delete;
222 #endif
223 
224 } // http
225 } // beast
226 } // boost
227 
228 #endif
BufferSequence< boost::asio::const_buffer > ConstBufferSequence
Definition: type_traits.hpp:280
std::ostream & operator<<(std::ostream &os, message< isRequest, basic_file_body< File >, Fields > const &msg)=delete
std::size_t size
Definition: buffer_body.hpp:78
Definition: async_result.hpp:20
detail::ostream_helper< DynamicBuffer, char, std::char_traits< char >, detail::basic_streambuf_movable::value > ostream(DynamicBuffer &buffer)
Definition: ostream.hpp:91
Definition: type_traits.hpp:25
detail::buffers_helper< ConstBufferSequence > buffers(ConstBufferSequence const &b)
Definition: ostream.hpp:50
bool more
Definition: buffer_body.hpp:91
Definition: beast_common.hpp:6
The type of the body member when used in a message.
Definition: buffer_body.hpp:35
std::size_t put(ConstBufferSequence const &buffers, error_code &ec)
Definition: buffer_body.hpp:184
void init(boost::optional< std::uint64_t > const &, error_code &ec)
Definition: buffer_body.hpp:177
boost::system::error_code error_code
The type of error code used by the library.
Definition: error.hpp:21
Definition: buffer_body.hpp:164
void * data
Definition: buffer_body.hpp:59
writer(message< isRequest, buffer_body, Fields > &m)
Definition: buffer_body.hpp:171
void init(error_code &ec)
Definition: buffer_body.hpp:119
reader(message< isRequest, buffer_body, Fields > const &msg)
Definition: buffer_body.hpp:112
boost::asio::const_buffers_1 const_buffers_type
Definition: buffer_body.hpp:108
void finish(error_code &ec)
Definition: buffer_body.hpp:208
Definition: buffer_body.hpp:101
Definition: buffer_body.hpp:32