2023-03-22 23:05:18 +01:00
|
|
|
#include <hex/helpers/http_requests.hpp>
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
#include <hex/helpers/crypto.hpp>
|
2023-03-22 23:05:18 +01:00
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
namespace hex {
|
2023-03-22 23:05:18 +01:00
|
|
|
|
2023-03-23 11:23:07 +01:00
|
|
|
size_t HttpRequest::writeToVector(void *contents, size_t size, size_t nmemb, void *userdata) {
|
2023-11-10 20:47:08 +01:00
|
|
|
auto &response = *static_cast<std::vector<u8>*>(userdata);
|
2023-03-23 11:23:07 +01:00
|
|
|
auto startSize = response.size();
|
|
|
|
|
|
|
|
response.resize(startSize + size * nmemb);
|
|
|
|
std::memcpy(response.data() + startSize, contents, size * nmemb);
|
|
|
|
|
|
|
|
return size * nmemb;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HttpRequest::writeToFile(void *contents, size_t size, size_t nmemb, void *userdata) {
|
2023-11-10 20:47:08 +01:00
|
|
|
auto &file = *static_cast<wolv::io::File*>(userdata);
|
2023-03-23 11:23:07 +01:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
file.writeBuffer(static_cast<const u8*>(contents), size * nmemb);
|
2023-03-23 11:23:07 +01:00
|
|
|
|
|
|
|
return size * nmemb;
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
std::string HttpRequest::urlEncode(const std::string &input) {
|
|
|
|
std::string result;
|
|
|
|
for (char c : input){
|
|
|
|
if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
|
|
|
|
result += c;
|
|
|
|
else
|
|
|
|
result += hex::format("%02X", c);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string HttpRequest::urlDecode(const std::string &input) {
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
for (u32 i = 0; i < input.size(); i++){
|
|
|
|
if (input[i] != '%'){
|
|
|
|
if (input[i] == '+')
|
|
|
|
result += ' ';
|
|
|
|
else
|
|
|
|
result += input[i];
|
|
|
|
} else {
|
|
|
|
const auto hex = crypt::decode16(input.substr(i + 1, 2));
|
|
|
|
if (hex.empty())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
result += char(hex[0]);
|
|
|
|
i += 2;
|
|
|
|
}
|
2023-07-26 13:50:51 +02:00
|
|
|
}
|
2023-11-17 13:13:17 +01:00
|
|
|
return result;
|
2023-07-26 13:50:51 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 23:05:18 +01:00
|
|
|
}
|