|
| 1 | +// Copyright 2019, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "opencensus/tags/propagation/grpc_tags_bin.h" |
| 16 | + |
| 17 | +#include <cstdint> |
| 18 | +#include <string> |
| 19 | +#include <unordered_map> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +#include "absl/strings/string_view.h" |
| 23 | +#include "opencensus/tags/tag_key.h" |
| 24 | +#include "opencensus/tags/tag_map.h" |
| 25 | + |
| 26 | +namespace opencensus { |
| 27 | +namespace tags { |
| 28 | +namespace propagation { |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +constexpr char kVersionId = '\0'; |
| 33 | +constexpr char kTagFieldId = '\0'; |
| 34 | +constexpr int kMaxLen = 8192; |
| 35 | + |
| 36 | +// Appends a variable-length encoded integer to the destination string. |
| 37 | +void AppendVarint(unsigned int i, std::string* out) { |
| 38 | + do { |
| 39 | + // Encode 7 bits. |
| 40 | + uint8_t c = i & 0x7F; |
| 41 | + i = i >> 7; |
| 42 | + if (i != 0) { |
| 43 | + c |= 0x80; |
| 44 | + } |
| 45 | + out->push_back(c); |
| 46 | + } while (i != 0); |
| 47 | +} |
| 48 | + |
| 49 | +// Parses a variable-length encoded integer from the input. Returns false on |
| 50 | +// failure. Returns true and consumes the bytes from the input, on success. |
| 51 | +bool ParseVarint(absl::string_view* input, int* out) { |
| 52 | + absl::string_view s = *input; |
| 53 | + int i = 0; |
| 54 | + uint8_t c; |
| 55 | + do { |
| 56 | + if (s.empty()) { |
| 57 | + return false; // Too short. |
| 58 | + } |
| 59 | + c = s[0]; |
| 60 | + s = s.substr(1); |
| 61 | + i = (i << 7) | (c & 0x7F); |
| 62 | + } while (c & 0x80); |
| 63 | + *input = s; |
| 64 | + *out = i; |
| 65 | + return true; |
| 66 | +} |
| 67 | + |
| 68 | +} // namespace |
| 69 | + |
| 70 | +bool FromGrpcTagsBinHeader(absl::string_view header, TagMap* out) { |
| 71 | + std::unordered_map<std::string, absl::string_view> keys_vals; |
| 72 | + if (header.length() < 1) { |
| 73 | + return false; // Too short. |
| 74 | + } |
| 75 | + if (header.length() > kMaxLen) { |
| 76 | + return false; // Too long. |
| 77 | + } |
| 78 | + if (header[0] != kVersionId) { |
| 79 | + return false; // Wrong version. |
| 80 | + } |
| 81 | + header = header.substr(1); |
| 82 | + while (header.length() > 0) { |
| 83 | + // Parse tag field id. |
| 84 | + if (header[0] != kTagFieldId) { |
| 85 | + return false; // Wrong field id. |
| 86 | + } |
| 87 | + header = header.substr(1); |
| 88 | + |
| 89 | + // Parse key. |
| 90 | + absl::string_view key; |
| 91 | + { |
| 92 | + int key_len; |
| 93 | + if (!ParseVarint(&header, &key_len)) { |
| 94 | + return false; // Invalid key_len. |
| 95 | + } |
| 96 | + if (key_len > header.length()) { |
| 97 | + return false; // Key len longer than remaining buffer. |
| 98 | + } |
| 99 | + key = header.substr(0, key_len); |
| 100 | + header = header.substr(key_len); |
| 101 | + } |
| 102 | + |
| 103 | + // Parse val. |
| 104 | + absl::string_view val; |
| 105 | + { |
| 106 | + int val_len; |
| 107 | + if (!ParseVarint(&header, &val_len)) { |
| 108 | + return false; // Invalid val_len. |
| 109 | + } |
| 110 | + if (val_len > header.length()) { |
| 111 | + return false; // Val len longer than remaining buffer. |
| 112 | + } |
| 113 | + val = header.substr(0, val_len); |
| 114 | + header = header.substr(val_len); |
| 115 | + } |
| 116 | + |
| 117 | + // Drop empty keys. |
| 118 | + if (!key.empty()) { |
| 119 | + // For duplicate keys, last wins. |
| 120 | + keys_vals[std::string(key)] = val; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Convert to tagmap. |
| 125 | + std::vector<std::pair<opencensus::tags::TagKey, std::string>> tags; |
| 126 | + tags.reserve(keys_vals.size()); |
| 127 | + for (const auto& kv : keys_vals) { |
| 128 | + tags.emplace_back(TagKey::Register(kv.first), std::string(kv.second)); |
| 129 | + } |
| 130 | + *out = TagMap(std::move(tags)); |
| 131 | + return true; |
| 132 | +} |
| 133 | + |
| 134 | +std::string ToGrpcTagsBinHeader(const TagMap& tags) { |
| 135 | + std::string out; |
| 136 | + out.push_back(kVersionId); |
| 137 | + for (const auto& key_val : tags.tags()) { |
| 138 | + const auto& key = key_val.first; |
| 139 | + const auto& val = key_val.second; |
| 140 | + out.push_back(kTagFieldId); |
| 141 | + AppendVarint(key.name().length(), &out); |
| 142 | + out.append(key.name()); |
| 143 | + AppendVarint(val.length(), &out); |
| 144 | + // Encoded value must be UTF-8. |
| 145 | + out.append(val); |
| 146 | + if (out.size() > kMaxLen) { |
| 147 | + break; |
| 148 | + } |
| 149 | + } |
| 150 | + if (out.size() > kMaxLen) { |
| 151 | + return ""; |
| 152 | + } |
| 153 | + return out; |
| 154 | +} |
| 155 | + |
| 156 | +} // namespace propagation |
| 157 | +} // namespace tags |
| 158 | +} // namespace opencensus |
0 commit comments