UriRelative.cxx 4.97 KB
Newer Older
Max Kellermann's avatar
Max Kellermann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright 2008-2019 Max Kellermann <max.kellermann@gmail.com>
 *
 * 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"
31
#include "UriExtract.hxx"
32
#include "StringAPI.hxx"
33
#include "StringCompare.hxx"
Max Kellermann's avatar
Max Kellermann committed
34

35 36
#include <cassert>

Max Kellermann's avatar
Max Kellermann committed
37 38 39 40 41 42 43 44 45 46 47
#include <string.h>

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

48
	const char *suffix = StringAfterPrefix(child, parent);
49 50
	return suffix != nullptr && *suffix != 0 &&
		(suffix == child || suffix[-1] == '/' || *suffix == '/');
Max Kellermann's avatar
Max Kellermann committed
51 52 53 54 55 56
}


bool
uri_is_child_or_same(const char *parent, const char *child) noexcept
{
57
	return StringIsEqual(parent, child) || uri_is_child(parent, child);
Max Kellermann's avatar
Max Kellermann committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

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;
}
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

static void
ClearFilename(StringView &path) noexcept
{
	const char *slash = path.FindLast('/');
	if (slash != nullptr)
		path.SetEnd(slash + 1);
	else
		path.size = 0;
}

static bool
ConsumeLastSegment(StringView &path) noexcept
{
	assert(!path.empty());
	assert(path.back() == '/');

	path.pop_back();
	const char *slash = path.FindLast('/');
	if (slash == nullptr)
		return false;

	path.SetEnd(slash + 1);
	return true;
}

static bool
ConsumeSpecial(const char *&relative_path, StringView &base_path) noexcept
{
	while (true) {
		if (const char *a = StringAfterPrefix(relative_path, "./")) {
			while (*a == '/')
				++a;
			relative_path = a;
		} else if (const char *b = StringAfterPrefix(relative_path, "../")) {
			while (*b == '/')
				++b;
			relative_path = b;

			if (!ConsumeLastSegment(base_path))
				return false;
		} else if (StringIsEqual(relative_path, ".")) {
			++relative_path;
			return true;
		} else
			return true;
	}
}

std::string
uri_apply_relative(const std::string &relative_uri,
		   const std::string &base_uri) noexcept
{
	if (relative_uri.empty())
		return base_uri;

	if (uri_has_scheme(relative_uri.c_str()))
		return relative_uri;

	const char *relative_path = relative_uri.c_str();

	// TODO: support double slash at beginning of relative_uri
	if (relative_uri.front() == '/') {
		/* absolute path: replace the whole URI path in base */

		auto i = base_uri.find("://");
		if (i == base_uri.npos)
			/* no scheme: override base completely */
			return relative_uri;

		/* find the first slash after the host part */
		i = base_uri.find('/', i + 3);
		if (i == base_uri.npos)
			/* there's no URI path - simply append uri */
			i = base_uri.length();

		return base_uri.substr(0, i) + relative_uri;
	}

166 167
	const auto _base_path = uri_get_path(base_uri);
	if (_base_path.data() == nullptr) {
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		std::string result(base_uri);
		if (relative_uri.front() != '/')
			result.push_back('/');
		while (const char *a = StringAfterPrefix(relative_path, "./"))
			relative_path = a;
		if (StringStartsWith(relative_path, "../"))
			return {};
		if (!StringIsEqual(relative_path, "."))
			result += relative_uri;
		return result;
	}

	StringView base_path(_base_path);
	ClearFilename(base_path);

	if (!ConsumeSpecial(relative_path, base_path))
		return {};

186
	std::string result(base_uri.data(), _base_path.data());
187 188 189 190
	result.append(base_path.data, base_path.size);
	result.append(relative_path);
	return result;
}