8#include <userver/utils/small_string_fwd.hpp>
9#include <userver/utils/trivial_map.hpp>
11USERVER_NAMESPACE_BEGIN
17inline constexpr std::size_t kTypicalHeadersSize = 1024;
27struct UnsafeConstexprHasher
final {
28 constexpr std::size_t operator()(std::string_view str)
const noexcept {
29 constexpr std::uint64_t mul = (0xc6a4a793UL << 32UL) + 0x5bd1e995UL;
31 std::uint64_t hash = seed_ ^ (str.size() * mul);
32 while (str.size() >= 8) {
33 const std::uint64_t data = ShiftMix(Load8(str.data()) * mul) * mul;
40 const std::uint64_t data = LoadN(str.data(), str.size());
45 hash = ShiftMix(hash) * mul;
46 hash = ShiftMix(hash);
51 static constexpr inline std::uint64_t ShiftMix(std::uint64_t v)
noexcept {
return v ^ (v >> 47); }
53 static constexpr inline std::uint64_t Load8(
const char* data)
noexcept {
return LoadN(data, 8); }
55 static constexpr inline std::uint64_t LoadN(
const char* data, std::size_t n)
noexcept {
62 constexpr std::uint64_t kDeliberatelyBrokenLowercaseMask = 0x2020202020202020UL;
64 std::uint64_t result = kDeliberatelyBrokenLowercaseMask >> (8 * (8 - n));
65 for (std::size_t i = 0; i < n; ++i) {
66 const std::uint8_t c = data[i];
67 result |=
static_cast<std::uint64_t>(c) << (8 * i);
79 std::uint64_t seed_{54999};
83inline constexpr utils::TrivialBiMap kKnownHeadersLowercaseMap = [](
auto selector) {
85 .Case(
"content-type", std::int8_t{1})
86 .Case(
"content-encoding", 2)
87 .Case(
"content-length", 3)
88 .Case(
"transfer-encoding", 4)
91 .Case(
"accept-encoding", 7)
92 .Case(
"accept-language", 8)
93 .Case(
"x-yataxi-api-key", 9)
94 .Case(
"user-agent", 10)
95 .Case(
"x-request-application", 11)
98 .Case(
"access-control-allow-headers", 14)
101 .Case(
"set-cookie", 17)
102 .Case(
"connection", 18)
104 .Case(
"x-yarequestid", 20)
105 .Case(
"x-yatraceid", 21)
106 .Case(
"x-yaspanid", 22)
107 .Case(
"x-requestid", 23)
108 .Case(
"x-backend-server", 24)
109 .Case(
"x-taxi-envoyproxy-dstvhost", 25)
111 .Case(
"x-yataxi-allow-auth-request", 27)
112 .Case(
"x-yataxi-allow-auth-response", 28)
113 .Case(
"x-yataxi-server-hostname", 29)
114 .Case(
"x-yataxi-client-timeoutms", 30)
115 .Case(
"x-yataxi-deadline-expired", 31)
116 .Case(
"x-yataxi-ratelimited-by", 32)
117 .Case(
"x-yataxi-ratelimit-reason", 33)
118 .Case(
"x-b3-traceid", 34)
119 .Case(
"x-b3-spanid", 35)
120 .Case(
"x-b3-sampled", 36)
121 .Case(
"x-b3-parentspanid", 37)
122 .Case(
"traceparent", 38)
123 .Case(
"tracestate", 39)
124 .Case(
"http2-settings", 40)
131inline constexpr std::int8_t kNoHeaderIndexLookup = -1;
132inline constexpr std::int8_t kNoHeaderIndexInsertion = -2;
133static_assert(kNoHeaderIndexLookup != kNoHeaderIndexInsertion);
134static_assert(kNoHeaderIndexLookup != 0 && kNoHeaderIndexInsertion != 0);
137constexpr std::int8_t GetHeaderIndexForLookup(std::string_view key) {
138 const auto opt = kKnownHeadersLowercaseMap.TryFindICaseByFirst(key);
139 return opt.value_or(kNoHeaderIndexLookup);
145inline std::int8_t GetHeaderIndexForInsertion(std::string_view key) {
146 const auto opt = kKnownHeadersLowercaseMap.TryFindICaseByFirst(key);
147 return opt.value_or(kNoHeaderIndexInsertion);
152namespace header_map {
167class PredefinedHeader
final {
169 explicit constexpr PredefinedHeader(std::string_view name)
170 : name{name}, hash{impl::UnsafeConstexprHasher{}(name)}, header_index{impl::GetHeaderIndexForLookup(name)} {}
172 constexpr operator std::string_view()
const {
return name; }
174 explicit operator std::string()
const {
return std::string{name}; }
177 friend class header_map::Danger;
178 friend class header_map::Map;
181 const std::string_view name;
184 const std::size_t hash;
192 const std::int8_t header_index;
200struct fmt::formatter<USERVER_NAMESPACE::http::headers::PredefinedHeader> : fmt::formatter<std::string_view> {
201 template <
typename FormatContext>
202 auto format(
const USERVER_NAMESPACE::http::headers::PredefinedHeader& value, FormatContext& ctx)
const
203 ->
decltype(ctx.out()) {
204 return formatter<std::string_view>::format(std::string_view{value}, ctx);