3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPCPP_IMPL_CODEGEN_METADATA_MAP_H
20 #define GRPCPP_IMPL_CODEGEN_METADATA_MAP_H
24 #include <grpc/impl/codegen/log.h>
25 #include <grpcpp/impl/codegen/slice.h>
31 const char kBinaryErrorDetailsKey[] = "grpc-status-details-bin";
35 MetadataMap() { Setup(); }
37 ~MetadataMap() { Destroy(); }
39 std::string GetBinaryErrorDetails() {
40 // if filled_, extract from the multimap for O(log(n))
42 auto iter = map_.find(kBinaryErrorDetailsKey);
43 if (iter != map_.end()) {
44 return std::string(iter->second.begin(), iter->second.length());
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.
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));
66 std::multimap<grpc::string_ref, grpc::string_ref>* map() {
70 grpc_metadata_array* arr() { return &arr_; }
81 grpc_metadata_array arr_;
82 std::multimap<grpc::string_ref, grpc::string_ref> map_;
85 g_core_codegen_interface->grpc_metadata_array_destroy(&arr_);
88 void Setup() { memset(&arr_, 0, sizeof(arr_)); }
93 for (size_t i = 0; i < arr_.count; i++) {
94 // TODO(yangg) handle duplicates?
95 map_.insert(std::pair<grpc::string_ref, grpc::string_ref>(
96 StringRefFromSlice(&arr_.metadata[i].key),
97 StringRefFromSlice(&arr_.metadata[i].value)));
101 } // namespace internal
105 #endif // GRPCPP_IMPL_CODEGEN_METADATA_MAP_H