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