| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#ifndef GRPC_CORE_LIB_TRANSPORT_PARSED_METADATA_H |
| 16 |
#define GRPC_CORE_LIB_TRANSPORT_PARSED_METADATA_H |
| 17 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <cstdint> |
| 23 |
#include <string> |
| 24 |
#include <utility> |
| 25 |
|
| 26 |
#include "absl/functional/function_ref.h" |
| 27 |
#include "absl/meta/type_traits.h" |
| 28 |
#include "absl/strings/match.h" |
| 29 |
#include "absl/strings/str_cat.h" |
| 30 |
#include "absl/strings/string_view.h" |
| 31 |
|
| 32 |
#include <grpc/slice.h> |
| 33 |
|
| 34 |
#include "src/core/lib/gprpp/time.h" |
| 35 |
#include "src/core/lib/slice/slice.h" |
| 36 |
|
| 37 |
namespace grpc_core { |
| 38 |
|
| 39 |
using MetadataParseErrorFn = |
| 40 |
absl::FunctionRef<void(absl::string_view error, const Slice& value)>; |
| 41 |
|
| 42 |
namespace metadata_detail { |
| 43 |
|
| 44 |
// Helper to determine whether a traits metadata is inlinable inside a memento, |
| 45 |
// or (if not) we'll need to take the memory allocation path. |
| 46 |
template <typename Which> |
| 47 |
struct HasSimpleMemento { |
| 48 |
static constexpr bool value = |
| 49 |
(std::is_trivial<typename Which::MementoType>::value && |
| 50 |
sizeof(typename Which::MementoType) <= sizeof(grpc_slice)) || |
| 51 |
std::is_same<typename Which::MementoType, Duration>::value; |
| 52 |
}; |
| 53 |
|
| 54 |
// Storage type for a single metadata entry. |
| 55 |
union Buffer { |
| 56 |
uint8_t trivial[sizeof(grpc_slice)]; |
| 57 |
void* pointer; |
| 58 |
grpc_slice slice; |
| 59 |
}; |
| 60 |
|
| 61 |
// Given a key and a value, concatenate together to make a debug string. |
| 62 |
// Split out to avoid template bloat. |
| 63 |
std::string MakeDebugString(absl::string_view key, absl::string_view value); |
| 64 |
|
| 65 |
// Wrapper around MakeDebugString. |
| 66 |
// For the value part, use two functions - one to extract a typed field from |
| 67 |
// Buffer, and a second (sourced from the trait) to generate a displayable debug |
| 68 |
// string from the field value. We try to maximize indirection/code sharing here |
| 69 |
// as this is not critical path code and we'd like to avoid some code bloat - |
| 70 |
// better to scale by number of types than then number of metadata traits! |
| 71 |
template <typename Field, typename CompatibleWithField, typename Display> |
| 72 |
GPR_ATTRIBUTE_NOINLINE std::string MakeDebugStringPipeline( |
| 73 |
absl::string_view key, const Buffer& value, |
| 74 |
Field (*field_from_buffer)(const Buffer&), |
| 75 |
Display (*display_from_field)(CompatibleWithField)) { |
| 76 |
return MakeDebugString( |
| 77 |
key, absl::StrCat(display_from_field(field_from_buffer(value)))); |
| 78 |
} |
| 79 |
|
| 80 |
// Extract a trivial field value from a Buffer - for MakeDebugStringPipeline. |
| 81 |
template <typename Field> |
| 82 |
Field FieldFromTrivial(const Buffer& value) { |
| 83 |
Field field; |
| 84 |
memcpy(&field, value.trivial, sizeof(Field)); |
| 85 |
return field; |
| 86 |
} |
| 87 |
|
| 88 |
// Extract a pointer field value from a Buffer - for MakeDebugStringPipeline. |
| 89 |
template <typename Field> |
| 90 |
Field FieldFromPointer(const Buffer& value) { |
| 91 |
return *static_cast<const Field*>(value.pointer); |
| 92 |
} |
| 93 |
|
| 94 |
// Extract a Slice from a Buffer. |
| 95 |
Slice SliceFromBuffer(const Buffer& buffer); |
| 96 |
|
| 97 |
// Unref the grpc_slice part of a Buffer (assumes it is in fact a grpc_slice). |
| 98 |
void DestroySliceValue(const Buffer& value); |
| 99 |
|
| 100 |
// Destroy a trivial memento (empty function). |
| 101 |
void DestroyTrivialMemento(const Buffer& value); |
| 102 |
|
| 103 |
// Set a slice value in a container |
| 104 |
template <Slice (*MementoToValue)(Slice)> |
| 105 |
void SetSliceValue(Slice* set, const Buffer& value) { |
| 106 |
*set = MementoToValue(SliceFromBuffer(value)); |
| 107 |
} |
| 108 |
|
| 109 |
} // namespace metadata_detail |
| 110 |
|
| 111 |
// A parsed metadata value. |
| 112 |
// This type captures a type erased MementoType from one trait of |
| 113 |
// MetadataContainer, and provides utilities to manipulate that and to set it on |
| 114 |
// a MetadataContainer. |
| 115 |
template <typename MetadataContainer> |
| 116 |
class ParsedMetadata { |
| 117 |
public: |
| 118 |
// Construct metadata from a trait Which of MetadataContainer. |
| 119 |
// Two versions: the first is for simple inlinable mementos, and the second |
| 120 |
// forces an allocation. |
| 121 |
template <typename Which> |
| 122 |
ParsedMetadata( |
| 123 |
Which, |
| 124 |
absl::enable_if_t<metadata_detail::HasSimpleMemento<Which>::value, |
| 125 |
typename Which::MementoType> |
| 126 |
value, |
| 127 |
uint32_t transport_size) |
| 128 |
: vtable_(ParsedMetadata::template TrivialTraitVTable<Which>()), |
| 129 |
transport_size_(transport_size) { |
| 130 |
memcpy(value_.trivial, &value, sizeof(value)); |
| 131 |
} |
| 132 |
template <typename Which> |
| 133 |
ParsedMetadata( |
| 134 |
Which, |
| 135 |
absl::enable_if_t< |
| 136 |
!metadata_detail::HasSimpleMemento<Which>::value && |
| 137 |
!std::is_convertible<typename Which::MementoType, Slice>::value, |
| 138 |
typename Which::MementoType> |
| 139 |
value, |
| 140 |
uint32_t transport_size) |
| 141 |
: vtable_(ParsedMetadata::template NonTrivialTraitVTable<Which>()), |
| 142 |
transport_size_(transport_size) { |
| 143 |
value_.pointer = new typename Which::MementoType(std::move(value)); |
| 144 |
} |
| 145 |
// Construct metadata from a Slice typed value. |
| 146 |
template <typename Which> |
| 147 |
ParsedMetadata(Which, Slice value, uint32_t transport_size) |
| 148 |
: vtable_(ParsedMetadata::template SliceTraitVTable<Which>()), |
| 149 |
transport_size_(transport_size) { |
| 150 |
value_.slice = value.TakeCSlice(); |
| 151 |
} |
| 152 |
// Construct metadata from a string key, slice value pair. |
| 153 |
ParsedMetadata(Slice key, Slice value) |
| 154 |
: vtable_(ParsedMetadata::KeyValueVTable(key.as_string_view())), |
| 155 |
transport_size_(static_cast<uint32_t>(key.size() + value.size() + 32)) { |
| 156 |
value_.pointer = |
| 157 |
new std::pair<Slice, Slice>(std::move(key), std::move(value)); |
| 158 |
} |
| 159 |
ParsedMetadata() : vtable_(EmptyVTable()), transport_size_(0) {} |
| 160 |
~ParsedMetadata() { vtable_->destroy(value_); } |
| 161 |
|
| 162 |
// Non copyable, but movable. |
| 163 |
ParsedMetadata(const ParsedMetadata&) = delete; |
| 164 |
ParsedMetadata& operator=(const ParsedMetadata&) = delete; |
| 165 |
ParsedMetadata(ParsedMetadata&& other) noexcept |
| 166 |
: vtable_(other.vtable_), |
| 167 |
value_(other.value_), |
| 168 |
transport_size_(other.transport_size_) { |
| 169 |
other.vtable_ = EmptyVTable(); |
| 170 |
} |
| 171 |
ParsedMetadata& operator=(ParsedMetadata&& other) noexcept { |
| 172 |
vtable_ = other.vtable_; |
| 173 |
value_ = other.value_; |
| 174 |
transport_size_ = other.transport_size_; |
| 175 |
other.vtable_ = EmptyVTable(); |
| 176 |
return *this; |
| 177 |
} |
| 178 |
|
| 179 |
// Set this parsed value on a container. |
| 180 |
void SetOnContainer(MetadataContainer* container) const { |
| 181 |
vtable_->set(value_, container); |
| 182 |
} |
| 183 |
|
| 184 |
// Is this a binary header or not? |
| 185 |
bool is_binary_header() const { return vtable_->is_binary_header; } |
| 186 |
// HTTP2 defined storage size of this metadatum. |
| 187 |
uint32_t transport_size() const { return transport_size_; } |
| 188 |
// Create a new parsed metadata with the same key but a different value. |
| 189 |
ParsedMetadata WithNewValue(Slice value, |
| 190 |
MetadataParseErrorFn on_error) const { |
| 191 |
ParsedMetadata result; |
| 192 |
result.vtable_ = vtable_; |
| 193 |
result.value_ = value_; |
| 194 |
result.transport_size_ = |
| 195 |
TransportSize(static_cast<uint32_t>(key().length()), |
| 196 |
static_cast<uint32_t>(value.length())); |
| 197 |
vtable_->with_new_value(&value, on_error, &result); |
| 198 |
return result; |
| 199 |
} |
| 200 |
std::string DebugString() const { return vtable_->debug_string(value_); } |
| 201 |
absl::string_view key() const { |
| 202 |
if (vtable_->key == nullptr) return vtable_->key_value; |
| 203 |
return vtable_->key(value_); |
| 204 |
} |
| 205 |
|
| 206 |
// TODO(ctiller): move to transport |
| 207 |
static uint32_t TransportSize(uint32_t key_size, uint32_t value_size) { |
| 208 |
// TODO(ctiller): use hpack constant? |
| 209 |
return key_size + value_size + 32; |
| 210 |
} |
| 211 |
|
| 212 |
private: |
| 213 |
using Buffer = metadata_detail::Buffer; |
| 214 |
|
| 215 |
struct VTable { |
| 216 |
const bool is_binary_header; |
| 217 |
void (*const destroy)(const Buffer& value); |
| 218 |
void (*const set)(const Buffer& value, MetadataContainer* container); |
| 219 |
// result is a bitwise copy of the originating ParsedMetadata. |
| 220 |
void (*const with_new_value)(Slice* new_value, |
| 221 |
MetadataParseErrorFn on_error, |
| 222 |
ParsedMetadata* result); |
| 223 |
std::string (*const debug_string)(const Buffer& value); |
| 224 |
// The key - if key is null, use key_value, otherwise call key. |
| 225 |
absl::string_view key_value; |
| 226 |
absl::string_view (*const key)(const Buffer& value); |
| 227 |
}; |
| 228 |
|
| 229 |
static const VTable* EmptyVTable(); |
| 230 |
static const VTable* KeyValueVTable(absl::string_view key); |
| 231 |
template <typename Which> |
| 232 |
static const VTable* TrivialTraitVTable(); |
| 233 |
template <typename Which> |
| 234 |
static const VTable* NonTrivialTraitVTable(); |
| 235 |
template <typename Which> |
| 236 |
static const VTable* SliceTraitVTable(); |
| 237 |
|
| 238 |
template <Slice (*ParseMemento)(Slice, MetadataParseErrorFn)> |
| 239 |
GPR_ATTRIBUTE_NOINLINE static void WithNewValueSetSlice( |
| 240 |
Slice* slice, MetadataParseErrorFn on_error, ParsedMetadata* result) { |
| 241 |
result->value_.slice = |
| 242 |
ParseMemento(std::move(*slice), on_error).TakeCSlice(); |
| 243 |
} |
| 244 |
|
| 245 |
template <typename T, T (*ParseMemento)(Slice, MetadataParseErrorFn)> |
| 246 |
GPR_ATTRIBUTE_NOINLINE static void WithNewValueSetTrivial( |
| 247 |
Slice* slice, MetadataParseErrorFn on_error, ParsedMetadata* result) { |
| 248 |
T memento = ParseMemento(std::move(*slice), on_error); |
| 249 |
memcpy(result->value_.trivial, &memento, sizeof(memento)); |
| 250 |
} |
| 251 |
|
| 252 |
const VTable* vtable_; |
| 253 |
Buffer value_; |
| 254 |
uint32_t transport_size_; |
| 255 |
}; |
| 256 |
|
| 257 |
namespace metadata_detail {} // namespace metadata_detail |
| 258 |
|
| 259 |
template <typename MetadataContainer> |
| 260 |
const typename ParsedMetadata<MetadataContainer>::VTable* |
| 261 |
ParsedMetadata<MetadataContainer>::EmptyVTable() { |
| 262 |
static const VTable vtable = { |
| 263 |
false, |
| 264 |
// destroy |
| 265 |
metadata_detail::DestroyTrivialMemento, |
| 266 |
// set |
| 267 |
[](const Buffer&, MetadataContainer*) {}, |
| 268 |
// with_new_value |
| 269 |
[](Slice*, MetadataParseErrorFn, ParsedMetadata*) {}, |
| 270 |
// debug_string |
| 271 |
[](const Buffer&) -> std::string { return "empty"; }, |
| 272 |
// key |
| 273 |
"", |
| 274 |
nullptr, |
| 275 |
}; |
| 276 |
return &vtable; |
| 277 |
} |
| 278 |
|
| 279 |
template <typename MetadataContainer> |
| 280 |
template <typename Which> |
| 281 |
const typename ParsedMetadata<MetadataContainer>::VTable* |
| 282 |
ParsedMetadata<MetadataContainer>::TrivialTraitVTable() { |
| 283 |
static const VTable vtable = { |
| 284 |
absl::EndsWith(Which::key(), "-bin"), |
| 285 |
// destroy |
| 286 |
metadata_detail::DestroyTrivialMemento, |
| 287 |
// set |
| 288 |
[](const Buffer& value, MetadataContainer* map) { |
| 289 |
map->Set( |
| 290 |
Which(), |
| 291 |
Which::MementoToValue( |
| 292 |
metadata_detail::FieldFromTrivial<typename Which::MementoType>( |
| 293 |
value))); |
| 294 |
}, |
| 295 |
// with_new_value |
| 296 |
WithNewValueSetTrivial<typename Which::MementoType, Which::ParseMemento>, |
| 297 |
// debug_string |
| 298 |
[](const Buffer& value) { |
| 299 |
return metadata_detail::MakeDebugStringPipeline( |
| 300 |
Which::key(), value, |
| 301 |
metadata_detail::FieldFromTrivial<typename Which::MementoType>, |
| 302 |
Which::DisplayValue); |
| 303 |
}, |
| 304 |
// key |
| 305 |
Which::key(), |
| 306 |
nullptr, |
| 307 |
}; |
| 308 |
return &vtable; |
| 309 |
} |
| 310 |
|
| 311 |
template <typename MetadataContainer> |
| 312 |
template <typename Which> |
| 313 |
const typename ParsedMetadata<MetadataContainer>::VTable* |
| 314 |
ParsedMetadata<MetadataContainer>::NonTrivialTraitVTable() { |
| 315 |
static const VTable vtable = { |
| 316 |
absl::EndsWith(Which::key(), "-bin"), |
| 317 |
// destroy |
| 318 |
[](const Buffer& value) { |
| 319 |
delete static_cast<typename Which::MementoType*>(value.pointer); |
| 320 |
}, |
| 321 |
// set |
| 322 |
[](const Buffer& value, MetadataContainer* map) { |
| 323 |
auto* p = static_cast<typename Which::MementoType*>(value.pointer); |
| 324 |
map->Set(Which(), Which::MementoToValue(*p)); |
| 325 |
}, |
| 326 |
// with_new_value |
| 327 |
[](Slice* value, MetadataParseErrorFn on_error, ParsedMetadata* result) { |
| 328 |
result->value_.pointer = new typename Which::MementoType( |
| 329 |
Which::ParseMemento(std::move(*value), on_error)); |
| 330 |
}, |
| 331 |
// debug_string |
| 332 |
[](const Buffer& value) { |
| 333 |
return metadata_detail::MakeDebugStringPipeline( |
| 334 |
Which::key(), value, |
| 335 |
metadata_detail::FieldFromPointer<typename Which::MementoType>, |
| 336 |
Which::DisplayValue); |
| 337 |
}, |
| 338 |
// key |
| 339 |
Which::key(), |
| 340 |
nullptr, |
| 341 |
}; |
| 342 |
return &vtable; |
| 343 |
} |
| 344 |
|
| 345 |
template <typename MetadataContainer> |
| 346 |
template <typename Which> |
| 347 |
const typename ParsedMetadata<MetadataContainer>::VTable* |
| 348 |
ParsedMetadata<MetadataContainer>::SliceTraitVTable() { |
| 349 |
static const VTable vtable = { |
| 350 |
absl::EndsWith(Which::key(), "-bin"), |
| 351 |
// destroy |
| 352 |
metadata_detail::DestroySliceValue, |
| 353 |
// set |
| 354 |
[](const Buffer& value, MetadataContainer* map) { |
| 355 |
metadata_detail::SetSliceValue<Which::MementoToValue>( |
| 356 |
map->GetOrCreatePointer(Which()), value); |
| 357 |
}, |
| 358 |
// with_new_value |
| 359 |
WithNewValueSetSlice<Which::ParseMemento>, |
| 360 |
// debug_string |
| 361 |
[](const Buffer& value) { |
| 362 |
return metadata_detail::MakeDebugStringPipeline( |
| 363 |
Which::key(), value, metadata_detail::SliceFromBuffer, |
| 364 |
Which::DisplayValue); |
| 365 |
}, |
| 366 |
// key |
| 367 |
Which::key(), |
| 368 |
nullptr, |
| 369 |
}; |
| 370 |
return &vtable; |
| 371 |
} |
| 372 |
|
| 373 |
template <typename MetadataContainer> |
| 374 |
const typename ParsedMetadata<MetadataContainer>::VTable* |
| 375 |
ParsedMetadata<MetadataContainer>::KeyValueVTable(absl::string_view key) { |
| 376 |
using KV = std::pair<Slice, Slice>; |
| 377 |
static const auto destroy = [](const Buffer& value) { |
| 378 |
delete static_cast<KV*>(value.pointer); |
| 379 |
}; |
| 380 |
static const auto set = [](const Buffer& value, MetadataContainer* map) { |
| 381 |
auto* p = static_cast<KV*>(value.pointer); |
| 382 |
map->unknown_.Append(p->first.as_string_view(), p->second.Ref()); |
| 383 |
}; |
| 384 |
static const auto with_new_value = [](Slice* value, MetadataParseErrorFn, |
| 385 |
ParsedMetadata* result) { |
| 386 |
auto* p = new KV{ |
| 387 |
static_cast<KV*>(result->value_.pointer)->first.Ref(), |
| 388 |
std::move(*value), |
| 389 |
}; |
| 390 |
result->value_.pointer = p; |
| 391 |
}; |
| 392 |
static const auto debug_string = [](const Buffer& value) { |
| 393 |
auto* p = static_cast<KV*>(value.pointer); |
| 394 |
return absl::StrCat(p->first.as_string_view(), ": ", |
| 395 |
p->second.as_string_view()); |
| 396 |
}; |
| 397 |
static const auto key_fn = [](const Buffer& value) { |
| 398 |
return static_cast<KV*>(value.pointer)->first.as_string_view(); |
| 399 |
}; |
| 400 |
static const VTable vtable[2] = { |
| 401 |
{false, destroy, set, with_new_value, debug_string, "", key_fn}, |
| 402 |
{true, destroy, set, with_new_value, debug_string, "", key_fn}, |
| 403 |
}; |
| 404 |
return &vtable[absl::EndsWith(key, "-bin")]; |
| 405 |
} |
| 406 |
|
| 407 |
} // namespace grpc_core |
| 408 |
|
| 409 |
#endif // GRPC_CORE_LIB_TRANSPORT_PARSED_METADATA_H |