ゴミ箱
basic_dynamic_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_BASIC_DYNAMIC_BODY_HPP
11 #define BOOST_BEAST_HTTP_BASIC_DYNAMIC_BODY_HPP
12 
13 #include <boost/beast/config.hpp>
17 #include <boost/optional.hpp>
18 #include <algorithm>
19 #include <cstdint>
20 #include <utility>
21 
22 namespace boost {
23 namespace beast {
24 namespace http {
25 
32 template<class DynamicBuffer>
34 {
36  "DynamicBuffer requirements not met");
37 
43  using value_type = DynamicBuffer;
44 
51  static
52  std::uint64_t
53  size(value_type const& v)
54  {
55  return v.size();
56  }
57 
62 #if BOOST_BEAST_DOXYGEN
63  using reader = implementation_defined;
64 #else
65  class reader
66  {
67  DynamicBuffer const& body_;
68 
69  public:
70  using const_buffers_type =
71  typename DynamicBuffer::const_buffers_type;
72 
73  template<bool isRequest, class Fields>
74  explicit
75  reader(message<isRequest,
76  basic_dynamic_body, Fields> const& m)
77  : body_(m.body)
78  {
79  }
80 
81  void
83  {
84  ec.assign(0, ec.category());
85  }
86 
87  boost::optional<std::pair<const_buffers_type, bool>>
88  get(error_code& ec)
89  {
90  ec.assign(0, ec.category());
91  return {{body_.data(), false}};
92  }
93  };
94 #endif
95 
100 #if BOOST_BEAST_DOXYGEN
101  using writer = implementation_defined;
102 #else
103  class writer
104  {
105  value_type& body_;
106 
107  public:
108  template<bool isRequest, class Fields>
109  explicit
110  writer(message<isRequest,
111  basic_dynamic_body, Fields>& msg)
112  : body_(msg.body)
113  {
114  }
115 
116  void
118  std::uint64_t> const&, error_code& ec)
119  {
120  ec.assign(0, ec.category());
121  }
122 
123  template<class ConstBufferSequence>
124  std::size_t
126  error_code& ec)
127  {
128  using boost::asio::buffer_copy;
129  using boost::asio::buffer_size;
130  auto const n = buffer_size(buffers);
131  if(body_.size() > body_.max_size() - n)
132  {
134  return 0;
135  }
136  boost::optional<typename
137  DynamicBuffer::mutable_buffers_type> b;
138  try
139  {
140  b.emplace(body_.prepare((std::min)(n,
141  body_.max_size() - body_.size())));
142  }
143  catch(std::length_error const&)
144  {
146  return 0;
147  }
148  ec.assign(0, ec.category());
149  auto const bytes_transferred =
150  buffer_copy(*b, buffers);
151  body_.commit(bytes_transferred);
152  return bytes_transferred;
153  }
154 
155  void
157  {
158  ec.assign(0, ec.category());
159  }
160  };
161 #endif
162 };
163 
164 } // http
165 } // beast
166 } // boost
167 
168 #endif
BufferSequence< boost::asio::const_buffer > ConstBufferSequence
Definition: type_traits.hpp:280
std::size_t put(ConstBufferSequence const &buffers, error_code &ec)
Definition: basic_dynamic_body.hpp:125
reader(message< isRequest, basic_dynamic_body, Fields > const &m)
Definition: basic_dynamic_body.hpp:75
Definition: basic_dynamic_body.hpp:33
Definition: async_result.hpp:20
writer(message< isRequest, basic_dynamic_body, Fields > &msg)
Definition: basic_dynamic_body.hpp:110
Definition: type_traits.hpp:25
detail::buffers_helper< ConstBufferSequence > buffers(ConstBufferSequence const &b)
Definition: ostream.hpp:50
DynamicBuffer value_type
Definition: basic_dynamic_body.hpp:43
Definition: beast_common.hpp:6
Definition: basic_dynamic_body.hpp:65
static std::uint64_t size(value_type const &v)
Definition: basic_dynamic_body.hpp:53
Definition: basic_dynamic_body.hpp:103
void init(error_code &ec)
Definition: basic_dynamic_body.hpp:82
boost::system::error_code error_code
The type of error code used by the library.
Definition: error.hpp:21
void finish(error_code &ec)
Definition: basic_dynamic_body.hpp:156
Definition: type_traits.hpp:134
typename DynamicBuffer::const_buffers_type const_buffers_type
Definition: basic_dynamic_body.hpp:71
void init(boost::optional< std::uint64_t > const &, error_code &ec)
Definition: basic_dynamic_body.hpp:117