basic_parser.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. trailer_fields,
  43. complete
  44. };
  45. static
  46. bool
  47. is_digit(char c)
  48. {
  49. return static_cast<unsigned char>(c-'0') < 10;
  50. }
  51. static
  52. bool
  53. is_print(char c)
  54. {
  55. return static_cast<unsigned char>(c-32) < 95;
  56. }
  57. BOOST_BEAST_DECL
  58. static
  59. char const*
  60. trim_front(char const* it, char const* end);
  61. BOOST_BEAST_DECL
  62. static
  63. char const*
  64. trim_back(
  65. char const* it, char const* first);
  66. static
  67. string_view
  68. make_string(char const* first, char const* last)
  69. {
  70. return {first, static_cast<
  71. std::size_t>(last - first)};
  72. }
  73. //--------------------------------------------------------------------------
  74. BOOST_BEAST_DECL
  75. static
  76. bool
  77. is_pathchar(char c);
  78. BOOST_BEAST_DECL
  79. static
  80. bool
  81. unhex(unsigned char& d, char c);
  82. BOOST_BEAST_DECL
  83. static
  84. std::pair<char const*, bool>
  85. find_fast(
  86. char const* buf,
  87. char const* buf_end,
  88. char const* ranges,
  89. size_t ranges_size);
  90. BOOST_BEAST_DECL
  91. static
  92. char const*
  93. find_eol(
  94. char const* it, char const* last,
  95. error_code& ec);
  96. //--------------------------------------------------------------------------
  97. BOOST_BEAST_DECL
  98. static
  99. char const*
  100. parse_token_to_eol(
  101. char const* p,
  102. char const* last,
  103. char const*& token_last,
  104. error_code& ec);
  105. BOOST_BEAST_DECL
  106. static
  107. bool
  108. parse_dec(string_view s, std::uint64_t& v);
  109. BOOST_BEAST_DECL
  110. static
  111. bool
  112. parse_hex(char const*& it, std::uint64_t& v);
  113. BOOST_BEAST_DECL
  114. static
  115. bool
  116. parse_crlf(char const*& it);
  117. BOOST_BEAST_DECL
  118. static
  119. void
  120. parse_method(
  121. char const*& it, char const* last,
  122. string_view& result, error_code& ec);
  123. BOOST_BEAST_DECL
  124. static
  125. void
  126. parse_target(
  127. char const*& it, char const* last,
  128. string_view& result, error_code& ec);
  129. BOOST_BEAST_DECL
  130. static
  131. void
  132. parse_version(
  133. char const*& it, char const* last,
  134. int& result, error_code& ec);
  135. BOOST_BEAST_DECL
  136. static
  137. void
  138. parse_status(
  139. char const*& it, char const* last,
  140. unsigned short& result, error_code& ec);
  141. BOOST_BEAST_DECL
  142. static
  143. void
  144. parse_reason(
  145. char const*& it, char const* last,
  146. string_view& result, error_code& ec);
  147. BOOST_BEAST_DECL
  148. static
  149. void
  150. parse_field(
  151. char const*& p,
  152. char const* last,
  153. string_view& name,
  154. string_view& value,
  155. beast::detail::char_buffer<max_obs_fold>& buf,
  156. error_code& ec);
  157. BOOST_BEAST_DECL
  158. static
  159. void
  160. parse_chunk_extensions(
  161. char const*& it,
  162. char const* last,
  163. error_code& ec);
  164. };
  165. } // detail
  166. } // http
  167. } // beast
  168. } // boost
  169. #ifdef BOOST_BEAST_HEADER_ONLY
  170. #include <boost/beast/http/detail/basic_parser.ipp>
  171. #endif
  172. #endif