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(char const* it, char const* last, std::uint64_t& v)
  145. {
  146. if(it == last)
  147. return false;
  148. std::uint64_t tmp = 0;
  149. do
  150. {
  151. if((! is_digit(*it)) ||
  152. tmp > (std::numeric_limits<std::uint64_t>::max)() / 10)
  153. return false;
  154. tmp *= 10;
  155. std::uint64_t const d = *it - '0';
  156. if((std::numeric_limits<std::uint64_t>::max)() - tmp < d)
  157. return false;
  158. tmp += d;
  159. }
  160. while(++it != last);
  161. v = tmp;
  162. return true;
  163. }
  164. bool
  165. basic_parser_base::
  166. parse_hex(char const*& it, std::uint64_t& v)
  167. {
  168. unsigned char d;
  169. if(! unhex(d, *it))
  170. return false;
  171. std::uint64_t tmp = 0;
  172. do
  173. {
  174. if(tmp > (std::numeric_limits<std::uint64_t>::max)() / 16)
  175. return false;
  176. tmp *= 16;
  177. if((std::numeric_limits<std::uint64_t>::max)() - tmp < d)
  178. return false;
  179. tmp += d;
  180. }
  181. while(unhex(d, *++it));
  182. v = tmp;
  183. return true;
  184. }
  185. char const*
  186. basic_parser_base::
  187. find_eom(char const* p, char const* last)
  188. {
  189. for(;;)
  190. {
  191. if(p + 4 > last)
  192. return nullptr;
  193. if(p[3] != '\n')
  194. {
  195. if(p[3] == '\r')
  196. ++p;
  197. else
  198. p += 4;
  199. }
  200. else if(p[2] != '\r')
  201. {
  202. p += 4;
  203. }
  204. else if(p[1] != '\n')
  205. {
  206. p += 2;
  207. }
  208. else if(p[0] != '\r')
  209. {
  210. p += 2;
  211. }
  212. else
  213. {
  214. return p + 4;
  215. }
  216. }
  217. }
  218. //--------------------------------------------------------------------------
  219. char const*
  220. basic_parser_base::
  221. parse_token_to_eol(
  222. char const* p,
  223. char const* last,
  224. char const*& token_last,
  225. error_code& ec)
  226. {
  227. for(;; ++p)
  228. {
  229. if(p >= last)
  230. {
  231. ec = error::need_more;
  232. return p;
  233. }
  234. if(BOOST_UNLIKELY(! is_print(*p)))
  235. if((BOOST_LIKELY(static_cast<
  236. unsigned char>(*p) < '\040') &&
  237. BOOST_LIKELY(*p != 9)) ||
  238. BOOST_UNLIKELY(*p == 127))
  239. goto found_control;
  240. }
  241. found_control:
  242. if(BOOST_LIKELY(*p == '\r'))
  243. {
  244. if(++p >= last)
  245. {
  246. ec = error::need_more;
  247. return last;
  248. }
  249. if(*p++ != '\n')
  250. {
  251. ec = error::bad_line_ending;
  252. return last;
  253. }
  254. token_last = p - 2;
  255. }
  256. #if 0
  257. // VFALCO This allows `\n` by itself
  258. // to terminate a line
  259. else if(*p == '\n')
  260. {
  261. token_last = p;
  262. ++p;
  263. }
  264. #endif
  265. else
  266. {
  267. // invalid character
  268. return nullptr;
  269. }
  270. return p;
  271. }
  272. bool
  273. basic_parser_base::
  274. parse_crlf(char const*& it)
  275. {
  276. if( it[0] != '\r' || it[1] != '\n')
  277. return false;
  278. it += 2;
  279. return true;
  280. }
  281. void
  282. basic_parser_base::
  283. parse_method(
  284. char const*& it, char const* last,
  285. string_view& result, error_code& ec)
  286. {
  287. // parse token SP
  288. auto const first = it;
  289. for(;; ++it)
  290. {
  291. if(it + 1 > last)
  292. {
  293. ec = error::need_more;
  294. return;
  295. }
  296. if(! detail::is_token_char(*it))
  297. break;
  298. }
  299. if(it + 1 > last)
  300. {
  301. ec = error::need_more;
  302. return;
  303. }
  304. if(*it != ' ')
  305. {
  306. ec = error::bad_method;
  307. return;
  308. }
  309. if(it == first)
  310. {
  311. // cannot be empty
  312. ec = error::bad_method;
  313. return;
  314. }
  315. result = make_string(first, it++);
  316. }
  317. void
  318. basic_parser_base::
  319. parse_target(
  320. char const*& it, char const* last,
  321. string_view& result, error_code& ec)
  322. {
  323. // parse target SP
  324. auto const first = it;
  325. for(;; ++it)
  326. {
  327. if(it + 1 > last)
  328. {
  329. ec = error::need_more;
  330. return;
  331. }
  332. if(! is_pathchar(*it))
  333. break;
  334. }
  335. if(it + 1 > last)
  336. {
  337. ec = error::need_more;
  338. return;
  339. }
  340. if(*it != ' ')
  341. {
  342. ec = error::bad_target;
  343. return;
  344. }
  345. if(it == first)
  346. {
  347. // cannot be empty
  348. ec = error::bad_target;
  349. return;
  350. }
  351. result = make_string(first, it++);
  352. }
  353. void
  354. basic_parser_base::
  355. parse_version(
  356. char const*& it, char const* last,
  357. int& result, error_code& ec)
  358. {
  359. if(it + 8 > last)
  360. {
  361. ec = error::need_more;
  362. return;
  363. }
  364. if(*it++ != 'H')
  365. {
  366. ec = error::bad_version;
  367. return;
  368. }
  369. if(*it++ != 'T')
  370. {
  371. ec = error::bad_version;
  372. return;
  373. }
  374. if(*it++ != 'T')
  375. {
  376. ec = error::bad_version;
  377. return;
  378. }
  379. if(*it++ != 'P')
  380. {
  381. ec = error::bad_version;
  382. return;
  383. }
  384. if(*it++ != '/')
  385. {
  386. ec = error::bad_version;
  387. return;
  388. }
  389. if(! is_digit(*it))
  390. {
  391. ec = error::bad_version;
  392. return;
  393. }
  394. result = 10 * (*it++ - '0');
  395. if(*it++ != '.')
  396. {
  397. ec = error::bad_version;
  398. return;
  399. }
  400. if(! is_digit(*it))
  401. {
  402. ec = error::bad_version;
  403. return;
  404. }
  405. result += *it++ - '0';
  406. }
  407. void
  408. basic_parser_base::
  409. parse_status(
  410. char const*& it, char const* last,
  411. unsigned short& result, error_code& ec)
  412. {
  413. // parse 3(digit) SP
  414. if(it + 4 > last)
  415. {
  416. ec = error::need_more;
  417. return;
  418. }
  419. if(! is_digit(*it))
  420. {
  421. ec = error::bad_status;
  422. return;
  423. }
  424. result = 100 * (*it++ - '0');
  425. if(! is_digit(*it))
  426. {
  427. ec = error::bad_status;
  428. return;
  429. }
  430. result += 10 * (*it++ - '0');
  431. if(! is_digit(*it))
  432. {
  433. ec = error::bad_status;
  434. return;
  435. }
  436. result += *it++ - '0';
  437. if(*it++ != ' ')
  438. {
  439. ec = error::bad_status;
  440. return;
  441. }
  442. }
  443. void
  444. basic_parser_base::
  445. parse_reason(
  446. char const*& it, char const* last,
  447. string_view& result, error_code& ec)
  448. {
  449. auto const first = it;
  450. char const* token_last = nullptr;
  451. auto p = parse_token_to_eol(
  452. it, last, token_last, ec);
  453. if(ec)
  454. return;
  455. if(! p)
  456. {
  457. ec = error::bad_reason;
  458. return;
  459. }
  460. result = make_string(first, token_last);
  461. it = p;
  462. }
  463. void
  464. basic_parser_base::
  465. parse_field(
  466. char const*& p,
  467. char const* last,
  468. string_view& name,
  469. string_view& value,
  470. static_string<max_obs_fold>& buf,
  471. error_code& ec)
  472. {
  473. /* header-field = field-name ":" OWS field-value OWS
  474. field-name = token
  475. field-value = *( field-content / obs-fold )
  476. field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
  477. field-vchar = VCHAR / obs-text
  478. obs-fold = CRLF 1*( SP / HTAB )
  479. ; obsolete line folding
  480. ; see Section 3.2.4
  481. token = 1*<any CHAR except CTLs or separators>
  482. CHAR = <any US-ASCII character (octets 0 - 127)>
  483. sep = "(" | ")" | "<" | ">" | "@"
  484. | "," | ";" | ":" | "\" | <">
  485. | "/" | "[" | "]" | "?" | "="
  486. | "{" | "}" | SP | HT
  487. */
  488. static char const* is_token =
  489. "\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"
  490. "\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"
  491. "\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"
  492. "\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"
  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\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"
  495. "\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"
  496. "\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";
  497. // name
  498. BOOST_ALIGNMENT(16) static const char ranges1[] =
  499. "\x00 " /* control chars and up to SP */
  500. "\"\"" /* 0x22 */
  501. "()" /* 0x28,0x29 */
  502. ",," /* 0x2c */
  503. "//" /* 0x2f */
  504. ":@" /* 0x3a-0x40 */
  505. "[]" /* 0x5b-0x5d */
  506. "{\377"; /* 0x7b-0xff */
  507. auto first = p;
  508. bool found;
  509. std::tie(p, found) = find_fast(
  510. p, last, ranges1, sizeof(ranges1)-1);
  511. if(! found && p >= last)
  512. {
  513. ec = error::need_more;
  514. return;
  515. }
  516. for(;;)
  517. {
  518. if(*p == ':')
  519. break;
  520. if(! is_token[static_cast<
  521. unsigned char>(*p)])
  522. {
  523. ec = error::bad_field;
  524. return;
  525. }
  526. ++p;
  527. if(p >= last)
  528. {
  529. ec = error::need_more;
  530. return;
  531. }
  532. }
  533. if(p == first)
  534. {
  535. // empty name
  536. ec = error::bad_field;
  537. return;
  538. }
  539. name = make_string(first, p);
  540. ++p; // eat ':'
  541. char const* token_last = nullptr;
  542. for(;;)
  543. {
  544. // eat leading ' ' and '\t'
  545. for(;;++p)
  546. {
  547. if(p + 1 > last)
  548. {
  549. ec = error::need_more;
  550. return;
  551. }
  552. if(! (*p == ' ' || *p == '\t'))
  553. break;
  554. }
  555. // parse to CRLF
  556. first = p;
  557. p = parse_token_to_eol(p, last, token_last, ec);
  558. if(ec)
  559. return;
  560. if(! p)
  561. {
  562. ec = error::bad_value;
  563. return;
  564. }
  565. // Look 1 char past the CRLF to handle obs-fold.
  566. if(p + 1 > last)
  567. {
  568. ec = error::need_more;
  569. return;
  570. }
  571. token_last =
  572. trim_back(token_last, first);
  573. if(*p != ' ' && *p != '\t')
  574. {
  575. value = make_string(first, token_last);
  576. return;
  577. }
  578. ++p;
  579. if(token_last != first)
  580. break;
  581. }
  582. buf.resize(0);
  583. buf.append(first, token_last);
  584. BOOST_ASSERT(! buf.empty());
  585. #ifndef BOOST_NO_EXCEPTIONS
  586. try
  587. #endif
  588. {
  589. for(;;)
  590. {
  591. // eat leading ' ' and '\t'
  592. for(;;++p)
  593. {
  594. if(p + 1 > last)
  595. {
  596. ec = error::need_more;
  597. return;
  598. }
  599. if(! (*p == ' ' || *p == '\t'))
  600. break;
  601. }
  602. // parse to CRLF
  603. first = p;
  604. p = parse_token_to_eol(p, last, token_last, ec);
  605. if(ec)
  606. return;
  607. if(! p)
  608. {
  609. ec = error::bad_value;
  610. return;
  611. }
  612. // Look 1 char past the CRLF to handle obs-fold.
  613. if(p + 1 > last)
  614. {
  615. ec = error::need_more;
  616. return;
  617. }
  618. token_last = trim_back(token_last, first);
  619. if(first != token_last)
  620. {
  621. buf.push_back(' ');
  622. buf.append(first, token_last);
  623. }
  624. if(*p != ' ' && *p != '\t')
  625. {
  626. value = {buf.data(), buf.size()};
  627. return;
  628. }
  629. ++p;
  630. }
  631. }
  632. #ifndef BOOST_NO_EXCEPTIONS
  633. catch(std::length_error const&)
  634. {
  635. ec = error::header_limit;
  636. return;
  637. }
  638. #endif
  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