metadata_map.h 2.9 KB

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