UriUtil.cxx 2.73 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 34
#include <cassert>

35 36
#include <string.h>

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

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

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

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

		if (*uri == 0)
			return true;

		assert(*uri == '/');

		++uri;
	}
}

70 71
gcc_pure
static const char *
72
SkipUriScheme(const char *uri) noexcept
73
{
74 75
	const char *const schemes[] = { "http://", "https://", "ftp://" };
	for (auto scheme : schemes) {
76
		auto result = StringAfterPrefixCaseASCII(uri, scheme);
77 78 79 80 81
		if (result != nullptr)
			return result;
	}

	return nullptr;
82 83 84
}

std::string
85
uri_remove_auth(const char *uri) noexcept
86 87 88
{
	const char *auth = SkipUriScheme(uri);
	if (auth == nullptr)
89
		/* unrecognized URI */
90
		return std::string();
91

92
	const char *slash = strchr(auth, '/');
Max Kellermann's avatar
Max Kellermann committed
93
	if (slash == nullptr)
94 95
		slash = auth + strlen(auth);

96
	const char *at = (const char *)memchr(auth, '@', slash - auth);
Max Kellermann's avatar
Max Kellermann committed
97
	if (at == nullptr)
98
		/* no auth info present, do nothing */
99
		return std::string();
100 101 102

	/* duplicate the full URI and then delete the auth
	   information */
103 104 105
	std::string result(uri);
	result.erase(auth - uri, at + 1 - auth);
	return result;
106
}