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