| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) | ||
| 3 | // Copyright (c) 2024 Mohammad Nejati | ||
| 4 | // | ||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 7 | // | ||
| 8 | // Official repository: https://github.com/cppalliance/http_proto | ||
| 9 | // | ||
| 10 | |||
| 11 | #ifndef BOOST_HTTP_PROTO_DETAIL_HEADER_HPP | ||
| 12 | #define BOOST_HTTP_PROTO_DETAIL_HEADER_HPP | ||
| 13 | |||
| 14 | #include <boost/http_proto/detail/align_up.hpp> | ||
| 15 | #include <boost/http_proto/detail/config.hpp> | ||
| 16 | #include <boost/http_proto/error.hpp> | ||
| 17 | #include <boost/http_proto/field.hpp> | ||
| 18 | #include <boost/http_proto/metadata.hpp> | ||
| 19 | #include <boost/http_proto/method.hpp> | ||
| 20 | #include <boost/http_proto/status.hpp> | ||
| 21 | #include <boost/http_proto/version.hpp> | ||
| 22 | #include <boost/core/detail/string_view.hpp> | ||
| 23 | #include <boost/assert.hpp> | ||
| 24 | #include <cstdint> | ||
| 25 | #include <type_traits> | ||
| 26 | |||
| 27 | namespace boost { | ||
| 28 | namespace http_proto { | ||
| 29 | |||
| 30 | class fields_base; | ||
| 31 | struct header_limits; | ||
| 32 | |||
| 33 | namespace detail { | ||
| 34 | |||
| 35 | enum kind : unsigned char | ||
| 36 | { | ||
| 37 | fields = 0, | ||
| 38 | request, | ||
| 39 | response, | ||
| 40 | }; | ||
| 41 | |||
| 42 | struct empty | ||
| 43 | { | ||
| 44 | kind param; | ||
| 45 | }; | ||
| 46 | |||
| 47 | struct header | ||
| 48 | { | ||
| 49 | // this field lookup table is | ||
| 50 | // stored at the end of the | ||
| 51 | // allocated buffer, in | ||
| 52 | // reverse order. | ||
| 53 | struct entry | ||
| 54 | { | ||
| 55 | offset_type np; // name pos | ||
| 56 | offset_type nn; // name size | ||
| 57 | offset_type vp; // value pos | ||
| 58 | offset_type vn; // value size | ||
| 59 | field id; | ||
| 60 | |||
| 61 | entry operator+( | ||
| 62 | std::size_t dv) const noexcept; | ||
| 63 | entry operator-( | ||
| 64 | std::size_t dv) const noexcept; | ||
| 65 | }; | ||
| 66 | |||
| 67 | // HTTP-message = start-line CRLF *( field-line CRLF ) CRLF | ||
| 68 | // start-line = request-line / status-line | ||
| 69 | // status-line = HTTP-version SP status-code SP [ reason-phrase ] | ||
| 70 | // status-code = 3DIGIT | ||
| 71 | // HTTP-name = %x48.54.54.50 ; HTTP | ||
| 72 | // HTTP-version = HTTP-name "/" DIGIT "." DIGIT | ||
| 73 | // | ||
| 74 | // => "HTTP/1.1 111 \r\n" + trailing "\r\n" | ||
| 75 | static | ||
| 76 | constexpr | ||
| 77 | std::size_t const min_status_line = 17; | ||
| 78 | |||
| 79 | // "X:\r\n" | ||
| 80 | static | ||
| 81 | constexpr | ||
| 82 | std::size_t const min_field_line = 4; | ||
| 83 | |||
| 84 | static | ||
| 85 | constexpr | ||
| 86 | std::size_t const max_field_lines = | ||
| 87 | (max_offset - min_status_line) / min_field_line; | ||
| 88 | |||
| 89 | /** Returns the largest permissible capacity in bytes | ||
| 90 | */ | ||
| 91 | static | ||
| 92 | constexpr | ||
| 93 | std::size_t | ||
| 94 | 11913 | max_capacity_in_bytes() noexcept | |
| 95 | { | ||
| 96 | // the entire serialized contents of the header | ||
| 97 | // must fit entirely in max_offset | ||
| 98 | 11913 | return align_up( | |
| 99 | (max_offset + (max_field_lines * sizeof(entry))), | ||
| 100 | 11913 | alignof(entry)); | |
| 101 | } | ||
| 102 | |||
| 103 | struct table | ||
| 104 | { | ||
| 105 | explicit | ||
| 106 | 26679 | table( | |
| 107 | void* end) noexcept | ||
| 108 | 26679 | : p_(reinterpret_cast< | |
| 109 | entry*>(end)) | ||
| 110 | { | ||
| 111 | 26679 | } | |
| 112 | |||
| 113 | entry& | ||
| 114 | 26709 | operator[]( | |
| 115 | std::size_t i) const noexcept | ||
| 116 | { | ||
| 117 | 26709 | return p_[-1 * ( | |
| 118 | 26709 | static_cast< | |
| 119 | 26709 | long>(i) + 1)]; | |
| 120 | } | ||
| 121 | |||
| 122 | private: | ||
| 123 | entry* p_; | ||
| 124 | }; | ||
| 125 | |||
| 126 | struct fld_t | ||
| 127 | { | ||
| 128 | }; | ||
| 129 | |||
| 130 | struct req_t | ||
| 131 | { | ||
| 132 | offset_type method_len; | ||
| 133 | offset_type target_len; | ||
| 134 | http_proto::method method; | ||
| 135 | }; | ||
| 136 | |||
| 137 | struct res_t | ||
| 138 | { | ||
| 139 | unsigned short status_int; | ||
| 140 | http_proto::status status; | ||
| 141 | }; | ||
| 142 | |||
| 143 | //-------------------------------------------- | ||
| 144 | |||
| 145 | detail::kind kind; | ||
| 146 | char const* cbuf = nullptr; | ||
| 147 | char* buf = nullptr; | ||
| 148 | std::size_t cap = 0; | ||
| 149 | std::size_t max_cap = max_capacity_in_bytes(); | ||
| 150 | |||
| 151 | offset_type size = 0; | ||
| 152 | offset_type count = 0; | ||
| 153 | offset_type prefix = 0; | ||
| 154 | |||
| 155 | http_proto::version version = | ||
| 156 | http_proto::version::http_1_1; | ||
| 157 | metadata md; | ||
| 158 | |||
| 159 | union | ||
| 160 | { | ||
| 161 | fld_t fld; | ||
| 162 | req_t req; | ||
| 163 | res_t res; | ||
| 164 | }; | ||
| 165 | |||
| 166 | private: | ||
| 167 | struct fields_tag {}; | ||
| 168 | struct request_tag {}; | ||
| 169 | struct response_tag {}; | ||
| 170 | |||
| 171 | constexpr header(fields_tag) noexcept; | ||
| 172 | constexpr header(request_tag) noexcept; | ||
| 173 | constexpr header(response_tag) noexcept; | ||
| 174 | |||
| 175 | public: | ||
| 176 | // in fields_base.hpp | ||
| 177 | static header& get(fields_base& f) noexcept; | ||
| 178 | |||
| 179 | BOOST_HTTP_PROTO_DECL static header const* | ||
| 180 | get_default(detail::kind k) noexcept; | ||
| 181 | |||
| 182 | // called from parser | ||
| 183 | explicit header(empty) noexcept; | ||
| 184 | |||
| 185 | BOOST_HTTP_PROTO_DECL header(detail::kind) noexcept; | ||
| 186 | BOOST_HTTP_PROTO_DECL void swap(header&) noexcept; | ||
| 187 | BOOST_HTTP_PROTO_DECL bool keep_alive() const noexcept; | ||
| 188 | |||
| 189 | static std::size_t bytes_needed( | ||
| 190 | std::size_t size, std::size_t count) noexcept; | ||
| 191 | static std::size_t table_space( | ||
| 192 | std::size_t count) noexcept; | ||
| 193 | std::size_t table_space() const noexcept; | ||
| 194 | |||
| 195 | table tab() const noexcept; | ||
| 196 | entry* tab_() const noexcept; | ||
| 197 | bool is_default() const noexcept; | ||
| 198 | std::size_t find(field) const noexcept; | ||
| 199 | std::size_t find(core::string_view) const noexcept; | ||
| 200 | void copy_table(void*, std::size_t) const noexcept; | ||
| 201 | void copy_table(void*) const noexcept; | ||
| 202 | void assign_to(header&) const noexcept; | ||
| 203 | |||
| 204 | // metadata | ||
| 205 | |||
| 206 | std::size_t maybe_count(field) const noexcept; | ||
| 207 | bool is_special(field) const noexcept; | ||
| 208 | void on_start_line(); | ||
| 209 | void on_insert(field, core::string_view); | ||
| 210 | void on_erase(field); | ||
| 211 | void on_insert_connection(core::string_view); | ||
| 212 | void on_insert_content_length(core::string_view); | ||
| 213 | void on_insert_expect(core::string_view); | ||
| 214 | void on_insert_transfer_encoding(); | ||
| 215 | void on_insert_content_encoding(core::string_view); | ||
| 216 | void on_insert_upgrade(core::string_view); | ||
| 217 | void on_erase_connection(); | ||
| 218 | void on_erase_content_length(); | ||
| 219 | void on_erase_expect(); | ||
| 220 | void on_erase_transfer_encoding(); | ||
| 221 | void on_erase_content_encoding(); | ||
| 222 | void on_erase_upgrade(); | ||
| 223 | void on_erase_all(field); | ||
| 224 | void update_payload() noexcept; | ||
| 225 | |||
| 226 | // parsing | ||
| 227 | |||
| 228 | static std::size_t count_crlf( | ||
| 229 | core::string_view s) noexcept; | ||
| 230 | BOOST_HTTP_PROTO_DECL void parse( | ||
| 231 | std::size_t, header_limits const&, | ||
| 232 | system::error_code&) noexcept; | ||
| 233 | }; | ||
| 234 | |||
| 235 | } // detail | ||
| 236 | } // http_proto | ||
| 237 | } // boost | ||
| 238 | |||
| 239 | #endif | ||
| 240 |