8#include <userver/utils/small_string_fwd.hpp>
9#include <userver/utils/string_literal.hpp>
10#include <userver/utils/trivial_map.hpp>
12USERVER_NAMESPACE_BEGIN
18inline constexpr std::size_t kTypicalHeadersSize = 1024;
19using HeadersString =
utils::SmallString<kTypicalHeadersSize>;
28struct UnsafeConstexprHasher
final {
29 constexpr std::size_t operator()(std::string_view str)
const noexcept {
30 constexpr std::uint64_t mul = (0xc6a4a793UL << 32UL) + 0x5bd1e995UL;
32 std::uint64_t hash = seed_ ^ (str.size() * mul);
33 while (str.size() >= 8) {
34 const std::uint64_t data = ShiftMix(Load8(str.data()) * mul) * mul;
41 const std::uint64_t data = LoadN(str.data(), str.size());
46 hash = ShiftMix(hash) * mul;
47 hash = ShiftMix(hash);
52 static constexpr inline std::uint64_t ShiftMix(std::uint64_t v)
noexcept {
return v ^ (v >> 47); }
54 static constexpr inline std::uint64_t Load8(
const char* data)
noexcept {
return LoadN(data, 8); }
56 static constexpr inline std::uint64_t LoadN(
const char* data, std::size_t n)
noexcept {
63 constexpr std::uint64_t kDeliberatelyBrokenLowercaseMask = 0x2020202020202020UL;
65 std::uint64_t result = kDeliberatelyBrokenLowercaseMask >> (8 * (8 - n));
66 for (std::size_t i = 0; i < n; ++i) {
67 const std::uint8_t c = data[i];
68 result |=
static_cast<std::uint64_t>(c) << (8 * i);
80 std::uint64_t seed_{54999};
84inline constexpr utils::TrivialBiMap kKnownHeadersLowercaseMap = [](
auto selector) {
86 .Case(
"content-type", std::int8_t{1})
87 .Case(
"content-encoding", 2)
88 .Case(
"content-length", 3)
89 .Case(
"transfer-encoding", 4)
92 .Case(
"accept-encoding", 7)
93 .Case(
"accept-language", 8)
94 .Case(
"x-yataxi-api-key", 9)
95 .Case(
"user-agent", 10)
96 .Case(
"x-request-application", 11)
99 .Case(
"access-control-allow-headers", 14)
102 .Case(
"set-cookie", 17)
103 .Case(
"connection", 18)
105 .Case(
"x-yarequestid", 20)
106 .Case(
"x-yatraceid", 21)
107 .Case(
"x-yaspanid", 22)
108 .Case(
"x-requestid", 23)
109 .Case(
"x-backend-server", 24)
110 .Case(
"x-taxi-envoyproxy-dstvhost", 25)
112 .Case(
"x-yataxi-allow-auth-request", 27)
113 .Case(
"x-yataxi-allow-auth-response", 28)
114 .Case(
"x-yataxi-server-hostname", 29)
115 .Case(
"x-yataxi-client-timeoutms", 30)
116 .Case(
"x-yataxi-deadline-expired", 31)
117 .Case(
"x-yataxi-ratelimited-by", 32)
118 .Case(
"x-yataxi-ratelimit-reason", 33)
119 .Case(
"x-b3-traceid", 34)
120 .Case(
"x-b3-spanid", 35)
121 .Case(
"x-b3-sampled", 36)
122 .Case(
"x-b3-parentspanid", 37)
123 .Case(
"traceparent", 38)
124 .Case(
"tracestate", 39)
125 .Case(
"http2-settings", 40)
132inline constexpr std::int8_t kNoHeaderIndexLookup = -1;
133inline constexpr std::int8_t kNoHeaderIndexInsertion = -2;
134static_assert(kNoHeaderIndexLookup != kNoHeaderIndexInsertion);
135static_assert(kNoHeaderIndexLookup != 0 && kNoHeaderIndexInsertion != 0);
138constexpr std::int8_t GetHeaderIndexForLookup(std::string_view key) {
140 return opt.value_or(kNoHeaderIndexLookup);
146inline std::int8_t GetHeaderIndexForInsertion(std::string_view key) {
148 return opt.value_or(kNoHeaderIndexInsertion);
153namespace header_map {
168class PredefinedHeader
final {
171 : name{name}, hash{impl::UnsafeConstexprHasher{}(name)}, header_index{impl::GetHeaderIndexForLookup(name)} {}
177 constexpr operator std::string_view()
const {
return name; }
179 explicit operator std::string()
const {
return std::string{name}; }
182 friend class header_map::Danger;
183 friend class header_map::Map;
189 const std::size_t hash;
197 const std::int8_t header_index;
205struct fmt::formatter<USERVER_NAMESPACE::
http::
headers::PredefinedHeader> : fmt::formatter<std::string_view> {
206 template <
typename FormatContext>
207 auto format(
const USERVER_NAMESPACE::
http::
headers::PredefinedHeader& value, FormatContext& ctx)
const
208 ->
decltype(ctx.out()) {
209 return fmt::format_to(ctx.out(),
"{}", std::string_view{value});