]>
Commit | Line | Data |
---|---|---|
b32b8144 | 1 | // |
92f5a8d4 | 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
b32b8144 FG |
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/core/detail/config.hpp> | |
92f5a8d4 TL |
14 | #include <boost/beast/core/buffer_traits.hpp> |
15 | #include <boost/beast/core/detail/buffer.hpp> | |
16 | #include <boost/beast/core/detail/clamp.hpp> | |
b32b8144 FG |
17 | #include <boost/beast/http/error.hpp> |
18 | #include <boost/beast/http/message.hpp> | |
19 | #include <boost/optional.hpp> | |
20 | #include <algorithm> | |
21 | #include <cstdint> | |
22 | #include <utility> | |
23 | ||
24 | namespace boost { | |
25 | namespace beast { | |
26 | namespace http { | |
27 | ||
92f5a8d4 | 28 | /** A <em>Body</em> using a <em>DynamicBuffer</em> |
b32b8144 | 29 | |
92f5a8d4 | 30 | This body uses a <em>DynamicBuffer</em> as a memory-based container |
b32b8144 FG |
31 | for holding message payloads. Messages using this body type |
32 | may be serialized and parsed. | |
33 | */ | |
34 | template<class DynamicBuffer> | |
35 | struct basic_dynamic_body | |
36 | { | |
37 | static_assert( | |
92f5a8d4 TL |
38 | net::is_dynamic_buffer<DynamicBuffer>::value, |
39 | "DynamicBuffer type requirements not met"); | |
b32b8144 FG |
40 | |
41 | /** The type of container used for the body | |
42 | ||
43 | This determines the type of @ref message::body | |
44 | when this body type is used with a message container. | |
45 | */ | |
46 | using value_type = DynamicBuffer; | |
47 | ||
48 | /** Returns the payload size of the body | |
49 | ||
50 | When this body is used with @ref message::prepare_payload, | |
51 | the Content-Length will be set to the payload size, and | |
52 | any chunked Transfer-Encoding will be removed. | |
53 | */ | |
54 | static | |
55 | std::uint64_t | |
56 | size(value_type const& v) | |
57 | { | |
58 | return v.size(); | |
59 | } | |
60 | ||
61 | /** The algorithm for parsing the body | |
62 | ||
92f5a8d4 | 63 | Meets the requirements of <em>BodyReader</em>. |
b32b8144 FG |
64 | */ |
65 | #if BOOST_BEAST_DOXYGEN | |
92f5a8d4 | 66 | using reader = __implementation_defined__; |
b32b8144 FG |
67 | #else |
68 | class reader | |
69 | { | |
70 | value_type& body_; | |
71 | ||
72 | public: | |
73 | template<bool isRequest, class Fields> | |
74 | explicit | |
11fdf7f2 TL |
75 | reader(header<isRequest, Fields>&, value_type& b) |
76 | : body_(b) | |
b32b8144 FG |
77 | { |
78 | } | |
79 | ||
80 | void | |
81 | init(boost::optional< | |
82 | std::uint64_t> const&, error_code& ec) | |
83 | { | |
92f5a8d4 | 84 | ec = {}; |
b32b8144 FG |
85 | } |
86 | ||
87 | template<class ConstBufferSequence> | |
88 | std::size_t | |
89 | put(ConstBufferSequence const& buffers, | |
90 | error_code& ec) | |
91 | { | |
92f5a8d4 TL |
92 | auto const n = buffer_bytes(buffers); |
93 | if(beast::detail::sum_exceeds(body_.size(), n, body_.max_size())) | |
b32b8144 FG |
94 | { |
95 | ec = error::buffer_overflow; | |
96 | return 0; | |
97 | } | |
92f5a8d4 TL |
98 | auto const mb = |
99 | beast::detail::dynamic_buffer_prepare( | |
100 | body_, (std::min)(n, | |
101 | body_.max_size() - body_.size()), | |
102 | ec, error::buffer_overflow); | |
103 | if(ec) | |
b32b8144 | 104 | return 0; |
b32b8144 | 105 | auto const bytes_transferred = |
92f5a8d4 | 106 | net::buffer_copy(*mb, buffers); |
b32b8144 FG |
107 | body_.commit(bytes_transferred); |
108 | return bytes_transferred; | |
109 | } | |
110 | ||
111 | void | |
112 | finish(error_code& ec) | |
113 | { | |
92f5a8d4 | 114 | ec = {}; |
b32b8144 FG |
115 | } |
116 | }; | |
117 | #endif | |
118 | ||
119 | /** The algorithm for serializing the body | |
120 | ||
92f5a8d4 | 121 | Meets the requirements of <em>BodyWriter</em>. |
b32b8144 FG |
122 | */ |
123 | #if BOOST_BEAST_DOXYGEN | |
92f5a8d4 | 124 | using writer = __implementation_defined__; |
b32b8144 FG |
125 | #else |
126 | class writer | |
127 | { | |
128 | DynamicBuffer const& body_; | |
129 | ||
130 | public: | |
131 | using const_buffers_type = | |
132 | typename DynamicBuffer::const_buffers_type; | |
133 | ||
134 | template<bool isRequest, class Fields> | |
135 | explicit | |
11fdf7f2 TL |
136 | writer(header<isRequest, Fields> const&, value_type const& b) |
137 | : body_(b) | |
b32b8144 FG |
138 | { |
139 | } | |
140 | ||
141 | void | |
142 | init(error_code& ec) | |
143 | { | |
92f5a8d4 | 144 | ec = {}; |
b32b8144 FG |
145 | } |
146 | ||
147 | boost::optional<std::pair<const_buffers_type, bool>> | |
148 | get(error_code& ec) | |
149 | { | |
92f5a8d4 | 150 | ec = {}; |
b32b8144 FG |
151 | return {{body_.data(), false}}; |
152 | } | |
153 | }; | |
154 | #endif | |
155 | }; | |
156 | ||
157 | } // http | |
158 | } // beast | |
159 | } // boost | |
160 | ||
161 | #endif |