UriUtil.cxx 3.39 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

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

38 39
#include <string_view>

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

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

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

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

		if (*uri == 0)
			return true;

		assert(*uri == '/');

		++uri;
	}
}

73 74
gcc_pure
static const char *
75
SkipUriScheme(const char *uri) noexcept
76
{
Rosen Penev's avatar
Rosen Penev committed
77
	static constexpr auto schemes = std::array {
78 79
		"http://", "https://",
		"ftp://",
80
		"smb://",
81 82
	};

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

	return nullptr;
90 91 92
}

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

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

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

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

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