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 <grpc/grpc.h> 23 #include <grpcpp/support/slice.h> 24 25 #include <map> 26 27 namespace grpc { 28 29 namespace internal { 30 31 const char kBinaryErrorDetailsKey[] = "grpc-status-details-bin"; 32 33 class MetadataMap { 34 public: MetadataMap()35 MetadataMap() { Setup(); } 36 ~MetadataMap()37 ~MetadataMap() { Destroy(); } 38 GetBinaryErrorDetails()39 std::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 std::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 std::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 std::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 Reset()72 void Reset() { 73 filled_ = false; 74 map_.clear(); 75 Destroy(); 76 Setup(); 77 } 78 79 private: 80 bool filled_ = false; 81 grpc_metadata_array arr_; 82 std::multimap<grpc::string_ref, grpc::string_ref> map_; 83 Destroy()84 void Destroy() { grpc_metadata_array_destroy(&arr_); } 85 Setup()86 void Setup() { memset(&arr_, 0, sizeof(arr_)); } 87 FillMap()88 void FillMap() { 89 if (filled_) return; 90 filled_ = true; 91 for (size_t i = 0; i < arr_.count; i++) { 92 // TODO(yangg) handle duplicates? 93 map_.insert(std::pair<grpc::string_ref, grpc::string_ref>( 94 StringRefFromSlice(&arr_.metadata[i].key), 95 StringRefFromSlice(&arr_.metadata[i].value))); 96 } 97 } 98 }; 99 } // namespace internal 100 101 } // namespace grpc 102 103 #endif // GRPCPP_IMPL_METADATA_MAP_H 104