parser.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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_PARSER_HPP
  10. #define BOOST_BEAST_HTTP_PARSER_HPP
  11. #include <boost/beast/http/parser_fwd.hpp>
  12. #include <boost/beast/core/detail/config.hpp>
  13. #include <boost/beast/http/basic_parser.hpp>
  14. #include <boost/beast/http/message.hpp>
  15. #include <boost/beast/http/type_traits.hpp>
  16. #include <boost/optional.hpp>
  17. #include <boost/throw_exception.hpp>
  18. #include <cstdint>
  19. #include <functional>
  20. #include <memory>
  21. #include <type_traits>
  22. #include <utility>
  23. namespace boost {
  24. namespace beast {
  25. namespace http {
  26. /** An HTTP/1 parser for producing a message.
  27. This class uses the basic HTTP/1 wire format parser to convert
  28. a series of octets into a @ref message using the @ref basic_fields
  29. container to represent the fields.
  30. @tparam isRequest Indicates whether a request or response
  31. will be parsed.
  32. @tparam Body The type used to represent the body. This must
  33. meet the requirements of <em>Body</em>.
  34. @tparam Allocator The type of allocator used with the
  35. @ref basic_fields container.
  36. @note A new instance of the parser is required for each message.
  37. */
  38. #if BOOST_BEAST_DOXYGEN
  39. template<
  40. bool isRequest,
  41. class Body,
  42. class Allocator = std::allocator<char>>
  43. #else
  44. template<
  45. bool isRequest,
  46. class Body,
  47. class Allocator>
  48. #endif
  49. class parser
  50. : public basic_parser<isRequest>
  51. {
  52. static_assert(is_body<Body>::value,
  53. "Body type requirements not met");
  54. static_assert(is_body_reader<Body>::value,
  55. "BodyReader type requirements not met");
  56. template<bool, class, class>
  57. friend class parser;
  58. message<isRequest, Body, basic_fields<Allocator>> m_;
  59. typename Body::reader rd_;
  60. bool rd_inited_ = false;
  61. bool used_ = false;
  62. bool merge_all_trailers_ = false;
  63. std::function<void(
  64. std::uint64_t,
  65. string_view,
  66. error_code&)> cb_h_;
  67. std::function<std::size_t(
  68. std::uint64_t,
  69. string_view,
  70. error_code&)> cb_b_;
  71. public:
  72. /// The type of message returned by the parser
  73. using value_type =
  74. message<isRequest, Body, basic_fields<Allocator>>;
  75. /// Destructor
  76. ~parser() = default;
  77. /// Constructor (disallowed)
  78. parser(parser const&) = delete;
  79. /// Assignment (disallowed)
  80. parser& operator=(parser const&) = delete;
  81. /// Constructor (disallowed)
  82. parser(parser&& other) = delete;
  83. /// Constructor
  84. parser();
  85. /** Constructor
  86. @param args Optional arguments forwarded to the
  87. @ref http::message constructor.
  88. @note This function participates in overload
  89. resolution only if the first argument is not a
  90. @ref parser.
  91. */
  92. #if BOOST_BEAST_DOXYGEN
  93. template<class... Args>
  94. explicit
  95. parser(Args&&... args);
  96. #else
  97. template<class Arg1, class... ArgN,
  98. class = typename std::enable_if<
  99. ! detail::is_parser<typename
  100. std::decay<Arg1>::type>::value>::type>
  101. explicit
  102. parser(Arg1&& arg1, ArgN&&... argn);
  103. #endif
  104. /** Construct a parser from another parser, changing the Body type.
  105. This constructs a new parser by move constructing the
  106. header from another parser with a different body type. The
  107. constructed-from parser must not have any parsed body octets or
  108. initialized <em>BodyReader</em>, otherwise an exception is generated.
  109. @par Example
  110. @code
  111. // Deferred body type commitment
  112. request_parser<empty_body> req0;
  113. ...
  114. request_parser<string_body> req{std::move(req0)};
  115. @endcode
  116. If an exception is thrown, the state of the constructed-from
  117. parser is undefined.
  118. @param parser The other parser to construct from. After
  119. this call returns, the constructed-from parser may only
  120. be destroyed.
  121. @param args Optional arguments forwarded to the message
  122. constructor.
  123. @throws std::invalid_argument Thrown when the constructed-from
  124. parser has already initialized a body reader.
  125. @note This function participates in overload resolution only
  126. if the other parser uses a different body type.
  127. */
  128. #if BOOST_BEAST_DOXYGEN
  129. template<class OtherBody, class... Args>
  130. #else
  131. template<class OtherBody, class... Args,
  132. class = typename std::enable_if<
  133. ! std::is_same<Body, OtherBody>::value>::type>
  134. #endif
  135. explicit
  136. parser(parser<isRequest, OtherBody,
  137. Allocator>&& parser, Args&&... args);
  138. /** Returns the parsed message.
  139. Depending on the parser's progress,
  140. parts of this object may be incomplete.
  141. */
  142. value_type const&
  143. get() const
  144. {
  145. return m_;
  146. }
  147. /** Returns the parsed message.
  148. Depending on the parser's progress,
  149. parts of this object may be incomplete.
  150. */
  151. value_type&
  152. get()
  153. {
  154. return m_;
  155. }
  156. /** Returns ownership of the parsed message.
  157. Ownership is transferred to the caller.
  158. Depending on the parser's progress,
  159. parts of this object may be incomplete.
  160. @par Requires
  161. @ref value_type is @b MoveConstructible
  162. */
  163. value_type
  164. release()
  165. {
  166. static_assert(std::is_move_constructible<decltype(m_)>::value,
  167. "MoveConstructible requirements not met");
  168. return std::move(m_);
  169. }
  170. /** Set a callback to be invoked on each chunk header.
  171. The callback will be invoked once for every chunk in the message
  172. payload, as well as once for the last chunk. The invocation
  173. happens after the chunk header is available but before any body
  174. octets have been parsed.
  175. The extensions are provided in raw, validated form, use
  176. @ref chunk_extensions::parse to parse the extensions into a
  177. structured container for easier access.
  178. The implementation type-erases the callback without requiring
  179. a dynamic allocation. For this reason, the callback object is
  180. passed by a non-constant reference.
  181. @par Example
  182. @code
  183. auto callback =
  184. [](std::uint64_t size, string_view extensions, error_code& ec)
  185. {
  186. //...
  187. };
  188. parser.on_chunk_header(callback);
  189. @endcode
  190. @param cb The function to set, which must be invocable with
  191. this equivalent signature:
  192. @code
  193. void
  194. on_chunk_header(
  195. std::uint64_t size, // Size of the chunk, zero for the last chunk
  196. string_view extensions, // The chunk-extensions in raw form
  197. error_code& ec); // May be set by the callback to indicate an error
  198. @endcode
  199. */
  200. template<class Callback>
  201. void
  202. on_chunk_header(Callback& cb)
  203. {
  204. // Callback may not be constant, caller is responsible for
  205. // managing the lifetime of the callback. Copies are not made.
  206. BOOST_CORE_STATIC_ASSERT(! std::is_const<Callback>::value);
  207. // Can't set the callback after receiving any chunk data!
  208. BOOST_ASSERT(! rd_inited_);
  209. cb_h_ = std::ref(cb);
  210. }
  211. /** Set a callback to be invoked on chunk body data
  212. The provided function object will be invoked one or more times
  213. to provide buffers corresponding to the chunk body for the current
  214. chunk. The callback receives the number of octets remaining in this
  215. chunk body including the octets in the buffer provided.
  216. The callback must return the number of octets actually consumed.
  217. Any octets not consumed will be presented again in a subsequent
  218. invocation of the callback.
  219. The implementation type-erases the callback without requiring
  220. a dynamic allocation. For this reason, the callback object is
  221. passed by a non-constant reference.
  222. @par Example
  223. @code
  224. auto callback =
  225. [](std::uint64_t remain, string_view body, error_code& ec)
  226. {
  227. //...
  228. };
  229. parser.on_chunk_body(callback);
  230. @endcode
  231. @param cb The function to set, which must be invocable with
  232. this equivalent signature:
  233. @code
  234. std::size_t
  235. on_chunk_header(
  236. std::uint64_t remain, // Octets remaining in this chunk, includes `body`
  237. string_view body, // A buffer holding some or all of the remainder of the chunk body
  238. error_code& ec); // May be set by the callback to indicate an error
  239. @endcode
  240. */
  241. template<class Callback>
  242. void
  243. on_chunk_body(Callback& cb)
  244. {
  245. // Callback may not be constant, caller is responsible for
  246. // managing the lifetime of the callback. Copies are not made.
  247. BOOST_CORE_STATIC_ASSERT(! std::is_const<Callback>::value);
  248. // Can't set the callback after receiving any chunk data!
  249. BOOST_ASSERT(! rd_inited_);
  250. cb_b_ = std::ref(cb);
  251. }
  252. /** Returns `true` if the parser is allowed to merge all trailer fields.
  253. @see
  254. merge_all_trailers(bool).
  255. */
  256. bool
  257. merge_all_trailers() const noexcept
  258. {
  259. return merge_all_trailers_;
  260. }
  261. /** Set whether the parser is allowed to merge all trailer fields.
  262. By default, the parser merges only a set of
  263. well-known trailer fields. When this option is
  264. enabled, the parser merges all trailer fields listed
  265. in the `Trailer` header field in the header section
  266. of the message.
  267. @note Enabling this option can introduce security
  268. risks if untrusted input is processed and the
  269. `Trailer` header field in the header section of the
  270. message is not properly validated.
  271. The default value is `false`, which merges only the
  272. following well-known trailer fields:
  273. @li `Digest`
  274. @li `Content-Digest`
  275. @li `Repr-Digest`
  276. @li `Signature`
  277. @li `Signature-Input`
  278. @li `Server-Timing`
  279. @li `ETag`
  280. @li `Link`
  281. @li `Alt-Svc`
  282. @param v `true` to merge all trailer fields, or
  283. `false` to merge only the well-known ones.
  284. */
  285. void
  286. merge_all_trailers(bool v)
  287. {
  288. merge_all_trailers_ = v;
  289. }
  290. private:
  291. parser(std::true_type);
  292. parser(std::false_type);
  293. template<class OtherBody, class... Args,
  294. class = typename std::enable_if<
  295. ! std::is_same<Body, OtherBody>::value>::type>
  296. parser(
  297. std::true_type,
  298. parser<isRequest, OtherBody, Allocator>&& parser,
  299. Args&&... args);
  300. template<class OtherBody, class... Args,
  301. class = typename std::enable_if<
  302. ! std::is_same<Body, OtherBody>::value>::type>
  303. parser(
  304. std::false_type,
  305. parser<isRequest, OtherBody, Allocator>&& parser,
  306. Args&&... args);
  307. template<class Arg1, class... ArgN,
  308. class = typename std::enable_if<
  309. ! detail::is_parser<typename
  310. std::decay<Arg1>::type>::value>::type>
  311. explicit
  312. parser(Arg1&& arg1, std::true_type, ArgN&&... argn);
  313. template<class Arg1, class... ArgN,
  314. class = typename std::enable_if<
  315. ! detail::is_parser<typename
  316. std::decay<Arg1>::type>::value>::type>
  317. explicit
  318. parser(Arg1&& arg1, std::false_type, ArgN&&... argn);
  319. void
  320. on_request_impl(
  321. verb method,
  322. string_view method_str,
  323. string_view target,
  324. int version,
  325. error_code& ec,
  326. std::true_type)
  327. {
  328. // If this assert goes off, it means you tried to re-use a
  329. // parser after it was done reading a message. This is not
  330. // allowed, you need to create a new parser for each message.
  331. // The easiest way to do that is to store the parser in
  332. // an optional object.
  333. BOOST_ASSERT(! used_);
  334. if(used_)
  335. {
  336. BOOST_BEAST_ASSIGN_EC(ec, error::stale_parser);
  337. return;
  338. }
  339. used_ = true;
  340. m_.target(target);
  341. if(method != verb::unknown)
  342. m_.method(method);
  343. else
  344. m_.method_string(method_str);
  345. m_.version(version);
  346. }
  347. void
  348. on_request_impl(
  349. verb, string_view, string_view,
  350. int, error_code&, std::false_type)
  351. {
  352. }
  353. void
  354. on_request_impl(
  355. verb method,
  356. string_view method_str,
  357. string_view target,
  358. int version,
  359. error_code& ec) override
  360. {
  361. this->on_request_impl(
  362. method, method_str, target, version, ec,
  363. std::integral_constant<bool, isRequest>{});
  364. }
  365. void
  366. on_response_impl(
  367. int code,
  368. string_view reason,
  369. int version,
  370. error_code& ec,
  371. std::true_type)
  372. {
  373. // If this assert goes off, it means you tried to re-use a
  374. // parser after it was done reading a message. This is not
  375. // allowed, you need to create a new parser for each message.
  376. // The easiest way to do that is to store the parser in
  377. // an optional object.
  378. BOOST_ASSERT(! used_);
  379. if(used_)
  380. {
  381. BOOST_BEAST_ASSIGN_EC(ec, error::stale_parser);
  382. return;
  383. }
  384. used_ = true;
  385. m_.result(code);
  386. m_.version(version);
  387. m_.reason(reason);
  388. }
  389. void
  390. on_response_impl(
  391. int, string_view, int,
  392. error_code&, std::false_type)
  393. {
  394. }
  395. void
  396. on_response_impl(
  397. int code,
  398. string_view reason,
  399. int version,
  400. error_code& ec) override
  401. {
  402. this->on_response_impl(
  403. code, reason, version, ec,
  404. std::integral_constant<bool, ! isRequest>{});
  405. }
  406. void
  407. on_field_impl(
  408. field name,
  409. string_view name_string,
  410. string_view value,
  411. error_code& ec) override
  412. {
  413. m_.insert(name, name_string, value, ec);
  414. }
  415. void
  416. on_trailer_field_impl(
  417. field name,
  418. string_view name_string,
  419. string_view value,
  420. error_code& ec) override
  421. {
  422. if(! token_list{m_[field::trailer]}.exists(name_string))
  423. return;
  424. switch(name)
  425. {
  426. case field::digest: // RFC 3230, Section 2 (obsolete)
  427. case field::content_digest: // RFC 9530, Section 2
  428. case field::repr_digest: // RFC 9530, Section 3
  429. case field::signature: // RFC 9421, Section 4
  430. case field::signature_input: // RFC 9421, Section 4
  431. case field::server_timing: // W3C Server Timing Spec
  432. case field::etag: // RFC 9110, Section 8.8.3
  433. case field::link: // RFC 8288, Section 3
  434. case field::alt_svc: // RFC 7838, Section 3
  435. break;
  436. default:
  437. if(! merge_all_trailers_)
  438. return;
  439. }
  440. m_.insert(name, name_string, value, ec);
  441. }
  442. void
  443. on_header_impl(error_code& ec) override
  444. {
  445. ec = {};
  446. }
  447. void
  448. on_body_init_impl(
  449. boost::optional<std::uint64_t> const& content_length,
  450. error_code& ec) override
  451. {
  452. rd_.init(content_length, ec);
  453. rd_inited_ = true;
  454. }
  455. std::size_t
  456. on_body_impl(
  457. string_view body,
  458. error_code& ec) override
  459. {
  460. return rd_.put(net::buffer(
  461. body.data(), body.size()), ec);
  462. }
  463. void
  464. on_chunk_header_impl(
  465. std::uint64_t size,
  466. string_view extensions,
  467. error_code& ec) override
  468. {
  469. if(cb_h_)
  470. return cb_h_(size, extensions, ec);
  471. }
  472. std::size_t
  473. on_chunk_body_impl(
  474. std::uint64_t remain,
  475. string_view body,
  476. error_code& ec) override
  477. {
  478. if(cb_b_)
  479. return cb_b_(remain, body, ec);
  480. return rd_.put(net::buffer(
  481. body.data(), body.size()), ec);
  482. }
  483. void
  484. on_finish_impl(
  485. error_code& ec) override
  486. {
  487. rd_.finish(ec);
  488. }
  489. };
  490. #if BOOST_BEAST_DOXYGEN
  491. /// An HTTP/1 parser for producing a request message.
  492. template<class Body, class Allocator = std::allocator<char>>
  493. using request_parser = parser<true, Body, Allocator>;
  494. /// An HTTP/1 parser for producing a response message.
  495. template<class Body, class Allocator = std::allocator<char>>
  496. using response_parser = parser<false, Body, Allocator>;
  497. #endif
  498. } // http
  499. } // beast
  500. } // boost
  501. #include <boost/beast/http/impl/parser.hpp>
  502. #endif