metadata_map.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_METADATA_MAP_H
  19. #define GRPCPP_IMPL_METADATA_MAP_H
  20. #include <map>
  21. #include <grpc/grpc.h>
  22. #include <grpc/support/log.h>
  23. #include <grpcpp/support/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. TString 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 TString(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 TString(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 TString();
  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() { grpc_metadata_array_destroy(&arr_); }
  73. void Setup() { memset(&arr_, 0, sizeof(arr_)); }
  74. void FillMap() {
  75. if (filled_) return;
  76. filled_ = true;
  77. for (size_t i = 0; i < arr_.count; i++) {
  78. // TODO(yangg) handle duplicates?
  79. map_.insert(std::pair<grpc::string_ref, grpc::string_ref>(
  80. StringRefFromSlice(&arr_.metadata[i].key),
  81. StringRefFromSlice(&arr_.metadata[i].value)));
  82. }
  83. }
  84. };
  85. } // namespace internal
  86. } // namespace grpc
  87. #endif // GRPCPP_IMPL_METADATA_MAP_H