|
9 | 9 |
|
10 | 10 | using namespace playapi; |
11 | 11 |
|
| 12 | +static std::string redact_url_query(const std::string& url) { |
| 13 | + auto pos = url.find('?'); |
| 14 | + if (pos == std::string::npos) return url; |
| 15 | + std::string base = url.substr(0, pos + 1); // include '?' |
| 16 | + std::string query = url.substr(pos + 1); |
| 17 | + std::string out; |
| 18 | + size_t i = 0; |
| 19 | + while (i < query.size()) { |
| 20 | + size_t amp = query.find('&', i); |
| 21 | + std::string part = (amp == std::string::npos) ? query.substr(i) : query.substr(i, amp - i); |
| 22 | + size_t eq = part.find('='); |
| 23 | + if (eq == std::string::npos) { |
| 24 | + // key without value |
| 25 | + out += part; |
| 26 | + } else { |
| 27 | + // keep key and '=' then redact value |
| 28 | + out += part.substr(0, eq + 1); |
| 29 | + out += "[redacted]"; |
| 30 | + } |
| 31 | + if (amp == std::string::npos) break; |
| 32 | + out += '&'; |
| 33 | + i = amp + 1; |
| 34 | + } |
| 35 | + return base + out; |
| 36 | +} |
| 37 | + |
12 | 38 | void url_encoded_entity::add_pair(const std::string& key, const std::string& val) { |
13 | 39 | pairs.push_back({key, val}); |
14 | 40 | } |
@@ -179,7 +205,7 @@ http_response http_request::perform() { |
179 | 205 | return http_response(curlerr, status, output.str()); |
180 | 206 | } else { |
181 | 207 | std::stringstream errormsg; |
182 | | - errormsg << "Failed to perform http request to " << url << " : CURLcode " << curlerr << " Details: " << errbuf; |
| 208 | + errormsg << "Failed to perform http request to " << redact_url_query(url) << " : CURLcode " << curlerr << " Details: " << errbuf; |
183 | 209 | curl_easy_cleanup(curl); |
184 | 210 | throw std::runtime_error(errormsg.str().data()); |
185 | 211 | } |
@@ -211,7 +237,7 @@ void http_request::perform(std::function<void(http_response)> success, std::func |
211 | 237 | success(http_response(curlerr, status, output.str())); |
212 | 238 | } else { |
213 | 239 | std::stringstream errormsg; |
214 | | - errormsg << "Failed to perform http request to " << req->url << " : CURLcode " << curlerr << " Details: " << errbuf; |
| 240 | + errormsg << "Failed to perform http request to " << redact_url_query(req->url) << " : CURLcode " << curlerr << " Details: " << errbuf; |
215 | 241 | try { |
216 | 242 | throw std::runtime_error(errormsg.str().data()); |
217 | 243 | } catch (...) { |
|
0 commit comments