You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

metadata_map.h 3.9 kB

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