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