|
| 1 | +#include "endpoint_guessing.h" |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | + |
| 5 | +namespace datadog::tracing { |
| 6 | + |
| 7 | +namespace { |
| 8 | + |
| 9 | +constexpr size_t MAX_COMPONENTS = 8; |
| 10 | + |
| 11 | +inline constexpr bool is_digit(char c) noexcept { return c >= '0' && c <= '9'; } |
| 12 | +inline constexpr bool is_hex_alpha(char c) noexcept { |
| 13 | + return (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); |
| 14 | +} |
| 15 | +inline constexpr bool is_delim(char c) noexcept { |
| 16 | + return c == '.' || c == '_' || c == '-'; |
| 17 | +} |
| 18 | +inline constexpr bool is_str_special(char c) noexcept { |
| 19 | + return c == '%' || c == '&' || c == '\'' || c == '(' || c == ')' || |
| 20 | + c == '*' || c == '+' || c == ',' || c == ':' || c == '=' || c == '@'; |
| 21 | +} |
| 22 | + |
| 23 | +/* |
| 24 | +clang-format off |
| 25 | +{param:int} [1-9][0-9]+ len≥2, digits only, first 1–9 |
| 26 | +{param:int_id} (?=.*[0-9])[0-9._-]{3,} len≥3, [0-9._-], must contain digit |
| 27 | +{param:hex} (?=.*[0-9])[A-Fa-f0-9]{6,} len≥6, hex digits, must contain decimal digit |
| 28 | +{param:hex_id} (?=.*[0-9])[A-Fa-f0-9._-]{6,} len≥6, hex+._-, must contain decimal digit |
| 29 | +{param:str} .{20,}|.*[%&'()*+,:=@].* any chars, valid if len≥20 or contains special |
| 30 | +clang-format on |
| 31 | +*/ |
| 32 | +enum component_type : std::uint8_t { |
| 33 | + none = 0, |
| 34 | + is_int = 1 << 0, |
| 35 | + is_int_id = 1 << 1, |
| 36 | + is_hex = 1 << 2, |
| 37 | + is_hex_id = 1 << 3, |
| 38 | + is_str = 1 << 4, |
| 39 | +}; |
| 40 | + |
| 41 | +std::string_view to_string(component_type type) noexcept { |
| 42 | + switch (type) { |
| 43 | + case component_type::is_int: |
| 44 | + return "{param:int}"; |
| 45 | + case component_type::is_int_id: |
| 46 | + return "{param:int_id}"; |
| 47 | + case component_type::is_hex: |
| 48 | + return "{param:hex}"; |
| 49 | + case component_type::is_hex_id: |
| 50 | + return "{param:hex_id}"; |
| 51 | + case component_type::is_str: |
| 52 | + return "{param:str}"; |
| 53 | + default: |
| 54 | + return ""; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +inline uint8_t bool2mask(bool x) noexcept { |
| 59 | + return static_cast<uint8_t>(-int{x}); // 0 -> 0x00, 1 -> 0xFF |
| 60 | +} |
| 61 | + |
| 62 | +component_type component_replacement(std::string_view path) noexcept { |
| 63 | + // viable_components is a bitset of the component types not yet excluded |
| 64 | + std::uint8_t viable_components = 0x1F; // (is_str << 1) - 1 |
| 65 | + bool found_special_char = false; |
| 66 | + bool found_digit = false; |
| 67 | + |
| 68 | + if (path.size() < 2) { |
| 69 | + viable_components &= ~(component_type::is_int | component_type::is_int_id | |
| 70 | + component_type::is_hex | component_type::is_hex_id); |
| 71 | + } else if (path.size() < 3) { |
| 72 | + viable_components &= ~(component_type::is_int_id | component_type::is_hex | |
| 73 | + component_type::is_hex_id); |
| 74 | + } else if (path.size() < 6) { |
| 75 | + viable_components &= ~(component_type::is_hex | component_type::is_hex_id); |
| 76 | + } |
| 77 | + |
| 78 | + // is_int does not allow a leading 0 |
| 79 | + if (!path.empty() && path[0] == '0') { |
| 80 | + viable_components &= ~component_type::is_int; |
| 81 | + } |
| 82 | + |
| 83 | + for (std::size_t i = 0; i < path.size(); ++i) { |
| 84 | + char c = path[i]; |
| 85 | + found_special_char = found_special_char || is_str_special(c); |
| 86 | + found_digit = found_digit || is_digit(c); |
| 87 | + |
| 88 | + std::uint8_t digit_mask = |
| 89 | + bool2mask(is_digit(c)) & |
| 90 | + (component_type::is_int | component_type::is_int_id | |
| 91 | + component_type::is_hex | component_type::is_hex_id); |
| 92 | + |
| 93 | + std::uint8_t hex_alpha_mask = |
| 94 | + bool2mask(is_hex_alpha(c)) & |
| 95 | + (component_type::is_hex | component_type::is_hex_id); |
| 96 | + |
| 97 | + std::uint8_t delimiter_mask = |
| 98 | + bool2mask(is_delim(c)) & |
| 99 | + (component_type::is_int_id | component_type::is_hex_id); |
| 100 | + |
| 101 | + viable_components &= |
| 102 | + (digit_mask | hex_alpha_mask | delimiter_mask | component_type::is_str); |
| 103 | + } |
| 104 | + |
| 105 | + // is_str requires a special char or a size >= 20 |
| 106 | + viable_components &= ~component_type::is_str | |
| 107 | + bool2mask(found_special_char || (path.size() >= 20)); |
| 108 | + // hex, and hex_id require a digit |
| 109 | + viable_components &= ~(component_type::is_hex | component_type::is_hex_id) | |
| 110 | + bool2mask(found_digit); |
| 111 | + |
| 112 | + if (viable_components == 0) { |
| 113 | + return component_type::none; |
| 114 | + } |
| 115 | + |
| 116 | + // c++20: use std::countr_zero |
| 117 | + std::uint8_t lsb = static_cast<std::uint8_t>( |
| 118 | + viable_components & |
| 119 | + static_cast<std::uint8_t>(-static_cast<int8_t>(viable_components))); |
| 120 | + return static_cast<component_type>(lsb); |
| 121 | +} |
| 122 | +} // namespace |
| 123 | + |
| 124 | +std::string guess_endpoint(std::string_view orig_path) { |
| 125 | + auto path = orig_path; |
| 126 | + |
| 127 | + // remove the query string if any |
| 128 | + auto query_pos = path.find('?'); |
| 129 | + if (query_pos != std::string_view::npos) { |
| 130 | + path = path.substr(0, query_pos); |
| 131 | + } |
| 132 | + |
| 133 | + if (path.empty() || path.front() != '/') { |
| 134 | + return "/"; |
| 135 | + } |
| 136 | + |
| 137 | + std::string result{}; |
| 138 | + size_t component_count = 0; |
| 139 | + |
| 140 | + path.remove_prefix(1); |
| 141 | + while (!path.empty()) { |
| 142 | + auto slash_pos = path.find('/'); |
| 143 | + |
| 144 | + std::string_view component = path.substr(0, slash_pos); |
| 145 | + |
| 146 | + // remove current component from the path |
| 147 | + if (slash_pos == std::string_view::npos) { |
| 148 | + path = std::string_view{}; |
| 149 | + } else { |
| 150 | + path.remove_prefix(slash_pos + 1); |
| 151 | + } |
| 152 | + |
| 153 | + if (component.empty()) { |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + result.append("/"); |
| 158 | + |
| 159 | + // replace the literal component with the appropriate placeholder |
| 160 | + // (if it matches one of the patterns) |
| 161 | + auto type = component_replacement(component); |
| 162 | + if (type == component_type::none) { |
| 163 | + result.append(component); |
| 164 | + } else { |
| 165 | + result.append(to_string(type)); |
| 166 | + } |
| 167 | + if (++component_count >= MAX_COMPONENTS) { |
| 168 | + break; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + if (result.empty()) { |
| 173 | + return "/"; |
| 174 | + } |
| 175 | + |
| 176 | + return result; |
| 177 | +} |
| 178 | + |
| 179 | +} // namespace datadog::tracing |
0 commit comments