diff options
Diffstat (limited to '')
-rw-r--r-- | src/util/UriUtil.cxx | 28 | ||||
-rw-r--r-- | src/util/UriUtil.hxx | 8 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/util/UriUtil.cxx b/src/util/UriUtil.cxx index a549f7938..fdca47c00 100644 --- a/src/util/UriUtil.cxx +++ b/src/util/UriUtil.cxx @@ -137,3 +137,31 @@ uri_is_child_or_same(const char *parent, const char *child) { return strcmp(parent, child) == 0 || uri_is_child(parent, child); } + +std::string +uri_apply_base(const std::string &uri, const std::string &base) +{ + if (uri.front() == '/') { + /* absolute path: replace the whole URI path in base */ + + auto i = base.find("://"); + if (i == base.npos) + /* no scheme: override base completely */ + return uri; + + /* find the first slash after the host part */ + i = base.find('/', i + 3); + if (i == base.npos) + /* there's no URI path - simply append uri */ + i = base.length(); + + return base.substr(0, i) + uri; + } + + std::string out(base); + if (out.back() != '/') + out.push_back('/'); + + out += uri; + return out; +} diff --git a/src/util/UriUtil.hxx b/src/util/UriUtil.hxx index 8e00f8cd8..c2cc97a63 100644 --- a/src/util/UriUtil.hxx +++ b/src/util/UriUtil.hxx @@ -77,4 +77,12 @@ gcc_pure gcc_nonnull_all bool uri_is_child_or_same(const char *parent, const char *child); +/** + * Translate the given URI in the context of #base. For example, + * uri_apply_base("foo", "http://bar/a/")=="http://bar/a/foo". + */ +gcc_pure +std::string +uri_apply_base(const std::string &uri, const std::string &base); + #endif |