include/boost/burl/request_head.hpp

100.0% Lines (9/9) 100.0% List of functions (3/3) 100.0% Branches (1/1)
request_head.hpp
f(x) Functions (3)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2021 Vinnie Falco ([email protected])
3 // Copyright (c) 2026 Mohammad Nejati
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/burl
9 //
10
11 #ifndef BOOST_BURL_REQUEST_HEAD_HPP
12 #define BOOST_BURL_REQUEST_HEAD_HPP
13
14 #include <boost/burl/detail/config.hpp>
15 #include <boost/burl/request_head_base.hpp>
16
17 #include <cstddef>
18 #include <string_view>
19
20 namespace boost
21 {
22 namespace burl
23 {
24
25 /** A dynamic container for an HTTP request header.
26
27 This container builds a request header in a single
28 allocation which grows as needed, mirroring
29 @ref fields for the field section while adding the
30 request line. It owns its storage and never runs
31 out of room.
32
33 A default-constructed object holds the default
34 request line ("GET / HTTP/1.1") and no fields;
35 the request line and fields are filled in through
36 the inherited @ref request_head_base interface.
37 Like @ref fields, a default-constructed object is a
38 non-owning view over a shared buffer and does not
39 allocate until it is first modified.
40
41 @ref message_head_base::buffer returns the complete
42 header bytes ready for the wire.
43
44 @see
45 @ref static_request_head,
46 @ref request_head_base.
47 */
48 class request_head : public request_head_base
49 {
50 public:
51 /** Constructor.
52
53 The header holds the default request line and
54 no fields. No allocation is performed until the
55 header is first modified.
56
57 @par Exception Safety
58 No-throw guarantee.
59 */
60 BOOST_BURL_DECL
61 request_head() noexcept;
62
63 /** Destructor.
64
65 Releases the allocation. All views obtained
66 from the header are invalidated.
67 */
68 BOOST_BURL_DECL
69 ~request_head();
70
71 /** Constructor.
72
73 The container acquires ownership of the
74 contents of `other`, which is left in a
75 valid but unspecified state and must not be
76 used except to be destroyed or assigned to.
77
78 @par Complexity
79 Constant.
80 */
81 BOOST_BURL_DECL
82 request_head(request_head&& other) noexcept;
83
84 /** Constructor.
85
86 The contents of `other` are copied into an
87 exact-fit allocation.
88
89 @par Complexity
90 Linear in `other.buffer().size()`.
91
92 @par Exception Safety
93 Strong guarantee. Calls to allocate may throw.
94 */
95 BOOST_BURL_DECL
96 request_head(request_head const& other);
97
98 /** Constructor.
99
100 The contents of `other` are copied into an
101 exact-fit allocation, taking ownership of a
102 header that may otherwise refer to external
103 storage, such as the one produced by
104 @ref head_parser.
105
106 The conversion is lossless and implicit: an
107 owning @ref request_head carries exactly the
108 state of a @ref request_head_base.
109
110 @par Complexity
111 Linear in `other.buffer().size()`.
112
113 @par Exception Safety
114 Strong guarantee. Calls to allocate may throw.
115
116 @param other The header to copy.
117 */
118 BOOST_BURL_DECL
119 request_head(request_head_base const& other);
120
121 /** Constructor.
122
123 The header holds a request line with the
124 given method, target, and version, and no
125 fields.
126
127 @par Exception Safety
128 Strong guarantee. Calls to allocate may throw.
129
130 @throw std::length_error
131 The storage cannot accommodate the request line.
132
133 @param m The method constant. Must not be
134 @ref http::method::unknown.
135
136 @param t The request-target. Must not be
137 empty.
138
139 @param v The version.
140 */
141 80x request_head(
142 http::method m,
143 std::string_view t,
144 http::version v =
145 http::version::http_1_1)
146 80x : request_head()
147 {
148
1/1
✓ Branch 1 taken 80 times.
80x set_start_line(m, t, v);
149 80x }
150
151 /** Assignment.
152
153 The container acquires ownership of the
154 contents of `other`, which is left in a
155 valid but unspecified state. The previous
156 contents are released.
157
158 @par Complexity
159 Constant.
160 */
161 BOOST_BURL_DECL
162 request_head&
163 operator=(request_head&& other) noexcept;
164
165 /** Assignment.
166
167 The contents are replaced with a copy of
168 `other`.
169
170 @par Complexity
171 Linear in `other.buffer().size()`.
172
173 @par Exception Safety
174 Strong guarantee. Calls to allocate may throw.
175 */
176 BOOST_BURL_DECL
177 request_head&
178 operator=(request_head const& other);
179
180 /** Assignment.
181
182 The contents are replaced with a copy of
183 `other`, taking ownership of a header that
184 may otherwise refer to external storage.
185
186 @par Complexity
187 Linear in `other.buffer().size()`.
188
189 @par Exception Safety
190 Strong guarantee. Calls to allocate may throw.
191
192 @param other The header to copy.
193 */
194 BOOST_BURL_DECL
195 request_head&
196 operator=(request_head_base const& other);
197
198 /** Swap the contents.
199
200 The contents of the two headers are
201 exchanged. No allocation occurs and no
202 bytes are copied.
203
204 Views obtained from either header remain
205 valid; they follow the contents into the
206 other header. Iterators are invalidated: an
207 iterator stays bound to the header it was
208 obtained from, which now holds different
209 fields.
210
211 If `this == &other`, this function call has
212 no effect.
213
214 @par Complexity
215 Constant.
216
217 @par Exception Safety
218 No-throw guarantee.
219
220 @param other The header to swap with.
221 */
222 BOOST_BURL_DECL
223 void
224 swap(request_head& other) noexcept;
225
226 /** Swap the contents.
227
228 The contents of the two headers are
229 exchanged. No allocation occurs and no
230 bytes are copied.
231
232 If `&v0 == &v1`, this function call has no
233 effect.
234
235 @par Effects
236 @code
237 v0.swap(v1);
238 @endcode
239
240 @par Complexity
241 Constant.
242
243 @par Exception Safety
244 No-throw guarantee.
245
246 @param v0 The first header to swap.
247
248 @param v1 The second header to swap.
249
250 @see @ref request_head::swap
251 */
252 friend
253 void
254 1x swap(
255 request_head& v0,
256 request_head& v1) noexcept
257 {
258 1x v0.swap(v1);
259 1x }
260
261 /** Reserve storage.
262
263 Ensures the field section can grow to `bytes`
264 bytes and the number of fields to `count`
265 without reallocating. Has no effect if the
266 current allocation is already sufficient.
267
268 The request line shares the allocation with the
269 field section, so a request line larger than the
270 one currently stored may still reallocate.
271
272 All views are invalidated when a reallocation
273 occurs.
274
275 @par Exception Safety
276 Strong guarantee. Calls to allocate may throw.
277
278 @throw std::length_error
279 `bytes` exceeds @ref max_buffer_size, or
280 `count` exceeds @ref max_field_count.
281
282 @param bytes The serialized field-section size,
283 as returned by `fields_base::buffer().size()`.
284
285 @param count The number of fields.
286 */
287 BOOST_BURL_DECL
288 void
289 reserve(
290 std::size_t bytes,
291 std::size_t count);
292
293 /** Remove excess capacity.
294
295 All views are invalidated when a reallocation
296 occurs.
297
298 @par Exception Safety
299 Strong guarantee. Calls to allocate may throw.
300 */
301 BOOST_BURL_DECL
302 void
303 shrink_to_fit();
304
305 private:
306 bool
307 858x static_() const noexcept override
308 {
309 858x return false;
310 }
311 };
312
313 } // namespace burl
314 } // namespace boost
315
316 #endif
317