| 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 | #include <boost/http_proto/detail/header.hpp> | ||
| 12 | #include <boost/http_proto/detail/align_up.hpp> | ||
| 13 | #include <boost/http_proto/field.hpp> | ||
| 14 | #include <boost/http_proto/fields_view_base.hpp> | ||
| 15 | #include <boost/http_proto/header_limits.hpp> | ||
| 16 | #include <boost/http_proto/rfc/list_rule.hpp> | ||
| 17 | #include <boost/http_proto/rfc/token_rule.hpp> | ||
| 18 | #include <boost/http_proto/rfc/upgrade_rule.hpp> | ||
| 19 | #include <boost/http_proto/rfc/detail/rules.hpp> | ||
| 20 | #include <boost/url/grammar/ci_string.hpp> | ||
| 21 | #include <boost/url/grammar/parse.hpp> | ||
| 22 | #include <boost/url/grammar/range_rule.hpp> | ||
| 23 | #include <boost/url/grammar/recycled.hpp> | ||
| 24 | #include <boost/url/grammar/unsigned_rule.hpp> | ||
| 25 | #include <boost/assert.hpp> | ||
| 26 | #include <boost/assert/source_location.hpp> | ||
| 27 | #include <boost/static_assert.hpp> | ||
| 28 | #include <string> | ||
| 29 | #include <utility> | ||
| 30 | |||
| 31 | #include "../rfc/transfer_encoding_rule.hpp" | ||
| 32 | |||
| 33 | namespace boost { | ||
| 34 | namespace http_proto { | ||
| 35 | namespace detail { | ||
| 36 | |||
| 37 | //------------------------------------------------ | ||
| 38 | |||
| 39 | auto | ||
| 40 | 115 | header:: | |
| 41 | entry:: | ||
| 42 | operator+( | ||
| 43 | std::size_t dv) const noexcept -> | ||
| 44 | entry | ||
| 45 | { | ||
| 46 | return { | ||
| 47 | static_cast< | ||
| 48 | 115 | offset_type>(np + dv), | |
| 49 | 115 | nn, | |
| 50 | static_cast< | ||
| 51 | 115 | offset_type>(vp + dv), | |
| 52 | 115 | vn, | |
| 53 | 115 | id }; | |
| 54 | } | ||
| 55 | |||
| 56 | auto | ||
| 57 | 79 | header:: | |
| 58 | entry:: | ||
| 59 | operator-( | ||
| 60 | std::size_t dv) const noexcept -> | ||
| 61 | entry | ||
| 62 | { | ||
| 63 | return { | ||
| 64 | static_cast< | ||
| 65 | 79 | offset_type>(np - dv), | |
| 66 | 79 | nn, | |
| 67 | static_cast< | ||
| 68 | 79 | offset_type>(vp - dv), | |
| 69 | 79 | vn, | |
| 70 | 79 | id }; | |
| 71 | } | ||
| 72 | |||
| 73 | //------------------------------------------------ | ||
| 74 | |||
| 75 | constexpr | ||
| 76 | header:: | ||
| 77 | header(fields_tag) noexcept | ||
| 78 | : kind(detail::kind::fields) | ||
| 79 | , cbuf("\r\n") | ||
| 80 | , size(2) | ||
| 81 | , fld{} | ||
| 82 | { | ||
| 83 | } | ||
| 84 | |||
| 85 | constexpr | ||
| 86 | header:: | ||
| 87 | header(request_tag) noexcept | ||
| 88 | : kind(detail::kind::request) | ||
| 89 | , cbuf("GET / HTTP/1.1\r\n\r\n") | ||
| 90 | , size(18) | ||
| 91 | , prefix(16) | ||
| 92 | , req{ 3, 1, | ||
| 93 | http_proto::method::get } | ||
| 94 | { | ||
| 95 | } | ||
| 96 | |||
| 97 | constexpr | ||
| 98 | header:: | ||
| 99 | header(response_tag) noexcept | ||
| 100 | : kind(detail::kind::response) | ||
| 101 | , cbuf("HTTP/1.1 200 OK\r\n\r\n") | ||
| 102 | , size(19) | ||
| 103 | , prefix(17) | ||
| 104 | , res{ 200, | ||
| 105 | http_proto::status::ok } | ||
| 106 | { | ||
| 107 | } | ||
| 108 | |||
| 109 | //------------------------------------------------ | ||
| 110 | |||
| 111 | header const* | ||
| 112 | 249 | header:: | |
| 113 | get_default(detail::kind k) noexcept | ||
| 114 | { | ||
| 115 | static constexpr header h[3] = { | ||
| 116 | fields_tag{}, | ||
| 117 | request_tag{}, | ||
| 118 | response_tag{}}; | ||
| 119 | 249 | return &h[k]; | |
| 120 | } | ||
| 121 | |||
| 122 | 11889 | header:: | |
| 123 | 11889 | header(empty v) noexcept | |
| 124 | 11889 | : kind(v.param) | |
| 125 | { | ||
| 126 | 11889 | } | |
| 127 | |||
| 128 | 230 | header:: | |
| 129 | 230 | header(detail::kind k) noexcept | |
| 130 | 230 | : header(*get_default(k)) | |
| 131 | { | ||
| 132 | 230 | } | |
| 133 | |||
| 134 | void | ||
| 135 | 74 | header:: | |
| 136 | swap(header& h) noexcept | ||
| 137 | { | ||
| 138 | 74 | std::swap(cbuf, h.cbuf); | |
| 139 | 74 | std::swap(buf, h.buf); | |
| 140 | 74 | std::swap(cap, h.cap); | |
| 141 | 74 | std::swap(max_cap, h.max_cap); | |
| 142 | 74 | std::swap(size, h.size); | |
| 143 | 74 | std::swap(count, h.count); | |
| 144 | 74 | std::swap(prefix, h.prefix); | |
| 145 | 74 | std::swap(version, h.version); | |
| 146 | 74 | std::swap(md, h.md); | |
| 147 |
3/3✓ Branch 0 taken 18 times.
✓ Branch 1 taken 47 times.
✓ Branch 2 taken 9 times.
|
74 | switch(kind) |
| 148 | { | ||
| 149 | 18 | default: | |
| 150 | case detail::kind::fields: | ||
| 151 | 18 | break; | |
| 152 | 47 | case detail::kind::request: | |
| 153 | 47 | std::swap( | |
| 154 | 47 | req.method_len, h.req.method_len); | |
| 155 | 47 | std::swap( | |
| 156 | 47 | req.target_len, h.req.target_len); | |
| 157 | 47 | std::swap(req.method, h.req.method); | |
| 158 | 47 | break; | |
| 159 | 9 | case detail::kind::response: | |
| 160 | 9 | std::swap( | |
| 161 | 9 | res.status_int, h.res.status_int); | |
| 162 | 9 | std::swap(res.status, h.res.status); | |
| 163 | 9 | break; | |
| 164 | } | ||
| 165 | 74 | } | |
| 166 | |||
| 167 | /* References: | ||
| 168 | |||
| 169 | 6.3. Persistence | ||
| 170 | https://datatracker.ietf.org/doc/html/rfc7230#section-6.3 | ||
| 171 | */ | ||
| 172 | bool | ||
| 173 | 22 | header:: | |
| 174 | keep_alive() const noexcept | ||
| 175 | { | ||
| 176 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21 times.
|
22 | if(md.payload == payload::error) |
| 177 | 1 | return false; | |
| 178 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8 times.
|
21 | if( version == |
| 179 | http_proto::version::http_1_1) | ||
| 180 | { | ||
| 181 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10 times.
|
13 | if(md.connection.close) |
| 182 | 3 | return false; | |
| 183 | } | ||
| 184 | else | ||
| 185 | { | ||
| 186 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if(! md.connection.keep_alive) |
| 187 | 4 | return false; | |
| 188 | } | ||
| 189 | // can't use to_eof in requests | ||
| 190 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
14 | BOOST_ASSERT( |
| 191 | kind != detail::kind::request || | ||
| 192 | md.payload != payload::to_eof); | ||
| 193 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
|
14 | if(md.payload == payload::to_eof) |
| 194 | 3 | return false; | |
| 195 | 11 | return true; | |
| 196 | } | ||
| 197 | |||
| 198 | //------------------------------------------------ | ||
| 199 | |||
| 200 | // return total bytes needed | ||
| 201 | // to store message of `size` | ||
| 202 | // bytes and `count` fields. | ||
| 203 | std::size_t | ||
| 204 | 911 | header:: | |
| 205 | bytes_needed( | ||
| 206 | std::size_t size, | ||
| 207 | std::size_t count) noexcept | ||
| 208 | { | ||
| 209 | // make sure `size` is big enough | ||
| 210 | // to hold the largest default buffer: | ||
| 211 | // "HTTP/1.1 200 OK\r\n\r\n" | ||
| 212 |
2/2✓ Branch 0 taken 173 times.
✓ Branch 1 taken 738 times.
|
911 | if( size < 19) |
| 213 | 173 | size = 19; | |
| 214 | static constexpr auto A = | ||
| 215 | alignof(header::entry); | ||
| 216 | 911 | return align_up(size, A) + | |
| 217 | 911 | (count * sizeof( | |
| 218 | 911 | header::entry)); | |
| 219 | } | ||
| 220 | |||
| 221 | std::size_t | ||
| 222 | 9822 | header:: | |
| 223 | table_space( | ||
| 224 | std::size_t count) noexcept | ||
| 225 | { | ||
| 226 | return count * | ||
| 227 | 9822 | sizeof(header::entry); | |
| 228 | } | ||
| 229 | |||
| 230 | std::size_t | ||
| 231 | 9822 | header:: | |
| 232 | table_space() const noexcept | ||
| 233 | { | ||
| 234 | 9822 | return table_space(count); | |
| 235 | } | ||
| 236 | |||
| 237 | auto | ||
| 238 | 15200 | header:: | |
| 239 | tab() const noexcept -> | ||
| 240 | table | ||
| 241 | { | ||
| 242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15200 times.
|
15200 | BOOST_ASSERT(cap > 0); |
| 243 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15200 times.
|
15200 | BOOST_ASSERT(buf != nullptr); |
| 244 | 15200 | return table(buf + cap); | |
| 245 | } | ||
| 246 | |||
| 247 | auto | ||
| 248 | 857 | header:: | |
| 249 | tab_() const noexcept -> | ||
| 250 | entry* | ||
| 251 | { | ||
| 252 | return reinterpret_cast< | ||
| 253 | 857 | entry*>(buf + cap); | |
| 254 | } | ||
| 255 | |||
| 256 | // return true if header cbuf is a default | ||
| 257 | bool | ||
| 258 | 43 | header:: | |
| 259 | is_default() const noexcept | ||
| 260 | { | ||
| 261 | 43 | return buf == nullptr; | |
| 262 | } | ||
| 263 | |||
| 264 | std::size_t | ||
| 265 | 4294 | header:: | |
| 266 | find( | ||
| 267 | field id) const noexcept | ||
| 268 | { | ||
| 269 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4288 times.
|
4294 | if(count == 0) |
| 270 | 6 | return 0; | |
| 271 | 4288 | std::size_t i = 0; | |
| 272 | 4288 | auto const* p = &tab()[0]; | |
| 273 |
1/2✓ Branch 0 taken 4333 times.
✗ Branch 1 not taken.
|
4333 | while(i < count) |
| 274 | { | ||
| 275 |
2/2✓ Branch 0 taken 4288 times.
✓ Branch 1 taken 45 times.
|
4333 | if(p->id == id) |
| 276 | 4288 | break; | |
| 277 | 45 | ++i; | |
| 278 | 45 | --p; | |
| 279 | } | ||
| 280 | 4288 | return i; | |
| 281 | } | ||
| 282 | |||
| 283 | std::size_t | ||
| 284 | 68 | header:: | |
| 285 | find( | ||
| 286 | core::string_view name) const noexcept | ||
| 287 | { | ||
| 288 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 15 times.
|
68 | if(count == 0) |
| 289 | 53 | return 0; | |
| 290 | 15 | std::size_t i = 0; | |
| 291 | 15 | auto const* p = &tab()[0]; | |
| 292 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 | while(i < count) |
| 293 | { | ||
| 294 | core::string_view s( | ||
| 295 | 21 | cbuf + prefix + p->np, | |
| 296 | 21 | p->nn); | |
| 297 |
2/2✓ Branch 1 taken 15 times.
✓ Branch 2 taken 6 times.
|
21 | if(grammar::ci_is_equal(s, name)) |
| 298 | 15 | break; | |
| 299 | 6 | ++i; | |
| 300 | 6 | --p; | |
| 301 | } | ||
| 302 | 15 | return i; | |
| 303 | } | ||
| 304 | |||
| 305 | void | ||
| 306 | 30 | header:: | |
| 307 | copy_table( | ||
| 308 | void* dest, | ||
| 309 | std::size_t n) const noexcept | ||
| 310 | { | ||
| 311 | 30 | std::memcpy( | |
| 312 | reinterpret_cast< | ||
| 313 | 30 | entry*>(dest) - n, | |
| 314 | reinterpret_cast< | ||
| 315 | entry const*>( | ||
| 316 | 30 | cbuf + cap) - n, | |
| 317 | n * sizeof(entry)); | ||
| 318 | 30 | } | |
| 319 | |||
| 320 | void | ||
| 321 | 30 | header:: | |
| 322 | copy_table( | ||
| 323 | void* dest) const noexcept | ||
| 324 | { | ||
| 325 | 30 | copy_table(dest, count); | |
| 326 | 30 | } | |
| 327 | |||
| 328 | // assign all the members but | ||
| 329 | // preserve the allocated memory | ||
| 330 | void | ||
| 331 | 30 | header:: | |
| 332 | assign_to( | ||
| 333 | header& dest) const noexcept | ||
| 334 | { | ||
| 335 | 30 | auto const buf_ = dest.buf; | |
| 336 | 30 | auto const cbuf_ = dest.cbuf; | |
| 337 | 30 | auto const cap_ = dest.cap; | |
| 338 | 30 | dest = *this; | |
| 339 | 30 | dest.buf = buf_; | |
| 340 | 30 | dest.cbuf = cbuf_; | |
| 341 | 30 | dest.cap = cap_; | |
| 342 | 30 | } | |
| 343 | |||
| 344 | //------------------------------------------------ | ||
| 345 | // | ||
| 346 | // Metadata | ||
| 347 | // | ||
| 348 | //------------------------------------------------ | ||
| 349 | |||
| 350 | std::size_t | ||
| 351 | ✗ | header:: | |
| 352 | maybe_count( | ||
| 353 | field id) const noexcept | ||
| 354 | { | ||
| 355 | ✗ | if(kind == detail::kind::fields) | |
| 356 | ✗ | return std::size_t(-1); | |
| 357 | ✗ | switch(id) | |
| 358 | { | ||
| 359 | ✗ | case field::connection: | |
| 360 | ✗ | return md.connection.count; | |
| 361 | ✗ | case field::content_encoding: | |
| 362 | ✗ | return md.content_encoding.count; | |
| 363 | ✗ | case field::content_length: | |
| 364 | ✗ | return md.content_length.count; | |
| 365 | ✗ | case field::expect: | |
| 366 | ✗ | return md.expect.count; | |
| 367 | ✗ | case field::transfer_encoding: | |
| 368 | ✗ | return md.transfer_encoding.count; | |
| 369 | ✗ | case field::upgrade: | |
| 370 | ✗ | return md.upgrade.count; | |
| 371 | ✗ | default: | |
| 372 | ✗ | break; | |
| 373 | } | ||
| 374 | ✗ | return std::size_t(-1); | |
| 375 | } | ||
| 376 | |||
| 377 | bool | ||
| 378 | 21 | header:: | |
| 379 | is_special( | ||
| 380 | field id) const noexcept | ||
| 381 | { | ||
| 382 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 17 times.
|
21 | if(kind == detail::kind::fields) |
| 383 | 4 | return false; | |
| 384 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8 times.
|
17 | switch(id) |
| 385 | { | ||
| 386 | 9 | case field::connection: | |
| 387 | case field::content_encoding: | ||
| 388 | case field::content_length: | ||
| 389 | case field::expect: | ||
| 390 | case field::transfer_encoding: | ||
| 391 | case field::upgrade: | ||
| 392 | 9 | return true; | |
| 393 | 8 | default: | |
| 394 | 8 | break; | |
| 395 | } | ||
| 396 | 8 | return false; | |
| 397 | } | ||
| 398 | |||
| 399 | //------------------------------------------------ | ||
| 400 | |||
| 401 | // called when the start-line changes | ||
| 402 | void | ||
| 403 | 10593 | header:: | |
| 404 | on_start_line() | ||
| 405 | { | ||
| 406 | // items in both the request-line | ||
| 407 | // and the status-line can affect | ||
| 408 | // the payload, for example whether | ||
| 409 | // or not EOF marks the end of the | ||
| 410 | // payload. | ||
| 411 | |||
| 412 | 10593 | update_payload(); | |
| 413 | 10593 | } | |
| 414 | |||
| 415 | // called after a field is inserted | ||
| 416 | void | ||
| 417 | 11752 | header:: | |
| 418 | on_insert( | ||
| 419 | field id, | ||
| 420 | core::string_view v) | ||
| 421 | { | ||
| 422 |
2/2✓ Branch 0 taken 510 times.
✓ Branch 1 taken 11242 times.
|
11752 | if(kind == detail::kind::fields) |
| 423 | 510 | return; | |
| 424 |
7/7✓ Branch 0 taken 78 times.
✓ Branch 1 taken 4853 times.
✓ Branch 2 taken 147 times.
✓ Branch 3 taken 47 times.
✓ Branch 4 taken 4275 times.
✓ Branch 5 taken 24 times.
✓ Branch 6 taken 1818 times.
|
11242 | switch(id) |
| 425 | { | ||
| 426 | 78 | case field::content_encoding: | |
| 427 | 78 | return on_insert_content_encoding(v); | |
| 428 | 4853 | case field::content_length: | |
| 429 | 4853 | return on_insert_content_length(v); | |
| 430 | 147 | case field::connection: | |
| 431 | 147 | return on_insert_connection(v); | |
| 432 | 47 | case field::expect: | |
| 433 | 47 | return on_insert_expect(v); | |
| 434 | 4275 | case field::transfer_encoding: | |
| 435 | 4275 | return on_insert_transfer_encoding(); | |
| 436 | 24 | case field::upgrade: | |
| 437 | 24 | return on_insert_upgrade(v); | |
| 438 | 1818 | default: | |
| 439 | 1818 | break; | |
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | // called when one field is erased | ||
| 444 | void | ||
| 445 | 40 | header:: | |
| 446 | on_erase(field id) | ||
| 447 | { | ||
| 448 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 37 times.
|
40 | if(kind == detail::kind::fields) |
| 449 | 3 | return; | |
| 450 |
6/7✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 5 times.
|
37 | switch(id) |
| 451 | { | ||
| 452 | 9 | case field::connection: | |
| 453 | 9 | return on_erase_connection(); | |
| 454 | ✗ | case field::content_encoding: | |
| 455 | ✗ | return on_erase_content_encoding(); | |
| 456 | 4 | case field::content_length: | |
| 457 | 4 | return on_erase_content_length(); | |
| 458 | 10 | case field::expect: | |
| 459 | 10 | return on_erase_expect(); | |
| 460 | 5 | case field::transfer_encoding: | |
| 461 | 5 | return on_erase_transfer_encoding(); | |
| 462 | 4 | case field::upgrade: | |
| 463 | 4 | return on_erase_upgrade(); | |
| 464 | 5 | default: | |
| 465 | 5 | break; | |
| 466 | } | ||
| 467 | } | ||
| 468 | |||
| 469 | //------------------------------------------------ | ||
| 470 | |||
| 471 | /* | ||
| 472 | https://datatracker.ietf.org/doc/html/rfc7230#section-6.1 | ||
| 473 | */ | ||
| 474 | void | ||
| 475 | 151 | header:: | |
| 476 | on_insert_connection( | ||
| 477 | core::string_view v) | ||
| 478 | { | ||
| 479 | 151 | ++md.connection.count; | |
| 480 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 150 times.
|
151 | if(md.connection.ec.failed()) |
| 481 | 5 | return; | |
| 482 | auto rv = grammar::parse( | ||
| 483 |
1/2✓ Branch 2 taken 150 times.
✗ Branch 3 not taken.
|
150 | v, list_rule(token_rule, 1)); |
| 484 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 146 times.
|
150 | if(! rv) |
| 485 | { | ||
| 486 | 4 | md.connection.ec = | |
| 487 | 8 | BOOST_HTTP_PROTO_ERR( | |
| 488 | error::bad_connection); | ||
| 489 | 4 | return; | |
| 490 | } | ||
| 491 | 146 | md.connection.ec = {}; | |
| 492 |
2/2✓ Branch 6 taken 157 times.
✓ Branch 7 taken 146 times.
|
303 | for(auto t : *rv) |
| 493 | { | ||
| 494 |
2/2✓ Branch 2 taken 107 times.
✓ Branch 3 taken 50 times.
|
157 | if(grammar::ci_is_equal( |
| 495 | t, "close")) | ||
| 496 | 107 | md.connection.close = true; | |
| 497 |
2/2✓ Branch 2 taken 26 times.
✓ Branch 3 taken 24 times.
|
50 | else if(grammar::ci_is_equal( |
| 498 | t, "keep-alive")) | ||
| 499 | 26 | md.connection.keep_alive = true; | |
| 500 |
2/2✓ Branch 2 taken 19 times.
✓ Branch 3 taken 5 times.
|
24 | else if(grammar::ci_is_equal( |
| 501 | t, "upgrade")) | ||
| 502 | 19 | md.connection.upgrade = true; | |
| 503 | } | ||
| 504 |
2/2✓ Branch 1 taken 146 times.
✓ Branch 2 taken 4 times.
|
150 | } |
| 505 | |||
| 506 | void | ||
| 507 | 4854 | header:: | |
| 508 | on_insert_content_length( | ||
| 509 | core::string_view v) | ||
| 510 | { | ||
| 511 | static | ||
| 512 | constexpr | ||
| 513 | grammar::unsigned_rule< | ||
| 514 | std::uint64_t> num_rule{}; | ||
| 515 | |||
| 516 | 4854 | ++md.content_length.count; | |
| 517 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4852 times.
|
4854 | if(md.content_length.ec.failed()) |
| 518 | 4671 | return; | |
| 519 | auto rv = | ||
| 520 | 4852 | grammar::parse(v, num_rule); | |
| 521 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4847 times.
|
4852 | if(! rv) |
| 522 | { | ||
| 523 | // parse failure | ||
| 524 | 5 | md.content_length.ec = | |
| 525 | 10 | BOOST_HTTP_PROTO_ERR( | |
| 526 | error::bad_content_length); | ||
| 527 | 5 | md.content_length.value = 0; | |
| 528 | 5 | update_payload(); | |
| 529 | 5 | return; | |
| 530 | } | ||
| 531 |
2/2✓ Branch 0 taken 4657 times.
✓ Branch 1 taken 190 times.
|
4847 | if(md.content_length.count == 1) |
| 532 | { | ||
| 533 | // one value | ||
| 534 | 4657 | md.content_length.ec = {}; | |
| 535 | 4657 | md.content_length.value = *rv; | |
| 536 | 4657 | update_payload(); | |
| 537 | 4657 | return; | |
| 538 | } | ||
| 539 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 183 times.
|
190 | if(*rv == md.content_length.value) |
| 540 | { | ||
| 541 | // ok: duplicate value | ||
| 542 | 7 | return; | |
| 543 | } | ||
| 544 | // bad: different values | ||
| 545 | 183 | md.content_length.ec = | |
| 546 | 366 | BOOST_HTTP_PROTO_ERR( | |
| 547 | error::multiple_content_length); | ||
| 548 | 183 | md.content_length.value = 0; | |
| 549 | 183 | update_payload(); | |
| 550 | } | ||
| 551 | |||
| 552 | void | ||
| 553 | 53 | header:: | |
| 554 | on_insert_expect( | ||
| 555 | core::string_view v) | ||
| 556 | { | ||
| 557 | 53 | ++md.expect.count; | |
| 558 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 45 times.
|
53 | if(kind != detail::kind::request) |
| 559 | 8 | return; | |
| 560 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 41 times.
|
45 | if(md.expect.ec.failed()) |
| 561 | 4 | return; | |
| 562 | // VFALCO Should we allow duplicate | ||
| 563 | // Expect fields that have 100-continue? | ||
| 564 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 9 times.
|
73 | if( md.expect.count > 1 || |
| 565 |
4/4✓ Branch 2 taken 10 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 19 times.
✓ Branch 5 taken 22 times.
|
73 | ! grammar::ci_is_equal(v, |
| 566 | "100-continue")) | ||
| 567 | { | ||
| 568 | 19 | md.expect.ec = | |
| 569 | 38 | BOOST_HTTP_PROTO_ERR( | |
| 570 | error::bad_expect); | ||
| 571 | 19 | md.expect.is_100_continue = false; | |
| 572 | 19 | return; | |
| 573 | } | ||
| 574 | 22 | md.expect.is_100_continue = true; | |
| 575 | } | ||
| 576 | |||
| 577 | void | ||
| 578 | 4278 | header:: | |
| 579 | on_insert_transfer_encoding() | ||
| 580 | { | ||
| 581 | 4278 | ++md.transfer_encoding.count; | |
| 582 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4273 times.
|
4278 | if(md.transfer_encoding.ec.failed()) |
| 583 | 5 | return; | |
| 584 | 4273 | auto const n = | |
| 585 | md.transfer_encoding.count; | ||
| 586 | 4273 | md.transfer_encoding = {}; | |
| 587 | 4273 | md.transfer_encoding.count = n; | |
| 588 | 4273 | for(auto s : | |
| 589 | fields_view_base::subrange( | ||
| 590 |
2/2✓ Branch 7 taken 4305 times.
✓ Branch 8 taken 4262 times.
|
12840 | this, find(field::transfer_encoding))) |
| 591 | { | ||
| 592 | auto rv = grammar::parse( | ||
| 593 |
1/2✓ Branch 1 taken 4305 times.
✗ Branch 2 not taken.
|
4305 | s, transfer_encoding_rule); |
| 594 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4301 times.
|
4305 | if(! rv) |
| 595 | { | ||
| 596 | // parse error | ||
| 597 | 4 | md.transfer_encoding.ec = | |
| 598 | 8 | BOOST_HTTP_PROTO_ERR( | |
| 599 | error::bad_transfer_encoding); | ||
| 600 | 4 | md.transfer_encoding.codings = 0; | |
| 601 | 4 | md.transfer_encoding.is_chunked = false; | |
| 602 | 4 | update_payload(); | |
| 603 | 4 | return; | |
| 604 | } | ||
| 605 | 4301 | md.transfer_encoding.codings += rv->size(); | |
| 606 |
2/2✓ Branch 7 taken 4310 times.
✓ Branch 8 taken 4294 times.
|
8604 | for(auto t : *rv) |
| 607 | { | ||
| 608 | 4310 | auto& mte = md.transfer_encoding; | |
| 609 | |||
| 610 |
2/2✓ Branch 0 taken 4306 times.
✓ Branch 1 taken 4 times.
|
4310 | if(! mte.is_chunked ) |
| 611 | { | ||
| 612 |
2/2✓ Branch 0 taken 4205 times.
✓ Branch 1 taken 101 times.
|
4306 | if( t.id == transfer_encoding::chunked ) |
| 613 | { | ||
| 614 | 4205 | mte.is_chunked = true; | |
| 615 | 4205 | continue; | |
| 616 | } | ||
| 617 | |||
| 618 | 101 | auto b = | |
| 619 | 101 | mte.encoding == | |
| 620 | http_proto::encoding::identity; | ||
| 621 | |||
| 622 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 64 times.
|
101 | if( t.id == transfer_encoding::deflate ) |
| 623 | 37 | mte.encoding = http_proto::encoding::deflate; | |
| 624 | |||
| 625 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 58 times.
|
101 | if( t.id == transfer_encoding::gzip ) |
| 626 | 43 | mte.encoding = http_proto::encoding::gzip; | |
| 627 | |||
| 628 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 3 times.
|
101 | if( b ) |
| 629 | 98 | continue; | |
| 630 | } | ||
| 631 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
|
7 | if(t.id == transfer_encoding::chunked) |
| 632 | { | ||
| 633 | // chunked appears twice | ||
| 634 | 2 | md.transfer_encoding.ec = | |
| 635 | 4 | BOOST_HTTP_PROTO_ERR( | |
| 636 | error::bad_transfer_encoding); | ||
| 637 | 2 | md.transfer_encoding.codings = 0; | |
| 638 | 2 | md.transfer_encoding.is_chunked = false; | |
| 639 | 2 | md.transfer_encoding.encoding = | |
| 640 | http_proto::encoding::identity; | ||
| 641 | 2 | update_payload(); | |
| 642 | 2 | return; | |
| 643 | } | ||
| 644 | // chunked must be last | ||
| 645 | 5 | md.transfer_encoding.ec = | |
| 646 | 10 | BOOST_HTTP_PROTO_ERR( | |
| 647 | error::bad_transfer_encoding); | ||
| 648 | 5 | md.transfer_encoding.codings = 0; | |
| 649 | 5 | md.transfer_encoding.is_chunked = false; | |
| 650 | 5 | md.transfer_encoding.encoding = | |
| 651 | http_proto::encoding::identity; | ||
| 652 | 5 | update_payload(); | |
| 653 | 5 | return; | |
| 654 |
6/6✓ Branch 1 taken 7 times.
✓ Branch 2 taken 4303 times.
✓ Branch 4 taken 4294 times.
✓ Branch 5 taken 7 times.
✓ Branch 7 taken 4294 times.
✓ Branch 8 taken 7 times.
|
8618 | } |
| 655 |
2/2✓ Branch 1 taken 4294 times.
✓ Branch 2 taken 11 times.
|
4305 | } |
| 656 | 4262 | update_payload(); | |
| 657 | } | ||
| 658 | |||
| 659 | void | ||
| 660 | 78 | header:: | |
| 661 | on_insert_content_encoding( | ||
| 662 | core::string_view v) | ||
| 663 | { | ||
| 664 | 78 | ++md.content_encoding.count; | |
| 665 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 78 times.
|
78 | if( md.content_encoding.ec.failed() ) |
| 666 | 3 | return; | |
| 667 | |||
| 668 | auto rv = grammar::parse( | ||
| 669 |
1/2✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
|
78 | v, list_rule(token_rule, 1)); |
| 670 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 77 times.
|
78 | if( !rv ) |
| 671 | { | ||
| 672 | 1 | md.content_encoding.ec = | |
| 673 | 2 | BOOST_HTTP_PROTO_ERR( | |
| 674 | error::bad_content_encoding); | ||
| 675 | 1 | return; | |
| 676 | } | ||
| 677 | |||
| 678 |
4/4✓ Branch 2 taken 76 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 75 times.
|
153 | if( rv->size() > 1 || |
| 679 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 75 times.
|
76 | md.content_encoding.count > 1) |
| 680 | { | ||
| 681 | 2 | md.content_encoding.encoding = | |
| 682 | encoding::unsupported; | ||
| 683 | 2 | return; | |
| 684 | } | ||
| 685 | |||
| 686 |
2/2✓ Branch 5 taken 37 times.
✓ Branch 6 taken 38 times.
|
75 | if( grammar::ci_is_equal(*(rv->begin()), |
| 687 | "deflate") ) | ||
| 688 | { | ||
| 689 | 37 | md.content_encoding.encoding = | |
| 690 | encoding::deflate; | ||
| 691 | } | ||
| 692 |
1/2✓ Branch 5 taken 38 times.
✗ Branch 6 not taken.
|
38 | else if( grammar::ci_is_equal(*(rv->begin()), |
| 693 | "gzip") ) | ||
| 694 | { | ||
| 695 | 38 | md.content_encoding.encoding = | |
| 696 | encoding::gzip; | ||
| 697 | } | ||
| 698 | else | ||
| 699 | { | ||
| 700 | ✗ | md.content_encoding.encoding = | |
| 701 | encoding::unsupported; | ||
| 702 | } | ||
| 703 |
2/2✓ Branch 1 taken 75 times.
✓ Branch 2 taken 3 times.
|
78 | } |
| 704 | |||
| 705 | void | ||
| 706 | 26 | header:: | |
| 707 | on_insert_upgrade( | ||
| 708 | core::string_view v) | ||
| 709 | { | ||
| 710 | 26 | ++md.upgrade.count; | |
| 711 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 25 times.
|
26 | if(md.upgrade.ec.failed()) |
| 712 | 5 | return; | |
| 713 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 24 times.
|
25 | if( version != |
| 714 | http_proto::version::http_1_1) | ||
| 715 | { | ||
| 716 | 1 | md.upgrade.ec = | |
| 717 | 2 | BOOST_HTTP_PROTO_ERR( | |
| 718 | error::bad_upgrade); | ||
| 719 | 1 | md.upgrade.websocket = false; | |
| 720 | 1 | return; | |
| 721 | } | ||
| 722 | auto rv = grammar::parse( | ||
| 723 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | v, upgrade_rule); |
| 724 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 21 times.
|
24 | if(! rv) |
| 725 | { | ||
| 726 | 3 | md.upgrade.ec = | |
| 727 | 6 | BOOST_HTTP_PROTO_ERR( | |
| 728 | error::bad_upgrade); | ||
| 729 | 3 | md.upgrade.websocket = false; | |
| 730 | 3 | return; | |
| 731 | } | ||
| 732 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5 times.
|
21 | if(! md.upgrade.websocket) |
| 733 | { | ||
| 734 |
2/2✓ Branch 6 taken 16 times.
✓ Branch 7 taken 7 times.
|
23 | for(auto t : *rv) |
| 735 | { | ||
| 736 | 16 | if( grammar::ci_is_equal( | |
| 737 |
6/6✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 7 times.
|
26 | t.name, "websocket") && |
| 738 | 10 | t.version.empty()) | |
| 739 | { | ||
| 740 | 9 | md.upgrade.websocket = true; | |
| 741 | 9 | break; | |
| 742 | } | ||
| 743 | } | ||
| 744 | } | ||
| 745 |
2/2✓ Branch 1 taken 21 times.
✓ Branch 2 taken 3 times.
|
24 | } |
| 746 | |||
| 747 | //------------------------------------------------ | ||
| 748 | |||
| 749 | void | ||
| 750 | 9 | header:: | |
| 751 | on_erase_connection() | ||
| 752 | { | ||
| 753 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | BOOST_ASSERT( |
| 754 | md.connection.count > 0); | ||
| 755 | // reset and re-insert | ||
| 756 | 9 | auto n = md.connection.count - 1; | |
| 757 | 9 | auto const p = cbuf + prefix; | |
| 758 | 9 | auto const* e = &tab()[0]; | |
| 759 | 9 | md.connection = {}; | |
| 760 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
|
14 | while(n > 0) |
| 761 | { | ||
| 762 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | if(e->id == field::connection) |
| 763 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | on_insert_connection( |
| 764 | core::string_view( | ||
| 765 | 4 | p + e->vp, e->vn)); | |
| 766 | 5 | --n; | |
| 767 | 5 | --e; | |
| 768 | } | ||
| 769 | 9 | } | |
| 770 | |||
| 771 | void | ||
| 772 | 4 | header:: | |
| 773 | on_erase_content_length() | ||
| 774 | { | ||
| 775 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | BOOST_ASSERT( |
| 776 | md.content_length.count > 0); | ||
| 777 | 4 | --md.content_length.count; | |
| 778 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | if(md.content_length.count == 0) |
| 779 | { | ||
| 780 | // no Content-Length | ||
| 781 | 1 | md.content_length = {}; | |
| 782 | 1 | update_payload(); | |
| 783 | 1 | return; | |
| 784 | } | ||
| 785 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if(! md.content_length.ec.failed()) |
| 786 | { | ||
| 787 | // removing a duplicate value | ||
| 788 | 2 | return; | |
| 789 | } | ||
| 790 | // reset and re-insert | ||
| 791 | 1 | auto n = md.content_length.count; | |
| 792 | 1 | auto const p = cbuf + prefix; | |
| 793 | 1 | auto const* e = &tab()[0]; | |
| 794 | 1 | md.content_length = {}; | |
| 795 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | while(n > 0) |
| 796 | { | ||
| 797 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if(e->id == field::content_length) |
| 798 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | on_insert_content_length( |
| 799 | core::string_view( | ||
| 800 | 1 | p + e->vp, e->vn)); | |
| 801 | 1 | --n; | |
| 802 | 1 | --e; | |
| 803 | } | ||
| 804 | 1 | update_payload(); | |
| 805 | } | ||
| 806 | |||
| 807 | void | ||
| 808 | 10 | header:: | |
| 809 | on_erase_expect() | ||
| 810 | { | ||
| 811 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | BOOST_ASSERT( |
| 812 | md.expect.count > 0); | ||
| 813 | 10 | --md.expect.count; | |
| 814 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
|
10 | if(kind != detail::kind::request) |
| 815 | 1 | return; | |
| 816 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
|
9 | if(md.expect.count == 0) |
| 817 | { | ||
| 818 | // no Expect | ||
| 819 | 3 | md.expect = {}; | |
| 820 | 3 | return; | |
| 821 | } | ||
| 822 | // VFALCO This should be uncommented | ||
| 823 | // if we want to allow multiple Expect | ||
| 824 | // fields with the value 100-continue | ||
| 825 | /* | ||
| 826 | if(! md.expect.ec.failed()) | ||
| 827 | return; | ||
| 828 | */ | ||
| 829 | // reset and re-insert | ||
| 830 | 6 | auto n = count; | |
| 831 | 6 | auto const p = cbuf + prefix; | |
| 832 | 6 | auto const* e = &tab()[0]; | |
| 833 | 6 | md.expect = {}; | |
| 834 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 6 times.
|
19 | while(n > 0) |
| 835 | { | ||
| 836 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7 times.
|
13 | if(e->id == field::expect) |
| 837 | 6 | on_insert_expect( | |
| 838 | core::string_view( | ||
| 839 | 6 | p + e->vp, e->vn)); | |
| 840 | 13 | --n; | |
| 841 | 13 | --e; | |
| 842 | } | ||
| 843 | } | ||
| 844 | |||
| 845 | void | ||
| 846 | 5 | header:: | |
| 847 | on_erase_transfer_encoding() | ||
| 848 | { | ||
| 849 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | BOOST_ASSERT( |
| 850 | md.transfer_encoding.count > 0); | ||
| 851 | 5 | --md.transfer_encoding.count; | |
| 852 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
|
5 | if(md.transfer_encoding.count == 0) |
| 853 | { | ||
| 854 | // no Transfer-Encoding | ||
| 855 | 2 | md.transfer_encoding = {}; | |
| 856 | 2 | update_payload(); | |
| 857 | 2 | return; | |
| 858 | } | ||
| 859 | // re-insert everything | ||
| 860 | 3 | --md.transfer_encoding.count; | |
| 861 | 3 | on_insert_transfer_encoding(); | |
| 862 | } | ||
| 863 | |||
| 864 | void | ||
| 865 | ✗ | header:: | |
| 866 | on_erase_content_encoding() | ||
| 867 | { | ||
| 868 | ✗ | BOOST_ASSERT( | |
| 869 | md.content_encoding.count > 0); | ||
| 870 | ✗ | --md.content_encoding.count; | |
| 871 | ✗ | if(md.content_encoding.count == 0) | |
| 872 | { | ||
| 873 | // no Content-Encoding | ||
| 874 | ✗ | md.content_encoding = {}; | |
| 875 | ✗ | return; | |
| 876 | } | ||
| 877 | // re-insert everything | ||
| 878 | ✗ | --md.content_encoding.count; | |
| 879 | // TODO | ||
| 880 | // on_insert_content_encoding(); | ||
| 881 | } | ||
| 882 | |||
| 883 | // called when Upgrade is erased | ||
| 884 | void | ||
| 885 | 4 | header:: | |
| 886 | on_erase_upgrade() | ||
| 887 | { | ||
| 888 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | BOOST_ASSERT( |
| 889 | md.upgrade.count > 0); | ||
| 890 | 4 | --md.upgrade.count; | |
| 891 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if(md.upgrade.count == 0) |
| 892 | { | ||
| 893 | // no Upgrade | ||
| 894 | 2 | md.upgrade = {}; | |
| 895 | 2 | return; | |
| 896 | } | ||
| 897 | // reset and re-insert | ||
| 898 | 2 | auto n = md.upgrade.count; | |
| 899 | 2 | auto const p = cbuf + prefix; | |
| 900 | 2 | auto const* e = &tab()[0]; | |
| 901 | 2 | md.upgrade = {}; | |
| 902 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | while(n > 0) |
| 903 | { | ||
| 904 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if(e->id == field::upgrade) |
| 905 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | on_insert_upgrade( |
| 906 | core::string_view( | ||
| 907 | 2 | p + e->vp, e->vn)); | |
| 908 | 2 | --n; | |
| 909 | 2 | --e; | |
| 910 | } | ||
| 911 | } | ||
| 912 | |||
| 913 | //------------------------------------------------ | ||
| 914 | |||
| 915 | // called when all fields with id are removed | ||
| 916 | void | ||
| 917 | 60 | header:: | |
| 918 | on_erase_all( | ||
| 919 | field id) | ||
| 920 | { | ||
| 921 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 43 times.
|
60 | if(kind == detail::kind::fields) |
| 922 | 17 | return; | |
| 923 |
6/6✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 31 times.
|
43 | switch(id) |
| 924 | { | ||
| 925 | 3 | case field::connection: | |
| 926 | 3 | md.connection = {}; | |
| 927 | 3 | return; | |
| 928 | |||
| 929 | 2 | case field::content_length: | |
| 930 | 2 | md.content_length = {}; | |
| 931 | 2 | update_payload(); | |
| 932 | 2 | return; | |
| 933 | |||
| 934 | 5 | case field::expect: | |
| 935 | 5 | md.expect = {}; | |
| 936 | 5 | update_payload(); | |
| 937 | 5 | return; | |
| 938 | |||
| 939 | 1 | case field::transfer_encoding: | |
| 940 | 1 | md.transfer_encoding = {}; | |
| 941 | 1 | update_payload(); | |
| 942 | 1 | return; | |
| 943 | |||
| 944 | 1 | case field::upgrade: | |
| 945 | 1 | md.upgrade = {}; | |
| 946 | 1 | return; | |
| 947 | |||
| 948 | 31 | default: | |
| 949 | 31 | break; | |
| 950 | } | ||
| 951 | } | ||
| 952 | |||
| 953 | //------------------------------------------------ | ||
| 954 | |||
| 955 | /* References: | ||
| 956 | |||
| 957 | 3.3. Message Body | ||
| 958 | https://datatracker.ietf.org/doc/html/rfc7230#section-3.3 | ||
| 959 | |||
| 960 | 3.3.1. Transfer-Encoding | ||
| 961 | https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.1 | ||
| 962 | |||
| 963 | 3.3.2. Content-Length | ||
| 964 | https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2 | ||
| 965 | */ | ||
| 966 | void | ||
| 967 | 19723 | header:: | |
| 968 | update_payload() noexcept | ||
| 969 | { | ||
| 970 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19723 times.
|
19723 | BOOST_ASSERT(kind != |
| 971 | detail::kind::fields); | ||
| 972 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19723 times.
|
19723 | if(md.payload_override) |
| 973 | { | ||
| 974 | // e.g. response to | ||
| 975 | // a HEAD request | ||
| 976 | ✗ | return; | |
| 977 | } | ||
| 978 | |||
| 979 | /* If there is an error in either Content-Length | ||
| 980 | or Transfer-Encoding, then the payload is | ||
| 981 | undefined. Clients should probably close the | ||
| 982 | connection. Servers can send a Bad Request | ||
| 983 | and avoid reading any payload bytes. | ||
| 984 | */ | ||
| 985 |
2/2✓ Branch 1 taken 188 times.
✓ Branch 2 taken 19535 times.
|
19723 | if(md.content_length.ec.failed()) |
| 986 | { | ||
| 987 | // invalid Content-Length | ||
| 988 | 188 | md.payload = payload::error; | |
| 989 | 188 | md.payload_size = 0; | |
| 990 | 188 | return; | |
| 991 | } | ||
| 992 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 19524 times.
|
19535 | if(md.transfer_encoding.ec.failed()) |
| 993 | { | ||
| 994 | // invalid Transfer-Encoding | ||
| 995 | 11 | md.payload = payload::error; | |
| 996 | 11 | md.payload_size = 0; | |
| 997 | 11 | return; | |
| 998 | } | ||
| 999 | |||
| 1000 | /* A sender MUST NOT send a Content-Length | ||
| 1001 | header field in any message that contains | ||
| 1002 | a Transfer-Encoding header field. | ||
| 1003 | https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2 | ||
| 1004 | */ | ||
| 1005 |
2/2✓ Branch 0 taken 4661 times.
✓ Branch 1 taken 14863 times.
|
19524 | if( md.content_length.count > 0 && |
| 1006 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4658 times.
|
4661 | md.transfer_encoding.count > 0) |
| 1007 | { | ||
| 1008 | 3 | md.payload = payload::error; | |
| 1009 | 3 | md.payload_size = 0; | |
| 1010 | 3 | return; | |
| 1011 | } | ||
| 1012 | |||
| 1013 |
2/2✓ Branch 0 taken 1323 times.
✓ Branch 1 taken 18198 times.
|
19521 | if(kind == detail::kind::response) |
| 1014 | 1323 | goto do_response; | |
| 1015 | |||
| 1016 | //-------------------------------------------- | ||
| 1017 | |||
| 1018 | /* The presence of a message body in a | ||
| 1019 | request is signaled by a Content-Length | ||
| 1020 | or Transfer-Encoding header field. Request | ||
| 1021 | message framing is independent of method | ||
| 1022 | semantics, even if the method does not | ||
| 1023 | define any use for a message body. | ||
| 1024 | */ | ||
| 1025 |
2/2✓ Branch 0 taken 4419 times.
✓ Branch 1 taken 13779 times.
|
18198 | if(md.content_length.count > 0) |
| 1026 | { | ||
| 1027 |
2/2✓ Branch 0 taken 4392 times.
✓ Branch 1 taken 27 times.
|
4419 | if(md.content_length.value > 0) |
| 1028 | { | ||
| 1029 | // non-zero Content-Length | ||
| 1030 | 4392 | md.payload = payload::size; | |
| 1031 | 4392 | md.payload_size = md.content_length.value; | |
| 1032 | 4392 | return; | |
| 1033 | } | ||
| 1034 | // Content-Length: 0 | ||
| 1035 | 27 | md.payload = payload::none; | |
| 1036 | 27 | md.payload_size = 0; | |
| 1037 | 27 | return; | |
| 1038 | } | ||
| 1039 |
2/2✓ Branch 0 taken 4010 times.
✓ Branch 1 taken 9769 times.
|
13779 | if(md.transfer_encoding.is_chunked) |
| 1040 | { | ||
| 1041 | // chunked | ||
| 1042 | 4010 | md.payload = payload::chunked; | |
| 1043 | 4010 | md.payload_size = 0; | |
| 1044 | 4010 | return; | |
| 1045 | } | ||
| 1046 | // no payload | ||
| 1047 | 9769 | md.payload = payload::none; | |
| 1048 | 9769 | md.payload_size = 0; | |
| 1049 | 9769 | return; | |
| 1050 | |||
| 1051 | //-------------------------------------------- | ||
| 1052 | 1323 | do_response: | |
| 1053 | |||
| 1054 |
2/2✓ Branch 0 taken 1313 times.
✓ Branch 1 taken 10 times.
|
1323 | if( res.status_int / 100 == 1 || // 1xx e.g. Continue |
| 1055 |
2/2✓ Branch 0 taken 1311 times.
✓ Branch 1 taken 2 times.
|
1313 | res.status_int == 204 || // No Content |
| 1056 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1309 times.
|
1311 | res.status_int == 304) // Not Modified |
| 1057 | { | ||
| 1058 | /* The correctness of any Content-Length | ||
| 1059 | here is defined by the particular | ||
| 1060 | resource, and cannot be determined | ||
| 1061 | here. In any case there is no payload. | ||
| 1062 | */ | ||
| 1063 | 14 | md.payload = payload::none; | |
| 1064 | 14 | md.payload_size = 0; | |
| 1065 | 14 | return; | |
| 1066 | } | ||
| 1067 |
2/2✓ Branch 0 taken 236 times.
✓ Branch 1 taken 1073 times.
|
1309 | if(md.content_length.count > 0) |
| 1068 | { | ||
| 1069 |
2/2✓ Branch 0 taken 223 times.
✓ Branch 1 taken 13 times.
|
236 | if(md.content_length.value > 0) |
| 1070 | { | ||
| 1071 | // Content-Length > 0 | ||
| 1072 | 223 | md.payload = payload::size; | |
| 1073 | 223 | md.payload_size = md.content_length.value; | |
| 1074 | 223 | return; | |
| 1075 | } | ||
| 1076 | // Content-Length: 0 | ||
| 1077 | 13 | md.payload = payload::none; | |
| 1078 | 13 | md.payload_size = 0; | |
| 1079 | 13 | return; | |
| 1080 | } | ||
| 1081 |
2/2✓ Branch 0 taken 188 times.
✓ Branch 1 taken 885 times.
|
1073 | if(md.transfer_encoding.is_chunked) |
| 1082 | { | ||
| 1083 | // chunked | ||
| 1084 | 188 | md.payload = payload::chunked; | |
| 1085 | 188 | md.payload_size = 0; | |
| 1086 | 188 | return; | |
| 1087 | } | ||
| 1088 | |||
| 1089 | // eof needed | ||
| 1090 | 885 | md.payload = payload::to_eof; | |
| 1091 | 885 | md.payload_size = 0; | |
| 1092 | } | ||
| 1093 | |||
| 1094 | //------------------------------------------------ | ||
| 1095 | |||
| 1096 | std::size_t | ||
| 1097 | 541 | header:: | |
| 1098 | count_crlf( | ||
| 1099 | core::string_view s) noexcept | ||
| 1100 | { | ||
| 1101 | 541 | auto it = s.data(); | |
| 1102 | 541 | auto len = s.size(); | |
| 1103 | 541 | std::size_t n = 0; | |
| 1104 |
2/2✓ Branch 0 taken 18496 times.
✓ Branch 1 taken 541 times.
|
19037 | while(len >= 2) |
| 1105 | { | ||
| 1106 |
2/2✓ Branch 0 taken 1730 times.
✓ Branch 1 taken 16766 times.
|
18496 | if( it[0] == '\r' && |
| 1107 |
1/2✓ Branch 0 taken 1730 times.
✗ Branch 1 not taken.
|
1730 | it[1] != '\r') |
| 1108 | { | ||
| 1109 |
1/2✓ Branch 0 taken 1730 times.
✗ Branch 1 not taken.
|
1730 | if(it[1] == '\n') |
| 1110 | 1730 | n++; | |
| 1111 | 1730 | it += 2; | |
| 1112 | 1730 | len -= 2; | |
| 1113 | } | ||
| 1114 | else | ||
| 1115 | { | ||
| 1116 | 16766 | it++; | |
| 1117 | 16766 | len--; | |
| 1118 | } | ||
| 1119 | } | ||
| 1120 | 541 | return n; | |
| 1121 | } | ||
| 1122 | |||
| 1123 | static | ||
| 1124 | void | ||
| 1125 | 12675 | parse_start_line( | |
| 1126 | header& h, | ||
| 1127 | header_limits const& lim, | ||
| 1128 | std::size_t new_size, | ||
| 1129 | system::error_code& ec) noexcept | ||
| 1130 | { | ||
| 1131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12675 times.
|
12675 | BOOST_ASSERT(h.size == 0); |
| 1132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12675 times.
|
12675 | BOOST_ASSERT(h.prefix == 0); |
| 1133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12675 times.
|
12675 | BOOST_ASSERT(h.cbuf != nullptr); |
| 1134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12675 times.
|
12675 | BOOST_ASSERT( |
| 1135 | h.kind != detail::kind::fields); | ||
| 1136 | |||
| 1137 | 12675 | auto const it0 = h.cbuf; | |
| 1138 | 12675 | auto const end = it0 + new_size; | |
| 1139 | 12675 | char const* it = it0; | |
| 1140 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 12639 times.
|
12675 | if( new_size > lim.max_start_line) |
| 1141 | 36 | new_size = lim.max_start_line; | |
| 1142 |
2/2✓ Branch 0 taken 11622 times.
✓ Branch 1 taken 1053 times.
|
12675 | if(h.kind == detail::kind::request) |
| 1143 | { | ||
| 1144 | auto rv = grammar::parse( | ||
| 1145 | 11622 | it, end, request_line_rule); | |
| 1146 |
2/2✓ Branch 1 taken 1892 times.
✓ Branch 2 taken 9730 times.
|
11622 | if(! rv) |
| 1147 | { | ||
| 1148 | 1892 | ec = rv.error(); | |
| 1149 |
2/4✓ Branch 2 taken 1892 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1892 times.
|
3784 | if( ec == grammar::error::need_more && |
| 1150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1892 times.
|
1892 | new_size == lim.max_start_line) |
| 1151 | ✗ | ec = BOOST_HTTP_PROTO_ERR( | |
| 1152 | error::start_line_limit); | ||
| 1153 | 1892 | return; | |
| 1154 | } | ||
| 1155 | // method | ||
| 1156 | 9730 | auto sm = std::get<0>(*rv); | |
| 1157 | 9730 | h.req.method = string_to_method(sm); | |
| 1158 | 9730 | h.req.method_len = | |
| 1159 | 9730 | static_cast<offset_type>(sm.size()); | |
| 1160 | // target | ||
| 1161 | 9730 | auto st = std::get<1>(*rv); | |
| 1162 | 9730 | h.req.target_len = | |
| 1163 | 9730 | static_cast<offset_type>(st.size()); | |
| 1164 | // version | ||
| 1165 |
2/3✓ Branch 2 taken 20 times.
✓ Branch 3 taken 9710 times.
✗ Branch 4 not taken.
|
9730 | switch(std::get<2>(*rv)) |
| 1166 | { | ||
| 1167 | 20 | case 10: | |
| 1168 | 20 | h.version = | |
| 1169 | http_proto::version::http_1_0; | ||
| 1170 | 20 | break; | |
| 1171 | 9710 | case 11: | |
| 1172 | 9710 | h.version = | |
| 1173 | http_proto::version::http_1_1; | ||
| 1174 | 9710 | break; | |
| 1175 | ✗ | default: | |
| 1176 | { | ||
| 1177 | ✗ | ec = BOOST_HTTP_PROTO_ERR( | |
| 1178 | error::bad_version); | ||
| 1179 | ✗ | return; | |
| 1180 | } | ||
| 1181 | } | ||
| 1182 | } | ||
| 1183 | else | ||
| 1184 | { | ||
| 1185 | auto rv = grammar::parse( | ||
| 1186 | 1053 | it, end, status_line_rule); | |
| 1187 |
2/2✓ Branch 1 taken 224 times.
✓ Branch 2 taken 829 times.
|
1053 | if(! rv) |
| 1188 | { | ||
| 1189 | 224 | ec = rv.error(); | |
| 1190 |
2/4✓ Branch 2 taken 224 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 224 times.
|
448 | if( ec == grammar::error::need_more && |
| 1191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | new_size == lim.max_start_line) |
| 1192 | ✗ | ec = BOOST_HTTP_PROTO_ERR( | |
| 1193 | error::start_line_limit); | ||
| 1194 | 224 | return; | |
| 1195 | } | ||
| 1196 | // version | ||
| 1197 |
2/3✓ Branch 2 taken 5 times.
✓ Branch 3 taken 824 times.
✗ Branch 4 not taken.
|
829 | switch(std::get<0>(*rv)) |
| 1198 | { | ||
| 1199 | 5 | case 10: | |
| 1200 | 5 | h.version = | |
| 1201 | http_proto::version::http_1_0; | ||
| 1202 | 5 | break; | |
| 1203 | 824 | case 11: | |
| 1204 | 824 | h.version = | |
| 1205 | http_proto::version::http_1_1; | ||
| 1206 | 824 | break; | |
| 1207 | ✗ | default: | |
| 1208 | { | ||
| 1209 | ✗ | ec = BOOST_HTTP_PROTO_ERR( | |
| 1210 | error::bad_version); | ||
| 1211 | ✗ | return; | |
| 1212 | } | ||
| 1213 | } | ||
| 1214 | // status-code | ||
| 1215 | 829 | h.res.status_int = | |
| 1216 | static_cast<unsigned short>( | ||
| 1217 | 829 | std::get<1>(*rv).v); | |
| 1218 | 829 | h.res.status = std::get<1>(*rv).st; | |
| 1219 | } | ||
| 1220 | 10559 | h.prefix = static_cast<offset_type>(it - it0); | |
| 1221 | 10559 | h.size = h.prefix; | |
| 1222 | 10559 | h.on_start_line(); | |
| 1223 | } | ||
| 1224 | |||
| 1225 | // returns: true if we added a field | ||
| 1226 | static | ||
| 1227 | void | ||
| 1228 | 24288 | parse_field( | |
| 1229 | header& h, | ||
| 1230 | header_limits const& lim, | ||
| 1231 | std::size_t new_size, | ||
| 1232 | system::error_code& ec) noexcept | ||
| 1233 | { | ||
| 1234 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24192 times.
|
24288 | if( new_size > lim.max_field) |
| 1235 | 96 | new_size = lim.max_field; | |
| 1236 | 24288 | auto const it0 = h.cbuf + h.size; | |
| 1237 | 24288 | auto const end = h.cbuf + new_size; | |
| 1238 | 24288 | char const* it = it0; | |
| 1239 | 24288 | auto rv = grammar::parse( | |
| 1240 | it, end, field_rule); | ||
| 1241 |
2/2✓ Branch 1 taken 12815 times.
✓ Branch 2 taken 11473 times.
|
24288 | if(rv.has_error()) |
| 1242 | { | ||
| 1243 | 12815 | ec = rv.error(); | |
| 1244 |
2/2✓ Branch 2 taken 10540 times.
✓ Branch 3 taken 2275 times.
|
12815 | if(ec == grammar::error::end_of_range) |
| 1245 | { | ||
| 1246 | // final CRLF | ||
| 1247 | 10540 | h.size = static_cast< | |
| 1248 | 10540 | offset_type>(it - h.cbuf); | |
| 1249 | 12815 | return; | |
| 1250 | } | ||
| 1251 |
3/4✓ Branch 2 taken 2016 times.
✓ Branch 3 taken 259 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2275 times.
|
4291 | if( ec == grammar::error::need_more && |
| 1252 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2016 times.
|
2016 | new_size == lim.max_field) |
| 1253 | { | ||
| 1254 | ✗ | ec = BOOST_HTTP_PROTO_ERR( | |
| 1255 | error::field_size_limit); | ||
| 1256 | } | ||
| 1257 | 2275 | return; | |
| 1258 | } | ||
| 1259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11473 times.
|
11473 | if(h.count >= lim.max_fields) |
| 1260 | { | ||
| 1261 | ✗ | ec = BOOST_HTTP_PROTO_ERR( | |
| 1262 | error::fields_limit); | ||
| 1263 | ✗ | return; | |
| 1264 | } | ||
| 1265 |
2/2✓ Branch 1 taken 210 times.
✓ Branch 2 taken 11263 times.
|
11473 | if(rv->has_obs_fold) |
| 1266 | { | ||
| 1267 | // obs fold not allowed in test views | ||
| 1268 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 210 times.
|
210 | BOOST_ASSERT(h.buf != nullptr); |
| 1269 | 210 | remove_obs_fold(h.buf + h.size, it); | |
| 1270 | } | ||
| 1271 | 11473 | auto id = string_to_field(rv->name); | |
| 1272 | 11473 | h.size = static_cast<offset_type>(it - h.cbuf); | |
| 1273 | |||
| 1274 | // add field table entry | ||
| 1275 |
1/2✓ Branch 0 taken 11473 times.
✗ Branch 1 not taken.
|
11473 | if(h.buf != nullptr) |
| 1276 | { | ||
| 1277 | 22946 | auto& e = header::table( | |
| 1278 | 11473 | h.buf + h.cap)[h.count]; | |
| 1279 | 11473 | auto const base = | |
| 1280 | 11473 | h.buf + h.prefix; | |
| 1281 | 11473 | e.np = static_cast<offset_type>( | |
| 1282 | 11473 | rv->name.data() - base); | |
| 1283 | 11473 | e.nn = static_cast<offset_type>( | |
| 1284 | 11473 | rv->name.size()); | |
| 1285 | 11473 | e.vp = static_cast<offset_type>( | |
| 1286 | 11473 | rv->value.data() - base); | |
| 1287 | 11473 | e.vn = static_cast<offset_type>( | |
| 1288 | 11473 | rv->value.size()); | |
| 1289 | 11473 | e.id = id; | |
| 1290 | } | ||
| 1291 | 11473 | ++h.count; | |
| 1292 | 11473 | h.on_insert(id, rv->value); | |
| 1293 | 11473 | ec = {}; | |
| 1294 | } | ||
| 1295 | |||
| 1296 | void | ||
| 1297 | 14931 | header:: | |
| 1298 | parse( | ||
| 1299 | std::size_t new_size, | ||
| 1300 | header_limits const& lim, | ||
| 1301 | system::error_code& ec) noexcept | ||
| 1302 | { | ||
| 1303 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 14895 times.
|
14931 | if( new_size > lim.max_size) |
| 1304 | 36 | new_size = lim.max_size; | |
| 1305 |
2/2✓ Branch 0 taken 2016 times.
✓ Branch 1 taken 12915 times.
|
14931 | if( this->prefix == 0 && |
| 1306 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 12675 times.
|
12915 | this->kind != |
| 1307 | detail::kind::fields) | ||
| 1308 | { | ||
| 1309 | 12675 | parse_start_line( | |
| 1310 | *this, lim, new_size, ec); | ||
| 1311 |
2/2✓ Branch 1 taken 10559 times.
✓ Branch 2 taken 2116 times.
|
12675 | if(ec.failed()) |
| 1312 | { | ||
| 1313 |
2/4✓ Branch 2 taken 2116 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2116 times.
|
4232 | if( ec == grammar::error::need_more && |
| 1314 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2116 times.
|
2116 | new_size == lim.max_fields) |
| 1315 | { | ||
| 1316 | ✗ | ec = BOOST_HTTP_PROTO_ERR( | |
| 1317 | error::headers_limit); | ||
| 1318 | } | ||
| 1319 | 2116 | return; | |
| 1320 | } | ||
| 1321 | } | ||
| 1322 | for(;;) | ||
| 1323 | { | ||
| 1324 | 24288 | parse_field( | |
| 1325 | *this, lim, new_size, ec); | ||
| 1326 |
2/2✓ Branch 1 taken 12815 times.
✓ Branch 2 taken 11473 times.
|
24288 | if(ec.failed()) |
| 1327 | { | ||
| 1328 |
3/4✓ Branch 2 taken 2016 times.
✓ Branch 3 taken 10799 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12815 times.
|
14831 | if( ec == grammar::error::need_more && |
| 1329 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2016 times.
|
2016 | new_size == lim.max_size) |
| 1330 | { | ||
| 1331 | ✗ | ec = BOOST_HTTP_PROTO_ERR( | |
| 1332 | error::headers_limit); | ||
| 1333 | ✗ | return; | |
| 1334 | } | ||
| 1335 | 12815 | break; | |
| 1336 | } | ||
| 1337 | 11473 | } | |
| 1338 |
2/2✓ Branch 2 taken 10540 times.
✓ Branch 3 taken 2275 times.
|
12815 | if(ec == grammar::error::end_of_range) |
| 1339 | 10540 | ec = {}; | |
| 1340 | } | ||
| 1341 | |||
| 1342 | } // detail | ||
| 1343 | } // http_proto | ||
| 1344 | } // boost | ||
| 1345 |