basic_parser.ipp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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_IMPL_BASIC_PARSER_IPP
  10. #define BOOST_BEAST_HTTP_IMPL_BASIC_PARSER_IPP
  11. #include <boost/beast/http/basic_parser.hpp>
  12. #include <boost/beast/http/error.hpp>
  13. #include <boost/beast/http/rfc7230.hpp>
  14. #include <boost/beast/core/buffer_traits.hpp>
  15. #include <boost/beast/core/detail/clamp.hpp>
  16. #include <boost/beast/core/detail/config.hpp>
  17. #include <boost/beast/core/detail/string.hpp>
  18. #include <boost/asio/buffer.hpp>
  19. #include <algorithm>
  20. #include <utility>
  21. namespace boost {
  22. namespace beast {
  23. namespace http {
  24. template<bool isRequest>
  25. bool
  26. basic_parser<isRequest>::
  27. keep_alive() const
  28. {
  29. BOOST_ASSERT(is_header_done());
  30. if(f_ & flagHTTP11)
  31. {
  32. if(f_ & flagConnectionClose)
  33. return false;
  34. }
  35. else
  36. {
  37. if(! (f_ & flagConnectionKeepAlive))
  38. return false;
  39. }
  40. return (f_ & flagNeedEOF) == 0;
  41. }
  42. template<bool isRequest>
  43. boost::optional<std::uint64_t>
  44. basic_parser<isRequest>::
  45. content_length() const
  46. {
  47. BOOST_ASSERT(is_header_done());
  48. return content_length_unchecked();
  49. }
  50. template<bool isRequest>
  51. boost::optional<std::uint64_t>
  52. basic_parser<isRequest>::
  53. content_length_remaining() const
  54. {
  55. BOOST_ASSERT(is_header_done());
  56. if(! (f_ & flagContentLength))
  57. return boost::none;
  58. return len_;
  59. }
  60. template<bool isRequest>
  61. void
  62. basic_parser<isRequest>::
  63. skip(bool v)
  64. {
  65. BOOST_ASSERT(! got_some());
  66. if(v)
  67. f_ |= flagSkipBody;
  68. else
  69. f_ &= ~flagSkipBody;
  70. }
  71. template<bool isRequest>
  72. std::size_t
  73. basic_parser<isRequest>::
  74. put(net::const_buffer buffer,
  75. error_code& ec)
  76. {
  77. // If this goes off you have tried to parse more data after the parser
  78. // has completed. A common cause of this is re-using a parser, which is
  79. // not supported. If you need to re-use a parser, consider storing it
  80. // in an optional. Then reset() and emplace() prior to parsing each new
  81. // message.
  82. BOOST_ASSERT(!is_done());
  83. if (is_done())
  84. {
  85. BOOST_BEAST_ASSIGN_EC(ec, error::stale_parser);
  86. return 0;
  87. }
  88. auto p = static_cast<char const*>(buffer.data());
  89. auto n = buffer.size();
  90. auto const p0 = p;
  91. auto const p1 = p0 + n;
  92. ec = {};
  93. loop:
  94. switch(state_)
  95. {
  96. case state::nothing_yet:
  97. if(n == 0)
  98. {
  99. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  100. return 0;
  101. }
  102. state_ = state::start_line;
  103. BOOST_FALLTHROUGH;
  104. case state::start_line:
  105. {
  106. maybe_need_more(p, n, ec);
  107. if(ec)
  108. goto done;
  109. parse_start_line(p, p + (std::min<std::size_t>)(
  110. header_limit_, n), ec, is_request{});
  111. if(ec)
  112. {
  113. if(ec == error::need_more)
  114. {
  115. if(n >= header_limit_)
  116. {
  117. BOOST_BEAST_ASSIGN_EC(ec, error::header_limit);
  118. goto done;
  119. }
  120. if(p + 3 <= p1)
  121. skip_ = static_cast<
  122. std::size_t>(p1 - p - 3);
  123. }
  124. goto done;
  125. }
  126. BOOST_ASSERT(! is_done());
  127. n = static_cast<std::size_t>(p1 - p);
  128. if(p >= p1)
  129. {
  130. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  131. goto done;
  132. }
  133. BOOST_FALLTHROUGH;
  134. }
  135. case state::fields:
  136. maybe_need_more(p, n, ec);
  137. if(ec)
  138. goto done;
  139. parse_fields(p, p + (std::min<std::size_t>)(
  140. header_limit_, n), ec);
  141. if(ec)
  142. {
  143. if(ec == error::need_more)
  144. {
  145. if(n >= header_limit_)
  146. {
  147. BOOST_BEAST_ASSIGN_EC(ec, error::header_limit);
  148. goto done;
  149. }
  150. if(p + 3 <= p1)
  151. skip_ = static_cast<
  152. std::size_t>(p1 - p - 3);
  153. }
  154. goto done;
  155. }
  156. finish_header(ec, is_request{});
  157. if(ec)
  158. goto done;
  159. break;
  160. case state::body0:
  161. BOOST_ASSERT(! skip_);
  162. this->on_body_init_impl(content_length(), ec);
  163. if(ec)
  164. goto done;
  165. state_ = state::body;
  166. BOOST_FALLTHROUGH;
  167. case state::body:
  168. BOOST_ASSERT(! skip_);
  169. parse_body(p, n, ec);
  170. if(ec)
  171. goto done;
  172. break;
  173. case state::body_to_eof0:
  174. BOOST_ASSERT(! skip_);
  175. this->on_body_init_impl(content_length(), ec);
  176. if(ec)
  177. goto done;
  178. state_ = state::body_to_eof;
  179. BOOST_FALLTHROUGH;
  180. case state::body_to_eof:
  181. BOOST_ASSERT(! skip_);
  182. parse_body_to_eof(p, n, ec);
  183. if(ec)
  184. goto done;
  185. break;
  186. case state::chunk_header0:
  187. this->on_body_init_impl(content_length(), ec);
  188. if(ec)
  189. goto done;
  190. state_ = state::chunk_header;
  191. BOOST_FALLTHROUGH;
  192. case state::chunk_header:
  193. parse_chunk_header(p, n, ec);
  194. if(ec)
  195. goto done;
  196. break;
  197. case state::chunk_body:
  198. parse_chunk_body(p, n, ec);
  199. if(ec)
  200. goto done;
  201. break;
  202. case state::complete:
  203. ec = {};
  204. goto done;
  205. }
  206. if(p < p1 && ! is_done() && eager())
  207. {
  208. n = static_cast<std::size_t>(p1 - p);
  209. goto loop;
  210. }
  211. done:
  212. return static_cast<std::size_t>(p - p0);
  213. }
  214. template<bool isRequest>
  215. void
  216. basic_parser<isRequest>::
  217. put_eof(error_code& ec)
  218. {
  219. BOOST_ASSERT(got_some());
  220. if( state_ == state::start_line ||
  221. state_ == state::fields)
  222. {
  223. BOOST_BEAST_ASSIGN_EC(ec, error::partial_message);
  224. return;
  225. }
  226. if(f_ & (flagContentLength | flagChunked))
  227. {
  228. if(state_ != state::complete)
  229. {
  230. BOOST_BEAST_ASSIGN_EC(ec, error::partial_message);
  231. return;
  232. }
  233. ec = {};
  234. return;
  235. }
  236. ec = {};
  237. this->on_finish_impl(ec);
  238. if(ec)
  239. return;
  240. state_ = state::complete;
  241. }
  242. template<bool isRequest>
  243. void
  244. basic_parser<isRequest>::
  245. maybe_need_more(
  246. char const* p, std::size_t n,
  247. error_code& ec)
  248. {
  249. if(skip_ == 0)
  250. return;
  251. if( n > header_limit_)
  252. n = header_limit_;
  253. if(n < skip_ + 4)
  254. {
  255. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  256. return;
  257. }
  258. auto const term =
  259. find_eom(p + skip_, p + n);
  260. if(! term)
  261. {
  262. skip_ = n - 3;
  263. if(skip_ + 4 > header_limit_)
  264. {
  265. BOOST_BEAST_ASSIGN_EC(ec, error::header_limit);
  266. return;
  267. }
  268. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  269. return;
  270. }
  271. skip_ = 0;
  272. }
  273. template<bool isRequest>
  274. void
  275. basic_parser<isRequest>::
  276. parse_start_line(
  277. char const*& in, char const* last,
  278. error_code& ec, std::true_type)
  279. {
  280. /*
  281. request-line = method SP request-target SP HTTP-version CRLF
  282. method = token
  283. */
  284. auto p = in;
  285. string_view method;
  286. parse_method(p, last, method, ec);
  287. if(ec)
  288. return;
  289. string_view target;
  290. parse_target(p, last, target, ec);
  291. if(ec)
  292. return;
  293. int version = 0;
  294. parse_version(p, last, version, ec);
  295. if(ec)
  296. return;
  297. if(version < 10 || version > 11)
  298. {
  299. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  300. return;
  301. }
  302. if(p + 2 > last)
  303. {
  304. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  305. return;
  306. }
  307. if(p[0] != '\r' || p[1] != '\n')
  308. {
  309. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  310. return;
  311. }
  312. p += 2;
  313. if(version >= 11)
  314. f_ |= flagHTTP11;
  315. this->on_request_impl(string_to_verb(method),
  316. method, target, version, ec);
  317. if(ec)
  318. return;
  319. in = p;
  320. state_ = state::fields;
  321. }
  322. template<bool isRequest>
  323. void
  324. basic_parser<isRequest>::
  325. parse_start_line(
  326. char const*& in, char const* last,
  327. error_code& ec, std::false_type)
  328. {
  329. /*
  330. status-line = HTTP-version SP status-code SP reason-phrase CRLF
  331. status-code = 3*DIGIT
  332. reason-phrase = *( HTAB / SP / VCHAR / obs-text )
  333. */
  334. auto p = in;
  335. int version = 0;
  336. parse_version(p, last, version, ec);
  337. if(ec)
  338. return;
  339. if(version < 10 || version > 11)
  340. {
  341. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  342. return;
  343. }
  344. // SP
  345. if(p + 1 > last)
  346. {
  347. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  348. return;
  349. }
  350. if(*p++ != ' ')
  351. {
  352. BOOST_BEAST_ASSIGN_EC(ec, error::bad_version);
  353. return;
  354. }
  355. parse_status(p, last, status_, ec);
  356. if(ec)
  357. return;
  358. // parse reason CRLF
  359. string_view reason;
  360. parse_reason(p, last, reason, ec);
  361. if(ec)
  362. return;
  363. if(version >= 11)
  364. f_ |= flagHTTP11;
  365. this->on_response_impl(
  366. status_, reason, version, ec);
  367. if(ec)
  368. return;
  369. in = p;
  370. state_ = state::fields;
  371. }
  372. template<bool isRequest>
  373. void
  374. basic_parser<isRequest>::
  375. parse_fields(char const*& in,
  376. char const* last, error_code& ec)
  377. {
  378. string_view name;
  379. string_view value;
  380. // https://stackoverflow.com/questions/686217/maximum-on-http-header-values
  381. beast::detail::char_buffer<max_obs_fold> buf;
  382. auto p = in;
  383. for(;;)
  384. {
  385. if(p + 2 > last)
  386. {
  387. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  388. return;
  389. }
  390. if(p[0] == '\r')
  391. {
  392. if(p[1] != '\n')
  393. {
  394. BOOST_BEAST_ASSIGN_EC(ec, error::bad_line_ending);
  395. }
  396. in = p + 2;
  397. return;
  398. }
  399. parse_field(p, last, name, value, buf, ec);
  400. if(ec)
  401. return;
  402. auto const f = string_to_field(name);
  403. do_field(f, value, ec);
  404. if(ec)
  405. return;
  406. this->on_field_impl(f, name, value, ec);
  407. if(ec)
  408. return;
  409. in = p;
  410. }
  411. }
  412. template<bool isRequest>
  413. void
  414. basic_parser<isRequest>::
  415. finish_header(error_code& ec, std::true_type)
  416. {
  417. // RFC 7230 section 3.3
  418. // https://tools.ietf.org/html/rfc7230#section-3.3
  419. if(f_ & flagSkipBody)
  420. {
  421. state_ = state::complete;
  422. }
  423. else if(f_ & flagContentLength)
  424. {
  425. if(body_limit_.has_value() &&
  426. len_ > body_limit_)
  427. {
  428. BOOST_BEAST_ASSIGN_EC(ec, error::body_limit);
  429. return;
  430. }
  431. if(len_ > 0)
  432. {
  433. f_ |= flagHasBody;
  434. state_ = state::body0;
  435. }
  436. else
  437. {
  438. state_ = state::complete;
  439. }
  440. }
  441. else if(f_ & flagChunked)
  442. {
  443. f_ |= flagHasBody;
  444. state_ = state::chunk_header0;
  445. }
  446. else
  447. {
  448. len_ = 0;
  449. len0_ = 0;
  450. state_ = state::complete;
  451. }
  452. ec = {};
  453. this->on_header_impl(ec);
  454. if(ec)
  455. return;
  456. if(state_ == state::complete)
  457. {
  458. this->on_finish_impl(ec);
  459. if(ec)
  460. return;
  461. }
  462. }
  463. template<bool isRequest>
  464. void
  465. basic_parser<isRequest>::
  466. finish_header(error_code& ec, std::false_type)
  467. {
  468. // RFC 7230 section 3.3
  469. // https://tools.ietf.org/html/rfc7230#section-3.3
  470. if( (f_ & flagSkipBody) || // e.g. response to a HEAD request
  471. status_ / 100 == 1 || // 1xx e.g. Continue
  472. status_ == 204 || // No Content
  473. status_ == 304) // Not Modified
  474. {
  475. // VFALCO Content-Length may be present, but we
  476. // treat the message as not having a body.
  477. // https://github.com/boostorg/beast/issues/692
  478. state_ = state::complete;
  479. }
  480. else if(f_ & flagContentLength)
  481. {
  482. if(len_ > 0)
  483. {
  484. f_ |= flagHasBody;
  485. state_ = state::body0;
  486. if(body_limit_.has_value() &&
  487. len_ > body_limit_)
  488. {
  489. BOOST_BEAST_ASSIGN_EC(ec, error::body_limit);
  490. return;
  491. }
  492. }
  493. else
  494. {
  495. state_ = state::complete;
  496. }
  497. }
  498. else if(f_ & flagChunked)
  499. {
  500. f_ |= flagHasBody;
  501. state_ = state::chunk_header0;
  502. }
  503. else
  504. {
  505. f_ |= flagHasBody;
  506. f_ |= flagNeedEOF;
  507. state_ = state::body_to_eof0;
  508. }
  509. ec = {};
  510. this->on_header_impl(ec);
  511. if(ec)
  512. return;
  513. if(state_ == state::complete)
  514. {
  515. this->on_finish_impl(ec);
  516. if(ec)
  517. return;
  518. }
  519. }
  520. template<bool isRequest>
  521. void
  522. basic_parser<isRequest>::
  523. parse_body(char const*& p,
  524. std::size_t n, error_code& ec)
  525. {
  526. ec = {};
  527. n = this->on_body_impl(string_view{p,
  528. beast::detail::clamp(len_, n)}, ec);
  529. p += n;
  530. len_ -= n;
  531. if(ec)
  532. return;
  533. if(len_ > 0)
  534. return;
  535. this->on_finish_impl(ec);
  536. if(ec)
  537. return;
  538. state_ = state::complete;
  539. }
  540. template<bool isRequest>
  541. void
  542. basic_parser<isRequest>::
  543. parse_body_to_eof(char const*& p,
  544. std::size_t n, error_code& ec)
  545. {
  546. if(body_limit_.has_value())
  547. {
  548. if (n > *body_limit_)
  549. {
  550. BOOST_BEAST_ASSIGN_EC(ec, error::body_limit);
  551. return;
  552. }
  553. *body_limit_ -= n;
  554. }
  555. ec = {};
  556. n = this->on_body_impl(string_view{p, n}, ec);
  557. p += n;
  558. if(ec)
  559. return;
  560. }
  561. template<bool isRequest>
  562. void
  563. basic_parser<isRequest>::
  564. parse_chunk_header(char const*& p0,
  565. std::size_t n, error_code& ec)
  566. {
  567. /*
  568. chunked-body = *chunk last-chunk trailer-part CRLF
  569. chunk = chunk-size [ chunk-ext ] CRLF chunk-data CRLF
  570. last-chunk = 1*("0") [ chunk-ext ] CRLF
  571. trailer-part = *( header-field CRLF )
  572. chunk-size = 1*HEXDIG
  573. chunk-data = 1*OCTET ; a sequence of chunk-size octets
  574. chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
  575. chunk-ext-name = token
  576. chunk-ext-val = token / quoted-string
  577. */
  578. auto p = p0;
  579. auto const pend = p + n;
  580. char const* eol;
  581. if(! (f_ & flagFinalChunk))
  582. {
  583. if(n < skip_ + 2)
  584. {
  585. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  586. return;
  587. }
  588. if(f_ & flagExpectCRLF)
  589. {
  590. // Treat the last CRLF in a chunk as
  591. // part of the next chunk, so p can
  592. // be parsed in one call instead of two.
  593. if(! parse_crlf(p))
  594. {
  595. BOOST_BEAST_ASSIGN_EC(ec, error::bad_chunk);
  596. return;
  597. }
  598. }
  599. eol = find_eol(p0 + skip_, pend, ec);
  600. if(ec)
  601. return;
  602. if(! eol)
  603. {
  604. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  605. skip_ = n - 1;
  606. return;
  607. }
  608. skip_ = static_cast<
  609. std::size_t>(eol - 2 - p0);
  610. std::uint64_t size;
  611. if(! parse_hex(p, size))
  612. {
  613. BOOST_BEAST_ASSIGN_EC(ec, error::bad_chunk);
  614. return;
  615. }
  616. if(size != 0)
  617. {
  618. if (body_limit_.has_value())
  619. {
  620. if (size > *body_limit_)
  621. {
  622. BOOST_BEAST_ASSIGN_EC(ec, error::body_limit);
  623. return;
  624. }
  625. *body_limit_ -= size;
  626. }
  627. auto const start = p;
  628. parse_chunk_extensions(p, pend, ec);
  629. if(ec)
  630. return;
  631. if(p != eol -2 )
  632. {
  633. BOOST_BEAST_ASSIGN_EC(ec, error::bad_chunk_extension);
  634. return;
  635. }
  636. auto const ext = make_string(start, p);
  637. this->on_chunk_header_impl(size, ext, ec);
  638. if(ec)
  639. return;
  640. len_ = size;
  641. skip_ = 2;
  642. p0 = eol;
  643. f_ |= flagExpectCRLF;
  644. state_ = state::chunk_body;
  645. return;
  646. }
  647. f_ |= flagFinalChunk;
  648. }
  649. else
  650. {
  651. BOOST_ASSERT(n >= 3);
  652. if(f_ & flagExpectCRLF)
  653. {
  654. BOOST_ASSERT(n >= 5);
  655. BOOST_VERIFY(parse_crlf(p));
  656. }
  657. std::uint64_t size;
  658. BOOST_VERIFY(parse_hex(p, size));
  659. eol = find_eol(p, pend, ec);
  660. BOOST_ASSERT(! ec);
  661. }
  662. auto eom = find_eom(p0 + skip_, pend);
  663. if(! eom)
  664. {
  665. BOOST_ASSERT(n >= 3);
  666. skip_ = n - 3;
  667. BOOST_BEAST_ASSIGN_EC(ec, error::need_more);
  668. return;
  669. }
  670. auto const start = p;
  671. parse_chunk_extensions(p, pend, ec);
  672. if(ec)
  673. return;
  674. if(p != eol - 2)
  675. {
  676. BOOST_BEAST_ASSIGN_EC(ec, error::bad_chunk_extension);
  677. return;
  678. }
  679. auto const ext = make_string(start, p);
  680. this->on_chunk_header_impl(0, ext, ec);
  681. if(ec)
  682. return;
  683. p = eol;
  684. parse_fields(p, eom, ec);
  685. if(ec)
  686. return;
  687. BOOST_ASSERT(p == eom);
  688. p0 = eom;
  689. this->on_finish_impl(ec);
  690. if(ec)
  691. return;
  692. state_ = state::complete;
  693. }
  694. template<bool isRequest>
  695. void
  696. basic_parser<isRequest>::
  697. parse_chunk_body(char const*& p,
  698. std::size_t n, error_code& ec)
  699. {
  700. ec = {};
  701. n = this->on_chunk_body_impl(
  702. len_, string_view{p,
  703. beast::detail::clamp(len_, n)}, ec);
  704. p += n;
  705. len_ -= n;
  706. if(len_ == 0)
  707. state_ = state::chunk_header;
  708. }
  709. template<bool isRequest>
  710. void
  711. basic_parser<isRequest>::
  712. do_field(field f,
  713. string_view value, error_code& ec)
  714. {
  715. using namespace beast::detail::string_literals;
  716. // Connection
  717. if(f == field::connection ||
  718. f == field::proxy_connection)
  719. {
  720. auto const list = opt_token_list{value};
  721. if(! validate_list(list))
  722. {
  723. // VFALCO Should this be a field specific error?
  724. BOOST_BEAST_ASSIGN_EC(ec, error::bad_value);
  725. return;
  726. }
  727. for(auto const& s : list)
  728. {
  729. if(beast::iequals("close"_sv, s))
  730. {
  731. f_ |= flagConnectionClose;
  732. continue;
  733. }
  734. if(beast::iequals("keep-alive"_sv, s))
  735. {
  736. f_ |= flagConnectionKeepAlive;
  737. continue;
  738. }
  739. if(beast::iequals("upgrade"_sv, s))
  740. {
  741. f_ |= flagConnectionUpgrade;
  742. continue;
  743. }
  744. }
  745. ec = {};
  746. return;
  747. }
  748. // Content-Length
  749. if(f == field::content_length)
  750. {
  751. auto bad_content_length = [&ec]
  752. {
  753. BOOST_BEAST_ASSIGN_EC(ec, error::bad_content_length);
  754. };
  755. auto multiple_content_length = [&ec]
  756. {
  757. BOOST_BEAST_ASSIGN_EC(ec, error::multiple_content_length);
  758. };
  759. // conflicting field
  760. if(f_ & flagChunked)
  761. return bad_content_length();
  762. // Content-length may be a comma-separated list of integers
  763. auto tokens_unprocessed = 1 +
  764. std::count(value.begin(), value.end(), ',');
  765. auto tokens = opt_token_list(value);
  766. if (tokens.begin() == tokens.end() ||
  767. !validate_list(tokens))
  768. return bad_content_length();
  769. auto existing = this->content_length_unchecked();
  770. for (auto tok : tokens)
  771. {
  772. std::uint64_t v;
  773. if (!parse_dec(tok, v))
  774. return bad_content_length();
  775. --tokens_unprocessed;
  776. if (existing.has_value())
  777. {
  778. if (v != *existing)
  779. return multiple_content_length();
  780. }
  781. else
  782. {
  783. existing = v;
  784. }
  785. }
  786. if (tokens_unprocessed)
  787. return bad_content_length();
  788. BOOST_ASSERT(existing.has_value());
  789. ec = {};
  790. len_ = *existing;
  791. len0_ = *existing;
  792. f_ |= flagContentLength;
  793. return;
  794. }
  795. // Transfer-Encoding
  796. if(f == field::transfer_encoding)
  797. {
  798. if(f_ & flagChunked)
  799. {
  800. // duplicate
  801. BOOST_BEAST_ASSIGN_EC(ec, error::bad_transfer_encoding);
  802. return;
  803. }
  804. if(f_ & flagContentLength)
  805. {
  806. // conflicting field
  807. BOOST_BEAST_ASSIGN_EC(ec, error::bad_transfer_encoding);
  808. return;
  809. }
  810. ec = {};
  811. auto const v = token_list{value};
  812. auto const p = std::find_if(v.begin(), v.end(),
  813. [&](string_view const& s)
  814. {
  815. return beast::iequals("chunked"_sv, s);
  816. });
  817. if(p == v.end())
  818. return;
  819. if(std::next(p) != v.end())
  820. return;
  821. len_ = 0;
  822. f_ |= flagChunked;
  823. return;
  824. }
  825. // Upgrade
  826. if(f == field::upgrade)
  827. {
  828. ec = {};
  829. f_ |= flagUpgrade;
  830. return;
  831. }
  832. ec = {};
  833. }
  834. #ifdef BOOST_BEAST_SOURCE
  835. template class http::basic_parser<true>;
  836. template class http::basic_parser<false>;
  837. #endif
  838. } // http
  839. } // beast
  840. } // boost
  841. #endif