GRPC C++  1.66.0
metadata_map.h
Go to the documentation of this file.
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 GRPCPP_IMPL_METADATA_MAP_H
20 #define GRPCPP_IMPL_METADATA_MAP_H
21 
22 #include <map>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/support/log.h>
26 #include <grpcpp/support/slice.h>
27 
28 namespace grpc {
29 
30 namespace internal {
31 
32 const char kBinaryErrorDetailsKey[] = "grpc-status-details-bin";
33 
34 class MetadataMap {
35  public:
36  MetadataMap() { Setup(); }
37 
38  ~MetadataMap() { Destroy(); }
39 
40  std::string GetBinaryErrorDetails() {
41  // if filled_, extract from the multimap for O(log(n))
42  if (filled_) {
43  auto iter = map_.find(kBinaryErrorDetailsKey);
44  if (iter != map_.end()) {
45  return std::string(iter->second.begin(), iter->second.length());
46  }
47  }
48  // if not yet filled, take the O(n) lookup to avoid allocating the
49  // multimap until it is requested.
50  // TODO(ncteisen): plumb this through core as a first class object, just
51  // like code and message.
52  else {
53  for (size_t i = 0; i < arr_.count; i++) {
54  if (strncmp(reinterpret_cast<const char*>(
57  GRPC_SLICE_LENGTH(arr_.metadata[i].key)) == 0) {
58  return std::string(reinterpret_cast<const char*>(
61  }
62  }
63  }
64  return std::string();
65  }
66 
67  std::multimap<grpc::string_ref, grpc::string_ref>* map() {
68  FillMap();
69  return &map_;
70  }
71  grpc_metadata_array* arr() { return &arr_; }
72 
73  void Reset() {
74  filled_ = false;
75  map_.clear();
76  Destroy();
77  Setup();
78  }
79 
80  private:
81  bool filled_ = false;
83  std::multimap<grpc::string_ref, grpc::string_ref> map_;
84 
85  void Destroy() { grpc_metadata_array_destroy(&arr_); }
86 
87  void Setup() { memset(&arr_, 0, sizeof(arr_)); }
88 
89  void FillMap() {
90  if (filled_) return;
91  filled_ = true;
92  for (size_t i = 0; i < arr_.count; i++) {
93  // TODO(yangg) handle duplicates?
94  map_.insert(std::pair<grpc::string_ref, grpc::string_ref>(
96  StringRefFromSlice(&arr_.metadata[i].value)));
97  }
98  }
99 };
100 } // namespace internal
101 
102 } // namespace grpc
103 
104 #endif // GRPCPP_IMPL_METADATA_MAP_H
grpc::StringRefFromSlice
grpc::string_ref StringRefFromSlice(const grpc_slice *slice)
Definition: slice.h:121
grpc_metadata_array::metadata
grpc_metadata * metadata
Definition: grpc_types.h:255
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: slice_type.h:99
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc_metadata_array
Definition: grpc_types.h:252
grpc::internal::MetadataMap::arr
grpc_metadata_array * arr()
Definition: metadata_map.h:71
grpc_metadata_array::count
size_t count
Definition: grpc_types.h:253
grpc::internal::MetadataMap
Definition: metadata_map.h:34
grpc_metadata_array_destroy
GRPCAPI void grpc_metadata_array_destroy(grpc_metadata_array *array)
grpc::internal::MetadataMap::map
std::multimap< grpc::string_ref, grpc::string_ref > * map()
Definition: metadata_map.h:67
grpc::internal::MetadataMap::~MetadataMap
~MetadataMap()
Definition: metadata_map.h:38
log.h
grpc::internal::kBinaryErrorDetailsKey
const char kBinaryErrorDetailsKey[]
Definition: metadata_map.h:32
grpc_metadata::value
grpc_slice value
Definition: grpc_types.h:214
grpc.h
grpc::internal::MetadataMap::MetadataMap
MetadataMap()
Definition: metadata_map.h:36
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: slice_type.h:102
grpc::internal::MetadataMap::GetBinaryErrorDetails
std::string GetBinaryErrorDetails()
Definition: metadata_map.h:40
slice.h
grpc_metadata::key
grpc_slice key
the key, value values are expected to line up with grpc_mdelem: if changing them, update metadata....
Definition: grpc_types.h:213
grpc::internal::MetadataMap::Reset
void Reset()
Definition: metadata_map.h:73