• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CODEGEN_METADATA_MAP_H
20 #define GRPCPP_IMPL_CODEGEN_METADATA_MAP_H
21 
22 #include <grpc/impl/codegen/log.h>
23 #include <grpcpp/impl/codegen/slice.h>
24 
25 namespace grpc {
26 
27 namespace internal {
28 
29 const char kBinaryErrorDetailsKey[] = "grpc-status-details-bin";
30 
31 class MetadataMap {
32  public:
MetadataMap()33   MetadataMap() { memset(&arr_, 0, sizeof(arr_)); }
34 
~MetadataMap()35   ~MetadataMap() {
36     g_core_codegen_interface->grpc_metadata_array_destroy(&arr_);
37   }
38 
GetBinaryErrorDetails()39   grpc::string GetBinaryErrorDetails() {
40     // if filled_, extract from the multimap for O(log(n))
41     if (filled_) {
42       auto iter = map_.find(kBinaryErrorDetailsKey);
43       if (iter != map_.end()) {
44         return grpc::string(iter->second.begin(), iter->second.length());
45       }
46     }
47     // if not yet filled, take the O(n) lookup to avoid allocating the
48     // multimap until it is requested.
49     // TODO(ncteisen): plumb this through core as a first class object, just
50     // like code and message.
51     else {
52       for (size_t i = 0; i < arr_.count; i++) {
53         if (strncmp(reinterpret_cast<const char*>(
54                         GRPC_SLICE_START_PTR(arr_.metadata[i].key)),
55                     kBinaryErrorDetailsKey,
56                     GRPC_SLICE_LENGTH(arr_.metadata[i].key)) == 0) {
57           return grpc::string(reinterpret_cast<const char*>(
58                                   GRPC_SLICE_START_PTR(arr_.metadata[i].value)),
59                               GRPC_SLICE_LENGTH(arr_.metadata[i].value));
60         }
61       }
62     }
63     return grpc::string();
64   }
65 
map()66   std::multimap<grpc::string_ref, grpc::string_ref>* map() {
67     FillMap();
68     return &map_;
69   }
arr()70   grpc_metadata_array* arr() { return &arr_; }
71 
72  private:
73   bool filled_ = false;
74   grpc_metadata_array arr_;
75   std::multimap<grpc::string_ref, grpc::string_ref> map_;
76 
FillMap()77   void FillMap() {
78     if (filled_) return;
79     filled_ = true;
80     for (size_t i = 0; i < arr_.count; i++) {
81       // TODO(yangg) handle duplicates?
82       map_.insert(std::pair<grpc::string_ref, grpc::string_ref>(
83           StringRefFromSlice(&arr_.metadata[i].key),
84           StringRefFromSlice(&arr_.metadata[i].value)));
85     }
86   }
87 };
88 }  // namespace internal
89 
90 }  // namespace grpc
91 
92 #endif  // GRPCPP_IMPL_CODEGEN_METADATA_MAP_H
93