Repos
C++
1.1 KB38 lines Opens in site
Directory
Raw
Download
#include "core/UrlCodec.hpp"
namespace dp {
std::string UrlCodec::encode(const std::string& text) {
static const char* hex = "0123456789ABCDEF";
std::string out;
out.reserve(text.size());
for (unsigned char c : text) {
const bool keep = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' ||
c == '.' || c == '_' || c == '~';
if (keep) {
out.push_back(static_cast<char>(c));
} else {
out.push_back('%');
out.push_back(hex[c >> 4]);
out.push_back(hex[c & 15]);
}
}
return out;
}
std::string UrlCodec::buildQuery(const std::vector<std::pair<std::string, std::string>>& params) {
std::string out;
for (const auto& [k, v] : params) {
if (!out.empty()) out.push_back('&');
out += encode(k) + "=" + encode(v);
}
return out;
}
std::string UrlCodec::toWebSocketScheme(const std::string& url) {
if (url.rfind("https://", 0) == 0) return "wss://" + url.substr(8);
if (url.rfind("http://", 0) == 0) return "ws://" + url.substr(7);
return url;
}
} // namespace dp