basic_parser.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_HPP
  10. #define BOOST_BEAST_HTTP_DETAIL_BASIC_PARSER_HPP
  11. #include <boost/beast/core/string.hpp>
  12. #include <boost/beast/core/detail/char_buffer.hpp>
  13. #include <boost/beast/http/error.hpp>
  14. #include <boost/beast/http/detail/rfc7230.hpp>
  15. #include <boost/config.hpp>
  16. #include <boost/version.hpp>
  17. #include <cstddef>
  18. #include <utility>
  19. namespace boost {
  20. namespace beast {
  21. namespace http {
  22. namespace detail {
  23. struct basic_parser_base
  24. {
  25. // limit on the size of the obs-fold buffer
  26. //
  27. // https://stackoverflow.com/questions/686217/maximum-on-http-header-values
  28. //
  29. static std::size_t constexpr max_obs_fold = 4096;
  30. enum class state
  31. {
  32. nothing_yet = 0,
  33. start_line,
  34. fields,
  35. body0,
  36. body,
  37. body_to_eof0,
  38. body_to_eof,
  39. chunk_header0,
  40. chunk_header,
  41. chunk_body,
  42. complete
  43. };
  44. static
  45. bool
  46. is_digit(char c)
  47. {
  48. return static_cast<unsigned char>(c-'0') < 10;
  49. }
  50. static
  51. bool
  52. is_print(char c)
  53. {
  54. return static_cast<unsigned char>(c-32) < 95;
  55. }
  56. BOOST_BEAST_DECL
  57. static
  58. char const*
  59. trim_front(char const* it, char const* end);
  60. BOOST_BEAST_DECL
  61. static
  62. char const*
  63. trim_back(
  64. char const* it, char const* first);
  65. static
  66. string_view
  67. make_string(char const* first, char const* last)
  68. {
  69. return {first, static_cast<
  70. std::size_t>(last - first)};
  71. }
  72. //--------------------------------------------------------------------------
  73. BOOST_BEAST_DECL
  74. static
  75. bool
  76. is_pathchar(char c);
  77. BOOST_BEAST_DECL
  78. static
  79. bool
  80. unhex(unsigned char& d, char c);
  81. BOOST_BEAST_DECL
  82. static
  83. std::pair<char const*, bool>
  84. find_fast(
  85. char const* buf,
  86. char const* buf_end,
  87. char const* ranges,
  88. size_t ranges_size);
  89. BOOST_BEAST_DECL
  90. static
  91. char const*
  92. find_eol(
  93. char const* it, char const* last,
  94. error_code& ec);
  95. BOOST_BEAST_DECL
  96. static
  97. char const*
  98. find_eom(char const* p, char const* last);
  99. //--------------------------------------------------------------------------
  100. BOOST_BEAST_DECL
  101. static
  102. char const*
  103. parse_token_to_eol(
  104. char const* p,
  105. char const* last,
  106. char const*& token_last,
  107. error_code& ec);
  108. BOOST_BEAST_DECL
  109. static
  110. bool
  111. parse_dec(string_view s, std::uint64_t& v);
  112. BOOST_BEAST_DECL
  113. static
  114. bool
  115. parse_hex(char const*& it, std::uint64_t& v);
  116. BOOST_BEAST_DECL
  117. static
  118. bool
  119. parse_crlf(char const*& it);
  120. BOOST_BEAST_DECL
  121. static
  122. void
  123. parse_method(
  124. char const*& it, char const* last,
  125. string_view& result, error_code& ec);
  126. BOOST_BEAST_DECL
  127. static
  128. void
  129. parse_target(
  130. char const*& it, char const* last,
  131. string_view& result, error_code& ec);
  132. BOOST_BEAST_DECL
  133. static
  134. void
  135. parse_version(
  136. char const*& it, char const* last,
  137. int& result, error_code& ec);
  138. BOOST_BEAST_DECL
  139. static
  140. void
  141. parse_status(
  142. char const*& it, char const* last,
  143. unsigned short& result, error_code& ec);
  144. BOOST_BEAST_DECL
  145. static
  146. void
  147. parse_reason(
  148. char const*& it, char const* last,
  149. string_view& result, error_code& ec);
  150. BOOST_BEAST_DECL
  151. static
  152. void
  153. parse_field(
  154. char const*& p,
  155. char const* last,
  156. string_view& name,
  157. string_view& value,
  158. beast::detail::char_buffer<max_obs_fold>& buf,
  159. error_code& ec);
  160. BOOST_BEAST_DECL
  161. static
  162. void
  163. parse_chunk_extensions(
  164. char const*& it,
  165. char const* last,
  166. error_code& ec);
  167. };
  168. } // detail
  169. } // http
  170. } // beast
  171. } // boost
  172. #ifdef BOOST_BEAST_HEADER_ONLY
  173. #include <boost/beast/http/detail/basic_parser.ipp>
  174. #endif
  175. #endif