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