mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-08 04:21:51 +00:00
url_encode: escape url query
For routes such as /api/auth/directory/search, when searching for names such as "Léo", jams would not respond to the request since the URL contained invalid characters defined in RFC3986. `url_encode` turns such characters into their equivalent escaped characters.
This commit is contained in:
1 parent
3e888d8e2d
commit
2f2ed9bca7
2 files changed
+31
-1
No files matched your search
+29
-1
@@ -33,6 +33,10 @@
|
||||
#include <openssl/x509.h>
|
||||
#include <openssl/x509_vfy.h>
|
||||
|
||||
#include <cctype>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#define MAXAGE_SEC (14*24*60*60)
|
||||
#define JITTER_SEC (60)
|
||||
#define OCSP_MAX_RESPONSE_SIZE (20480)
|
||||
@@ -1048,7 +1052,7 @@ Request::build()
|
||||
bool append_body = !body_.empty();
|
||||
|
||||
// first header
|
||||
request << header_.method().c_str() << " " << header_.request_target() << " " <<
|
||||
request << header_.method().c_str() << " " << url_encode(header_.request_target()) << " " <<
|
||||
"HTTP/" << header_.http_major() << "." << header_.http_minor() << "\r\n";
|
||||
|
||||
// other headers
|
||||
@@ -1084,6 +1088,30 @@ Request::build()
|
||||
request_ = request.str();
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/a/17708801
|
||||
static std::string
|
||||
Request::url_encode(const std::string& value)
|
||||
{
|
||||
std::ostringstream escaped;
|
||||
escaped.fill('0');
|
||||
escaped << std::hex;
|
||||
|
||||
for (const char& c : value) {
|
||||
// Keep alphanumeric and other accepted characters intact
|
||||
if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
|
||||
escaped << c;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Any other characters are percent-encoded
|
||||
escaped << std::uppercase;
|
||||
escaped << '%' << std::setw(2) << static_cast<int>(static_cast<unsigned char>(c));
|
||||
escaped << std::nouppercase;
|
||||
}
|
||||
|
||||
return escaped.str();
|
||||
}
|
||||
|
||||
void
|
||||
Request::add_on_status_callback(OnStatusCb cb) {
|
||||
cbs_.on_status = std::move(cb);
|
||||
|
||||
Reference in new issue
Block a user