Skip to content
Snippets Groups Projects
Commit e6ab5866 authored by Konstantinos Sideris's avatar Konstantinos Sideris
Browse files

Add URL encoding for query params

parent dbb47233
No related branches found
No related tags found
No related merge requests found
......@@ -39,6 +39,10 @@ query_params(const std::map<std::string, std::string> &params) noexcept;
std::string
decompress(const boost::iostreams::array_source &src, const std::string &type) noexcept;
//! URL-encode the input string.
std::string
url_encode(const std::string &s) noexcept;
template<class T>
inline T
deserialize(const std::string &data)
......
......@@ -78,14 +78,14 @@ mtx::client::utils::query_params(const std::map<std::string, std::string> &param
auto pb = params.cbegin();
auto pe = params.cend();
std::string data = pb->first + "=" + pb->second;
std::string data = pb->first + "=" + url_encode(pb->second);
++pb;
if (pb == pe)
return data;
for (; pb != pe; ++pb)
data += "&" + pb->first + "=" + pb->second;
data += "&" + pb->first + "=" + url_encode(pb->second);
return data;
}
......@@ -109,3 +109,29 @@ mtx::client::utils::decompress(const boost::iostreams::array_source &src,
return decompressed.str();
}
std::string
mtx::client::utils::url_encode(const std::string &value) noexcept
{
// https: // stackoverflow.com/questions/154536/encode-decode-urls-in-c
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (auto i = value.begin(), n = value.end(); i != n; ++i) {
std::string::value_type c = (*i);
// 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) << int((unsigned char)c);
escaped << std::nouppercase;
}
return escaped.str();
}
......@@ -104,6 +104,13 @@ TEST(MediaAPI, UploadImage)
const auto img = read_file("./fixtures/test.jpeg");
carl->upload(img,
"image/jpeg",
"a name that needs to be encode/d.jpeg",
[carl, img](const mtx::responses::ContentURI &res, RequestErr err) {
validate_upload(res, err);
});
carl->upload(
img,
"image/jpeg",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment