UriUtil.cxx 3.4 KB
Newer Older
1
/*
2
 * Copyright 2008-2019 Max Kellermann <max.kellermann@gmail.com>
3
 *
4 5 6
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
7
 *
8 9
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
10
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * - 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.
28 29
 */

Max Kellermann's avatar
Max Kellermann committed
30
#include "UriUtil.hxx"
31
#include "ASCII.hxx"
32
#include "SplitString.hxx"
33

34
#include <cassert>
Rosen Penev's avatar
Rosen Penev committed
35
#include <cstring>
36

37 38
#include <string_view>

39
static const char *
40
verify_uri_segment(const char *p) noexcept
41
{
42
	unsigned dots = 0;
43
	while (*p == '.') {
44
		++p;
45 46 47
		++dots;
	}

48
	if (dots <= 2 && (*p == 0 || *p == '/'))
Max Kellermann's avatar
Max Kellermann committed
49
		return nullptr;
50

Rosen Penev's avatar
Rosen Penev committed
51
	const char *q = std::strchr(p + 1, '/');
Max Kellermann's avatar
Max Kellermann committed
52
	return q != nullptr ? q : "";
53 54 55
}

bool
56
uri_safe_local(const char *uri) noexcept
57 58 59
{
	while (true) {
		uri = verify_uri_segment(uri);
Max Kellermann's avatar
Max Kellermann committed
60
		if (uri == nullptr)
61 62 63 64 65 66 67 68 69 70 71
			return false;

		if (*uri == 0)
			return true;

		assert(*uri == '/');

		++uri;
	}
}

72 73
gcc_pure
static const char *
74
SkipUriScheme(const char *uri) noexcept
75
{
76 77 78
	static const char *const schemes[] = {
		"http://", "https://",
		"ftp://",
79
		"smb://",
80 81
	};

82
	for (auto scheme : schemes) {
83
		auto result = StringAfterPrefixCaseASCII(uri, scheme);
84 85 86 87 88
		if (result != nullptr)
			return result;
	}

	return nullptr;
89 90 91
}

std::string
92
uri_remove_auth(const char *uri) noexcept
93 94 95
{
	const char *auth = SkipUriScheme(uri);
	if (auth == nullptr)
96
		/* unrecognized URI */
97
		return std::string();
98

Rosen Penev's avatar
Rosen Penev committed
99
	const char *slash = std::strchr(auth, '/');
Max Kellermann's avatar
Max Kellermann committed
100
	if (slash == nullptr)
101 102
		slash = auth + strlen(auth);

Rosen Penev's avatar
Rosen Penev committed
103
	const char *at = (const char *)std::memchr(auth, '@', slash - auth);
Max Kellermann's avatar
Max Kellermann committed
104
	if (at == nullptr)
105
		/* no auth info present, do nothing */
106
		return std::string();
107 108 109

	/* duplicate the full URI and then delete the auth
	   information */
110 111 112
	std::string result(uri);
	result.erase(auth - uri, at + 1 - auth);
	return result;
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

std::string
uri_squash_dot_segments(const char *uri) noexcept
{
	std::forward_list<std::string_view> path = SplitString(std::string_view(uri), '/');
	path.remove_if([](const std::string_view &seg) { return seg == "."; });
	path.reverse();

	std::string result;

	int segskips = 0;
	auto it = path.begin();
	while (it != path.end()) {
		if (*it == "..") {
			segskips++;
			it++;
			continue;
		} else if (segskips != 0) {
			segskips--;
			it++;
			continue;
		}

		result.insert(0, *it);

		if (it != path.begin()) {
			result.insert(it->length(), "/");
		}

		it++;
	}

	return result;
}