libs/http/src/rfc/detail/rules.cpp
95.8% Lines (159/166)
100.0% Functions (10/10)
91.2% Branches (93/102)
libs/http/src/rfc/detail/rules.cpp
| Line | Branch | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com) | ||
| 3 | // | ||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 6 | // | ||
| 7 | // Official repository: https://github.com/cppalliance/http | ||
| 8 | // | ||
| 9 | |||
| 10 | #include "src/rfc/detail/rules.hpp" | ||
| 11 | |||
| 12 | #include <boost/http/error.hpp> | ||
| 13 | #include <boost/http/detail/config.hpp> | ||
| 14 | #include <boost/http/rfc/token_rule.hpp> | ||
| 15 | |||
| 16 | #include <boost/core/detail/string_view.hpp> | ||
| 17 | #include <boost/url/grammar/delim_rule.hpp> | ||
| 18 | #include <boost/url/grammar/digit_chars.hpp> | ||
| 19 | #include <boost/url/grammar/error.hpp> | ||
| 20 | #include <boost/url/grammar/hexdig_chars.hpp> | ||
| 21 | #include <boost/url/grammar/lut_chars.hpp> | ||
| 22 | #include <boost/url/grammar/parse.hpp> | ||
| 23 | #include <boost/url/grammar/tuple_rule.hpp> | ||
| 24 | |||
| 25 | #include "src/rfc/detail/rules.hpp" | ||
| 26 | |||
| 27 | namespace boost { | ||
| 28 | namespace http { | ||
| 29 | namespace detail { | ||
| 30 | |||
| 31 | auto | ||
| 32 | 26062 | crlf_rule_t:: | |
| 33 | parse( | ||
| 34 | char const*& it, | ||
| 35 | char const* end) const noexcept -> | ||
| 36 | system::result<value_type> | ||
| 37 | { | ||
| 38 |
2/2✓ Branch 0 taken 4509 times.
✓ Branch 1 taken 21553 times.
|
26062 | if(it == end) |
| 39 | 4509 | return grammar::error::need_more; | |
| 40 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 21524 times.
|
21553 | if(*it != '\r') |
| 41 | 29 | return grammar::error::mismatch; | |
| 42 | 21524 | ++it; | |
| 43 |
2/2✓ Branch 0 taken 645 times.
✓ Branch 1 taken 20879 times.
|
21524 | if(it == end) |
| 44 | 645 | return grammar::error::need_more; | |
| 45 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 20828 times.
|
20879 | if(*it != '\n') |
| 46 | 51 | return grammar::error::mismatch; | |
| 47 | 20828 | ++it; | |
| 48 | 20828 | return {}; | |
| 49 | } | ||
| 50 | |||
| 51 | //------------------------------------------------ | ||
| 52 | |||
| 53 | auto | ||
| 54 | 21678 | version_rule_t:: | |
| 55 | parse( | ||
| 56 | char const*& it, | ||
| 57 | char const* end) const noexcept -> | ||
| 58 | system::result<value_type> | ||
| 59 | { | ||
| 60 | 21678 | value_type v = 0; | |
| 61 |
2/2✓ Branch 0 taken 1141 times.
✓ Branch 1 taken 20537 times.
|
21678 | if(it == end) |
| 62 | { | ||
| 63 | // expected "HTTP/" | ||
| 64 | 1141 | BOOST_HTTP_RETURN_EC( | |
| 65 | grammar::error::need_more); | ||
| 66 | } | ||
| 67 |
2/2✓ Branch 0 taken 17523 times.
✓ Branch 1 taken 3014 times.
|
20537 | if(end - it >= 5) |
| 68 | { | ||
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17523 times.
|
17523 | if(std::memcmp( |
| 70 | it, "HTTP/", 5) != 0) | ||
| 71 | { | ||
| 72 | ✗ | BOOST_HTTP_RETURN_EC( | |
| 73 | grammar::error::mismatch); | ||
| 74 | } | ||
| 75 | 17523 | it += 5; | |
| 76 | } | ||
| 77 |
2/2✓ Branch 0 taken 683 times.
✓ Branch 1 taken 19854 times.
|
20537 | if(it == end) |
| 78 | { | ||
| 79 | // expected DIGIT | ||
| 80 | 683 | BOOST_HTTP_RETURN_EC( | |
| 81 | grammar::error::need_more); | ||
| 82 | } | ||
| 83 |
2/2✓ Branch 1 taken 3014 times.
✓ Branch 2 taken 16840 times.
|
19854 | if(! grammar::digit_chars(*it)) |
| 84 | { | ||
| 85 | // expected DIGIT | ||
| 86 | 3014 | BOOST_HTTP_RETURN_EC( | |
| 87 | grammar::error::need_more); | ||
| 88 | } | ||
| 89 | 16840 | v = 10 * (*it++ - '0'); | |
| 90 |
2/2✓ Branch 0 taken 817 times.
✓ Branch 1 taken 16023 times.
|
16840 | if(it == end) |
| 91 | { | ||
| 92 | // expected "." | ||
| 93 | 817 | BOOST_HTTP_RETURN_EC( | |
| 94 | grammar::error::need_more); | ||
| 95 | } | ||
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16023 times.
|
16023 | if(*it != '.') |
| 97 | { | ||
| 98 | // expected "." | ||
| 99 | ✗ | BOOST_HTTP_RETURN_EC( | |
| 100 | grammar::error::need_more); | ||
| 101 | } | ||
| 102 | 16023 | ++it; | |
| 103 |
2/2✓ Branch 0 taken 663 times.
✓ Branch 1 taken 15360 times.
|
16023 | if(it == end) |
| 104 | { | ||
| 105 | // expected DIGIT | ||
| 106 | 663 | BOOST_HTTP_RETURN_EC( | |
| 107 | grammar::error::need_more); | ||
| 108 | } | ||
| 109 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 15360 times.
|
15360 | if(! grammar::digit_chars(*it)) |
| 110 | { | ||
| 111 | // expected DIGIT | ||
| 112 | ✗ | BOOST_HTTP_RETURN_EC( | |
| 113 | grammar::error::need_more); | ||
| 114 | } | ||
| 115 | 15360 | v += *it++ - '0'; | |
| 116 | 15360 | return v; | |
| 117 | } | ||
| 118 | |||
| 119 | //------------------------------------------------ | ||
| 120 | |||
| 121 | auto | ||
| 122 | 5101 | status_code_rule_t:: | |
| 123 | parse( | ||
| 124 | char const*& it, | ||
| 125 | char const* end) const noexcept -> | ||
| 126 | system::result<value_type> | ||
| 127 | { | ||
| 128 | auto const dig = | ||
| 129 | 11971 | [](char c) -> int | |
| 130 | { | ||
| 131 | 11971 | unsigned char uc(c - '0'); | |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11971 times.
|
11971 | if(uc > 9) |
| 133 | ✗ | return -1; | |
| 134 | 11971 | return uc; | |
| 135 | }; | ||
| 136 | |||
| 137 |
2/2✓ Branch 0 taken 562 times.
✓ Branch 1 taken 4539 times.
|
5101 | if(it == end) |
| 138 | { | ||
| 139 | // end | ||
| 140 | 562 | BOOST_HTTP_RETURN_EC( | |
| 141 | grammar::error::need_more); | ||
| 142 | } | ||
| 143 | 4539 | auto it0 = it; | |
| 144 | 4539 | int v = dig(*it); | |
| 145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4539 times.
|
4539 | if(v == -1) |
| 146 | { | ||
| 147 | // expected DIGIT | ||
| 148 | ✗ | BOOST_HTTP_RETURN_EC( | |
| 149 | grammar::error::mismatch); | ||
| 150 | } | ||
| 151 | 4539 | value_type t; | |
| 152 | 4539 | t.v = 100 * v; | |
| 153 | 4539 | ++it; | |
| 154 |
2/2✓ Branch 0 taken 552 times.
✓ Branch 1 taken 3987 times.
|
4539 | if(it == end) |
| 155 | { | ||
| 156 | // end | ||
| 157 | 552 | BOOST_HTTP_RETURN_EC( | |
| 158 | grammar::error::need_more); | ||
| 159 | } | ||
| 160 | 3987 | v = dig(*it); | |
| 161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3987 times.
|
3987 | if(v == -1) |
| 162 | { | ||
| 163 | // expected DIGIT | ||
| 164 | ✗ | BOOST_HTTP_RETURN_EC( | |
| 165 | grammar::error::mismatch); | ||
| 166 | } | ||
| 167 | 3987 | t.v = t.v + (10 * v); | |
| 168 | 3987 | ++it; | |
| 169 |
2/2✓ Branch 0 taken 542 times.
✓ Branch 1 taken 3445 times.
|
3987 | if(it == end) |
| 170 | { | ||
| 171 | // end | ||
| 172 | 542 | BOOST_HTTP_RETURN_EC( | |
| 173 | grammar::error::need_more); | ||
| 174 | } | ||
| 175 | 3445 | v = dig(*it); | |
| 176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3445 times.
|
3445 | if(v == -1) |
| 177 | { | ||
| 178 | // expected DIGIT | ||
| 179 | ✗ | BOOST_HTTP_RETURN_EC( | |
| 180 | grammar::error::need_more); | ||
| 181 | } | ||
| 182 | 3445 | t.v = t.v + v; | |
| 183 | 3445 | ++it; | |
| 184 | |||
| 185 | 3445 | t.s = core::string_view(it0, it - it0); | |
| 186 | 3445 | t.st = int_to_status(t.v); | |
| 187 | 3445 | return t; | |
| 188 | } | ||
| 189 | |||
| 190 | //------------------------------------------------ | ||
| 191 | |||
| 192 | auto | ||
| 193 | 2913 | reason_phrase_rule_t:: | |
| 194 | parse( | ||
| 195 | char const*& it, | ||
| 196 | char const* end) const noexcept -> | ||
| 197 | system::result<value_type> | ||
| 198 | { | ||
| 199 | 2913 | auto begin = it; | |
| 200 | 2913 | it = grammar::find_if_not(it, end, ws_vchars); | |
| 201 | 2913 | return core::string_view(begin, it); | |
| 202 | } | ||
| 203 | |||
| 204 | //------------------------------------------------ | ||
| 205 | |||
| 206 | auto | ||
| 207 | 21191 | field_name_rule_t:: | |
| 208 | parse( | ||
| 209 | char const*& it, | ||
| 210 | char const* end) const noexcept -> | ||
| 211 | system::result<value_type> | ||
| 212 | { | ||
| 213 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 21190 times.
|
21191 | if( it == end ) |
| 214 | 1 | BOOST_HTTP_RETURN_EC( | |
| 215 | grammar::error::need_more); | ||
| 216 | |||
| 217 | 21190 | value_type v; | |
| 218 | |||
| 219 | 21190 | auto begin = it; | |
| 220 | 21190 | auto rv = grammar::parse( | |
| 221 | it, end, token_rule); | ||
| 222 |
6/6✓ Branch 1 taken 21124 times.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 14425 times.
✓ Branch 4 taken 6699 times.
✓ Branch 5 taken 14491 times.
✓ Branch 6 taken 6699 times.
|
21190 | if( rv.has_error() || (it != end) ) |
| 223 | { | ||
| 224 |
2/2✓ Branch 0 taken 14425 times.
✓ Branch 1 taken 66 times.
|
14491 | if( it != begin ) |
| 225 | { | ||
| 226 | 14425 | v = core::string_view(begin, it - begin); | |
| 227 | 14425 | return v; | |
| 228 | } | ||
| 229 | 66 | return error::bad_field_name; | |
| 230 | } | ||
| 231 | |||
| 232 | 6699 | v = core::string_view(begin, end - begin); | |
| 233 | 6699 | return v; | |
| 234 | } | ||
| 235 | |||
| 236 | auto | ||
| 237 | 14668 | field_value_rule_t:: | |
| 238 | parse( | ||
| 239 | char const*& it, | ||
| 240 | char const* end) const noexcept -> | ||
| 241 | system::result<value_type> | ||
| 242 | { | ||
| 243 | 14668 | value_type v; | |
| 244 |
2/2✓ Branch 0 taken 511 times.
✓ Branch 1 taken 14157 times.
|
14668 | if( it == end ) |
| 245 | { | ||
| 246 | 511 | v.value = core::string_view(it, 0); | |
| 247 | 511 | return v; | |
| 248 | } | ||
| 249 | |||
| 250 | // field-line = field-name ":" OWS field-value OWS | ||
| 251 | // field-value = *field-content | ||
| 252 | // field-content = field-vchar | ||
| 253 | // [ 1*( SP / HTAB / field-vchar ) field-vchar ] | ||
| 254 | // field-vchar = VCHAR / obs-text | ||
| 255 | // obs-text = %x80-FF | ||
| 256 | // VCHAR = %x21-7E | ||
| 257 | // ; visible (printing) characters | ||
| 258 | |||
| 259 | 62151 | auto is_field_vchar = [](unsigned char ch) | |
| 260 | { | ||
| 261 |
6/6✓ Branch 0 taken 62117 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 62105 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 34 times.
|
62151 | return (ch >= 0x21 && ch <= 0x7e) || ch >= 0x80; |
| 262 | }; | ||
| 263 | |||
| 264 | 14157 | char const* s0 = nullptr; | |
| 265 | 14157 | char const* s1 = nullptr; | |
| 266 | |||
| 267 | 14157 | bool has_crlf = false; | |
| 268 | 14157 | bool has_obs_fold = false; | |
| 269 | |||
| 270 |
2/2✓ Branch 0 taken 90458 times.
✓ Branch 1 taken 2622 times.
|
93080 | while( it < end ) |
| 271 | { | ||
| 272 | 90458 | auto ch = *it; | |
| 273 |
2/2✓ Branch 1 taken 16076 times.
✓ Branch 2 taken 74382 times.
|
90458 | if( ws(ch) ) |
| 274 | { | ||
| 275 | 16076 | ++it; | |
| 276 | 16076 | continue; | |
| 277 | } | ||
| 278 | |||
| 279 |
2/2✓ Branch 0 taken 12231 times.
✓ Branch 1 taken 62151 times.
|
74382 | if( ch == '\r' ) |
| 280 | { | ||
| 281 | // too short to know if we have a potential obs-fold | ||
| 282 | // occurrence | ||
| 283 |
2/2✓ Branch 0 taken 454 times.
✓ Branch 1 taken 11777 times.
|
12231 | if( end - it < 2 ) |
| 284 | 454 | BOOST_HTTP_RETURN_EC( | |
| 285 | grammar::error::need_more); | ||
| 286 | |||
| 287 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 11724 times.
|
11777 | if( it[1] != '\n' ) |
| 288 | 53 | goto done; | |
| 289 | |||
| 290 |
2/2✓ Branch 0 taken 416 times.
✓ Branch 1 taken 11308 times.
|
11724 | if( end - it < 3 ) |
| 291 | 416 | BOOST_HTTP_RETURN_EC( | |
| 292 | grammar::error::need_more); | ||
| 293 | |||
| 294 |
2/2✓ Branch 1 taken 10578 times.
✓ Branch 2 taken 730 times.
|
11308 | if(! ws(it[2]) ) |
| 295 | { | ||
| 296 | 10578 | has_crlf = true; | |
| 297 | 10578 | goto done; | |
| 298 | } | ||
| 299 | |||
| 300 | 730 | has_obs_fold = true; | |
| 301 | 730 | it = it + 3; | |
| 302 | 730 | continue; | |
| 303 | 730 | } | |
| 304 | |||
| 305 |
2/2✓ Branch 1 taken 34 times.
✓ Branch 2 taken 62117 times.
|
62151 | if(! is_field_vchar(ch) ) |
| 306 | { | ||
| 307 | 34 | goto done; | |
| 308 | } | ||
| 309 | |||
| 310 |
2/2✓ Branch 0 taken 13228 times.
✓ Branch 1 taken 48889 times.
|
62117 | if(! s0 ) |
| 311 | 13228 | s0 = it; | |
| 312 | |||
| 313 | 62117 | ++it; | |
| 314 | 62117 | s1 = it; | |
| 315 | } | ||
| 316 | |||
| 317 | 2622 | done: | |
| 318 | // later routines wind up doing pointer | ||
| 319 | // subtraction using the .data() member | ||
| 320 | // of the value so we need a valid 0-len range | ||
| 321 |
2/2✓ Branch 0 taken 765 times.
✓ Branch 1 taken 12522 times.
|
13287 | if(! s0 ) |
| 322 | { | ||
| 323 | 765 | s0 = it; | |
| 324 | 765 | s1 = s0; | |
| 325 | } | ||
| 326 | |||
| 327 | 13287 | v.value = core::string_view(s0, s1 - s0); | |
| 328 | 13287 | v.has_crlf = has_crlf; | |
| 329 | 13287 | v.has_obs_fold = has_obs_fold; | |
| 330 | 13287 | return v; | |
| 331 | } | ||
| 332 | |||
| 333 | auto | ||
| 334 | 32000 | field_rule_t:: | |
| 335 | parse( | ||
| 336 | char const*& it, | ||
| 337 | char const* end) const noexcept -> | ||
| 338 | system::result<value_type> | ||
| 339 | { | ||
| 340 |
2/2✓ Branch 0 taken 671 times.
✓ Branch 1 taken 31329 times.
|
32000 | if(it == end) |
| 341 | { | ||
| 342 | 671 | BOOST_HTTP_RETURN_EC( | |
| 343 | grammar::error::need_more); | ||
| 344 | } | ||
| 345 | // check for leading CRLF | ||
| 346 |
2/2✓ Branch 0 taken 10379 times.
✓ Branch 1 taken 20950 times.
|
31329 | if(it[0] == '\r') |
| 347 | { | ||
| 348 | 10379 | ++it; | |
| 349 |
2/2✓ Branch 0 taken 367 times.
✓ Branch 1 taken 10012 times.
|
10379 | if(it == end) |
| 350 | { | ||
| 351 | 367 | BOOST_HTTP_RETURN_EC( | |
| 352 | grammar::error::need_more); | ||
| 353 | } | ||
| 354 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 9991 times.
|
10012 | if(*it != '\n') |
| 355 | { | ||
| 356 | 21 | BOOST_HTTP_RETURN_EC( | |
| 357 | grammar::error::mismatch); | ||
| 358 | } | ||
| 359 | // end of fields | ||
| 360 | 9991 | ++it; | |
| 361 | 9991 | BOOST_HTTP_RETURN_EC( | |
| 362 | grammar::error::end_of_range); | ||
| 363 | } | ||
| 364 | |||
| 365 | 20950 | value_type v; | |
| 366 | auto rv = grammar::parse( | ||
| 367 | 20950 | it, end, grammar::tuple_rule( | |
| 368 | field_name_rule, | ||
| 369 | 20950 | grammar::delim_rule(':'), | |
| 370 | field_value_rule, | ||
| 371 | 20950 | crlf_rule)); | |
| 372 | |||
| 373 |
2/2✓ Branch 1 taken 10388 times.
✓ Branch 2 taken 10562 times.
|
20950 | if( rv.has_error() ) |
| 374 | 10388 | return rv.error(); | |
| 375 | |||
| 376 | 10562 | auto val = rv.value(); | |
| 377 | 10562 | v.name = std::get<0>(val); | |
| 378 | 10562 | v.value = std::get<2>(val).value; | |
| 379 | 10562 | v.has_obs_fold = std::get<2>(val).has_obs_fold; | |
| 380 | |||
| 381 | 10562 | return v; | |
| 382 | } | ||
| 383 | |||
| 384 | //------------------------------------------------ | ||
| 385 | |||
| 386 | void | ||
| 387 | 244 | remove_obs_fold( | |
| 388 | char* it, | ||
| 389 | char const* const end) noexcept | ||
| 390 | { | ||
| 391 |
2/2✓ Branch 0 taken 2236 times.
✓ Branch 1 taken 26 times.
|
2262 | while(it != end) |
| 392 | { | ||
| 393 |
2/2✓ Branch 0 taken 1637 times.
✓ Branch 1 taken 599 times.
|
2236 | if(*it != '\r') |
| 394 | { | ||
| 395 | 1637 | ++it; | |
| 396 | 1637 | continue; | |
| 397 | } | ||
| 398 |
2/2✓ Branch 0 taken 218 times.
✓ Branch 1 taken 381 times.
|
599 | if(end - it < 3) |
| 399 | 218 | break; | |
| 400 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 381 times.
|
381 | BOOST_ASSERT(it[1] == '\n'); |
| 401 |
5/6✓ Branch 0 taken 381 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 378 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 378 times.
✓ Branch 5 taken 3 times.
|
762 | if( it[1] == '\n' && |
| 402 | 381 | ws(it[2])) | |
| 403 | { | ||
| 404 | 378 | it[0] = ' '; | |
| 405 | 378 | it[1] = ' '; | |
| 406 | 378 | it += 3; | |
| 407 | } | ||
| 408 | else | ||
| 409 | { | ||
| 410 | 3 | ++it; | |
| 411 | } | ||
| 412 | } | ||
| 413 | 244 | } | |
| 414 | |||
| 415 | } // detail | ||
| 416 | } // http | ||
| 417 | } // boost | ||
| 418 |