basic_parser.ipp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_HTTP_DETAIL_BASIC_PARSER_IPP
  10. #define BOOST_BEAST_HTTP_DETAIL_BASIC_PARSER_IPP
  11. #include <boost/beast/http/detail/basic_parser.hpp>
  12. #include <limits>
  13. namespace boost {
  14. namespace beast {
  15. namespace http {
  16. namespace detail {
  17. char const*
  18. basic_parser_base::
  19. trim_front(char const* it, char const* end)
  20. {
  21. while(it != end)
  22. {
  23. if(*it != ' ' && *it != '\t')
  24. break;
  25. ++it;
  26. }
  27. return it;
  28. }
  29. char const*
  30. basic_parser_base::
  31. trim_back(
  32. char const* it, char const* first)
  33. {
  34. while(it != first)
  35. {
  36. auto const c = it[-1];
  37. if(c != ' ' && c != '\t')
  38. break;
  39. --it;
  40. }
  41. return it;
  42. }
  43. bool
  44. basic_parser_base::
  45. is_pathchar(char c)
  46. {
  47. // VFALCO This looks the same as the one below...
  48. // TEXT = <any OCTET except CTLs, and excluding LWS>
  49. static bool constexpr tab[256] = {
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16
  52. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 32
  53. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 48
  54. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64
  55. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80
  56. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96
  57. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 112
  58. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 128
  59. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 144
  60. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 160
  61. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 176
  62. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 192
  63. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 208
  64. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 224
  65. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 240
  66. };
  67. return tab[static_cast<unsigned char>(c)];
  68. }
  69. bool
  70. basic_parser_base::
  71. unhex(unsigned char& d, char c)
  72. {
  73. static signed char constexpr tab[256] = {
  74. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0
  75. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 16
  76. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 32
  77. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, // 48
  78. -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 64
  79. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 80
  80. -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 96
  81. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 112
  82. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 128
  83. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 144
  84. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 160
  85. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 176
  86. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 192
  87. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 208
  88. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 224
  89. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 // 240
  90. };
  91. d = static_cast<unsigned char>(
  92. tab[static_cast<unsigned char>(c)]);
  93. return d != static_cast<unsigned char>(-1);
  94. }
  95. //--------------------------------------------------------------------------
  96. std::pair<char const*, bool>
  97. basic_parser_base::
  98. find_fast(
  99. char const* buf,
  100. char const* buf_end,
  101. char const* ranges,
  102. size_t ranges_size)
  103. {
  104. bool found = false;
  105. boost::ignore_unused(buf_end, ranges, ranges_size);
  106. return {buf, found};
  107. }
  108. // VFALCO Can SIMD help this?
  109. char const*
  110. basic_parser_base::
  111. find_eol(
  112. char const* it, char const* last,
  113. error_code& ec)
  114. {
  115. for(;;)
  116. {
  117. if(it == last)
  118. {
  119. ec = {};
  120. return nullptr;
  121. }
  122. if(*it == '\r')
  123. {
  124. if(++it == last)
  125. {
  126. ec = {};
  127. return nullptr;
  128. }
  129. if(*it != '\n')
  130. {
  131. ec = error::bad_line_ending;
  132. return nullptr;
  133. }
  134. ec = {};
  135. return ++it;
  136. }
  137. // VFALCO Should we handle the legacy case
  138. // for lines terminated with a single '\n'?
  139. ++it;
  140. }
  141. }
  142. bool
  143. basic_parser_base::
  144. parse_dec(
  145. string_view s,
  146. std::uint64_t& v)
  147. {
  148. char const* it = s.data();
  149. char const* last = it + s.size();
  150. if(it == last)
  151. return false;
  152. std::uint64_t tmp = 0;
  153. do
  154. {
  155. if((! is_digit(*it)) ||
  156. tmp > (std::numeric_limits<std::uint64_t>::max)() / 10)
  157. return false;
  158. tmp *= 10;
  159. std::uint64_t const d = *it - '0';
  160. if((std::numeric_limits<std::uint64_t>::max)() - tmp < d)
  161. return false;
  162. tmp += d;
  163. }
  164. while(++it != last);
  165. v = tmp;
  166. return true;
  167. }
  168. bool
  169. basic_parser_base::
  170. parse_hex(char const*& it, std::uint64_t& v)
  171. {
  172. unsigned char d;
  173. if(! unhex(d, *it))
  174. return false;
  175. std::uint64_t tmp = 0;
  176. do
  177. {
  178. if(tmp > (std::numeric_limits<std::uint64_t>::max)() / 16)
  179. return false;
  180. tmp *= 16;
  181. if((std::numeric_limits<std::uint64_t>::max)() - tmp < d)
  182. return false;
  183. tmp += d;
  184. }
  185. while(unhex(d, *++it));
  186. v = tmp;
  187. return true;
  188. }
  189. char const*
  190. basic_parser_base::
  191. find_eom(char const* p, char const* last)
  192. {
  193. for(;;)
  194. {
  195. if(p + 4 > last)
  196. return nullptr;
  197. if(p[3] != '\n')
  198. {
  199. if(p[3] == '\r')
  200. ++p;
  201. else
  202. p += 4;
  203. }
  204. else if(p[2] != '\r')
  205. {
  206. p += 4;
  207. }
  208. else if(p[1] != '\n')
  209. {
  210. p += 2;
  211. }
  212. else if(p[0] != '\r')
  213. {
  214. p += 2;
  215. }
  216. else
  217. {
  218. return p + 4;
  219. }
  220. }
  221. }
  222. //--------------------------------------------------------------------------
  223. char const*
  224. basic_parser_base::
  225. parse_token_to_eol(
  226. char const* p,
  227. char const* last,
  228. char const*& token_last,
  229. error_code& ec)
  230. {
  231. for(;; ++p)
  232. {
  233. if(p >= last)
  234. {
  235. ec = error::need_more;
  236. return p;
  237. }
  238. if(BOOST_UNLIKELY(! is_print(*p)))
  239. if((BOOST_LIKELY(static_cast<
  240. unsigned char>(*p) < '\040') &&
  241. BOOST_LIKELY(*p != 9)) ||
  242. BOOST_UNLIKELY(*p == 127))
  243. goto found_control;
  244. }
  245. found_control:
  246. if(BOOST_LIKELY(*p == '\r'))
  247. {
  248. if(++p >= last)
  249. {
  250. ec = error::need_more;
  251. return last;
  252. }
  253. if(*p++ != '\n')
  254. {
  255. ec = error::bad_line_ending;
  256. return last;
  257. }
  258. token_last = p - 2;
  259. }
  260. #if 0
  261. // VFALCO This allows `\n` by itself
  262. // to terminate a line
  263. else if(*p == '\n')
  264. {
  265. token_last = p;
  266. ++p;
  267. }
  268. #endif
  269. else
  270. {
  271. // invalid character
  272. return nullptr;
  273. }
  274. return p;
  275. }
  276. bool
  277. basic_parser_base::
  278. parse_crlf(char const*& it)
  279. {
  280. if( it[0] != '\r' || it[1] != '\n')
  281. return false;
  282. it += 2;
  283. return true;
  284. }
  285. void
  286. basic_parser_base::
  287. parse_method(
  288. char const*& it, char const* last,
  289. string_view& result, error_code& ec)
  290. {
  291. // parse token SP
  292. auto const first = it;
  293. for(;; ++it)
  294. {
  295. if(it + 1 > last)
  296. {
  297. ec = error::need_more;
  298. return;
  299. }
  300. if(! detail::is_token_char(*it))
  301. break;
  302. }
  303. if(it + 1 > last)
  304. {
  305. ec = error::need_more;
  306. return;
  307. }
  308. if(*it != ' ')
  309. {
  310. ec = error::bad_method;
  311. return;
  312. }
  313. if(it == first)
  314. {
  315. // cannot be empty
  316. ec = error::bad_method;
  317. return;
  318. }
  319. result = make_string(first, it++);
  320. }
  321. void
  322. basic_parser_base::
  323. parse_target(
  324. char const*& it, char const* last,
  325. string_view& result, error_code& ec)
  326. {
  327. // parse target SP
  328. auto const first = it;
  329. for(;; ++it)
  330. {
  331. if(it + 1 > last)
  332. {
  333. ec = error::need_more;
  334. return;
  335. }
  336. if(! is_pathchar(*it))
  337. break;
  338. }
  339. if(it + 1 > last)
  340. {
  341. ec = error::need_more;
  342. return;
  343. }
  344. if(*it != ' ')
  345. {
  346. ec = error::bad_target;
  347. return;
  348. }
  349. if(it == first)
  350. {
  351. // cannot be empty
  352. ec = error::bad_target;
  353. return;
  354. }
  355. result = make_string(first, it++);
  356. }
  357. void
  358. basic_parser_base::
  359. parse_version(
  360. char const*& it, char const* last,
  361. int& result, error_code& ec)
  362. {
  363. if(it + 8 > last)
  364. {
  365. ec = error::need_more;
  366. return;
  367. }
  368. if(*it++ != 'H')
  369. {
  370. ec = error::bad_version;
  371. return;
  372. }
  373. if(*it++ != 'T')
  374. {
  375. ec = error::bad_version;
  376. return;
  377. }
  378. if(*it++ != 'T')
  379. {
  380. ec = error::bad_version;
  381. return;
  382. }
  383. if(*it++ != 'P')
  384. {
  385. ec = error::bad_version;
  386. return;
  387. }
  388. if(*it++ != '/')
  389. {
  390. ec = error::bad_version;
  391. return;
  392. }
  393. if(! is_digit(*it))
  394. {
  395. ec = error::bad_version;
  396. return;
  397. }
  398. result = 10 * (*it++ - '0');
  399. if(*it++ != '.')
  400. {
  401. ec = error::bad_version;
  402. return;
  403. }
  404. if(! is_digit(*it))
  405. {
  406. ec = error::bad_version;
  407. return;
  408. }
  409. result += *it++ - '0';
  410. }
  411. void
  412. basic_parser_base::
  413. parse_status(
  414. char const*& it, char const* last,
  415. unsigned short& result, error_code& ec)
  416. {
  417. // parse 3(digit) SP
  418. if(it + 4 > last)
  419. {
  420. ec = error::need_more;
  421. return;
  422. }
  423. if(! is_digit(*it))
  424. {
  425. ec = error::bad_status;
  426. return;
  427. }
  428. result = 100 * (*it++ - '0');
  429. if(! is_digit(*it))
  430. {
  431. ec = error::bad_status;
  432. return;
  433. }
  434. result += 10 * (*it++ - '0');
  435. if(! is_digit(*it))
  436. {
  437. ec = error::bad_status;
  438. return;
  439. }
  440. result += *it++ - '0';
  441. if(*it++ != ' ')
  442. {
  443. ec = error::bad_status;
  444. return;
  445. }
  446. }
  447. void
  448. basic_parser_base::
  449. parse_reason(
  450. char const*& it, char const* last,
  451. string_view& result, error_code& ec)
  452. {
  453. auto const first = it;
  454. char const* token_last = nullptr;
  455. auto p = parse_token_to_eol(
  456. it, last, token_last, ec);
  457. if(ec)
  458. return;
  459. if(! p)
  460. {
  461. ec = error::bad_reason;
  462. return;
  463. }
  464. result = make_string(first, token_last);
  465. it = p;
  466. }
  467. void
  468. basic_parser_base::
  469. parse_field(
  470. char const*& p,
  471. char const* last,
  472. string_view& name,
  473. string_view& value,
  474. beast::detail::char_buffer<max_obs_fold>& buf,
  475. error_code& ec)
  476. {
  477. /* header-field = field-name ":" OWS field-value OWS
  478. field-name = token
  479. field-value = *( field-content / obs-fold )
  480. field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
  481. field-vchar = VCHAR / obs-text
  482. obs-fold = CRLF 1*( SP / HTAB )
  483. ; obsolete line folding
  484. ; see Section 3.2.4
  485. token = 1*<any CHAR except CTLs or separators>
  486. CHAR = <any US-ASCII character (octets 0 - 127)>
  487. sep = "(" | ")" | "<" | ">" | "@"
  488. | "," | ";" | ":" | "\" | <">
  489. | "/" | "[" | "]" | "?" | "="
  490. | "{" | "}" | SP | HT
  491. */
  492. static char const* is_token =
  493. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  494. "\0\1\0\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0"
  495. "\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1"
  496. "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0"
  497. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  498. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  499. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  500. "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  501. // name
  502. BOOST_ALIGNMENT(16) static const char ranges1[] =
  503. "\x00 " /* control chars and up to SP */
  504. "\"\"" /* 0x22 */
  505. "()" /* 0x28,0x29 */
  506. ",," /* 0x2c */
  507. "//" /* 0x2f */
  508. ":@" /* 0x3a-0x40 */
  509. "[]" /* 0x5b-0x5d */
  510. "{\377"; /* 0x7b-0xff */
  511. auto first = p;
  512. bool found;
  513. std::tie(p, found) = find_fast(
  514. p, last, ranges1, sizeof(ranges1)-1);
  515. if(! found && p >= last)
  516. {
  517. ec = error::need_more;
  518. return;
  519. }
  520. for(;;)
  521. {
  522. if(*p == ':')
  523. break;
  524. if(! is_token[static_cast<
  525. unsigned char>(*p)])
  526. {
  527. ec = error::bad_field;
  528. return;
  529. }
  530. ++p;
  531. if(p >= last)
  532. {
  533. ec = error::need_more;
  534. return;
  535. }
  536. }
  537. if(p == first)
  538. {
  539. // empty name
  540. ec = error::bad_field;
  541. return;
  542. }
  543. name = make_string(first, p);
  544. ++p; // eat ':'
  545. char const* token_last = nullptr;
  546. for(;;)
  547. {
  548. // eat leading ' ' and '\t'
  549. for(;;++p)
  550. {
  551. if(p + 1 > last)
  552. {
  553. ec = error::need_more;
  554. return;
  555. }
  556. if(! (*p == ' ' || *p == '\t'))
  557. break;
  558. }
  559. // parse to CRLF
  560. first = p;
  561. p = parse_token_to_eol(p, last, token_last, ec);
  562. if(ec)
  563. return;
  564. if(! p)
  565. {
  566. ec = error::bad_value;
  567. return;
  568. }
  569. // Look 1 char past the CRLF to handle obs-fold.
  570. if(p + 1 > last)
  571. {
  572. ec = error::need_more;
  573. return;
  574. }
  575. token_last =
  576. trim_back(token_last, first);
  577. if(*p != ' ' && *p != '\t')
  578. {
  579. value = make_string(first, token_last);
  580. return;
  581. }
  582. ++p;
  583. if(token_last != first)
  584. break;
  585. }
  586. buf.clear();
  587. if (!buf.try_append(first, token_last))
  588. {
  589. ec = error::header_limit;
  590. return;
  591. }
  592. BOOST_ASSERT(! buf.empty());
  593. for(;;)
  594. {
  595. // eat leading ' ' and '\t'
  596. for(;;++p)
  597. {
  598. if(p + 1 > last)
  599. {
  600. ec = error::need_more;
  601. return;
  602. }
  603. if(! (*p == ' ' || *p == '\t'))
  604. break;
  605. }
  606. // parse to CRLF
  607. first = p;
  608. p = parse_token_to_eol(p, last, token_last, ec);
  609. if(ec)
  610. return;
  611. if(! p)
  612. {
  613. ec = error::bad_value;
  614. return;
  615. }
  616. // Look 1 char past the CRLF to handle obs-fold.
  617. if(p + 1 > last)
  618. {
  619. ec = error::need_more;
  620. return;
  621. }
  622. token_last = trim_back(token_last, first);
  623. if(first != token_last)
  624. {
  625. if (!buf.try_push_back(' ') ||
  626. !buf.try_append(first, token_last))
  627. {
  628. ec = error::header_limit;
  629. return;
  630. }
  631. }
  632. if(*p != ' ' && *p != '\t')
  633. {
  634. value = {buf.data(), buf.size()};
  635. return;
  636. }
  637. ++p;
  638. }
  639. }
  640. void
  641. basic_parser_base::
  642. parse_chunk_extensions(
  643. char const*& it,
  644. char const* last,
  645. error_code& ec)
  646. {
  647. /*
  648. chunk-ext = *( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val ] )
  649. BWS = *( SP / HTAB ) ; "Bad White Space"
  650. chunk-ext-name = token
  651. chunk-ext-val = token / quoted-string
  652. token = 1*tchar
  653. quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
  654. qdtext = HTAB / SP / "!" / %x23-5B ; '#'-'[' / %x5D-7E ; ']'-'~' / obs-text
  655. quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
  656. obs-text = %x80-FF
  657. https://www.rfc-editor.org/errata_search.php?rfc=7230&eid=4667
  658. */
  659. loop:
  660. if(it == last)
  661. {
  662. ec = error::need_more;
  663. return;
  664. }
  665. if(*it != ' ' && *it != '\t' && *it != ';')
  666. return;
  667. // BWS
  668. if(*it == ' ' || *it == '\t')
  669. {
  670. for(;;)
  671. {
  672. ++it;
  673. if(it == last)
  674. {
  675. ec = error::need_more;
  676. return;
  677. }
  678. if(*it != ' ' && *it != '\t')
  679. break;
  680. }
  681. }
  682. // ';'
  683. if(*it != ';')
  684. {
  685. ec = error::bad_chunk_extension;
  686. return;
  687. }
  688. semi:
  689. ++it; // skip ';'
  690. // BWS
  691. for(;;)
  692. {
  693. if(it == last)
  694. {
  695. ec = error::need_more;
  696. return;
  697. }
  698. if(*it != ' ' && *it != '\t')
  699. break;
  700. ++it;
  701. }
  702. // chunk-ext-name
  703. if(! detail::is_token_char(*it))
  704. {
  705. ec = error::bad_chunk_extension;
  706. return;
  707. }
  708. for(;;)
  709. {
  710. ++it;
  711. if(it == last)
  712. {
  713. ec = error::need_more;
  714. return;
  715. }
  716. if(! detail::is_token_char(*it))
  717. break;
  718. }
  719. // BWS [ ";" / "=" ]
  720. {
  721. bool bws;
  722. if(*it == ' ' || *it == '\t')
  723. {
  724. for(;;)
  725. {
  726. ++it;
  727. if(it == last)
  728. {
  729. ec = error::need_more;
  730. return;
  731. }
  732. if(*it != ' ' && *it != '\t')
  733. break;
  734. }
  735. bws = true;
  736. }
  737. else
  738. {
  739. bws = false;
  740. }
  741. if(*it == ';')
  742. goto semi;
  743. if(*it != '=')
  744. {
  745. if(bws)
  746. ec = error::bad_chunk_extension;
  747. return;
  748. }
  749. ++it; // skip '='
  750. }
  751. // BWS
  752. for(;;)
  753. {
  754. if(it == last)
  755. {
  756. ec = error::need_more;
  757. return;
  758. }
  759. if(*it != ' ' && *it != '\t')
  760. break;
  761. ++it;
  762. }
  763. // chunk-ext-val
  764. if(*it != '"')
  765. {
  766. // token
  767. if(! detail::is_token_char(*it))
  768. {
  769. ec = error::bad_chunk_extension;
  770. return;
  771. }
  772. for(;;)
  773. {
  774. ++it;
  775. if(it == last)
  776. {
  777. ec = error::need_more;
  778. return;
  779. }
  780. if(! detail::is_token_char(*it))
  781. break;
  782. }
  783. }
  784. else
  785. {
  786. // quoted-string
  787. for(;;)
  788. {
  789. ++it;
  790. if(it == last)
  791. {
  792. ec = error::need_more;
  793. return;
  794. }
  795. if(*it == '"')
  796. break;
  797. if(*it == '\\')
  798. {
  799. ++it;
  800. if(it == last)
  801. {
  802. ec = error::need_more;
  803. return;
  804. }
  805. }
  806. }
  807. ++it;
  808. }
  809. goto loop;
  810. }
  811. } // detail
  812. } // http
  813. } // beast
  814. } // boost
  815. #endif