Encodes the input string as a URL-encoded string based on UTF-8. If 'hive_compat' is set to true, the string is encoded in a Hive-compatible way; otherwise, a more standard URL encoding is used, similar to the URLEncoder.encode() method in Java.
| 58 | // otherwise, a more standard URL encoding is used, similar to the URLEncoder.encode() |
| 59 | // method in Java. |
| 60 | static inline void UrlEncode(const char* in, int in_len, string* out, bool hive_compat) { |
| 61 | stringstream ss; |
| 62 | // "uppercase" and "hex" only affect the insertion of integers, not that of char values. |
| 63 | ss << uppercase << hex << setfill('0'); |
| 64 | for (char ch : std::string_view(in, in_len)) { |
| 65 | // Escape the character iff |
| 66 | // a) we are in Hive-compat mode and the character is in the Hive whitelist or |
| 67 | // b) we are not in Hive-compat mode and the character is not alphanumeric |
| 68 | // and it is not safe to use in URLs (see IsUrlSafe()). |
| 69 | if ((hive_compat && SpecialCharacters.count(ch) > 0) || (!hive_compat && |
| 70 | !isalnum(static_cast<unsigned char>(ch)) && !IsUrlSafe(ch))) { |
| 71 | // Iff we are not in Hive-compat mode, we encode space as '+'. |
| 72 | if (!hive_compat && ch == ' ') { |
| 73 | ss << '+'; |
| 74 | } else { |
| 75 | ss << '%' << setw(2) << static_cast<uint32_t>(static_cast<unsigned char>(ch)); |
| 76 | } |
| 77 | } else { |
| 78 | ss << ch; |
| 79 | } |
| 80 | } |
| 81 | (*out) = ss.str(); |
| 82 | } |
| 83 | |
| 84 | void UrlEncode(const vector<uint8_t>& in, string* out, bool hive_compat) { |
| 85 | if (in.empty()) { |