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
#include <assert.h>
34 35
#include <string.h>

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

48
	const char *q = 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
	const char *const schemes[] = { "http://", "https://", "ftp://" };
	for (auto scheme : schemes) {
75
		auto result = StringAfterPrefixCaseASCII(uri, scheme);
76 77 78 79 80
		if (result != nullptr)
			return result;
	}

	return nullptr;
81 82 83
}

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

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

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

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