/* * Copyright 2008-2019 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "UriRelative.hxx" #include "StringAPI.hxx" #include #include bool uri_is_child(const char *parent, const char *child) noexcept { #if !CLANG_CHECK_VERSION(3,6) /* disabled on clang due to -Wtautological-pointer-compare */ assert(parent != nullptr); assert(child != nullptr); #endif const size_t parent_length = strlen(parent); return memcmp(parent, child, parent_length) == 0 && child[parent_length] == '/'; } bool uri_is_child_or_same(const char *parent, const char *child) noexcept { return StringIsEqual(parent, child) || uri_is_child(parent, child); } std::string uri_apply_base(const std::string &uri, const std::string &base) noexcept { 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; }