| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef GRPC_CORE_LIB_TRANSPORT_METADATA_BATCH_H |
| 20 |
#define GRPC_CORE_LIB_TRANSPORT_METADATA_BATCH_H |
| 21 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <stdlib.h> |
| 25 |
|
| 26 |
#include <cstdint> |
| 27 |
#include <string> |
| 28 |
#include <utility> |
| 29 |
|
| 30 |
#include "absl/container/inlined_vector.h" |
| 31 |
#include "absl/functional/function_ref.h" |
| 32 |
#include "absl/meta/type_traits.h" |
| 33 |
#include "absl/strings/numbers.h" |
| 34 |
#include "absl/strings/string_view.h" |
| 35 |
#include "absl/types/optional.h" |
| 36 |
|
| 37 |
#include <grpc/impl/codegen/compression_types.h> |
| 38 |
#include <grpc/status.h> |
| 39 |
#include <grpc/support/log.h> |
| 40 |
|
| 41 |
#include "src/core/lib/compression/compression_internal.h" |
| 42 |
#include "src/core/lib/gprpp/chunked_vector.h" |
| 43 |
#include "src/core/lib/gprpp/packed_table.h" |
| 44 |
#include "src/core/lib/gprpp/time.h" |
| 45 |
#include "src/core/lib/resource_quota/arena.h" |
| 46 |
#include "src/core/lib/slice/slice.h" |
| 47 |
#include "src/core/lib/transport/parsed_metadata.h" |
| 48 |
|
| 49 |
namespace grpc_core { |
| 50 |
|
| 51 |
// grpc-timeout metadata trait. |
| 52 |
// ValueType is defined as Timestamp - an absolute timestamp (i.e. a |
| 53 |
// deadline!), that is converted to a duration by transports before being |
| 54 |
// sent. |
| 55 |
// TODO(ctiller): Move this elsewhere. During the transition we need to be able |
| 56 |
// to name this in MetadataMap, but ultimately once the transition is done we |
| 57 |
// should not need to. |
| 58 |
struct GrpcTimeoutMetadata { |
| 59 |
static constexpr bool kRepeatable = false; |
| 60 |
using ValueType = Timestamp; |
| 61 |
using MementoType = Duration; |
| 62 |
static absl::string_view key() { return "grpc-timeout"; } |
| 63 |
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error); |
| 64 |
static ValueType MementoToValue(MementoType timeout); |
| 65 |
static Slice Encode(ValueType x); |
| 66 |
static std::string DisplayValue(MementoType x) { return x.ToString(); } |
| 67 |
}; |
| 68 |
|
| 69 |
// TE metadata trait. |
| 70 |
struct TeMetadata { |
| 71 |
static constexpr bool kRepeatable = false; |
| 72 |
// HTTP2 says that TE can either be empty or "trailers". |
| 73 |
// Empty means this trait is not included, "trailers" means kTrailers, and |
| 74 |
// kInvalid is used to remember an invalid value. |
| 75 |
enum ValueType : uint8_t { |
| 76 |
kTrailers, |
| 77 |
kInvalid, |
| 78 |
}; |
| 79 |
using MementoType = ValueType; |
| 80 |
static absl::string_view key() { return "te"; } |
| 81 |
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error); |
| 82 |
static ValueType MementoToValue(MementoType te) { return te; } |
| 83 |
static StaticSlice Encode(ValueType x) { |
| 84 |
GPR_ASSERT(x == kTrailers); |
| 85 |
return StaticSlice::FromStaticString("trailers"); |
| 86 |
} |
| 87 |
static const char* DisplayValue(MementoType te); |
| 88 |
}; |
| 89 |
|
| 90 |
// content-type metadata trait. |
| 91 |
struct ContentTypeMetadata { |
| 92 |
static constexpr bool kRepeatable = false; |
| 93 |
// gRPC says that content-type can be application/grpc[;something] |
| 94 |
// Core has only ever verified the prefix. |
| 95 |
// IF we want to start verifying more, we can expand this type. |
| 96 |
enum ValueType : uint8_t { |
| 97 |
kApplicationGrpc, |
| 98 |
kEmpty, |
| 99 |
kInvalid, |
| 100 |
}; |
| 101 |
using MementoType = ValueType; |
| 102 |
static absl::string_view key() { return "content-type"; } |
| 103 |
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error); |
| 104 |
static ValueType MementoToValue(MementoType content_type) { |
| 105 |
return content_type; |
| 106 |
} |
| 107 |
|
| 108 |
static StaticSlice Encode(ValueType x); |
| 109 |
static const char* DisplayValue(MementoType content_type); |
| 110 |
}; |
| 111 |
|
| 112 |
// scheme metadata trait. |
| 113 |
struct HttpSchemeMetadata { |
| 114 |
static constexpr bool kRepeatable = false; |
| 115 |
enum ValueType : uint8_t { |
| 116 |
kHttp, |
| 117 |
kHttps, |
| 118 |
kInvalid, |
| 119 |
}; |
| 120 |
using MementoType = ValueType; |
| 121 |
static absl::string_view key() { return ":scheme"; } |
| 122 |
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error) { |
| 123 |
return Parse(value.as_string_view(), on_error); |
| 124 |
} |
| 125 |
static ValueType Parse(absl::string_view value, |
| 126 |
MetadataParseErrorFn on_error); |
| 127 |
static ValueType MementoToValue(MementoType content_type) { |
| 128 |
return content_type; |
| 129 |
} |
| 130 |
static StaticSlice Encode(ValueType x); |
| 131 |
static const char* DisplayValue(MementoType content_type); |
| 132 |
}; |
| 133 |
|
| 134 |
// method metadata trait. |
| 135 |
struct HttpMethodMetadata { |
| 136 |
static constexpr bool kRepeatable = false; |
| 137 |
enum ValueType : uint8_t { |
| 138 |
kPost, |
| 139 |
kGet, |
| 140 |
kPut, |
| 141 |
kInvalid, |
| 142 |
}; |
| 143 |
using MementoType = ValueType; |
| 144 |
static absl::string_view key() { return ":method"; } |
| 145 |
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error); |
| 146 |
static ValueType MementoToValue(MementoType content_type) { |
| 147 |
return content_type; |
| 148 |
} |
| 149 |
static StaticSlice Encode(ValueType x); |
| 150 |
static const char* DisplayValue(MementoType content_type); |
| 151 |
}; |
| 152 |
|
| 153 |
// Base type for metadata pertaining to a single compression algorithm |
| 154 |
// (e.g., "grpc-encoding"). |
| 155 |
struct CompressionAlgorithmBasedMetadata { |
| 156 |
using ValueType = grpc_compression_algorithm; |
| 157 |
using MementoType = ValueType; |
| 158 |
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error); |
| 159 |
static ValueType MementoToValue(MementoType x) { return x; } |
| 160 |
static Slice Encode(ValueType x) { |
| 161 |
GPR_ASSERT(x != GRPC_COMPRESS_ALGORITHMS_COUNT); |
| 162 |
return Slice::FromStaticString(CompressionAlgorithmAsString(x)); |
| 163 |
} |
| 164 |
static const char* DisplayValue(MementoType x) { |
| 165 |
if (const char* p = CompressionAlgorithmAsString(x)) { |
| 166 |
return p; |
| 167 |
} else { |
| 168 |
return "<discarded-invalid-value>"; |
| 169 |
} |
| 170 |
} |
| 171 |
}; |
| 172 |
|
| 173 |
// grpc-encoding metadata trait. |
| 174 |
struct GrpcEncodingMetadata : public CompressionAlgorithmBasedMetadata { |
| 175 |
static constexpr bool kRepeatable = false; |
| 176 |
static absl::string_view key() { return "grpc-encoding"; } |
| 177 |
}; |
| 178 |
|
| 179 |
// grpc-internal-encoding-request metadata trait. |
| 180 |
struct GrpcInternalEncodingRequest : public CompressionAlgorithmBasedMetadata { |
| 181 |
static constexpr bool kRepeatable = false; |
| 182 |
static absl::string_view key() { return "grpc-internal-encoding-request"; } |
| 183 |
}; |
| 184 |
|
| 185 |
// grpc-accept-encoding metadata trait. |
| 186 |
struct GrpcAcceptEncodingMetadata { |
| 187 |
static constexpr bool kRepeatable = false; |
| 188 |
static absl::string_view key() { return "grpc-accept-encoding"; } |
| 189 |
using ValueType = CompressionAlgorithmSet; |
| 190 |
using MementoType = ValueType; |
| 191 |
static MementoType ParseMemento(Slice value, MetadataParseErrorFn) { |
| 192 |
return CompressionAlgorithmSet::FromString(value.as_string_view()); |
| 193 |
} |
| 194 |
static ValueType MementoToValue(MementoType x) { return x; } |
| 195 |
static Slice Encode(ValueType x) { return x.ToSlice(); } |
| 196 |
static absl::string_view DisplayValue(MementoType x) { return x.ToString(); } |
| 197 |
}; |
| 198 |
|
| 199 |
struct SimpleSliceBasedMetadata { |
| 200 |
using ValueType = Slice; |
| 201 |
using MementoType = Slice; |
| 202 |
static MementoType ParseMemento(Slice value, MetadataParseErrorFn) { |
| 203 |
return value.TakeOwned(); |
| 204 |
} |
| 205 |
static ValueType MementoToValue(MementoType value) { return value; } |
| 206 |
static Slice Encode(const ValueType& x) { return x.Ref(); } |
| 207 |
static absl::string_view DisplayValue(const MementoType& value) { |
| 208 |
return value.as_string_view(); |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
// user-agent metadata trait. |
| 213 |
struct UserAgentMetadata : public SimpleSliceBasedMetadata { |
| 214 |
static constexpr bool kRepeatable = false; |
| 215 |
static absl::string_view key() { return "user-agent"; } |
| 216 |
}; |
| 217 |
|
| 218 |
// grpc-message metadata trait. |
| 219 |
struct GrpcMessageMetadata : public SimpleSliceBasedMetadata { |
| 220 |
static constexpr bool kRepeatable = false; |
| 221 |
static absl::string_view key() { return "grpc-message"; } |
| 222 |
}; |
| 223 |
|
| 224 |
// host metadata trait. |
| 225 |
struct HostMetadata : public SimpleSliceBasedMetadata { |
| 226 |
static constexpr bool kRepeatable = false; |
| 227 |
static absl::string_view key() { return "host"; } |
| 228 |
}; |
| 229 |
|
| 230 |
// endpoint-load-metrics-bin metadata trait. |
| 231 |
struct EndpointLoadMetricsBinMetadata : public SimpleSliceBasedMetadata { |
| 232 |
static constexpr bool kRepeatable = false; |
| 233 |
static absl::string_view key() { return "endpoint-load-metrics-bin"; } |
| 234 |
}; |
| 235 |
|
| 236 |
// grpc-server-stats-bin metadata trait. |
| 237 |
struct GrpcServerStatsBinMetadata : public SimpleSliceBasedMetadata { |
| 238 |
static constexpr bool kRepeatable = false; |
| 239 |
static absl::string_view key() { return "grpc-server-stats-bin"; } |
| 240 |
}; |
| 241 |
|
| 242 |
// grpc-trace-bin metadata trait. |
| 243 |
struct GrpcTraceBinMetadata : public SimpleSliceBasedMetadata { |
| 244 |
static constexpr bool kRepeatable = false; |
| 245 |
static absl::string_view key() { return "grpc-trace-bin"; } |
| 246 |
}; |
| 247 |
|
| 248 |
// grpc-tags-bin metadata trait. |
| 249 |
struct GrpcTagsBinMetadata : public SimpleSliceBasedMetadata { |
| 250 |
static constexpr bool kRepeatable = false; |
| 251 |
static absl::string_view key() { return "grpc-tags-bin"; } |
| 252 |
}; |
| 253 |
|
| 254 |
// :authority metadata trait. |
| 255 |
struct HttpAuthorityMetadata : public SimpleSliceBasedMetadata { |
| 256 |
static constexpr bool kRepeatable = false; |
| 257 |
static absl::string_view key() { return ":authority"; } |
| 258 |
}; |
| 259 |
|
| 260 |
// :path metadata trait. |
| 261 |
struct HttpPathMetadata : public SimpleSliceBasedMetadata { |
| 262 |
static constexpr bool kRepeatable = false; |
| 263 |
static absl::string_view key() { return ":path"; } |
| 264 |
}; |
| 265 |
|
| 266 |
// We separate SimpleIntBasedMetadata into two pieces: one that does not |
| 267 |
// depend on the invalid value, and one that does. This allows the compiler to |
| 268 |
// easily see the functions that are shared, and helps reduce code bloat here. |
| 269 |
template <typename Int> |
| 270 |
struct SimpleIntBasedMetadataBase { |
| 271 |
using ValueType = Int; |
| 272 |
using MementoType = Int; |
| 273 |
static ValueType MementoToValue(MementoType value) { return value; } |
| 274 |
static Slice Encode(ValueType x) { return Slice::FromInt64(x); } |
| 275 |
static Int DisplayValue(MementoType x) { return x; } |
| 276 |
}; |
| 277 |
|
| 278 |
template <typename Int, Int kInvalidValue> |
| 279 |
struct SimpleIntBasedMetadata : public SimpleIntBasedMetadataBase<Int> { |
| 280 |
static constexpr Int invalid_value() { return kInvalidValue; } |
| 281 |
static Int ParseMemento(Slice value, MetadataParseErrorFn on_error) { |
| 282 |
Int out; |
| 283 |
if (!absl::SimpleAtoi(value.as_string_view(), &out)) { |
| 284 |
on_error("not an integer", value); |
| 285 |
out = kInvalidValue; |
| 286 |
} |
| 287 |
return out; |
| 288 |
} |
| 289 |
}; |
| 290 |
|
| 291 |
// grpc-status metadata trait. |
| 292 |
struct GrpcStatusMetadata |
| 293 |
: public SimpleIntBasedMetadata<grpc_status_code, GRPC_STATUS_UNKNOWN> { |
| 294 |
static constexpr bool kRepeatable = false; |
| 295 |
static absl::string_view key() { return "grpc-status"; } |
| 296 |
}; |
| 297 |
|
| 298 |
// grpc-previous-rpc-attempts metadata trait. |
| 299 |
struct GrpcPreviousRpcAttemptsMetadata |
| 300 |
: public SimpleIntBasedMetadata<uint32_t, 0> { |
| 301 |
static constexpr bool kRepeatable = false; |
| 302 |
static absl::string_view key() { return "grpc-previous-rpc-attempts"; } |
| 303 |
}; |
| 304 |
|
| 305 |
// grpc-retry-pushback-ms metadata trait. |
| 306 |
struct GrpcRetryPushbackMsMetadata { |
| 307 |
static constexpr bool kRepeatable = false; |
| 308 |
static absl::string_view key() { return "grpc-retry-pushback-ms"; } |
| 309 |
using ValueType = Duration; |
| 310 |
using MementoType = Duration; |
| 311 |
static ValueType MementoToValue(MementoType x) { return x; } |
| 312 |
static Slice Encode(Duration x) { return Slice::FromInt64(x.millis()); } |
| 313 |
static int64_t DisplayValue(Duration x) { return x.millis(); } |
| 314 |
static Duration ParseMemento(Slice value, MetadataParseErrorFn on_error); |
| 315 |
}; |
| 316 |
|
| 317 |
// :status metadata trait. |
| 318 |
// TODO(ctiller): consider moving to uint16_t |
| 319 |
struct HttpStatusMetadata : public SimpleIntBasedMetadata<uint32_t, 0> { |
| 320 |
static constexpr bool kRepeatable = false; |
| 321 |
static absl::string_view key() { return ":status"; } |
| 322 |
}; |
| 323 |
|
| 324 |
// "secret" metadata trait used to pass load balancing token between filters. |
| 325 |
// This should not be exposed outside of gRPC core. |
| 326 |
class GrpcLbClientStats; |
| 327 |
|
| 328 |
struct GrpcLbClientStatsMetadata { |
| 329 |
static constexpr bool kRepeatable = false; |
| 330 |
static absl::string_view key() { return "grpclb_client_stats"; } |
| 331 |
using ValueType = GrpcLbClientStats*; |
| 332 |
using MementoType = ValueType; |
| 333 |
static ValueType MementoToValue(MementoType value) { return value; } |
| 334 |
static Slice Encode(ValueType) { abort(); } |
| 335 |
static const char* DisplayValue(MementoType) { return "<internal-lb-stats>"; } |
| 336 |
static MementoType ParseMemento(Slice, MetadataParseErrorFn) { |
| 337 |
return nullptr; |
| 338 |
} |
| 339 |
}; |
| 340 |
|
| 341 |
// lb-token metadata |
| 342 |
struct LbTokenMetadata : public SimpleSliceBasedMetadata { |
| 343 |
static constexpr bool kRepeatable = false; |
| 344 |
static absl::string_view key() { return "lb-token"; } |
| 345 |
}; |
| 346 |
|
| 347 |
// lb-cost-bin metadata |
| 348 |
struct LbCostBinMetadata { |
| 349 |
static constexpr bool kRepeatable = true; |
| 350 |
static absl::string_view key() { return "lb-cost-bin"; } |
| 351 |
struct ValueType { |
| 352 |
double cost; |
| 353 |
std::string name; |
| 354 |
}; |
| 355 |
using MementoType = ValueType; |
| 356 |
static ValueType MementoToValue(MementoType value) { return value; } |
| 357 |
static Slice Encode(const ValueType& x); |
| 358 |
static std::string DisplayValue(MementoType x); |
| 359 |
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error); |
| 360 |
}; |
| 361 |
|
| 362 |
// Annotation added by a transport to note whether a failed request was never |
| 363 |
// placed on the wire, or never seen by a server. |
| 364 |
struct GrpcStreamNetworkState { |
| 365 |
static absl::string_view DebugKey() { return "GrpcStreamNetworkState"; } |
| 366 |
static constexpr bool kRepeatable = false; |
| 367 |
enum ValueType : uint8_t { |
| 368 |
kNotSentOnWire, |
| 369 |
kNotSeenByServer, |
| 370 |
}; |
| 371 |
static std::string DisplayValue(ValueType x); |
| 372 |
}; |
| 373 |
|
| 374 |
// Annotation added by a server transport to note the peer making a request. |
| 375 |
struct PeerString { |
| 376 |
static absl::string_view DebugKey() { return "PeerString"; } |
| 377 |
static constexpr bool kRepeatable = false; |
| 378 |
using ValueType = absl::string_view; |
| 379 |
static std::string DisplayValue(ValueType x); |
| 380 |
}; |
| 381 |
|
| 382 |
// Annotation added by various systems to describe the reason for a failure. |
| 383 |
struct GrpcStatusContext { |
| 384 |
static absl::string_view DebugKey() { return "GrpcStatusContext"; } |
| 385 |
static constexpr bool kRepeatable = true; |
| 386 |
using ValueType = std::string; |
| 387 |
static const std::string& DisplayValue(const std::string& x); |
| 388 |
}; |
| 389 |
|
| 390 |
// Annotation added by a transport to note that the status came from the wire. |
| 391 |
struct GrpcStatusFromWire { |
| 392 |
static absl::string_view DebugKey() { return "GrpcStatusFromWire"; } |
| 393 |
static constexpr bool kRepeatable = false; |
| 394 |
using ValueType = bool; |
| 395 |
static absl::string_view DisplayValue(bool x) { return x ? "true" : "false"; } |
| 396 |
}; |
| 397 |
|
| 398 |
// Annotation added by client surface code to denote wait-for-ready state |
| 399 |
struct WaitForReady { |
| 400 |
struct ValueType { |
| 401 |
bool value = false; |
| 402 |
bool explicitly_set = false; |
| 403 |
}; |
| 404 |
static absl::string_view DebugKey() { return "WaitForReady"; } |
| 405 |
static constexpr bool kRepeatable = false; |
| 406 |
static std::string DisplayValue(ValueType x); |
| 407 |
}; |
| 408 |
|
| 409 |
namespace metadata_detail { |
| 410 |
|
| 411 |
// Build a key/value formatted debug string. |
| 412 |
// Output looks like 'key1: value1, key2: value2' |
| 413 |
// The string is expected to be readable, but not necessarily parsable. |
| 414 |
class DebugStringBuilder { |
| 415 |
public: |
| 416 |
// Add one key/value pair to the output. |
| 417 |
void Add(absl::string_view key, absl::string_view value); |
| 418 |
|
| 419 |
// Finalize the output and return the string. |
| 420 |
// Subsequent Add calls are UB. |
| 421 |
std::string TakeOutput() { return std::move(out_); } |
| 422 |
|
| 423 |
private: |
| 424 |
std::string out_; |
| 425 |
}; |
| 426 |
|
| 427 |
// IsEncodable: Given a trait, determine if that trait is encodable, or is |
| 428 |
// just a value attached to a MetadataMap. We use the presence of the key() |
| 429 |
// static method to determine if a trait is encodable or not - encodable |
| 430 |
// traits have string names, and non-encodable traits do not. |
| 431 |
template <typename Trait, typename Ignored = void> |
| 432 |
struct IsEncodableTrait { |
| 433 |
static const bool value = false; |
| 434 |
}; |
| 435 |
|
| 436 |
template <typename Trait> |
| 437 |
struct IsEncodableTrait<Trait, absl::void_t<decltype(Trait::key())>> { |
| 438 |
static const bool value = true; |
| 439 |
}; |
| 440 |
|
| 441 |
// Helper type - maps a string name to a trait. |
| 442 |
template <typename MustBeVoid, typename... Traits> |
| 443 |
struct NameLookup; |
| 444 |
|
| 445 |
template <typename Trait, typename... Traits> |
| 446 |
struct NameLookup<absl::enable_if_t<IsEncodableTrait<Trait>::value, void>, |
| 447 |
Trait, Traits...> { |
| 448 |
// Call op->Found(Trait()) if op->name == Trait::key() for some Trait in |
| 449 |
// Traits. If not found, call op->NotFound(). |
| 450 |
template <typename Op> |
| 451 |
static auto Lookup(absl::string_view key, Op* op) |
| 452 |
-> decltype(op->Found(Trait())) { |
| 453 |
if (key == Trait::key()) { |
| 454 |
return op->Found(Trait()); |
| 455 |
} |
| 456 |
return NameLookup<void, Traits...>::Lookup(key, op); |
| 457 |
} |
| 458 |
}; |
| 459 |
|
| 460 |
template <typename Trait, typename... Traits> |
| 461 |
struct NameLookup<absl::enable_if_t<!IsEncodableTrait<Trait>::value, void>, |
| 462 |
Trait, Traits...> { |
| 463 |
template <typename Op> |
| 464 |
static auto Lookup(absl::string_view key, Op* op) |
| 465 |
-> decltype(NameLookup<void, Traits...>::Lookup(key, op)) { |
| 466 |
return NameLookup<void, Traits...>::Lookup(key, op); |
| 467 |
} |
| 468 |
}; |
| 469 |
|
| 470 |
template <> |
| 471 |
struct NameLookup<void> { |
| 472 |
template <typename Op> |
| 473 |
static auto Lookup(absl::string_view key, Op* op) |
| 474 |
-> decltype(op->NotFound(key)) { |
| 475 |
return op->NotFound(key); |
| 476 |
} |
| 477 |
}; |
| 478 |
|
| 479 |
// Helper to take a slice to a memento to a value. |
| 480 |
// By splitting this part out we can scale code size as the number of |
| 481 |
// (memento, value) types, rather than as the number of traits. |
| 482 |
template <typename ParseMementoFn, typename MementoToValueFn> |
| 483 |
struct ParseValue { |
| 484 |
template <ParseMementoFn parse_memento, MementoToValueFn memento_to_value> |
| 485 |
static GPR_ATTRIBUTE_NOINLINE auto Parse(Slice* value, |
| 486 |
MetadataParseErrorFn on_error) |
| 487 |
-> decltype(memento_to_value(parse_memento(std::move(*value), |
| 488 |
on_error))) { |
| 489 |
return memento_to_value(parse_memento(std::move(*value), on_error)); |
| 490 |
} |
| 491 |
}; |
| 492 |
|
| 493 |
// This is an "Op" type for NameLookup. |
| 494 |
// Used for MetadataMap::Parse, its Found/NotFound methods turn a slice into a |
| 495 |
// ParsedMetadata object. |
| 496 |
template <typename Container> |
| 497 |
class ParseHelper { |
| 498 |
public: |
| 499 |
ParseHelper(Slice value, MetadataParseErrorFn on_error, size_t transport_size) |
| 500 |
: value_(std::move(value)), |
| 501 |
on_error_(on_error), |
| 502 |
transport_size_(transport_size) {} |
| 503 |
|
| 504 |
template <typename Trait> |
| 505 |
GPR_ATTRIBUTE_NOINLINE ParsedMetadata<Container> Found(Trait trait) { |
| 506 |
return ParsedMetadata<Container>( |
| 507 |
trait, |
| 508 |
ParseValueToMemento<typename Trait::MementoType, Trait::ParseMemento>(), |
| 509 |
static_cast<uint32_t>(transport_size_)); |
| 510 |
} |
| 511 |
|
| 512 |
GPR_ATTRIBUTE_NOINLINE ParsedMetadata<Container> NotFound( |
| 513 |
absl::string_view key) { |
| 514 |
return ParsedMetadata<Container>(Slice::FromCopiedString(key), |
| 515 |
std::move(value_)); |
| 516 |
} |
| 517 |
|
| 518 |
private: |
| 519 |
template <typename T, T (*parse_memento)(Slice, MetadataParseErrorFn)> |
| 520 |
GPR_ATTRIBUTE_NOINLINE T ParseValueToMemento() { |
| 521 |
return parse_memento(std::move(value_), on_error_); |
| 522 |
} |
| 523 |
|
| 524 |
Slice value_; |
| 525 |
MetadataParseErrorFn on_error_; |
| 526 |
const size_t transport_size_; |
| 527 |
}; |
| 528 |
|
| 529 |
// This is an "Op" type for NameLookup. |
| 530 |
// Used for MetadataMap::Append, its Found/NotFound methods turn a slice into |
| 531 |
// a value and add it to a container. |
| 532 |
template <typename Container> |
| 533 |
class AppendHelper { |
| 534 |
public: |
| 535 |
AppendHelper(Container* container, Slice value, MetadataParseErrorFn on_error) |
| 536 |
: container_(container), value_(std::move(value)), on_error_(on_error) {} |
| 537 |
|
| 538 |
template <typename Trait> |
| 539 |
GPR_ATTRIBUTE_NOINLINE void Found(Trait trait) { |
| 540 |
container_->Set( |
| 541 |
trait, ParseValue<decltype(Trait::ParseMemento), |
| 542 |
decltype(Trait::MementoToValue)>:: |
| 543 |
template Parse<Trait::ParseMemento, Trait::MementoToValue>( |
| 544 |
&value_, on_error_)); |
| 545 |
} |
| 546 |
|
| 547 |
GPR_ATTRIBUTE_NOINLINE void NotFound(absl::string_view key) { |
| 548 |
container_->unknown_.Append(key, std::move(value_)); |
| 549 |
} |
| 550 |
|
| 551 |
private: |
| 552 |
Container* const container_; |
| 553 |
Slice value_; |
| 554 |
MetadataParseErrorFn on_error_; |
| 555 |
}; |
| 556 |
|
| 557 |
// This is an "Op" type for NameLookup. |
| 558 |
// Used for MetadataMap::Remove, its Found/NotFound methods remove a key from |
| 559 |
// the container. |
| 560 |
template <typename Container> |
| 561 |
class RemoveHelper { |
| 562 |
public: |
| 563 |
explicit RemoveHelper(Container* container) : container_(container) {} |
| 564 |
|
| 565 |
template <typename Trait> |
| 566 |
GPR_ATTRIBUTE_NOINLINE void Found(Trait trait) { |
| 567 |
container_->Remove(trait); |
| 568 |
} |
| 569 |
|
| 570 |
GPR_ATTRIBUTE_NOINLINE void NotFound(absl::string_view key) { |
| 571 |
container_->unknown_.Remove(key); |
| 572 |
} |
| 573 |
|
| 574 |
private: |
| 575 |
Container* const container_; |
| 576 |
}; |
| 577 |
|
| 578 |
// This is an "Op" type for NameLookup. |
| 579 |
// Used for MetadataMap::GetStringValue, its Found/NotFound methods generated |
| 580 |
// a string value from the container. |
| 581 |
template <typename Container> |
| 582 |
class GetStringValueHelper { |
| 583 |
public: |
| 584 |
explicit GetStringValueHelper(const Container* container, |
| 585 |
std::string* backing) |
| 586 |
: container_(container), backing_(backing) {} |
| 587 |
|
| 588 |
template <typename Trait> |
| 589 |
GPR_ATTRIBUTE_NOINLINE absl::enable_if_t< |
| 590 |
Trait::kRepeatable == false && |
| 591 |
std::is_same<Slice, typename Trait::ValueType>::value, |
| 592 |
absl::optional<absl::string_view>> |
| 593 |
Found(Trait) { |
| 594 |
const auto* value = container_->get_pointer(Trait()); |
| 595 |
if (value == nullptr) return absl::nullopt; |
| 596 |
return value->as_string_view(); |
| 597 |
} |
| 598 |
|
| 599 |
template <typename Trait> |
| 600 |
GPR_ATTRIBUTE_NOINLINE absl::enable_if_t< |
| 601 |
Trait::kRepeatable == true && |
| 602 |
!std::is_same<Slice, typename Trait::ValueType>::value, |
| 603 |
absl::optional<absl::string_view>> |
| 604 |
Found(Trait) { |
| 605 |
const auto* value = container_->get_pointer(Trait()); |
| 606 |
if (value == nullptr) return absl::nullopt; |
| 607 |
backing_->clear(); |
| 608 |
for (const auto& v : *value) { |
| 609 |
if (!backing_->empty()) backing_->push_back(','); |
| 610 |
auto new_segment = Trait::Encode(v); |
| 611 |
backing_->append(new_segment.begin(), new_segment.end()); |
| 612 |
} |
| 613 |
return *backing_; |
| 614 |
} |
| 615 |
|
| 616 |
template <typename Trait> |
| 617 |
GPR_ATTRIBUTE_NOINLINE absl::enable_if_t< |
| 618 |
Trait::kRepeatable == false && |
| 619 |
!std::is_same<Slice, typename Trait::ValueType>::value, |
| 620 |
absl::optional<absl::string_view>> |
| 621 |
Found(Trait) { |
| 622 |
const auto* value = container_->get_pointer(Trait()); |
| 623 |
if (value == nullptr) return absl::nullopt; |
| 624 |
*backing_ = std::string(Trait::Encode(*value).as_string_view()); |
| 625 |
return *backing_; |
| 626 |
} |
| 627 |
|
| 628 |
GPR_ATTRIBUTE_NOINLINE absl::optional<absl::string_view> NotFound( |
| 629 |
absl::string_view key) { |
| 630 |
return container_->unknown_.GetStringValue(key, backing_); |
| 631 |
} |
| 632 |
|
| 633 |
private: |
| 634 |
const Container* const container_; |
| 635 |
std::string* backing_; |
| 636 |
}; |
| 637 |
|
| 638 |
// Sink for key value logs |
| 639 |
using LogFn = absl::FunctionRef<void(absl::string_view, absl::string_view)>; |
| 640 |
|
| 641 |
template <typename T> |
| 642 |
struct AdaptDisplayValueToLog { |
| 643 |
static std::string ToString(const T& value) { return std::to_string(value); } |
| 644 |
}; |
| 645 |
|
| 646 |
template <> |
| 647 |
struct AdaptDisplayValueToLog<std::string> { |
| 648 |
static std::string ToString(const std::string& value) { return value; } |
| 649 |
}; |
| 650 |
|
| 651 |
template <> |
| 652 |
struct AdaptDisplayValueToLog<const std::string&> { |
| 653 |
static std::string ToString(const std::string& value) { return value; } |
| 654 |
}; |
| 655 |
|
| 656 |
template <> |
| 657 |
struct AdaptDisplayValueToLog<absl::string_view> { |
| 658 |
static std::string ToString(absl::string_view value) { |
| 659 |
return std::string(value); |
| 660 |
} |
| 661 |
}; |
| 662 |
|
| 663 |
template <> |
| 664 |
struct AdaptDisplayValueToLog<Slice> { |
| 665 |
static std::string ToString(Slice value) { |
| 666 |
return std::string(value.as_string_view()); |
| 667 |
} |
| 668 |
}; |
| 669 |
|
| 670 |
template <> |
| 671 |
struct AdaptDisplayValueToLog<StaticSlice> { |
| 672 |
static absl::string_view ToString(StaticSlice value) { |
| 673 |
return value.as_string_view(); |
| 674 |
} |
| 675 |
}; |
| 676 |
|
| 677 |
template <typename T, typename U, typename V> |
| 678 |
GPR_ATTRIBUTE_NOINLINE void LogKeyValueTo(absl::string_view key, const T& value, |
| 679 |
V (*display_value)(U), LogFn log_fn) { |
| 680 |
log_fn(key, AdaptDisplayValueToLog<V>::ToString(display_value(value))); |
| 681 |
} |
| 682 |
|
| 683 |
// Generate a strong type for metadata values per trait. |
| 684 |
template <typename Which, typename Ignored = void> |
| 685 |
struct Value; |
| 686 |
|
| 687 |
template <typename Which> |
| 688 |
struct Value<Which, absl::enable_if_t<Which::kRepeatable == false && |
| 689 |
IsEncodableTrait<Which>::value, |
| 690 |
void>> { |
| 691 |
Value() = default; |
| 692 |
explicit Value(const typename Which::ValueType& value) : value(value) {} |
| 693 |
explicit Value(typename Which::ValueType&& value) |
| 694 |
: value(std::forward<typename Which::ValueType>(value)) {} |
| 695 |
Value(const Value&) = delete; |
| 696 |
Value& operator=(const Value&) = delete; |
| 697 |
Value(Value&&) noexcept = default; |
| 698 |
Value& operator=(Value&& other) noexcept { |
| 699 |
value = std::move(other.value); |
| 700 |
return *this; |
| 701 |
} |
| 702 |
template <typename Encoder> |
| 703 |
void EncodeTo(Encoder* encoder) const { |
| 704 |
encoder->Encode(Which(), value); |
| 705 |
} |
| 706 |
template <typename Encoder> |
| 707 |
void VisitWith(Encoder* encoder) const { |
| 708 |
return EncodeTo(encoder); |
| 709 |
} |
| 710 |
void LogTo(LogFn log_fn) const { |
| 711 |
LogKeyValueTo(Which::key(), value, Which::Encode, log_fn); |
| 712 |
} |
| 713 |
using StorageType = typename Which::ValueType; |
| 714 |
GPR_NO_UNIQUE_ADDRESS StorageType value; |
| 715 |
}; |
| 716 |
|
| 717 |
template <typename Which> |
| 718 |
struct Value<Which, absl::enable_if_t<Which::kRepeatable == false && |
| 719 |
!IsEncodableTrait<Which>::value, |
| 720 |
void>> { |
| 721 |
Value() = default; |
| 722 |
explicit Value(const typename Which::ValueType& value) : value(value) {} |
| 723 |
explicit Value(typename Which::ValueType&& value) |
| 724 |
: value(std::forward<typename Which::ValueType>(value)) {} |
| 725 |
Value(const Value&) = delete; |
| 726 |
Value& operator=(const Value&) = delete; |
| 727 |
Value(Value&&) noexcept = default; |
| 728 |
Value& operator=(Value&& other) noexcept { |
| 729 |
value = std::move(other.value); |
| 730 |
return *this; |
| 731 |
} |
| 732 |
template <typename Encoder> |
| 733 |
void EncodeTo(Encoder*) const {} |
| 734 |
template <typename Encoder> |
| 735 |
void VisitWith(Encoder* encoder) const { |
| 736 |
encoder->Encode(Which(), value); |
| 737 |
} |
| 738 |
void LogTo(LogFn log_fn) const { |
| 739 |
LogKeyValueTo(Which::DebugKey(), value, Which::DisplayValue, log_fn); |
| 740 |
} |
| 741 |
using StorageType = typename Which::ValueType; |
| 742 |
GPR_NO_UNIQUE_ADDRESS StorageType value; |
| 743 |
}; |
| 744 |
|
| 745 |
template <typename Which> |
| 746 |
struct Value<Which, absl::enable_if_t<Which::kRepeatable == true && |
| 747 |
IsEncodableTrait<Which>::value, |
| 748 |
void>> { |
| 749 |
Value() = default; |
| 750 |
explicit Value(const typename Which::ValueType& value) { |
| 751 |
this->value.push_back(value); |
| 752 |
} |
| 753 |
explicit Value(typename Which::ValueType&& value) { |
| 754 |
this->value.emplace_back(std::forward<typename Which::ValueType>(value)); |
| 755 |
} |
| 756 |
Value(const Value&) = delete; |
| 757 |
Value& operator=(const Value&) = delete; |
| 758 |
Value(Value&& other) noexcept : value(std::move(other.value)) {} |
| 759 |
Value& operator=(Value&& other) noexcept { |
| 760 |
value = std::move(other.value); |
| 761 |
return *this; |
| 762 |
} |
| 763 |
template <typename Encoder> |
| 764 |
void EncodeTo(Encoder* encoder) const { |
| 765 |
for (const auto& v : value) { |
| 766 |
encoder->Encode(Which(), v); |
| 767 |
} |
| 768 |
} |
| 769 |
template <typename Encoder> |
| 770 |
void VisitWith(Encoder* encoder) const { |
| 771 |
return EncodeTo(encoder); |
| 772 |
} |
| 773 |
void LogTo(LogFn log_fn) const { |
| 774 |
for (const auto& v : value) { |
| 775 |
LogKeyValueTo(Which::key(), v, Which::Encode, log_fn); |
| 776 |
} |
| 777 |
} |
| 778 |
using StorageType = absl::InlinedVector<typename Which::ValueType, 1>; |
| 779 |
StorageType value; |
| 780 |
}; |
| 781 |
|
| 782 |
template <typename Which> |
| 783 |
struct Value<Which, absl::enable_if_t<Which::kRepeatable == true && |
| 784 |
!IsEncodableTrait<Which>::value, |
| 785 |
void>> { |
| 786 |
Value() = default; |
| 787 |
explicit Value(const typename Which::ValueType& value) { |
| 788 |
this->value.push_back(value); |
| 789 |
} |
| 790 |
explicit Value(typename Which::ValueType&& value) { |
| 791 |
this->value.emplace_back(std::forward<typename Which::ValueType>(value)); |
| 792 |
} |
| 793 |
Value(const Value&) = delete; |
| 794 |
Value& operator=(const Value&) = delete; |
| 795 |
Value(Value&& other) noexcept : value(std::move(other.value)) {} |
| 796 |
Value& operator=(Value&& other) noexcept { |
| 797 |
value = std::move(other.value); |
| 798 |
return *this; |
| 799 |
} |
| 800 |
template <typename Encoder> |
| 801 |
void EncodeTo(Encoder*) const {} |
| 802 |
template <typename Encoder> |
| 803 |
void VisitWith(Encoder* encoder) const { |
| 804 |
for (const auto& v : value) { |
| 805 |
encoder->Encode(Which(), v); |
| 806 |
} |
| 807 |
} |
| 808 |
void LogTo(LogFn log_fn) const { |
| 809 |
for (const auto& v : value) { |
| 810 |
LogKeyValueTo(Which::DebugKey(), v, Which::DisplayValue, log_fn); |
| 811 |
} |
| 812 |
} |
| 813 |
using StorageType = absl::InlinedVector<typename Which::ValueType, 1>; |
| 814 |
StorageType value; |
| 815 |
}; |
| 816 |
|
| 817 |
// Encoder to copy some metadata |
| 818 |
template <typename Output> |
| 819 |
class CopySink { |
| 820 |
public: |
| 821 |
explicit CopySink(Output* dst) : dst_(dst) {} |
| 822 |
|
| 823 |
template <class T, class V> |
| 824 |
void Encode(T trait, V value) { |
| 825 |
dst_->Set(trait, value); |
| 826 |
} |
| 827 |
|
| 828 |
template <class T> |
| 829 |
void Encode(T trait, const Slice& value) { |
| 830 |
dst_->Set(trait, std::move(value.AsOwned())); |
| 831 |
} |
| 832 |
|
| 833 |
void Encode(const Slice& key, const Slice& value) { |
| 834 |
dst_->unknown_.Append(key.as_string_view(), value.Ref()); |
| 835 |
} |
| 836 |
|
| 837 |
private: |
| 838 |
Output* dst_; |
| 839 |
}; |
| 840 |
|
| 841 |
// Callable for the ForEach in Encode() -- for each value, call the |
| 842 |
// appropriate encoder method. |
| 843 |
template <typename Encoder> |
| 844 |
struct EncodeWrapper { |
| 845 |
Encoder* encoder; |
| 846 |
template <typename Which> |
| 847 |
void operator()(const Value<Which>& which) { |
| 848 |
which.EncodeTo(encoder); |
| 849 |
} |
| 850 |
}; |
| 851 |
|
| 852 |
// Callable for the table ForEach in ForEach() -- for each value, call the |
| 853 |
// appropriate visitor method. |
| 854 |
template <typename Encoder> |
| 855 |
struct ForEachWrapper { |
| 856 |
Encoder* encoder; |
| 857 |
template <typename Which> |
| 858 |
void operator()(const Value<Which>& which) { |
| 859 |
which.VisitWith(encoder); |
| 860 |
} |
| 861 |
}; |
| 862 |
|
| 863 |
// Callable for the ForEach in Log() |
| 864 |
struct LogWrapper { |
| 865 |
LogFn log_fn; |
| 866 |
template <typename Which> |
| 867 |
void operator()(const Value<Which>& which) { |
| 868 |
which.LogTo(log_fn); |
| 869 |
} |
| 870 |
}; |
| 871 |
|
| 872 |
// Encoder to compute TransportSize |
| 873 |
class TransportSizeEncoder { |
| 874 |
public: |
| 875 |
void Encode(const Slice& key, const Slice& value) { |
| 876 |
size_ += key.length() + value.length() + 32; |
| 877 |
} |
| 878 |
|
| 879 |
template <typename Which> |
| 880 |
void Encode(Which, const typename Which::ValueType& value) { |
| 881 |
Add(Which(), value); |
| 882 |
} |
| 883 |
|
| 884 |
void Encode(ContentTypeMetadata, |
| 885 |
const typename ContentTypeMetadata::ValueType& value) { |
| 886 |
if (value == ContentTypeMetadata::kInvalid) return; |
| 887 |
Add(ContentTypeMetadata(), value); |
| 888 |
} |
| 889 |
|
| 890 |
size_t size() const { return size_; } |
| 891 |
|
| 892 |
private: |
| 893 |
template <typename Which> |
| 894 |
void Add(Which, const typename Which::ValueType& value) { |
| 895 |
size_ += Which::key().length() + Which::Encode(value).length() + 32; |
| 896 |
} |
| 897 |
|
| 898 |
uint32_t size_ = 0; |
| 899 |
}; |
| 900 |
|
| 901 |
// Handle unknown (non-trait-based) fields in the metadata map. |
| 902 |
class UnknownMap { |
| 903 |
public: |
| 904 |
explicit UnknownMap(Arena* arena) : unknown_(arena) {} |
| 905 |
|
| 906 |
using BackingType = ChunkedVector<std::pair<Slice, Slice>, 10>; |
| 907 |
|
| 908 |
void Append(absl::string_view key, Slice value); |
| 909 |
void Remove(absl::string_view key); |
| 910 |
absl::optional<absl::string_view> GetStringValue(absl::string_view key, |
| 911 |
std::string* backing) const; |
| 912 |
|
| 913 |
BackingType::ConstForwardIterator begin() const { return unknown_.cbegin(); } |
| 914 |
BackingType::ConstForwardIterator end() const { return unknown_.cend(); } |
| 915 |
|
| 916 |
bool empty() const { return unknown_.empty(); } |
| 917 |
size_t size() const { return unknown_.size(); } |
| 918 |
void Clear() { unknown_.Clear(); } |
| 919 |
Arena* arena() const { return unknown_.arena(); } |
| 920 |
|
| 921 |
private: |
| 922 |
// Backing store for added metadata. |
| 923 |
ChunkedVector<std::pair<Slice, Slice>, 10> unknown_; |
| 924 |
}; |
| 925 |
|
| 926 |
} // namespace metadata_detail |
| 927 |
|
| 928 |
// Helper function for encoders |
| 929 |
// Given a metadata trait, convert the value to a slice. |
| 930 |
template <typename Which> |
| 931 |
absl::enable_if_t<std::is_same<typename Which::ValueType, Slice>::value, |
| 932 |
const Slice&> |
| 933 |
MetadataValueAsSlice(const Slice& slice) { |
| 934 |
return slice; |
| 935 |
} |
| 936 |
|
| 937 |
template <typename Which> |
| 938 |
absl::enable_if_t<!std::is_same<typename Which::ValueType, Slice>::value, Slice> |
| 939 |
MetadataValueAsSlice(typename Which::ValueType value) { |
| 940 |
return Slice(Which::Encode(value)); |
| 941 |
} |
| 942 |
|
| 943 |
// MetadataMap encodes the mapping of metadata keys to metadata values. |
| 944 |
// |
| 945 |
// MetadataMap takes a derived class and list of traits. Each of these trait |
| 946 |
// objects defines one metadata field that is used by core, and so should have |
| 947 |
// more specialized handling than just using the generic APIs. |
| 948 |
// |
| 949 |
// MetadataMap is the backing type for some derived type via the curiously |
| 950 |
// recursive template pattern. This is because many types consumed by |
| 951 |
// MetadataMap require the container type to operate on, and many of those |
| 952 |
// types are instantiated one per trait. A naive implementation without the |
| 953 |
// Derived type would, for traits A,B,C, then instantiate for some |
| 954 |
// T<Container, Trait>: |
| 955 |
// - T<MetadataMap<A,B,C>, A>, |
| 956 |
// - T<MetadataMap<A,B,C>, B>, |
| 957 |
// - T<MetadataMap<A,B,C>, C>. |
| 958 |
// Since these types ultimately need to be recorded in the .dynstr segment |
| 959 |
// for dynamic linkers (if gRPC is linked as a static library) this would |
| 960 |
// create O(N^2) bytes of symbols even in stripped libraries. To avoid this |
| 961 |
// we use the derived type (e.g. grpc_metadata_batch right now) to capture |
| 962 |
// the container type, and we would write T<grpc_metadata_batch, A>, etc... |
| 963 |
// Note that now the container type uses a number of bytes that is independent |
| 964 |
// of the number of traits, and so we return to a linear symbol table growth |
| 965 |
// function. |
| 966 |
// |
| 967 |
// Each trait object has one of two possible signatures, depending on whether |
| 968 |
// that traits field is encodable or not. |
| 969 |
// Non-encodable traits are carried in a MetadataMap, but are never passed to |
| 970 |
// the application nor serialized to wire. |
| 971 |
// |
| 972 |
// Encodable traits have the following signature: |
| 973 |
// // Traits for the "grpc-xyz" metadata field: |
| 974 |
// struct GrpcXyzMetadata { |
| 975 |
// // Can this metadata field be repeated? |
| 976 |
// static constexpr bool kRepeatable = ...; |
| 977 |
// // The type that's stored on MetadataBatch |
| 978 |
// using ValueType = ...; |
| 979 |
// // The type that's stored in compression/decompression tables |
| 980 |
// using MementoType = ...; |
| 981 |
// // The string key for this metadata type (for transports that require it) |
| 982 |
// static absl::string_view key() { return "grpc-xyz"; } |
| 983 |
// // Parse a memento from a slice |
| 984 |
// // Takes ownership of value |
| 985 |
// // Calls fn in the case of an error that should be reported to the user |
| 986 |
// static MementoType ParseMemento(Slice value, MementoParseErrorFn fn) { |
| 987 |
// ... |
| 988 |
// } |
| 989 |
// // Convert a memento to a value |
| 990 |
// static ValueType MementoToValue(MementoType memento) { ... } |
| 991 |
// // Convert a value to its canonical text wire format (the format that |
| 992 |
// // ParseMemento will accept!) |
| 993 |
// static Slice Encode(const ValueType& value); |
| 994 |
// // Convert a value to something that can be passed to StrCat and |
| 995 |
// displayed |
| 996 |
// // for debugging |
| 997 |
// static SomeStrCatableType DisplayValue(MementoType value) { ... } |
| 998 |
// }; |
| 999 |
// |
| 1000 |
// Non-encodable traits are determined by missing the key() method, and have |
| 1001 |
// the following signature (and by convention omit the Metadata part of the |
| 1002 |
// type name): |
| 1003 |
// // Traits for the GrpcXyz field: |
| 1004 |
// struct GrpcXyz { |
| 1005 |
// // The string key that should be used for debug dumps - should not be a |
| 1006 |
// // valid http2 key (ie all lower case) |
| 1007 |
// static absl::string_view DebugKey() { return "GRPC_XYZ"; } |
| 1008 |
// // Can this metadata field be repeated? |
| 1009 |
// static constexpr bool kRepeatable = ...; |
| 1010 |
// // The type that's stored on MetadataBatch |
| 1011 |
// using ValueType = ...; |
| 1012 |
// // Convert a value to something that can be passed to StrCat and |
| 1013 |
// displayed |
| 1014 |
// // for debugging |
| 1015 |
// static SomeStrCatableType DisplayValue(ValueType value) { ... } |
| 1016 |
// }; |
| 1017 |
// |
| 1018 |
// About parsing and mementos: |
| 1019 |
// |
| 1020 |
// Many gRPC transports exchange metadata as key/value strings, but also allow |
| 1021 |
// for a more efficient representation as a single integer. We can use this |
| 1022 |
// integer representation to avoid reparsing too, by storing the parsed value |
| 1023 |
// in the compression table. This is what mementos are used for. |
| 1024 |
// |
| 1025 |
// A trait offers the capability to turn a slice into a memento via |
| 1026 |
// ParseMemento. This is exposed to users of MetadataMap via the Parse() |
| 1027 |
// method, that returns a ParsedMetadata object. That ParsedMetadata object |
| 1028 |
// can in turn be used to set the same value on many different MetadataMaps |
| 1029 |
// without having to reparse. |
| 1030 |
// |
| 1031 |
// Implementation wise, ParsedMetadata is a type erased wrapper around |
| 1032 |
// MementoType. When we set a value on MetadataMap, we first turn that memento |
| 1033 |
// into a value. For most types, this is going to be a no-op, but for example |
| 1034 |
// for grpc-timeout we make the memento the timeout expressed on the wire, but |
| 1035 |
// we make the value the timestamp of when the timeout will expire (i.e. the |
| 1036 |
// deadline). |
| 1037 |
template <class Derived, typename... Traits> |
| 1038 |
class MetadataMap { |
| 1039 |
public: |
| 1040 |
explicit MetadataMap(Arena* arena); |
| 1041 |
~MetadataMap(); |
| 1042 |
|
| 1043 |
MetadataMap(const MetadataMap&) = delete; |
| 1044 |
MetadataMap& operator=(const MetadataMap&) = delete; |
| 1045 |
MetadataMap(MetadataMap&&) noexcept; |
| 1046 |
// We never create MetadataMap directly, instead we create Derived, but we |
| 1047 |
// want to be able to move it without redeclaring this. |
| 1048 |
// NOLINTNEXTLINE(misc-unconventional-assign-operator) |
| 1049 |
Derived& operator=(MetadataMap&&) noexcept; |
| 1050 |
|
| 1051 |
// Encode this metadata map into some encoder. |
| 1052 |
// For each field that is set in the MetadataMap, call |
| 1053 |
// encoder->Encode. |
| 1054 |
// |
| 1055 |
// For fields for which we have traits, this will be a method with |
| 1056 |
// the signature: |
| 1057 |
// void Encode(TraitsType, typename TraitsType::ValueType value); |
| 1058 |
// For fields for which we do not have traits, this will be a method |
| 1059 |
// with the signature: |
| 1060 |
// void Encode(string_view key, Slice value); |
| 1061 |
template <typename Encoder> |
| 1062 |
void Encode(Encoder* encoder) const { |
| 1063 |
table_.template ForEachIn<metadata_detail::EncodeWrapper<Encoder>, |
| 1064 |
Value<Traits>...>( |
| 1065 |
metadata_detail::EncodeWrapper<Encoder>{encoder}); |
| 1066 |
for (const auto& unk : unknown_) { |
| 1067 |
encoder->Encode(unk.first, unk.second); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
// Like Encode, but also visit the non-encodable fields. |
| 1072 |
template <typename Encoder> |
| 1073 |
void ForEach(Encoder* encoder) const { |
| 1074 |
table_.ForEach(metadata_detail::ForEachWrapper<Encoder>{encoder}); |
| 1075 |
for (const auto& unk : unknown_) { |
| 1076 |
encoder->Encode(unk.first, unk.second); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
// Similar to Encode, but targeted at logging: for each metadatum, |
| 1081 |
// call f(key, value) as absl::string_views. |
| 1082 |
void Log(metadata_detail::LogFn log_fn) const { |
| 1083 |
table_.ForEach(metadata_detail::LogWrapper{log_fn}); |
| 1084 |
for (const auto& unk : unknown_) { |
| 1085 |
log_fn(unk.first.as_string_view(), unk.second.as_string_view()); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
std::string DebugString() const { |
| 1090 |
metadata_detail::DebugStringBuilder builder; |
| 1091 |
Log([&builder](absl::string_view key, absl::string_view value) { |
| 1092 |
builder.Add(key, value); |
| 1093 |
}); |
| 1094 |
return builder.TakeOutput(); |
| 1095 |
} |
| 1096 |
|
| 1097 |
// Get the pointer to the value of some known metadata. |
| 1098 |
// Returns nullptr if the metadata is not present. |
| 1099 |
// Causes a compilation error if Which is not an element of Traits. |
| 1100 |
template <typename Which> |
| 1101 |
const typename metadata_detail::Value<Which>::StorageType* get_pointer( |
| 1102 |
Which) const { |
| 1103 |
if (auto* p = table_.template get<Value<Which>>()) return &p->value; |
| 1104 |
return nullptr; |
| 1105 |
} |
| 1106 |
|
| 1107 |
// Get the pointer to the value of some known metadata. |
| 1108 |
// Returns nullptr if the metadata is not present. |
| 1109 |
// Causes a compilation error if Which is not an element of Traits. |
| 1110 |
template <typename Which> |
| 1111 |
typename metadata_detail::Value<Which>::StorageType* get_pointer(Which) { |
| 1112 |
if (auto* p = table_.template get<Value<Which>>()) return &p->value; |
| 1113 |
return nullptr; |
| 1114 |
} |
| 1115 |
|
| 1116 |
// Get the pointer to the value of some known metadata. |
| 1117 |
// Adds the default value for the metadata is not present. |
| 1118 |
// Causes a compilation error if Which is not an element of Traits. |
| 1119 |
template <typename Which> |
| 1120 |
typename metadata_detail::Value<Which>::StorageType* GetOrCreatePointer( |
| 1121 |
Which) { |
| 1122 |
return &table_.template get_or_create<Value<Which>>()->value; |
| 1123 |
} |
| 1124 |
|
| 1125 |
// Get the value of some known metadata. |
| 1126 |
// Returns nullopt if the metadata is not present. |
| 1127 |
// Causes a compilation error if Which is not an element of Traits. |
| 1128 |
template <typename Which> |
| 1129 |
absl::optional<typename Which::ValueType> get(Which) const { |
| 1130 |
if (auto* p = table_.template get<Value<Which>>()) return p->value; |
| 1131 |
return absl::nullopt; |
| 1132 |
} |
| 1133 |
|
| 1134 |
// Set the value of some known metadata. |
| 1135 |
// Returns a pointer to the new value. |
| 1136 |
template <typename Which, typename... Args> |
| 1137 |
absl::enable_if_t<Which::kRepeatable == false, void> Set(Which, |
| 1138 |
Args&&... args) { |
| 1139 |
table_.template set<Value<Which>>(std::forward<Args>(args)...); |
| 1140 |
} |
| 1141 |
template <typename Which, typename... Args> |
| 1142 |
absl::enable_if_t<Which::kRepeatable == true, void> Set(Which, |
| 1143 |
Args&&... args) { |
| 1144 |
GetOrCreatePointer(Which())->emplace_back(std::forward<Args>(args)...); |
| 1145 |
} |
| 1146 |
|
| 1147 |
// Remove a specific piece of known metadata. |
| 1148 |
template <typename Which> |
| 1149 |
void Remove(Which) { |
| 1150 |
table_.template clear<Value<Which>>(); |
| 1151 |
} |
| 1152 |
|
| 1153 |
// Remove some metadata by name |
| 1154 |
void Remove(absl::string_view key) { |
| 1155 |
metadata_detail::RemoveHelper<Derived> helper(static_cast<Derived*>(this)); |
| 1156 |
metadata_detail::NameLookup<void, Traits...>::Lookup(key, &helper); |
| 1157 |
} |
| 1158 |
|
| 1159 |
void Remove(const char* key) { Remove(absl::string_view(key)); } |
| 1160 |
|
| 1161 |
// Retrieve some metadata by name |
| 1162 |
absl::optional<absl::string_view> GetStringValue(absl::string_view name, |
| 1163 |
std::string* buffer) const { |
| 1164 |
metadata_detail::GetStringValueHelper<Derived> helper( |
| 1165 |
static_cast<const Derived*>(this), buffer); |
| 1166 |
return metadata_detail::NameLookup<void, Traits...>::Lookup(name, &helper); |
| 1167 |
} |
| 1168 |
|
| 1169 |
// Extract a piece of known metadata. |
| 1170 |
// Returns nullopt if the metadata was not present, or the value if it was. |
| 1171 |
// The same as: |
| 1172 |
// auto value = m.get(T()); |
| 1173 |
// m.Remove(T()); |
| 1174 |
template <typename Which> |
| 1175 |
absl::enable_if_t<Which::kRepeatable == false, |
| 1176 |
absl::optional<typename Which::ValueType>> |
| 1177 |
Take(Which which) { |
| 1178 |
if (auto* p = get_pointer(which)) { |
| 1179 |
absl::optional<typename Which::ValueType> value(std::move(*p)); |
| 1180 |
Remove(which); |
| 1181 |
return value; |
| 1182 |
} |
| 1183 |
return {}; |
| 1184 |
} |
| 1185 |
|
| 1186 |
// Extract repeated known metadata. |
| 1187 |
// Returns an empty vector if the metadata was not present. |
| 1188 |
template <typename Which> |
| 1189 |
absl::enable_if_t<Which::kRepeatable == true, |
| 1190 |
typename metadata_detail::Value<Which>::StorageType> |
| 1191 |
Take(Which which) { |
| 1192 |
if (auto* p = get_pointer(which)) { |
| 1193 |
typename Value<Which>::StorageType value = std::move(*p); |
| 1194 |
Remove(which); |
| 1195 |
return value; |
| 1196 |
} |
| 1197 |
return {}; |
| 1198 |
} |
| 1199 |
|
| 1200 |
// Parse metadata from a key/value pair, and return an object representing |
| 1201 |
// that result. |
| 1202 |
// TODO(ctiller): key should probably be an absl::string_view. |
| 1203 |
// Once we don't care about interning anymore, make that change! |
| 1204 |
static ParsedMetadata<Derived> Parse(absl::string_view key, Slice value, |
| 1205 |
uint32_t transport_size, |
| 1206 |
MetadataParseErrorFn on_error) { |
| 1207 |
metadata_detail::ParseHelper<Derived> helper(value.TakeOwned(), on_error, |
| 1208 |
transport_size); |
| 1209 |
return metadata_detail::NameLookup<void, Traits...>::Lookup(key, &helper); |
| 1210 |
} |
| 1211 |
|
| 1212 |
// Set a value from a parsed metadata object. |
| 1213 |
void Set(const ParsedMetadata<Derived>& m) { |
| 1214 |
m.SetOnContainer(static_cast<Derived*>(this)); |
| 1215 |
} |
| 1216 |
|
| 1217 |
// Append a key/value pair - takes ownership of value |
| 1218 |
void Append(absl::string_view key, Slice value, |
| 1219 |
MetadataParseErrorFn on_error) { |
| 1220 |
metadata_detail::AppendHelper<Derived> helper(static_cast<Derived*>(this), |
| 1221 |
value.TakeOwned(), on_error); |
| 1222 |
metadata_detail::NameLookup<void, Traits...>::Lookup(key, &helper); |
| 1223 |
} |
| 1224 |
|
| 1225 |
void Clear(); |
| 1226 |
size_t TransportSize() const; |
| 1227 |
Derived Copy() const; |
| 1228 |
bool empty() const { return table_.empty() && unknown_.empty(); } |
| 1229 |
size_t count() const { return table_.count() + unknown_.size(); } |
| 1230 |
|
| 1231 |
private: |
| 1232 |
friend class metadata_detail::AppendHelper<Derived>; |
| 1233 |
friend class metadata_detail::GetStringValueHelper<Derived>; |
| 1234 |
friend class metadata_detail::RemoveHelper<Derived>; |
| 1235 |
friend class metadata_detail::CopySink<Derived>; |
| 1236 |
friend class ParsedMetadata<Derived>; |
| 1237 |
|
| 1238 |
template <typename Which> |
| 1239 |
using Value = metadata_detail::Value<Which>; |
| 1240 |
|
| 1241 |
// Table of known metadata types. |
| 1242 |
PackedTable<Value<Traits>...> table_; |
| 1243 |
metadata_detail::UnknownMap unknown_; |
| 1244 |
}; |
| 1245 |
|
| 1246 |
// Ok/not-ok check for metadata maps that contain GrpcStatusMetadata, so that |
| 1247 |
// they can be used as result types for TrySeq. |
| 1248 |
template <typename Derived, typename... Args> |
| 1249 |
inline bool IsStatusOk(const MetadataMap<Derived, Args...>& m) { |
| 1250 |
return m.get(GrpcStatusMetadata()).value_or(GRPC_STATUS_UNKNOWN) == |
| 1251 |
GRPC_STATUS_OK; |
| 1252 |
} |
| 1253 |
|
| 1254 |
template <typename Derived, typename... Traits> |
| 1255 |
MetadataMap<Derived, Traits...>::MetadataMap(Arena* arena) : unknown_(arena) {} |
| 1256 |
|
| 1257 |
template <typename Derived, typename... Traits> |
| 1258 |
MetadataMap<Derived, Traits...>::MetadataMap(MetadataMap&& other) noexcept |
| 1259 |
: table_(std::move(other.table_)), unknown_(std::move(other.unknown_)) {} |
| 1260 |
|
| 1261 |
// We never create MetadataMap directly, instead we create Derived, but we |
| 1262 |
// want to be able to move it without redeclaring this. |
| 1263 |
// NOLINTNEXTLINE(misc-unconventional-assign-operator) |
| 1264 |
template <typename Derived, typename... Traits> |
| 1265 |
Derived& MetadataMap<Derived, Traits...>::operator=( |
| 1266 |
MetadataMap&& other) noexcept { |
| 1267 |
table_ = std::move(other.table_); |
| 1268 |
unknown_ = std::move(other.unknown_); |
| 1269 |
return static_cast<Derived&>(*this); |
| 1270 |
} |
| 1271 |
|
| 1272 |
template <typename Derived, typename... Traits> |
| 1273 |
MetadataMap<Derived, Traits...>::~MetadataMap() = default; |
| 1274 |
|
| 1275 |
template <typename Derived, typename... Traits> |
| 1276 |
void MetadataMap<Derived, Traits...>::Clear() { |
| 1277 |
table_.ClearAll(); |
| 1278 |
unknown_.Clear(); |
| 1279 |
} |
| 1280 |
|
| 1281 |
template <typename Derived, typename... Traits> |
| 1282 |
size_t MetadataMap<Derived, Traits...>::TransportSize() const { |
| 1283 |
metadata_detail::TransportSizeEncoder enc; |
| 1284 |
Encode(&enc); |
| 1285 |
return enc.size(); |
| 1286 |
} |
| 1287 |
|
| 1288 |
template <typename Derived, typename... Traits> |
| 1289 |
Derived MetadataMap<Derived, Traits...>::Copy() const { |
| 1290 |
Derived out(unknown_.arena()); |
| 1291 |
metadata_detail::CopySink<Derived> sink(&out); |
| 1292 |
ForEach(&sink); |
| 1293 |
return out; |
| 1294 |
} |
| 1295 |
|
| 1296 |
} // namespace grpc_core |
| 1297 |
|
| 1298 |
struct grpc_metadata_batch; |
| 1299 |
|
| 1300 |
using grpc_metadata_batch_base = grpc_core::MetadataMap< |
| 1301 |
grpc_metadata_batch, |
| 1302 |
// Colon prefixed headers first |
| 1303 |
grpc_core::HttpPathMetadata, grpc_core::HttpAuthorityMetadata, |
| 1304 |
grpc_core::HttpMethodMetadata, grpc_core::HttpStatusMetadata, |
| 1305 |
grpc_core::HttpSchemeMetadata, |
| 1306 |
// Non-colon prefixed headers begin here |
| 1307 |
grpc_core::ContentTypeMetadata, grpc_core::TeMetadata, |
| 1308 |
grpc_core::GrpcEncodingMetadata, grpc_core::GrpcInternalEncodingRequest, |
| 1309 |
grpc_core::GrpcAcceptEncodingMetadata, grpc_core::GrpcStatusMetadata, |
| 1310 |
grpc_core::GrpcTimeoutMetadata, grpc_core::GrpcPreviousRpcAttemptsMetadata, |
| 1311 |
grpc_core::GrpcRetryPushbackMsMetadata, grpc_core::UserAgentMetadata, |
| 1312 |
grpc_core::GrpcMessageMetadata, grpc_core::HostMetadata, |
| 1313 |
grpc_core::EndpointLoadMetricsBinMetadata, |
| 1314 |
grpc_core::GrpcServerStatsBinMetadata, grpc_core::GrpcTraceBinMetadata, |
| 1315 |
grpc_core::GrpcTagsBinMetadata, grpc_core::GrpcLbClientStatsMetadata, |
| 1316 |
grpc_core::LbCostBinMetadata, grpc_core::LbTokenMetadata, |
| 1317 |
// Non-encodable things |
| 1318 |
grpc_core::GrpcStreamNetworkState, grpc_core::PeerString, |
| 1319 |
grpc_core::GrpcStatusContext, grpc_core::GrpcStatusFromWire, |
| 1320 |
grpc_core::WaitForReady>; |
| 1321 |
|
| 1322 |
struct grpc_metadata_batch : public grpc_metadata_batch_base { |
| 1323 |
using grpc_metadata_batch_base::grpc_metadata_batch_base; |
| 1324 |
}; |
| 1325 |
|
| 1326 |
#endif /* GRPC_CORE_LIB_TRANSPORT_METADATA_BATCH_H */ |