db/upnp/Util: move caturl() to util/UriUtil.cxx

This commit is contained in:
Max Kellermann
2014-01-26 11:37:52 +01:00
parent a9c3ca8606
commit 464767c5fd
6 changed files with 39 additions and 32 deletions

View File

@@ -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;
}

View File

@@ -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