Added HTTP::URL::getBase() and HTTP::URL::getLinkFrom() functions

Change-Id: I87dcefb4287e8c5c22a4ae59898cb97371c441de
This commit is contained in:
Thulinma 2022-12-26 13:20:34 +01:00
parent 05afba74aa
commit 9c0b0e28d8
2 changed files with 29 additions and 9 deletions

View file

@ -271,6 +271,30 @@ std::string HTTP::URL::getBareUrl() const{
return ret; return ret;
} }
/// Returns a string guaranteed to end in a slash, pointing to the base directory-equivalent of the URL
std::string HTTP::URL::getBase() const{
std::string tmpUrl = getBareUrl();
size_t slashPos = tmpUrl.rfind('/');
if (slashPos == std::string::npos){
tmpUrl += "/";
}else{
tmpUrl.erase(slashPos + 1);
}
return tmpUrl;
}
/// Returns a string that links to the URL from the given URL
/// If the given URL is in a parent directory of the URL, it will be relative
/// Otherwise, it will be absolute
std::string HTTP::URL::getLinkFrom(const HTTP::URL & fromUrl) const{
std::string to = getUrl();
std::string from = fromUrl.getBase();
if (to.substr(0, from.size()) == from){
return to.substr(from.size());
}
return to;
}
/// Returns a URL object for the given link, resolved relative to the current URL object. /// Returns a URL object for the given link, resolved relative to the current URL object.
HTTP::URL HTTP::URL::link(const std::string &l) const{ HTTP::URL HTTP::URL::link(const std::string &l) const{
// Full link // Full link
@ -294,13 +318,7 @@ HTTP::URL HTTP::URL::link(const std::string &l) const{
} }
} }
// Relative link // Relative link
std::string tmpUrl = getBareUrl(); std::string base = getBase();
size_t slashPos = tmpUrl.rfind('/'); DONTEVEN_MSG("Relative link: %s+%s", base.c_str(), l.c_str());
if (slashPos == std::string::npos){ return URL(base + l);
tmpUrl += "/";
}else{
tmpUrl.erase(slashPos + 1);
}
DONTEVEN_MSG("Relative link: %s+%s", tmpUrl.c_str(), l.c_str());
return URL(tmpUrl + l);
} }

View file

@ -18,8 +18,10 @@ namespace HTTP{
std::string getExt() const; std::string getExt() const;
std::string getUrl() const; std::string getUrl() const;
std::string getFilePath() const; std::string getFilePath() const;
std::string getBase() const;
std::string getBareUrl() const; std::string getBareUrl() const;
std::string getProxyUrl() const; std::string getProxyUrl() const;
std::string getLinkFrom(const URL &) const;
bool isLocalPath() const; bool isLocalPath() const;
std::string host; ///< Hostname or IP address of URL std::string host; ///< Hostname or IP address of URL
std::string protocol; ///< Protocol of URL std::string protocol; ///< Protocol of URL