From 464767c5fd0671b93f97f79905bdfb987826f6ef Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 26 Jan 2014 11:37:52 +0100 Subject: db/upnp/Util: move caturl() to util/UriUtil.cxx --- src/util/UriUtil.cxx | 28 ++++++++++++++++++++++++++++ src/util/UriUtil.hxx | 8 ++++++++ 2 files changed, 36 insertions(+) (limited to 'src/util') 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 -- cgit v1.2.3