Line data Source code
1 : //
2 : // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2025 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/http_proto
9 : //
10 :
11 : #ifndef BOOST_HTTP_PROTO_REQUEST_BASE_HPP
12 : #define BOOST_HTTP_PROTO_REQUEST_BASE_HPP
13 :
14 : #include <boost/http_proto/detail/config.hpp>
15 : #include <boost/http_proto/message_base.hpp>
16 :
17 : #if defined(BOOST_MSVC)
18 : # pragma warning(push)
19 : # pragma warning(disable:4251)
20 : #endif
21 :
22 : namespace boost {
23 : namespace http_proto {
24 :
25 : /** Mixin for modifing HTTP requests.
26 :
27 : @see
28 : @ref message_base,
29 : @ref request,
30 : @ref static_request.
31 : */
32 : class BOOST_HTTP_PROTO_DECL request_base
33 : : public message_base
34 : {
35 : friend class request;
36 : friend class static_request;
37 :
38 66 : request_base() noexcept
39 66 : : message_base(detail::kind::request)
40 : {
41 66 : }
42 :
43 : explicit
44 212 : request_base(core::string_view s)
45 212 : : message_base(detail::kind::request, s)
46 : {
47 211 : }
48 :
49 1062 : request_base(
50 : void* storage,
51 : std::size_t cap) noexcept
52 1062 : : message_base(
53 1062 : detail::kind::request, storage, cap)
54 : {
55 1062 : }
56 :
57 : public:
58 : //--------------------------------------------
59 : //
60 : // Observers
61 : //
62 : //--------------------------------------------
63 :
64 : /** Return the method as a name constant.
65 :
66 : If the method returned is equal to
67 : @ref method::unknown, the method may
68 : be obtained as a string instead, by
69 : calling @ref method_text.
70 : */
71 : http_proto::method
72 78 : method() const noexcept
73 : {
74 78 : return h_.req.method;
75 : }
76 :
77 : /** Return the method as a string.
78 : */
79 : core::string_view
80 87 : method_text() const noexcept
81 : {
82 174 : return core::string_view(
83 87 : h_.cbuf,
84 87 : h_.req.method_len);
85 : }
86 :
87 : /** Return the request-target string.
88 : */
89 : core::string_view
90 74 : target() const noexcept
91 : {
92 148 : return core::string_view(
93 74 : h_.cbuf +
94 74 : h_.req.method_len + 1,
95 74 : h_.req.target_len);
96 : }
97 :
98 : //--------------------------------------------
99 : //
100 : // Modifiers
101 : //
102 : //--------------------------------------------
103 :
104 : /** Set the method of the request to the enum.
105 :
106 : @par Exception Safety
107 : Strong guarantee.
108 : Calls to allocate may throw.
109 : Exception thrown if max capacity exceeded.
110 :
111 : @throw std::length_error
112 : Max capacity would be exceeded.
113 :
114 : @param m The method to set.
115 : */
116 : void
117 2 : set_method(
118 : http_proto::method m)
119 : {
120 2 : set_start_line_impl(
121 : m,
122 : to_string(m),
123 : target(),
124 : version());
125 2 : }
126 :
127 : /** Set the method of the request to the string.
128 :
129 : @par Exception Safety
130 : Strong guarantee.
131 : Calls to allocate may throw.
132 : Exception thrown on invalid input.
133 : Exception thrown if max capacity exceeded.
134 :
135 : @throw system_error
136 : Input is invalid.
137 :
138 : @throw std::length_error
139 : Max capacity would be exceeded.
140 :
141 : @param s A string view representing the
142 : method to set.
143 : */
144 : void
145 5 : set_method(
146 : core::string_view s)
147 : {
148 5 : set_start_line_impl(
149 : string_to_method(s),
150 : s,
151 : target(),
152 : version());
153 5 : }
154 :
155 : /** Set the target string of the request.
156 :
157 : This function sets the request-target.
158 : The caller is responsible for ensuring
159 : that the string passed is syntactically
160 : valid.
161 :
162 : @par Exception Safety
163 : Strong guarantee.
164 : Calls to allocate may throw.
165 : Exception thrown on invalid input.
166 : Exception thrown if max capacity exceeded.
167 :
168 : @throw system_error
169 : Input is invalid.
170 :
171 : @throw std::length_error
172 : Max capacity would be exceeded.
173 :
174 : @param s A string view representing the
175 : target to set.
176 : */
177 : void
178 7 : set_target(
179 : core::string_view s)
180 : {
181 7 : set_start_line_impl(
182 : h_.req.method,
183 : method_text(),
184 : s,
185 : version());
186 6 : }
187 :
188 : /** Set the HTTP version of the request.
189 :
190 : @par Exception Safety
191 : Strong guarantee.
192 : Calls to allocate may throw.
193 : Exception thrown if max capacity exceeded.
194 :
195 : @throw std::length_error
196 : Max capacity would be exceeded.
197 :
198 : @param v The version to set.
199 : */
200 : void
201 2 : set_version(
202 : http_proto::version v)
203 : {
204 2 : set_start_line_impl(
205 : h_.req.method,
206 : method_text(),
207 : target(),
208 : v);
209 2 : }
210 :
211 : /** Set the method, target, and version of the request.
212 :
213 : This is more efficient than setting the
214 : properties individually.
215 :
216 : @par Exception Safety
217 : Strong guarantee.
218 : Calls to allocate may throw.
219 : Exception thrown on invalid input.
220 : Exception thrown if max capacity exceeded.
221 :
222 : @throw system_error
223 : Input is invalid.
224 :
225 : @throw std::length_error
226 : Max capacity would be exceeded.
227 :
228 : @param m The method to set.
229 :
230 : @param t A string view representing the
231 : target to set.
232 :
233 : @param v The version to set.
234 : */
235 : void
236 2 : set_start_line(
237 : http_proto::method m,
238 : core::string_view t,
239 : http_proto::version v =
240 : http_proto::version::http_1_1)
241 : {
242 2 : set_start_line_impl(m, to_string(m), t, v);
243 1 : }
244 :
245 : /** Set the method, target, and version of the request.
246 :
247 : This is more efficient than setting the
248 : properties individually.
249 :
250 : @par Exception Safety
251 : Strong guarantee.
252 : Calls to allocate may throw.
253 : Exception thrown on invalid input.
254 : Exception thrown if max capacity exceeded.
255 :
256 : @throw system_error
257 : Input is invalid.
258 :
259 : @throw std::length_error
260 : Max capacity would be exceeded.
261 :
262 : @param m A string view representing the
263 : method to set.
264 :
265 : @param t A string view representing the
266 : target to set.
267 :
268 : @param v The version to set.
269 : */
270 : void
271 : set_start_line(
272 : core::string_view m,
273 : core::string_view t,
274 : http_proto::version v =
275 : http_proto::version::http_1_1)
276 : {
277 : set_start_line_impl(string_to_method(m), m, t, v);
278 : }
279 :
280 : /** Set the `Expect: 100-continue` header.
281 :
282 : @par Exception Safety
283 : Strong guarantee.
284 : Calls to allocate may throw.
285 : Exception thrown if max capacity exceeded.
286 :
287 : @throw std::length_error
288 : Max capacity would be exceeded.
289 :
290 : @param b If `true` sets `Expect: 100-continue`
291 : header otherwise erase it.
292 : */
293 : void
294 : set_expect_100_continue(bool b);
295 :
296 : private:
297 :
298 : void
299 : set_start_line_impl(
300 : http_proto::method m,
301 : core::string_view ms,
302 : core::string_view t,
303 : http_proto::version v);
304 : };
305 :
306 : #if defined(BOOST_MSVC)
307 : # pragma warning(pop)
308 : #endif
309 :
310 : } // http_proto
311 : } // boost
312 :
313 : #endif
|