Something went wrong. Try again.
A fork of https://github.com/crosspoint-reader/crosspoint-reader
Something went wrong. Try again.
1.6 kB · 52 lines
C++
at master
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253#include "UrlUtils.h"
namespace UrlUtils {
bool isHttpsUrl(const std::string& url) { return url.rfind("https://", 0) == 0; }
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) { // If path is already an absolute URL (has protocol), use it directly if (path.find("://") != std::string::npos) { return 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 - strip query string from base before appending std::string base = urlWithProtocol; const size_t queryPos = base.find('?'); if (queryPos != std::string::npos) { base.resize(queryPos); } if (base.back() == '/') { return base + path; } return base + "/" + path;}
} // namespace UrlUtils