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