| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) | ||
| 3 | // Copyright (c) 2024 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_PARSER_HPP | ||
| 12 | #define BOOST_HTTP_PROTO_PARSER_HPP | ||
| 13 | |||
| 14 | #include <boost/http_proto/detail/config.hpp> | ||
| 15 | #include <boost/http_proto/detail/header.hpp> | ||
| 16 | #include <boost/http_proto/detail/type_traits.hpp> | ||
| 17 | #include <boost/http_proto/detail/workspace.hpp> | ||
| 18 | #include <boost/http_proto/error.hpp> | ||
| 19 | #include <boost/http_proto/header_limits.hpp> | ||
| 20 | #include <boost/http_proto/sink.hpp> | ||
| 21 | |||
| 22 | #include <boost/buffers/any_dynamic_buffer.hpp> | ||
| 23 | #include <boost/buffers/circular_buffer.hpp> | ||
| 24 | #include <boost/buffers/flat_buffer.hpp> | ||
| 25 | #include <boost/buffers/mutable_buffer_pair.hpp> | ||
| 26 | #include <boost/buffers/mutable_buffer_span.hpp> | ||
| 27 | #include <boost/buffers/type_traits.hpp> | ||
| 28 | #include <boost/url/grammar/error.hpp> | ||
| 29 | |||
| 30 | #include <cstddef> | ||
| 31 | #include <cstdint> | ||
| 32 | |||
| 33 | namespace boost { | ||
| 34 | namespace http_proto { | ||
| 35 | |||
| 36 | #ifndef BOOST_HTTP_PROTO_DOCS | ||
| 37 | class parser_service; | ||
| 38 | class request_parser; | ||
| 39 | class response_parser; | ||
| 40 | class context; | ||
| 41 | namespace detail { | ||
| 42 | class filter; | ||
| 43 | } // detail | ||
| 44 | #endif | ||
| 45 | |||
| 46 | /** A parser for HTTP/1 messages. | ||
| 47 | |||
| 48 | The parser is strict. Any malformed | ||
| 49 | inputs according to the documented | ||
| 50 | HTTP ABNFs is treated as an | ||
| 51 | unrecoverable error. | ||
| 52 | */ | ||
| 53 | class BOOST_SYMBOL_VISIBLE | ||
| 54 | parser | ||
| 55 | { | ||
| 56 | BOOST_HTTP_PROTO_DECL | ||
| 57 | parser(context& ctx, detail::kind); | ||
| 58 | |||
| 59 | public: | ||
| 60 | /** Parser configuration settings | ||
| 61 | |||
| 62 | @see | ||
| 63 | @li <a href="https://stackoverflow.com/questions/686217/maximum-on-http-header-values" | ||
| 64 | >Maximum on HTTP header values (Stackoverflow)</a> | ||
| 65 | */ | ||
| 66 | struct config_base | ||
| 67 | { | ||
| 68 | header_limits headers; | ||
| 69 | |||
| 70 | /** Largest allowed size for a content body. | ||
| 71 | |||
| 72 | The size of the body is measured | ||
| 73 | after removing any transfer encodings, | ||
| 74 | including a chunked encoding. | ||
| 75 | */ | ||
| 76 | std::uint64_t body_limit = 64 * 1024; | ||
| 77 | |||
| 78 | /** True if parser can decode deflate transfer and content encodings. | ||
| 79 | |||
| 80 | The zlib service must already be | ||
| 81 | installed thusly, or else an exception | ||
| 82 | is thrown. | ||
| 83 | */ | ||
| 84 | bool apply_deflate_decoder = false; | ||
| 85 | |||
| 86 | /** True if parser can decode gzip transfer and content encodings. | ||
| 87 | |||
| 88 | The zlib service must already be | ||
| 89 | installed thusly, or else an exception | ||
| 90 | is thrown. | ||
| 91 | */ | ||
| 92 | bool apply_gzip_decoder = false; | ||
| 93 | |||
| 94 | /** Minimum space for payload buffering. | ||
| 95 | |||
| 96 | This value controls the following | ||
| 97 | settings: | ||
| 98 | |||
| 99 | @li The smallest allocated size of | ||
| 100 | the buffers used for reading | ||
| 101 | and decoding the payload. | ||
| 102 | |||
| 103 | @li The lowest guaranteed size of | ||
| 104 | an in-place body. | ||
| 105 | |||
| 106 | @li The largest size used to reserve | ||
| 107 | space in dynamic buffer bodies | ||
| 108 | when the payload size is not | ||
| 109 | known ahead of time. | ||
| 110 | |||
| 111 | This cannot be zero, and this cannot | ||
| 112 | be greater than @ref body_limit. | ||
| 113 | */ | ||
| 114 | std::size_t min_buffer = 4096; | ||
| 115 | |||
| 116 | /** Largest permissible output size in prepare. | ||
| 117 | |||
| 118 | This cannot be zero. | ||
| 119 | */ | ||
| 120 | std::size_t max_prepare = std::size_t(-1); | ||
| 121 | |||
| 122 | /** Space to reserve for type-erasure. | ||
| 123 | */ | ||
| 124 | std::size_t max_type_erase = 1024; | ||
| 125 | }; | ||
| 126 | |||
| 127 | using mutable_buffers_type = | ||
| 128 | buffers::mutable_buffer_span; | ||
| 129 | |||
| 130 | using const_buffers_type = | ||
| 131 | buffers::const_buffer_span; | ||
| 132 | |||
| 133 | struct stream; | ||
| 134 | |||
| 135 | //-------------------------------------------- | ||
| 136 | // | ||
| 137 | // Special Members | ||
| 138 | // | ||
| 139 | //-------------------------------------------- | ||
| 140 | |||
| 141 | /** Destructor. | ||
| 142 | */ | ||
| 143 | BOOST_HTTP_PROTO_DECL | ||
| 144 | ~parser(); | ||
| 145 | |||
| 146 | /** Constructor (deleted) | ||
| 147 | */ | ||
| 148 | parser(parser&&) = delete; | ||
| 149 | |||
| 150 | /** Assignment (deleted) | ||
| 151 | */ | ||
| 152 | parser& operator=(parser&&) = delete; | ||
| 153 | |||
| 154 | //-------------------------------------------- | ||
| 155 | // | ||
| 156 | // Observers | ||
| 157 | // | ||
| 158 | //-------------------------------------------- | ||
| 159 | |||
| 160 | #if 0 | ||
| 161 | /** Return true if any input was committed. | ||
| 162 | */ | ||
| 163 | bool | ||
| 164 | got_some() const noexcept | ||
| 165 | { | ||
| 166 | return st_ != state::need_start; | ||
| 167 | } | ||
| 168 | #endif | ||
| 169 | |||
| 170 | /** Return true if the complete header was parsed. | ||
| 171 | */ | ||
| 172 | bool | ||
| 173 | 55402 | got_header() const noexcept | |
| 174 | { | ||
| 175 | 55402 | return st_ > state::header; | |
| 176 | } | ||
| 177 | |||
| 178 | /** Returns `true` if a complete message has been parsed. | ||
| 179 | |||
| 180 | Calling @ref reset prepares the parser | ||
| 181 | to process the next message in the stream. | ||
| 182 | |||
| 183 | */ | ||
| 184 | bool | ||
| 185 | 54600 | is_complete() const noexcept | |
| 186 | { | ||
| 187 | 54600 | return st_ >= state::complete_in_place; | |
| 188 | } | ||
| 189 | |||
| 190 | /** Returns `true` if the end of the stream was reached. | ||
| 191 | |||
| 192 | The end of the stream is encountered | ||
| 193 | when one of the following conditions | ||
| 194 | occurs: | ||
| 195 | |||
| 196 | @li @ref commit_eof was called and there | ||
| 197 | is no more data left to parse, or | ||
| 198 | |||
| 199 | @li An unrecoverable error occurred | ||
| 200 | during parsing. | ||
| 201 | |||
| 202 | When the end of stream is reached, the | ||
| 203 | function @ref reset must be called | ||
| 204 | to start parsing a new stream. | ||
| 205 | */ | ||
| 206 | bool | ||
| 207 | 1071 | is_end_of_stream() const noexcept | |
| 208 | { | ||
| 209 | return | ||
| 210 |
2/2✓ Branch 0 taken 874 times.
✓ Branch 1 taken 197 times.
|
1945 | st_ == state::reset || |
| 211 |
3/4✓ Branch 0 taken 874 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 350 times.
✓ Branch 3 taken 524 times.
|
1945 | (st_ >= state::complete_in_place && got_eof_); |
| 212 | } | ||
| 213 | |||
| 214 | //-------------------------------------------- | ||
| 215 | // | ||
| 216 | // Modifiers | ||
| 217 | // | ||
| 218 | //-------------------------------------------- | ||
| 219 | |||
| 220 | /** Prepare for a new stream. | ||
| 221 | */ | ||
| 222 | BOOST_HTTP_PROTO_DECL | ||
| 223 | void | ||
| 224 | reset() noexcept; | ||
| 225 | |||
| 226 | /** Prepare for the next message on the stream. | ||
| 227 | */ | ||
| 228 | void | ||
| 229 | 10303 | start() | |
| 230 | { | ||
| 231 | 10303 | start_impl(false); | |
| 232 | 10298 | } | |
| 233 | |||
| 234 | private: | ||
| 235 | // New message on the current stream | ||
| 236 | BOOST_HTTP_PROTO_DECL void | ||
| 237 | start_impl(bool head_response); | ||
| 238 | public: | ||
| 239 | |||
| 240 | /** Return the input buffer | ||
| 241 | */ | ||
| 242 | BOOST_HTTP_PROTO_DECL | ||
| 243 | mutable_buffers_type | ||
| 244 | prepare(); | ||
| 245 | |||
| 246 | /** Commit bytes to the input buffer | ||
| 247 | */ | ||
| 248 | BOOST_HTTP_PROTO_DECL | ||
| 249 | void | ||
| 250 | commit( | ||
| 251 | std::size_t n); | ||
| 252 | |||
| 253 | /** Indicate there will be no more input | ||
| 254 | |||
| 255 | @par Postconditions | ||
| 256 | All buffer sequences previously obtained | ||
| 257 | by calling @ref prepare are invalidated. | ||
| 258 | */ | ||
| 259 | BOOST_HTTP_PROTO_DECL | ||
| 260 | void | ||
| 261 | commit_eof(); | ||
| 262 | |||
| 263 | /** Parse pending input data | ||
| 264 | */ | ||
| 265 | // VFALCO return result<void>? | ||
| 266 | BOOST_HTTP_PROTO_DECL | ||
| 267 | void | ||
| 268 | parse( | ||
| 269 | system::error_code& ec); | ||
| 270 | |||
| 271 | /** Attach a body. | ||
| 272 | |||
| 273 | This function attaches the specified elastic | ||
| 274 | buffer as the storage for the message body. | ||
| 275 | The parser acquires ownership of the object | ||
| 276 | `eb` and destroys it when: | ||
| 277 | |||
| 278 | @li @ref is_complete returns `true`, or | ||
| 279 | @li @ref reset is called, or | ||
| 280 | @li an unrecoverable parsing error occurs, or | ||
| 281 | @li the parser is destroyed. | ||
| 282 | */ | ||
| 283 | // VFALCO Should this function have | ||
| 284 | // error_code& ec and call parse? | ||
| 285 | template<class ElasticBuffer> | ||
| 286 | #ifndef BOOST_HTTP_PROTO_DOCS | ||
| 287 | typename std::enable_if< | ||
| 288 | ! detail::is_reference_wrapper< | ||
| 289 | ElasticBuffer>::value && | ||
| 290 | ! is_sink<ElasticBuffer>::value>::type | ||
| 291 | #else | ||
| 292 | void | ||
| 293 | #endif | ||
| 294 | set_body(ElasticBuffer&& eb); | ||
| 295 | |||
| 296 | /** Attach a body. | ||
| 297 | |||
| 298 | This function attaches the specified elastic | ||
| 299 | buffer reference as the storage for the message body. | ||
| 300 | Ownership is not transferred; the caller must | ||
| 301 | ensure that the lifetime of the object | ||
| 302 | reference by `eb` extends until: | ||
| 303 | |||
| 304 | @li @ref is_complete returns `true`, or | ||
| 305 | @li @ref reset is called, or | ||
| 306 | @li an unrecoverable parsing error occurs, or | ||
| 307 | @li the parser is destroyed. | ||
| 308 | */ | ||
| 309 | template<class ElasticBuffer> | ||
| 310 | void set_body( | ||
| 311 | std::reference_wrapper<ElasticBuffer> eb); | ||
| 312 | |||
| 313 | /** Attach a body | ||
| 314 | */ | ||
| 315 | template<class Sink> | ||
| 316 | #ifndef BOOST_HTTP_PROTO_DOCS | ||
| 317 | typename std::enable_if< | ||
| 318 | is_sink<Sink>::value, | ||
| 319 | typename std::decay<Sink>::type | ||
| 320 | >::type& | ||
| 321 | #else | ||
| 322 | typename std::decay<Sink>::type& | ||
| 323 | #endif | ||
| 324 | set_body(Sink&& sink); | ||
| 325 | |||
| 326 | /** Return the available body data. | ||
| 327 | |||
| 328 | The returned buffer span will be invalidated if any member | ||
| 329 | function of the parser is subsequently called. | ||
| 330 | */ | ||
| 331 | BOOST_HTTP_PROTO_DECL | ||
| 332 | const_buffers_type | ||
| 333 | pull_body(); | ||
| 334 | |||
| 335 | /** Consumes bytes from the available body data. | ||
| 336 | */ | ||
| 337 | BOOST_HTTP_PROTO_DECL | ||
| 338 | void | ||
| 339 | consume_body(std::size_t n); | ||
| 340 | |||
| 341 | /** Return the complete body as a contiguous character buffer. | ||
| 342 | */ | ||
| 343 | BOOST_HTTP_PROTO_DECL | ||
| 344 | core::string_view | ||
| 345 | body() const noexcept; | ||
| 346 | |||
| 347 | //-------------------------------------------- | ||
| 348 | |||
| 349 | /** Return any leftover data | ||
| 350 | |||
| 351 | This is used to forward unconsumed data | ||
| 352 | that could lie past the last message. | ||
| 353 | For example on a CONNECT request there | ||
| 354 | could be additional protocol-dependent | ||
| 355 | data that we want to retrieve. | ||
| 356 | */ | ||
| 357 | // VFALCO rename to get_leftovers()? | ||
| 358 | BOOST_HTTP_PROTO_DECL | ||
| 359 | core::string_view | ||
| 360 | release_buffered_data() noexcept; | ||
| 361 | |||
| 362 | private: | ||
| 363 | friend class request_parser; | ||
| 364 | friend class response_parser; | ||
| 365 | |||
| 366 | detail::header const* | ||
| 367 | safe_get_header() const; | ||
| 368 | |||
| 369 | bool | ||
| 370 | is_plain() const noexcept; | ||
| 371 | |||
| 372 | void | ||
| 373 | on_headers(system::error_code&); | ||
| 374 | |||
| 375 | BOOST_HTTP_PROTO_DECL | ||
| 376 | void | ||
| 377 | on_set_body(); | ||
| 378 | |||
| 379 | std::size_t | ||
| 380 | apply_filter( | ||
| 381 | system::error_code&, | ||
| 382 | std::size_t, | ||
| 383 | bool); | ||
| 384 | |||
| 385 | static constexpr unsigned buffers_N = 8; | ||
| 386 | |||
| 387 | enum class state | ||
| 388 | { | ||
| 389 | reset, | ||
| 390 | start, | ||
| 391 | header, | ||
| 392 | body, | ||
| 393 | set_body, | ||
| 394 | complete_in_place, | ||
| 395 | complete | ||
| 396 | }; | ||
| 397 | |||
| 398 | enum class how | ||
| 399 | { | ||
| 400 | in_place, | ||
| 401 | sink, | ||
| 402 | elastic, | ||
| 403 | }; | ||
| 404 | |||
| 405 | context& ctx_; | ||
| 406 | parser_service& svc_; | ||
| 407 | |||
| 408 | detail::workspace ws_; | ||
| 409 | detail::header h_; | ||
| 410 | std::size_t body_avail_ = 0; | ||
| 411 | std::uint64_t body_total_ = 0; | ||
| 412 | std::uint64_t payload_remain_ = 0; | ||
| 413 | std::uint64_t chunk_remain_ = 0; | ||
| 414 | std::size_t nprepare_ = 0; | ||
| 415 | |||
| 416 | // used to store initial headers + any potential overread | ||
| 417 | buffers::flat_buffer fb_; | ||
| 418 | |||
| 419 | // used for raw input once headers are read | ||
| 420 | buffers::circular_buffer cb0_; | ||
| 421 | |||
| 422 | // used for transformed output, if applicable | ||
| 423 | // can be empty/null | ||
| 424 | buffers::circular_buffer cb1_; | ||
| 425 | |||
| 426 | // used to provide stable storage when returning | ||
| 427 | // `mutable_buffers_type` from relevant functions | ||
| 428 | buffers::mutable_buffer_pair mbp_; | ||
| 429 | |||
| 430 | // used to provide stable storage when returning | ||
| 431 | // `const_buffers_type` from relevant functions | ||
| 432 | buffers::const_buffer_pair cbp_; | ||
| 433 | |||
| 434 | buffers::any_dynamic_buffer* eb_ = nullptr; | ||
| 435 | detail::filter* filter_ = nullptr; | ||
| 436 | sink* sink_ = nullptr; | ||
| 437 | |||
| 438 | state st_ = state::start; | ||
| 439 | how how_ = how::in_place; | ||
| 440 | bool got_eof_ = false; | ||
| 441 | bool head_response_ = false; | ||
| 442 | bool needs_chunk_close_ = false; | ||
| 443 | bool trailer_headers_ = false; | ||
| 444 | bool chunked_body_ended = false; | ||
| 445 | }; | ||
| 446 | |||
| 447 | //------------------------------------------------ | ||
| 448 | |||
| 449 | /** Install the parser service. | ||
| 450 | */ | ||
| 451 | BOOST_HTTP_PROTO_DECL | ||
| 452 | void | ||
| 453 | install_parser_service( | ||
| 454 | context& ctx, | ||
| 455 | parser::config_base const& cfg); | ||
| 456 | |||
| 457 | } // http_proto | ||
| 458 | } // boost | ||
| 459 | |||
| 460 | #include <boost/http_proto/impl/parser.hpp> | ||
| 461 | |||
| 462 | #endif | ||
| 463 |