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

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

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

45
	if (dots <= 2 && (*p == 0 || *p == '/'))
Max Kellermann's avatar
Max Kellermann committed
46
		return nullptr;
47

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

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

		if (*uri == 0)
			return true;

		assert(*uri == '/');

		++uri;
	}
}

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

79
	for (auto scheme : schemes) {
80
		auto result = StringAfterPrefixCaseASCII(uri, scheme);
81 82 83 84 85
		if (result != nullptr)
			return result;
	}

	return nullptr;
86 87 88
}

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

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

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

	/* duplicate the full URI and then delete the auth
	   information */
107 108 109
	std::string result(uri);
	result.erase(auth - uri, at + 1 - auth);
	return result;
110
}