metadata_batch.cc
Go to the documentation of this file.
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 
16 
18 
19 #include <string.h>
20 
21 #include <algorithm>
22 
23 #include "absl/strings/escaping.h"
24 #include "absl/strings/match.h"
25 #include "absl/strings/str_cat.h"
26 
29 
30 namespace grpc_core {
31 namespace metadata_detail {
32 
34  if (!out_.empty()) out_.append(", ");
36 }
37 
39  unknown_.EmplaceBack(Slice::FromCopiedString(key), value.Ref());
40 }
41 
43  unknown_.SetEnd(std::remove_if(unknown_.begin(), unknown_.end(),
44  [key](const std::pair<Slice, Slice>& p) {
45  return p.first.as_string_view() == key;
46  }));
47 }
48 
50  absl::string_view key, std::string* backing) const {
52  for (const auto& p : unknown_) {
53  if (p.first.as_string_view() == key) {
54  if (!out.has_value()) {
55  out = p.second.as_string_view();
56  } else {
57  out = *backing = absl::StrCat(*out, ",", p.second.as_string_view());
58  }
59  }
60  }
61  return out;
62 }
63 
64 } // namespace metadata_detail
65 
67  Slice value, MetadataParseErrorFn on_error) {
68  auto out = kInvalid;
69  auto value_string = value.as_string_view();
70  if (value_string == "application/grpc") {
72  } else if (absl::StartsWith(value_string, "application/grpc;")) {
74  } else if (absl::StartsWith(value_string, "application/grpc+")) {
76  } else if (value_string.empty()) {
77  out = kEmpty;
78  } else {
79  on_error("invalid value", value);
80  }
81  return out;
82 }
83 
85  switch (x) {
86  case kEmpty:
88  case kApplicationGrpc:
89  return StaticSlice::FromStaticString("application/grpc");
90  case kInvalid:
91  return StaticSlice::FromStaticString("application/grpc+unknown");
92  }
94  return StaticSlice::FromStaticString("unrepresentable value"));
95 }
96 
98  switch (content_type) {
99  case ValueType::kApplicationGrpc:
100  return "application/grpc";
101  case ValueType::kEmpty:
102  return "";
103  default:
104  return "<discarded-invalid-value>";
105  }
106 }
107 
109  Slice value, MetadataParseErrorFn on_error) {
110  auto timeout = ParseTimeout(value);
111  if (!timeout.has_value()) {
112  on_error("invalid value", value);
113  return Duration::Infinity();
114  }
115  return *timeout;
116 }
117 
120  if (timeout == Duration::Infinity()) {
121  return Timestamp::InfFuture();
122  }
123  return ExecCtx::Get()->Now() + timeout;
124 }
125 
127  return Timeout::FromDuration(x - ExecCtx::Get()->Now()).Encode();
128 }
129 
131  Slice value, MetadataParseErrorFn on_error) {
132  auto out = kInvalid;
133  if (value == "trailers") {
134  out = kTrailers;
135  } else {
136  on_error("invalid value", value);
137  }
138  return out;
139 }
140 
142  switch (te) {
143  case ValueType::kTrailers:
144  return "trailers";
145  default:
146  return "<discarded-invalid-value>";
147  }
148 }
149 
152  if (value == "http") {
153  return kHttp;
154  } else if (value == "https") {
155  return kHttps;
156  }
157  on_error("invalid value", Slice::FromCopiedBuffer(value));
158  return kInvalid;
159 }
160 
162  switch (x) {
163  case kHttp:
164  return StaticSlice::FromStaticString("http");
165  case kHttps:
166  return StaticSlice::FromStaticString("https");
167  default:
168  abort();
169  }
170 }
171 
173  switch (content_type) {
174  case kHttp:
175  return "http";
176  case kHttps:
177  return "https";
178  default:
179  return "<discarded-invalid-value>";
180  }
181 }
182 
184  Slice value, MetadataParseErrorFn on_error) {
185  auto out = kInvalid;
186  auto value_string = value.as_string_view();
187  if (value_string == "POST") {
188  out = kPost;
189  } else if (value_string == "PUT") {
190  out = kPut;
191  } else if (value_string == "GET") {
192  out = kGet;
193  } else {
194  on_error("invalid value", value);
195  }
196  return out;
197 }
198 
200  switch (x) {
201  case kPost:
202  return StaticSlice::FromStaticString("POST");
203  case kPut:
204  return StaticSlice::FromStaticString("PUT");
205  case kGet:
206  return StaticSlice::FromStaticString("GET");
207  default:
208  abort();
209  }
210 }
211 
213  switch (content_type) {
214  case kPost:
215  return "POST";
216  case kGet:
217  return "GET";
218  case kPut:
219  return "PUT";
220  default:
221  return "<discarded-invalid-value>";
222  }
223 }
224 
227  MetadataParseErrorFn on_error) {
228  auto algorithm = ParseCompressionAlgorithm(value.as_string_view());
229  if (!algorithm.has_value()) {
230  on_error("invalid value", value);
231  return GRPC_COMPRESS_NONE;
232  }
233  return *algorithm;
234 }
235 
237  Slice value, MetadataParseErrorFn on_error) {
238  int64_t out;
239  if (!absl::SimpleAtoi(value.as_string_view(), &out)) {
240  on_error("not an integer", value);
242  }
243  return Duration::Milliseconds(out);
244 }
245 
247  auto slice =
248  MutableSlice::CreateUninitialized(sizeof(double) + x.name.length());
249  memcpy(slice.data(), &x.cost, sizeof(double));
250  memcpy(slice.data() + sizeof(double), x.name.data(), x.name.length());
251  return Slice(std::move(slice));
252 }
253 
255  return absl::StrCat(x.name, ":", x.cost);
256 }
257 
259  Slice value, MetadataParseErrorFn on_error) {
260  if (value.length() < sizeof(double)) {
261  on_error("too short", value);
262  return {0, ""};
263  }
265  memcpy(&out.cost, value.data(), sizeof(double));
266  out.name =
267  std::string(reinterpret_cast<const char*>(value.data()) + sizeof(double),
268  value.length() - sizeof(double));
269  return out;
270 }
271 
273  switch (x) {
274  case kNotSentOnWire:
275  return "not sent on wire";
276  case kNotSeenByServer:
277  return "not seen by server";
278  }
279  GPR_UNREACHABLE_CODE(return "unknown value");
280 }
281 
284  return x;
285 }
286 
287 } // namespace grpc_core
grpc_core::TeMetadata::ParseMemento
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error)
Definition: metadata_batch.cc:130
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
grpc_core::metadata_detail::DebugStringBuilder::out_
std::string out_
Definition: metadata_batch.h:406
metadata_batch.h
absl::StrAppend
void StrAppend(std::string *dest, const AlphaNum &a)
Definition: abseil-cpp/absl/strings/str_cat.cc:193
grpc_core::ParseTimeout
absl::optional< Duration > ParseTimeout(const Slice &text)
Definition: timeout_encoding.cc:228
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
grpc_core::ParseCompressionAlgorithm
absl::optional< grpc_compression_algorithm > ParseCompressionAlgorithm(absl::string_view algorithm)
Definition: compression_internal.cc:94
grpc_core::TeMetadata::kInvalid
@ kInvalid
Definition: metadata_batch.h:78
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::Slice
Definition: src/core/lib/slice/slice.h:282
string.h
grpc_compression_algorithm
grpc_compression_algorithm
Definition: compression_types.h:60
grpc_core::HttpMethodMetadata::DisplayValue
static const char * DisplayValue(MementoType content_type)
Definition: metadata_batch.cc:212
grpc_core::slice_detail::StaticConstructors< StaticSlice >::FromStaticString
static StaticSlice FromStaticString(const char *s)
Definition: src/core/lib/slice/slice.h:201
absl::StartsWith
bool StartsWith(absl::string_view text, absl::string_view prefix) noexcept
Definition: third_party/abseil-cpp/absl/strings/match.h:58
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
grpc_core::HttpSchemeMetadata::ValueType
ValueType
Definition: metadata_batch.h:116
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::HttpMethodMetadata::kPut
@ kPut
Definition: metadata_batch.h:141
grpc_core::HttpSchemeMetadata::kHttp
@ kHttp
Definition: metadata_batch.h:117
absl::CEscape
std::string CEscape(absl::string_view src)
Definition: abseil-cpp/absl/strings/escaping.cc:854
grpc_core::Timeout::Encode
Slice Encode() const
Definition: timeout_encoding.cc:91
grpc_core::metadata_detail::DebugStringBuilder::Add
void Add(absl::string_view key, absl::string_view value)
Definition: metadata_batch.cc:33
GRPC_COMPRESS_NONE
@ GRPC_COMPRESS_NONE
Definition: compression_types.h:61
grpc_core::HttpSchemeMetadata::Encode
static StaticSlice Encode(ValueType x)
Definition: metadata_batch.cc:161
grpc_core::CompressionAlgorithmBasedMetadata::ParseMemento
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error)
Definition: metadata_batch.cc:226
grpc_core::Timeout::FromDuration
static Timeout FromDuration(Duration duration)
Definition: timeout_encoding.cc:47
timeout_encoding.h
grpc_core::MutableSlice::CreateUninitialized
static MutableSlice CreateUninitialized(size_t length)
Definition: src/core/lib/slice/slice.h:263
absl::SimpleAtoi
ABSL_NAMESPACE_BEGIN ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str, int_type *out)
Definition: abseil-cpp/absl/strings/numbers.h:271
grpc_core::LbCostBinMetadata::ValueType
Definition: metadata_batch.h:352
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
grpc_core::ContentTypeMetadata::ParseMemento
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error)
Definition: metadata_batch.cc:66
grpc_core::slice_detail::CopyConstructors< Slice >::FromCopiedString
static Slice FromCopiedString(const char *s)
Definition: src/core/lib/slice/slice.h:173
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::TeMetadata::DisplayValue
static const char * DisplayValue(MementoType te)
Definition: metadata_batch.cc:141
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
grpc_core::slice_detail::CopyConstructors< Slice >::FromCopiedBuffer
static Slice FromCopiedBuffer(const char *p, size_t len)
Definition: src/core/lib/slice/slice.h:182
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc_core::LbCostBinMetadata::DisplayValue
static std::string DisplayValue(MementoType x)
Definition: metadata_batch.cc:254
grpc_core::GrpcStatusContext::DisplayValue
static const std::string & DisplayValue(const std::string &x)
Definition: metadata_batch.cc:283
grpc_core::ContentTypeMetadata::DisplayValue
static const char * DisplayValue(MementoType content_type)
Definition: metadata_batch.cc:97
grpc_core::HttpMethodMetadata::ParseMemento
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error)
Definition: metadata_batch.cc:183
grpc_core::HttpMethodMetadata::ValueType
ValueType
Definition: metadata_batch.h:138
absl::optional< absl::string_view >
grpc_core::GrpcRetryPushbackMsMetadata::ParseMemento
static Duration ParseMemento(Slice value, MetadataParseErrorFn on_error)
Definition: metadata_batch.cc:236
grpc_core::TeMetadata::kTrailers
@ kTrailers
Definition: metadata_batch.h:77
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
grpc_core::HttpMethodMetadata::Encode
static StaticSlice Encode(ValueType x)
Definition: metadata_batch.cc:199
GPR_UNREACHABLE_CODE
#define GPR_UNREACHABLE_CODE(STATEMENT)
Definition: impl/codegen/port_platform.h:652
grpc_core::HttpSchemeMetadata::DisplayValue
static const char * DisplayValue(MementoType content_type)
Definition: metadata_batch.cc:172
grpc_core::ContentTypeMetadata::kEmpty
@ kEmpty
Definition: metadata_batch.h:99
grpc_core::LbCostBinMetadata::ParseMemento
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error)
Definition: metadata_batch.cc:258
grpc_core::GrpcStreamNetworkState::kNotSentOnWire
@ kNotSentOnWire
Definition: metadata_batch.h:369
grpc_core::Duration::NegativeInfinity
static constexpr Duration NegativeInfinity()
Definition: src/core/lib/gprpp/time.h:135
grpc_core::GrpcStreamNetworkState::DisplayValue
static std::string DisplayValue(ValueType x)
Definition: metadata_batch.cc:272
grpc_core::ContentTypeMetadata::kInvalid
@ kInvalid
Definition: metadata_batch.h:100
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_core::metadata_detail::UnknownMap::unknown_
ChunkedVector< std::pair< Slice, Slice >, 10 > unknown_
Definition: metadata_batch.h:869
grpc_core::StaticSlice
Definition: src/core/lib/slice/slice.h:221
grpc_core::Duration::Milliseconds
static constexpr Duration Milliseconds(int64_t millis)
Definition: src/core/lib/gprpp/time.h:155
absl::Now
ABSL_NAMESPACE_BEGIN Time Now()
Definition: abseil-cpp/absl/time/clock.cc:39
grpc_core::ContentTypeMetadata::ValueType
ValueType
Definition: metadata_batch.h:97
key
const char * key
Definition: hpack_parser_table.cc:164
grpc_core::HttpSchemeMetadata::Parse
static ValueType Parse(absl::string_view value, MetadataParseErrorFn on_error)
Definition: metadata_batch.cc:150
grpc_core::GrpcTimeoutMetadata::Encode
static Slice Encode(ValueType x)
Definition: metadata_batch.cc:126
grpc_core::HttpMethodMetadata::kPost
@ kPost
Definition: metadata_batch.h:139
grpc_core::metadata_detail::UnknownMap::Append
void Append(absl::string_view key, Slice value)
Definition: metadata_batch.cc:38
absl::container_internal::kEmpty
@ kEmpty
Definition: bloaty/third_party/abseil-cpp/absl/container/internal/raw_hash_set.h:261
grpc_core::HttpSchemeMetadata::kHttps
@ kHttps
Definition: metadata_batch.h:118
grpc_core::HttpMethodMetadata::kGet
@ kGet
Definition: metadata_batch.h:140
grpc_core::PeerString::DisplayValue
static std::string DisplayValue(ValueType x)
Definition: metadata_batch.cc:282
grpc_core::ContentTypeMetadata::Encode
static StaticSlice Encode(ValueType x)
Definition: metadata_batch.cc:84
grpc_slice::data
union grpc_slice::grpc_slice_data data
exec_ctx.h
grpc_core::GrpcStreamNetworkState::ValueType
ValueType
Definition: metadata_batch.h:368
absl::FunctionRef< void(absl::string_view error, const Slice &value)>
grpc_core::ContentTypeMetadata::kApplicationGrpc
@ kApplicationGrpc
Definition: metadata_batch.h:98
grpc_core::GrpcStreamNetworkState::kNotSeenByServer
@ kNotSeenByServer
Definition: metadata_batch.h:370
grpc_core::GrpcTimeoutMetadata::MementoToValue
static ValueType MementoToValue(MementoType timeout)
Definition: metadata_batch.cc:118
grpc_core::Timestamp::InfFuture
static constexpr Timestamp InfFuture()
Definition: src/core/lib/gprpp/time.h:79
grpc_core::GrpcTimeoutMetadata::ParseMemento
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error)
Definition: metadata_batch.cc:108
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
grpc_core::LbCostBinMetadata::Encode
static Slice Encode(const ValueType &x)
Definition: metadata_batch.cc:246
grpc_core::metadata_detail::UnknownMap::GetStringValue
absl::optional< absl::string_view > GetStringValue(absl::string_view key, std::string *backing) const
Definition: metadata_batch.cc:49
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
grpc_core::TeMetadata::ValueType
ValueType
Definition: metadata_batch.h:76
grpc_core::HttpSchemeMetadata::kInvalid
@ kInvalid
Definition: metadata_batch.h:119
grpc_core::HttpMethodMetadata::kInvalid
@ kInvalid
Definition: metadata_batch.h:142
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
grpc_core::Duration::Infinity
static constexpr Duration Infinity()
Definition: src/core/lib/gprpp/time.h:139
grpc_core::metadata_detail::UnknownMap::Remove
void Remove(absl::string_view key)
Definition: metadata_batch.cc:42
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:39