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