| 3 | namespace tt::network { |
| 4 | |
| 5 | std::map<std::string, std::string> parseUrlQuery(std::string query) { |
| 6 | std::map<std::string, std::string> result; |
| 7 | |
| 8 | if (query.empty()) { |
| 9 | return result; |
| 10 | } |
| 11 | |
| 12 | size_t current_index = query[0] == '?' ? 1U : 0U; |
| 13 | auto equals_index = query.find_first_of('=', current_index); |
| 14 | while (equals_index != std::string::npos) { |
| 15 | auto index_boundary = query.find_first_of('&', equals_index + 1); |
| 16 | if (index_boundary == std::string::npos) { |
| 17 | index_boundary = query.size(); |
| 18 | } |
| 19 | auto key = query.substr(current_index, (equals_index - current_index)); |
| 20 | auto decodedKey = urlDecode(key); |
| 21 | auto value = query.substr(equals_index + 1, (index_boundary - equals_index - 1)); |
| 22 | auto decodedValue = urlDecode(value); |
| 23 | |
| 24 | result[decodedKey] = decodedValue; |
| 25 | |
| 26 | // Find next token |
| 27 | current_index = index_boundary + 1; |
| 28 | equals_index = query.find_first_of('=', current_index); |
| 29 | } |
| 30 | |
| 31 | return result; |
| 32 | } |
| 33 | |
| 34 | // Adapted from https://stackoverflow.com/a/29962178/3848666 |
| 35 | std::string urlEncode(const std::string& input) { |
no test coverage detected