| 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 |
#include <grpc/support/port_platform.h> |
| 16 |
|
| 17 |
#include "src/core/lib/transport/metadata_batch.h" |
| 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 |
|
| 27 |
#include "src/core/lib/transport/timeout_encoding.h" |
| 28 |
|
| 29 |
namespace grpc_core { |
| 30 |
namespace metadata_detail { |
| 31 |
|
| 32 |
void DebugStringBuilder::Add(absl::string_view key, absl::string_view value) { |
| 33 |
if (!out_.empty()) out_.append(", "); |
| 34 |
absl::StrAppend(&out_, absl::CEscape(key), ": ", absl::CEscape(value)); |
| 35 |
} |
| 36 |
|
| 37 |
void UnknownMap::Append(absl::string_view key, Slice value) { |
| 38 |
unknown_.EmplaceBack(Slice::FromCopiedString(key), value.Ref()); |
| 39 |
} |
| 40 |
|
| 41 |
void UnknownMap::Remove(absl::string_view key) { |
| 42 |
unknown_.SetEnd(std::remove_if(unknown_.begin(), unknown_.end(), |
| 43 |
[key](const std::pair<Slice, Slice>& p) { |
| 44 |
return p.first.as_string_view() == key; |
| 45 |
})); |
| 46 |
} |
| 47 |
|
| 48 |
absl::optional<absl::string_view> UnknownMap::GetStringValue( |
| 49 |
absl::string_view key, std::string* backing) const { |
| 50 |
absl::optional<absl::string_view> out; |
| 51 |
for (const auto& p : unknown_) { |
| 52 |
if (p.first.as_string_view() == key) { |
| 53 |
if (!out.has_value()) { |
| 54 |
out = p.second.as_string_view(); |
| 55 |
} else { |
| 56 |
out = *backing = absl::StrCat(*out, ",", p.second.as_string_view()); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
return out; |
| 61 |
} |
| 62 |
|
| 63 |
} // namespace metadata_detail |
| 64 |
|
| 65 |
ContentTypeMetadata::MementoType ContentTypeMetadata::ParseMemento( |
| 66 |
Slice value, MetadataParseErrorFn on_error) { |
| 67 |
auto out = kInvalid; |
| 68 |
auto value_string = value.as_string_view(); |
| 69 |
if (value_string == "application/grpc") { |
| 70 |
out = kApplicationGrpc; |
| 71 |
} else if (absl::StartsWith(value_string, "application/grpc;")) { |
| 72 |
out = kApplicationGrpc; |
| 73 |
} else if (absl::StartsWith(value_string, "application/grpc+")) { |
| 74 |
out = kApplicationGrpc; |
| 75 |
} else if (value_string.empty()) { |
| 76 |
out = kEmpty; |
| 77 |
} else { |
| 78 |
on_error("invalid value", value); |
| 79 |
} |
| 80 |
return out; |
| 81 |
} |
| 82 |
|
| 83 |
StaticSlice ContentTypeMetadata::Encode(ValueType x) { |
| 84 |
switch (x) { |
| 85 |
case kEmpty: |
| 86 |
return StaticSlice::FromStaticString(""); |
| 87 |
case kApplicationGrpc: |
| 88 |
return StaticSlice::FromStaticString("application/grpc"); |
| 89 |
case kInvalid: |
| 90 |
return StaticSlice::FromStaticString("application/grpc+unknown"); |
| 91 |
} |
| 92 |
GPR_UNREACHABLE_CODE( |
| 93 |
return StaticSlice::FromStaticString("unrepresentable value")); |
| 94 |
} |
| 95 |
|
| 96 |
const char* ContentTypeMetadata::DisplayValue(MementoType content_type) { |
| 97 |
switch (content_type) { |
| 98 |
case ValueType::kApplicationGrpc: |
| 99 |
return "application/grpc"; |
| 100 |
case ValueType::kEmpty: |
| 101 |
return ""; |
| 102 |
default: |
| 103 |
return "<discarded-invalid-value>"; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
GrpcTimeoutMetadata::MementoType GrpcTimeoutMetadata::ParseMemento( |
| 108 |
Slice value, MetadataParseErrorFn on_error) { |
| 109 |
auto timeout = ParseTimeout(value); |
| 110 |
if (!timeout.has_value()) { |
| 111 |
on_error("invalid value", value); |
| 112 |
return Duration::Infinity(); |
| 113 |
} |
| 114 |
return *timeout; |
| 115 |
} |
| 116 |
|
| 117 |
GrpcTimeoutMetadata::ValueType GrpcTimeoutMetadata::MementoToValue( |
| 118 |
MementoType timeout) { |
| 119 |
if (timeout == Duration::Infinity()) { |
| 120 |
return Timestamp::InfFuture(); |
| 121 |
} |
| 122 |
return Timestamp::Now() + timeout; |
| 123 |
} |
| 124 |
|
| 125 |
Slice GrpcTimeoutMetadata::Encode(ValueType x) { |
| 126 |
return Timeout::FromDuration(x - Timestamp::Now()).Encode(); |
| 127 |
} |
| 128 |
|
| 129 |
TeMetadata::MementoType TeMetadata::ParseMemento( |
| 130 |
Slice value, MetadataParseErrorFn on_error) { |
| 131 |
auto out = kInvalid; |
| 132 |
if (value == "trailers") { |
| 133 |
out = kTrailers; |
| 134 |
} else { |
| 135 |
on_error("invalid value", value); |
| 136 |
} |
| 137 |
return out; |
| 138 |
} |
| 139 |
|
| 140 |
const char* TeMetadata::DisplayValue(MementoType te) { |
| 141 |
switch (te) { |
| 142 |
case ValueType::kTrailers: |
| 143 |
return "trailers"; |
| 144 |
default: |
| 145 |
return "<discarded-invalid-value>"; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
HttpSchemeMetadata::ValueType HttpSchemeMetadata::Parse( |
| 150 |
absl::string_view value, MetadataParseErrorFn on_error) { |
| 151 |
if (value == "http") { |
| 152 |
return kHttp; |
| 153 |
} else if (value == "https") { |
| 154 |
return kHttps; |
| 155 |
} |
| 156 |
on_error("invalid value", Slice::FromCopiedBuffer(value)); |
| 157 |
return kInvalid; |
| 158 |
} |
| 159 |
|
| 160 |
StaticSlice HttpSchemeMetadata::Encode(ValueType x) { |
| 161 |
switch (x) { |
| 162 |
case kHttp: |
| 163 |
return StaticSlice::FromStaticString("http"); |
| 164 |
case kHttps: |
| 165 |
return StaticSlice::FromStaticString("https"); |
| 166 |
default: |
| 167 |
abort(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
const char* HttpSchemeMetadata::DisplayValue(MementoType content_type) { |
| 172 |
switch (content_type) { |
| 173 |
case kHttp: |
| 174 |
return "http"; |
| 175 |
case kHttps: |
| 176 |
return "https"; |
| 177 |
default: |
| 178 |
return "<discarded-invalid-value>"; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
HttpMethodMetadata::MementoType HttpMethodMetadata::ParseMemento( |
| 183 |
Slice value, MetadataParseErrorFn on_error) { |
| 184 |
auto out = kInvalid; |
| 185 |
auto value_string = value.as_string_view(); |
| 186 |
if (value_string == "POST") { |
| 187 |
out = kPost; |
| 188 |
} else if (value_string == "PUT") { |
| 189 |
out = kPut; |
| 190 |
} else if (value_string == "GET") { |
| 191 |
out = kGet; |
| 192 |
} else { |
| 193 |
on_error("invalid value", value); |
| 194 |
} |
| 195 |
return out; |
| 196 |
} |
| 197 |
|
| 198 |
StaticSlice HttpMethodMetadata::Encode(ValueType x) { |
| 199 |
switch (x) { |
| 200 |
case kPost: |
| 201 |
return StaticSlice::FromStaticString("POST"); |
| 202 |
case kPut: |
| 203 |
return StaticSlice::FromStaticString("PUT"); |
| 204 |
case kGet: |
| 205 |
return StaticSlice::FromStaticString("GET"); |
| 206 |
default: |
| 207 |
abort(); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
const char* HttpMethodMetadata::DisplayValue(MementoType content_type) { |
| 212 |
switch (content_type) { |
| 213 |
case kPost: |
| 214 |
return "POST"; |
| 215 |
case kGet: |
| 216 |
return "GET"; |
| 217 |
case kPut: |
| 218 |
return "PUT"; |
| 219 |
default: |
| 220 |
return "<discarded-invalid-value>"; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
CompressionAlgorithmBasedMetadata::MementoType |
| 225 |
CompressionAlgorithmBasedMetadata::ParseMemento(Slice value, |
| 226 |
MetadataParseErrorFn on_error) { |
| 227 |
auto algorithm = ParseCompressionAlgorithm(value.as_string_view()); |
| 228 |
if (!algorithm.has_value()) { |
| 229 |
on_error("invalid value", value); |
| 230 |
return GRPC_COMPRESS_NONE; |
| 231 |
} |
| 232 |
return *algorithm; |
| 233 |
} |
| 234 |
|
| 235 |
Duration GrpcRetryPushbackMsMetadata::ParseMemento( |
| 236 |
Slice value, MetadataParseErrorFn on_error) { |
| 237 |
int64_t out; |
| 238 |
if (!absl::SimpleAtoi(value.as_string_view(), &out)) { |
| 239 |
on_error("not an integer", value); |
| 240 |
return Duration::NegativeInfinity(); |
| 241 |
} |
| 242 |
return Duration::Milliseconds(out); |
| 243 |
} |
| 244 |
|
| 245 |
Slice LbCostBinMetadata::Encode(const ValueType& x) { |
| 246 |
auto slice = |
| 247 |
MutableSlice::CreateUninitialized(sizeof(double) + x.name.length()); |
| 248 |
memcpy(slice.data(), &x.cost, sizeof(double)); |
| 249 |
memcpy(slice.data() + sizeof(double), x.name.data(), x.name.length()); |
| 250 |
return Slice(std::move(slice)); |
| 251 |
} |
| 252 |
|
| 253 |
std::string LbCostBinMetadata::DisplayValue(MementoType x) { |
| 254 |
return absl::StrCat(x.name, ":", x.cost); |
| 255 |
} |
| 256 |
|
| 257 |
LbCostBinMetadata::MementoType LbCostBinMetadata::ParseMemento( |
| 258 |
Slice value, MetadataParseErrorFn on_error) { |
| 259 |
if (value.length() < sizeof(double)) { |
| 260 |
on_error("too short", value); |
| 261 |
return {0, ""}; |
| 262 |
} |
| 263 |
MementoType out; |
| 264 |
memcpy(&out.cost, value.data(), sizeof(double)); |
| 265 |
out.name = |
| 266 |
std::string(reinterpret_cast<const char*>(value.data()) + sizeof(double), |
| 267 |
value.length() - sizeof(double)); |
| 268 |
return out; |
| 269 |
} |
| 270 |
|
| 271 |
std::string GrpcStreamNetworkState::DisplayValue(ValueType x) { |
| 272 |
switch (x) { |
| 273 |
case kNotSentOnWire: |
| 274 |
return "not sent on wire"; |
| 275 |
case kNotSeenByServer: |
| 276 |
return "not seen by server"; |
| 277 |
} |
| 278 |
GPR_UNREACHABLE_CODE(return "unknown value"); |
| 279 |
} |
| 280 |
|
| 281 |
std::string PeerString::DisplayValue(ValueType x) { return std::string(x); } |
| 282 |
const std::string& GrpcStatusContext::DisplayValue(const std::string& x) { |
| 283 |
return x; |
| 284 |
} |
| 285 |
|
| 286 |
std::string WaitForReady::DisplayValue(ValueType x) { |
| 287 |
return absl::StrCat(x.value ? "true" : "false", |
| 288 |
x.explicitly_set ? " (explicit)" : ""); |
| 289 |
} |
| 290 |
|
| 291 |
} // namespace grpc_core |