validate_metadata.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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 
20 
22 
23 #include "absl/strings/string_view.h"
24 
25 #include <grpc/grpc.h>
26 
31 
32 #if __cplusplus > 201103l
33 #define GRPC_VALIDATE_METADATA_CONSTEXPR_FN constexpr
34 #define GRPC_VALIDATE_METADATA_CONSTEXPR_VALUE constexpr
35 #else
36 #define GRPC_VALIDATE_METADATA_CONSTEXPR_FN
37 #define GRPC_VALIDATE_METADATA_CONSTEXPR_VALUE const
38 #endif
39 
41  const grpc_core::BitSet<256>& legal_bits,
42  const char* err_desc) {
44  const uint8_t* e = GRPC_SLICE_END_PTR(slice);
45  for (; p != e; p++) {
46  if (!legal_bits.is_set(*p)) {
47  size_t len;
49  reinterpret_cast<const char*> GRPC_SLICE_START_PTR(slice),
56  return error;
57  }
58  }
59  return GRPC_ERROR_NONE;
60 }
61 
63  int r = (GRPC_ERROR_IS_NONE(error));
65  return r;
66 }
67 
68 namespace {
69 class LegalHeaderKeyBits : public grpc_core::BitSet<256> {
70  public:
71  GRPC_VALIDATE_METADATA_CONSTEXPR_FN LegalHeaderKeyBits() {
72  for (int i = 'a'; i <= 'z'; i++) set(i);
73  for (int i = '0'; i <= '9'; i++) set(i);
74  set('-');
75  set('_');
76  set('.');
77  }
78 };
80  g_legal_header_key_bits;
81 } // namespace
82 
84  if (GRPC_SLICE_LENGTH(slice) == 0) {
86  "Metadata keys cannot be zero length");
87  }
90  "Metadata keys cannot be larger than UINT32_MAX");
91  }
92  if (GRPC_SLICE_START_PTR(slice)[0] == ':') {
94  "Metadata keys cannot start with :");
95  }
96  return conforms_to(slice, g_legal_header_key_bits, "Illegal header key");
97 }
98 
101 }
102 
103 namespace {
104 class LegalHeaderNonBinValueBits : public grpc_core::BitSet<256> {
105  public:
106  GRPC_VALIDATE_METADATA_CONSTEXPR_FN LegalHeaderNonBinValueBits() {
107  for (int i = 32; i <= 126; i++) {
108  set(i);
109  }
110  }
111 };
112 GRPC_VALIDATE_METADATA_CONSTEXPR_VALUE LegalHeaderNonBinValueBits
113  g_legal_header_non_bin_value_bits;
114 } // namespace
115 
117  const grpc_slice& slice) {
118  return conforms_to(slice, g_legal_header_non_bin_value_bits,
119  "Illegal header value");
120 }
121 
124 }
125 
129 }
130 
133 }
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
grpc_validate_header_nonbin_value_is_legal
grpc_error_handle grpc_validate_header_nonbin_value_is_legal(const grpc_slice &slice)
Definition: validate_metadata.cc:116
grpc_header_nonbin_value_is_legal
int grpc_header_nonbin_value_is_legal(grpc_slice slice)
Definition: validate_metadata.cc:122
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
GRPC_ERROR_CREATE_FROM_COPIED_STRING
#define GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc)
Definition: error.h:294
grpc_core::BitSet::set
GRPC_BITSET_CONSTEXPR_MUTATOR void set(int i)
Definition: bitset.h:93
conforms_to
static grpc_error_handle conforms_to(const grpc_slice &slice, const grpc_core::BitSet< 256 > &legal_bits, const char *err_desc)
Definition: validate_metadata.cc:40
grpc_header_key_is_legal
int grpc_header_key_is_legal(grpc_slice slice)
Definition: validate_metadata.cc:99
string.h
GRPC_VALIDATE_METADATA_CONSTEXPR_FN
#define GRPC_VALIDATE_METADATA_CONSTEXPR_FN
Definition: validate_metadata.cc:36
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
error
grpc_error_handle error
Definition: retry_filter.cc:499
UINT32_MAX
#define UINT32_MAX
Definition: stdint-msvc2008.h:142
GRPC_ERROR_STR_RAW_BYTES
@ GRPC_ERROR_STR_RAW_BYTES
hex dump (or similar) with the data that generated this error
Definition: error.h:123
xds_manager.p
p
Definition: xds_manager.py:60
GPR_DUMP_HEX
#define GPR_DUMP_HEX
Definition: string.h:34
grpc_is_binary_header_internal
int grpc_is_binary_header_internal(const grpc_slice &slice)
Definition: validate_metadata.cc:126
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
GPR_DUMP_ASCII
#define GPR_DUMP_ASCII
Definition: string.h:35
grpc_error_set_str
grpc_error_handle grpc_error_set_str(grpc_error_handle src, grpc_error_strs which, absl::string_view str)
Definition: error.cc:650
grpc_core::BitSet::is_set
constexpr bool is_set(int i) const
Definition: bitset.h:112
memory.h
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc.h
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
error.h
GRPC_ERROR_INT_OFFSET
@ GRPC_ERROR_INT_OFFSET
Definition: error.h:70
GRPC_SLICE_END_PTR
#define GRPC_SLICE_END_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:110
grpc_core::UniquePtr
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: src/core/lib/gprpp/memory.h:43
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
grpc_core::BitSet
Definition: bitset.h:85
grpc_error_set_int
grpc_error_handle grpc_error_set_int(grpc_error_handle src, grpc_error_ints which, intptr_t value)
Definition: error.cc:613
grpc_is_binary_header
int grpc_is_binary_header(grpc_slice slice)
Definition: validate_metadata.cc:131
validate_metadata.h
fix_build_deps.r
r
Definition: fix_build_deps.py:491
cpp.gmock_class.set
set
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/gmock_class.py:44
grpc_validate_header_key_is_legal
grpc_error_handle grpc_validate_header_key_is_legal(const grpc_slice &slice)
Definition: validate_metadata.cc:83
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
GRPC_VALIDATE_METADATA_CONSTEXPR_VALUE
#define GRPC_VALIDATE_METADATA_CONSTEXPR_VALUE
Definition: validate_metadata.cc:37
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
grpc_error
Definition: error_internal.h:42
error2int
static int error2int(grpc_error_handle error)
Definition: validate_metadata.cc:62
grpc_key_is_binary_header
int grpc_key_is_binary_header(const uint8_t *buf, size_t length)
Definition: validate_metadata.h:38
bitset.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
gpr_dump_return_len
char * gpr_dump_return_len(const char *buf, size_t len, uint32_t flags, size_t *out_len)
Definition: string.cc:130
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:49