ゴミ箱
span_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_SPAN_BODY_HPP
11 #define BOOST_BEAST_HTTP_SPAN_BODY_HPP
12 
13 #include <boost/beast/config.hpp>
17 #include <boost/optional.hpp>
18 
19 namespace boost {
20 namespace beast {
21 namespace http {
22 
34 template<class T>
35 struct span_body
36 {
37 private:
38  static_assert(std::is_pod<T>::value,
39  "POD requirements not met");
40 
41 public:
48 
55  static
56  std::uint64_t
58  {
59  return body.size();
60  }
61 
66 #if BOOST_BEAST_DOXYGEN
67  using reader = implementation_defined;
68 #else
69  class reader
70  {
71  value_type const& body_;
72 
73  public:
74  using const_buffers_type =
75  boost::asio::const_buffers_1;
76 
77  template<bool isRequest, class Fields>
78  explicit
79  reader(message<isRequest,
80  span_body, Fields> const& msg)
81  : body_(msg.body)
82  {
83  }
84 
85  void
87  {
88  ec.assign(0, ec.category());
89  }
90 
91  boost::optional<std::pair<const_buffers_type, bool>>
92  get(error_code& ec)
93  {
94  ec.assign(0, ec.category());
95  return {{
96  { body_.data(),
97  body_.size() * sizeof(typename
99  false}};
100  }
101  };
102 #endif
103 
108 #if BOOST_BEAST_DOXYGEN
109  using writer = implementation_defined;
110 #else
111  class writer
112  {
113  value_type& body_;
114 
115  public:
116  template<bool isRequest, class Fields>
117  explicit
118  writer(message<isRequest,
119  span_body, Fields>& m)
120  : body_(m.body)
121  {
122  }
123 
124  void
126  std::uint64_t> const& length, error_code& ec)
127  {
128  if(length && *length > body_.size())
129  {
131  return;
132  }
133  ec.assign(0, ec.category());
134  }
135 
136  template<class ConstBufferSequence>
137  std::size_t
139  error_code& ec)
140  {
141  using boost::asio::buffer_size;
142  using boost::asio::buffer_copy;
143  auto const n = buffer_size(buffers);
144  auto const len = body_.size();
145  if(n > len)
146  {
148  return 0;
149  }
150  ec.assign(0, ec.category());
151  buffer_copy(boost::asio::buffer(
152  body_.data(), n), buffers);
153  body_ = value_type{
154  body_.data() + n, body_.size() - n};
155  return n;
156  }
157 
158  void
160  {
161  ec.assign(0, ec.category());
162  }
163  };
164 #endif
165 };
166 
167 } // http
168 } // beast
169 } // boost
170 
171 #endif
BufferSequence< boost::asio::const_buffer > ConstBufferSequence
Definition: type_traits.hpp:280
boost::asio::const_buffers_1 const_buffers_type
Definition: span_body.hpp:75
Definition: async_result.hpp:20
T * data() const
Returns a pointer to the beginning of the span.
Definition: span.hpp:171
Definition: type_traits.hpp:25
detail::buffers_helper< ConstBufferSequence > buffers(ConstBufferSequence const &b)
Definition: ostream.hpp:50
void init(boost::optional< std::uint64_t > const &length, error_code &ec)
Definition: span_body.hpp:125
Definition: span.hpp:32
Definition: beast_common.hpp:6
typename std::remove_const< T >::type value_type
The type of value of each span element.
Definition: span.hpp:42
Definition: span_body.hpp:35
boost::system::error_code error_code
The type of error code used by the library.
Definition: error.hpp:21
std::size_t put(ConstBufferSequence const &buffers, error_code &ec)
Definition: span_body.hpp:138
Definition: span_body.hpp:111
std::size_t size() const
Returns the number of elements in the span.
Definition: span.hpp:178
writer(message< isRequest, span_body, Fields > &m)
Definition: span_body.hpp:118
reader(message< isRequest, span_body, Fields > const &msg)
Definition: span_body.hpp:79
void init(error_code &ec)
Definition: span_body.hpp:86
Definition: span_body.hpp:69
static std::uint64_t size(value_type const &body)
Definition: span_body.hpp:57
void finish(error_code &ec)
Definition: span_body.hpp:159