42 lines
1.2 KiB
C++
42 lines
1.2 KiB
C++
|
|
#include "UrlUtils.h"
|
||
|
|
|
||
|
|
namespace UrlUtils {
|
||
|
|
|
||
|
|
std::string ensureProtocol(const std::string& url) {
|
||
|
|
if (url.find("://") == std::string::npos) {
|
||
|
|
return "http://" + url;
|
||
|
|
}
|
||
|
|
return url;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string extractHost(const std::string& url) {
|
||
|
|
const size_t protocolEnd = url.find("://");
|
||
|
|
if (protocolEnd == std::string::npos) {
|
||
|
|
// No protocol, find first slash
|
||
|
|
const size_t firstSlash = url.find('/');
|
||
|
|
return firstSlash == std::string::npos ? url : url.substr(0, firstSlash);
|
||
|
|
}
|
||
|
|
// Find the first slash after the protocol
|
||
|
|
const size_t hostStart = protocolEnd + 3;
|
||
|
|
const size_t pathStart = url.find('/', hostStart);
|
||
|
|
return pathStart == std::string::npos ? url : url.substr(0, pathStart);
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string buildUrl(const std::string& serverUrl, const std::string& path) {
|
||
|
|
const std::string urlWithProtocol = ensureProtocol(serverUrl);
|
||
|
|
if (path.empty()) {
|
||
|
|
return urlWithProtocol;
|
||
|
|
}
|
||
|
|
if (path[0] == '/') {
|
||
|
|
// Absolute path - use just the host
|
||
|
|
return extractHost(urlWithProtocol) + path;
|
||
|
|
}
|
||
|
|
// Relative path - append to server URL
|
||
|
|
if (urlWithProtocol.back() == '/') {
|
||
|
|
return urlWithProtocol + path;
|
||
|
|
}
|
||
|
|
return urlWithProtocol + "/" + path;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace UrlUtils
|