basic_parser.ipp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. BOOST_BEAST_ASSIGN_EC(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. //--------------------------------------------------------------------------
  190. char const*
  191. basic_parser_base::
  192. parse_token_to_eol(
  193. char const* p,
  194. char const* last,
  195. char const*& token_last,
  196. error_code& ec)
  197. {
  198. for(;; ++p)
  199. {
  200. if(p >= last)
  201. {
  202. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  203. return p;
  204. }
  205. if(BOOST_UNLIKELY(! is_print(*p)))
  206. if((BOOST_LIKELY(static_cast<
  207. unsigned char>(*p) < '\040') &&
  208. BOOST_LIKELY(*p != 9)) ||
  209. BOOST_UNLIKELY(*p == 127))
  210. goto found_control;
  211. }
  212. found_control:
  213. if(BOOST_LIKELY(*p == '\r'))
  214. {
  215. if(++p >= last)
  216. {
  217. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  218. return last;
  219. }
  220. if(*p++ != '\n')
  221. {
  222. BOOST_BEAST_ASSIGN_EC(ec, error::bad_line_ending);
  223. return last;
  224. }
  225. token_last = p - 2;
  226. }
  227. #if 0
  228. // VFALCO This allows `\n` by itself
  229. // to terminate a line
  230. else if(*p == '\n')
  231. {
  232. token_last = p;
  233. ++p;
  234. }
  235. #endif
  236. else
  237. {
  238. // invalid character
  239. return nullptr;
  240. }
  241. return p;
  242. }
  243. bool
  244. basic_parser_base::
  245. parse_crlf(char const*& it)
  246. {
  247. if( it[0] != '\r' || it[1] != '\n')
  248. return false;
  249. it += 2;
  250. return true;
  251. }
  252. void
  253. basic_parser_base::
  254. parse_method(
  255. char const*& it, char const* last,
  256. string_view& result, error_code& ec)
  257. {
  258. // parse token SP
  259. auto const first = it;
  260. for(;; ++it)
  261. {
  262. if(it + 1 > last)
  263. {
  264. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  265. return;
  266. }
  267. if(! detail::is_token_char(*it))
  268. break;
  269. }
  270. if(it + 1 > last)
  271. {
  272. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  273. return;
  274. }
  275. if(*it != ' ')
  276. {
  277. BOOST_BEAST_ASSIGN_EC(ec, error::bad_method);
  278. return;
  279. }
  280. if(it == first)
  281. {
  282. // cannot be empty
  283. BOOST_BEAST_ASSIGN_EC(ec, error::bad_method);
  284. return;
  285. }
  286. result = make_string(first, it++);
  287. }
  288. void
  289. basic_parser_base::
  290. parse_target(
  291. char const*& it, char const* last,
  292. string_view& result, error_code& ec)
  293. {
  294. // parse target SP
  295. auto const first = it;
  296. for(;; ++it)
  297. {
  298. if(it + 1 > last)
  299. {
  300. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  301. return;
  302. }
  303. if(! is_pathchar(*it))
  304. break;
  305. }
  306. if(it + 1 > last)
  307. {
  308. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  309. return;
  310. }
  311. if(*it != ' ')
  312. {
  313. BOOST_BEAST_ASSIGN_EC(ec, error::bad_target);
  314. return;
  315. }
  316. if(it == first)
  317. {
  318. // cannot be empty
  319. BOOST_BEAST_ASSIGN_EC(ec, error::bad_target);
  320. return;
  321. }
  322. result = make_string(first, it++);
  323. }
  324. void
  325. basic_parser_base::
  326. parse_version(
  327. char const*& it, char const* last,
  328. int& result, error_code& ec)
  329. {
  330. if(it + 8 > last)
  331. {
  332. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  333. return;
  334. }
  335. if(*it++ != 'H')
  336. {
  337. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  338. return;
  339. }
  340. if(*it++ != 'T')
  341. {
  342. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  343. return;
  344. }
  345. if(*it++ != 'T')
  346. {
  347. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  348. return;
  349. }
  350. if(*it++ != 'P')
  351. {
  352. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  353. return;
  354. }
  355. if(*it++ != '/')
  356. {
  357. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  358. return;
  359. }
  360. if(! is_digit(*it))
  361. {
  362. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  363. return;
  364. }
  365. result = 10 * (*it++ - '0');
  366. if(*it++ != '.')
  367. {
  368. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  369. return;
  370. }
  371. if(! is_digit(*it))
  372. {
  373. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  374. return;
  375. }
  376. result += *it++ - '0';
  377. }
  378. void
  379. basic_parser_base::
  380. parse_status(
  381. char const*& it, char const* last,
  382. unsigned short& result, error_code& ec)
  383. {
  384. // parse 3(digit) SP
  385. if(it + 4 > last)
  386. {
  387. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  388. return;
  389. }
  390. if(! is_digit(*it))
  391. {
  392. BOOST_BEAST_ASSIGN_EC(ec, error::bad_status);
  393. return;
  394. }
  395. result = 100 * (*it++ - '0');
  396. if(! is_digit(*it))
  397. {
  398. BOOST_BEAST_ASSIGN_EC(ec, error::bad_status);
  399. return;
  400. }
  401. result += 10 * (*it++ - '0');
  402. if(! is_digit(*it))
  403. {
  404. BOOST_BEAST_ASSIGN_EC(ec, error::bad_status);
  405. return;
  406. }
  407. result += *it++ - '0';
  408. if(*it++ != ' ')
  409. {
  410. BOOST_BEAST_ASSIGN_EC(ec, error::bad_status);
  411. return;
  412. }
  413. }
  414. void
  415. basic_parser_base::
  416. parse_reason(
  417. char const*& it, char const* last,
  418. string_view& result, error_code& ec)
  419. {
  420. auto const first = it;
  421. char const* token_last = nullptr;
  422. auto p = parse_token_to_eol(
  423. it, last, token_last, ec);
  424. if(ec)
  425. return;
  426. if(! p)
  427. {
  428. BOOST_BEAST_ASSIGN_EC(ec, error::bad_reason);
  429. return;
  430. }
  431. result = make_string(first, token_last);
  432. it = p;
  433. }
  434. void
  435. basic_parser_base::
  436. parse_field(
  437. char const*& p,
  438. char const* last,
  439. string_view& name,
  440. string_view& value,
  441. beast::detail::char_buffer<max_obs_fold>& buf,
  442. error_code& ec)
  443. {
  444. /* header-field = field-name ":" OWS field-value OWS
  445. field-name = token
  446. field-value = *( field-content / obs-fold )
  447. field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
  448. field-vchar = VCHAR / obs-text
  449. obs-fold = CRLF 1*( SP / HTAB )
  450. ; obsolete line folding
  451. ; see Section 3.2.4
  452. token = 1*<any CHAR except CTLs or separators>
  453. CHAR = <any US-ASCII character (octets 0 - 127)>
  454. sep = "(" | ")" | "<" | ">" | "@"
  455. | "," | ";" | ":" | "\" | <">
  456. | "/" | "[" | "]" | "?" | "="
  457. | "{" | "}" | SP | HT
  458. */
  459. static char const* is_token =
  460. "\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"
  461. "\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"
  462. "\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"
  463. "\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"
  464. "\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"
  465. "\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"
  466. "\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"
  467. "\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";
  468. // name
  469. BOOST_ALIGNMENT(16) static const char ranges1[] =
  470. "\x00 " /* control chars and up to SP */
  471. "\"\"" /* 0x22 */
  472. "()" /* 0x28,0x29 */
  473. ",," /* 0x2c */
  474. "//" /* 0x2f */
  475. ":@" /* 0x3a-0x40 */
  476. "[]" /* 0x5b-0x5d */
  477. "{\377"; /* 0x7b-0xff */
  478. auto first = p;
  479. bool found;
  480. std::tie(p, found) = find_fast(
  481. p, last, ranges1, sizeof(ranges1)-1);
  482. if(! found && p >= last)
  483. {
  484. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  485. return;
  486. }
  487. for(;;)
  488. {
  489. if(*p == ':')
  490. break;
  491. if(! is_token[static_cast<
  492. unsigned char>(*p)])
  493. {
  494. BOOST_BEAST_ASSIGN_EC(ec, error::bad_field);
  495. return;
  496. }
  497. ++p;
  498. if(p >= last)
  499. {
  500. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  501. return;
  502. }
  503. }
  504. if(p == first)
  505. {
  506. // empty name
  507. BOOST_BEAST_ASSIGN_EC(ec, error::bad_field);
  508. return;
  509. }
  510. name = make_string(first, p);
  511. ++p; // eat ':'
  512. char const* token_last = nullptr;
  513. for(;;)
  514. {
  515. // eat leading ' ' and '\t'
  516. for(;;++p)
  517. {
  518. if(p + 1 > last)
  519. {
  520. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  521. return;
  522. }
  523. if(! (*p == ' ' || *p == '\t'))
  524. break;
  525. }
  526. // parse to CRLF
  527. first = p;
  528. p = parse_token_to_eol(p, last, token_last, ec);
  529. if(ec)
  530. return;
  531. if(! p)
  532. {
  533. BOOST_BEAST_ASSIGN_EC(ec, error::bad_value);
  534. return;
  535. }
  536. // Look 1 char past the CRLF to handle obs-fold.
  537. if(p + 1 > last)
  538. {
  539. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  540. return;
  541. }
  542. token_last =
  543. trim_back(token_last, first);
  544. if(*p != ' ' && *p != '\t')
  545. {
  546. value = make_string(first, token_last);
  547. return;
  548. }
  549. ++p;
  550. if(token_last != first)
  551. break;
  552. }
  553. buf.clear();
  554. if (!buf.try_append(first, token_last))
  555. {
  556. BOOST_BEAST_ASSIGN_EC(ec, error::header_limit);
  557. return;
  558. }
  559. BOOST_ASSERT(! buf.empty());
  560. for(;;)
  561. {
  562. // eat leading ' ' and '\t'
  563. for(;;++p)
  564. {
  565. if(p + 1 > last)
  566. {
  567. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  568. return;
  569. }
  570. if(! (*p == ' ' || *p == '\t'))
  571. break;
  572. }
  573. // parse to CRLF
  574. first = p;
  575. p = parse_token_to_eol(p, last, token_last, ec);
  576. if(ec)
  577. return;
  578. if(! p)
  579. {
  580. BOOST_BEAST_ASSIGN_EC(ec, error::bad_value);
  581. return;
  582. }
  583. // Look 1 char past the CRLF to handle obs-fold.
  584. if(p + 1 > last)
  585. {
  586. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  587. return;
  588. }
  589. token_last = trim_back(token_last, first);
  590. if(first != token_last)
  591. {
  592. if (!buf.try_push_back(' ') ||
  593. !buf.try_append(first, token_last))
  594. {
  595. BOOST_BEAST_ASSIGN_EC(ec, error::header_limit);
  596. return;
  597. }
  598. }
  599. if(*p != ' ' && *p != '\t')
  600. {
  601. value = {buf.data(), buf.size()};
  602. return;
  603. }
  604. ++p;
  605. }
  606. }
  607. void
  608. basic_parser_base::
  609. parse_chunk_extensions(
  610. char const*& it,
  611. char const* last,
  612. error_code& ec)
  613. {
  614. /*
  615. chunk-ext = *( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val ] )
  616. BWS = *( SP / HTAB ) ; "Bad White Space"
  617. chunk-ext-name = token
  618. chunk-ext-val = token / quoted-string
  619. token = 1*tchar
  620. quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
  621. qdtext = HTAB / SP / "!" / %x23-5B ; '#'-'[' / %x5D-7E ; ']'-'~' / obs-text
  622. quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
  623. obs-text = %x80-FF
  624. https://www.rfc-editor.org/errata_search.php?rfc=7230&eid=4667
  625. */
  626. loop:
  627. if(it == last)
  628. {
  629. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  630. return;
  631. }
  632. if(*it != ' ' && *it != '\t' && *it != ';')
  633. return;
  634. // BWS
  635. if(*it == ' ' || *it == '\t')
  636. {
  637. for(;;)
  638. {
  639. ++it;
  640. if(it == last)
  641. {
  642. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  643. return;
  644. }
  645. if(*it != ' ' && *it != '\t')
  646. break;
  647. }
  648. }
  649. // ';'
  650. if(*it != ';')
  651. {
  652. BOOST_BEAST_ASSIGN_EC(ec, error::bad_chunk_extension);
  653. return;
  654. }
  655. semi:
  656. ++it; // skip ';'
  657. // BWS
  658. for(;;)
  659. {
  660. if(it == last)
  661. {
  662. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  663. return;
  664. }
  665. if(*it != ' ' && *it != '\t')
  666. break;
  667. ++it;
  668. }
  669. // chunk-ext-name
  670. if(! detail::is_token_char(*it))
  671. {
  672. ec = error::bad_chunk_extension;
  673. return;
  674. }
  675. for(;;)
  676. {
  677. ++it;
  678. if(it == last)
  679. {
  680. ec = error::need_more;
  681. return;
  682. }
  683. if(! detail::is_token_char(*it))
  684. break;
  685. }
  686. // BWS [ ";" / "=" ]
  687. {
  688. bool bws;
  689. if(*it == ' ' || *it == '\t')
  690. {
  691. for(;;)
  692. {
  693. ++it;
  694. if(it == last)
  695. {
  696. ec = error::need_more;
  697. return;
  698. }
  699. if(*it != ' ' && *it != '\t')
  700. break;
  701. }
  702. bws = true;
  703. }
  704. else
  705. {
  706. bws = false;
  707. }
  708. if(*it == ';')
  709. goto semi;
  710. if(*it != '=')
  711. {
  712. if(bws)
  713. ec = error::bad_chunk_extension;
  714. return;
  715. }
  716. ++it; // skip '='
  717. }
  718. // BWS
  719. for(;;)
  720. {
  721. if(it == last)
  722. {
  723. ec = error::need_more;
  724. return;
  725. }
  726. if(*it != ' ' && *it != '\t')
  727. break;
  728. ++it;
  729. }
  730. // chunk-ext-val
  731. if(*it != '"')
  732. {
  733. // token
  734. if(! detail::is_token_char(*it))
  735. {
  736. ec = error::bad_chunk_extension;
  737. return;
  738. }
  739. for(;;)
  740. {
  741. ++it;
  742. if(it == last)
  743. {
  744. ec = error::need_more;
  745. return;
  746. }
  747. if(! detail::is_token_char(*it))
  748. break;
  749. }
  750. }
  751. else
  752. {
  753. // quoted-string
  754. for(;;)
  755. {
  756. ++it;
  757. if(it == last)
  758. {
  759. ec = error::need_more;
  760. return;
  761. }
  762. if(*it == '"')
  763. break;
  764. if(*it == '\\')
  765. {
  766. ++it;
  767. if(it == last)
  768. {
  769. ec = error::need_more;
  770. return;
  771. }
  772. }
  773. }
  774. ++it;
  775. }
  776. goto loop;
  777. }
  778. } // detail
  779. } // http
  780. } // beast
  781. } // boost
  782. #endif