UriUtil.cxx 2.64 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

Max Kellermann's avatar
Max Kellermann committed
20
#include "UriUtil.hxx"
21

22
#include <assert.h>
23 24 25 26
#include <string.h>

bool uri_has_scheme(const char *uri)
{
Max Kellermann's avatar
Max Kellermann committed
27
	return strstr(uri, "://") != nullptr;
28 29 30 31 32 33
}

/* suffixes should be ascii only characters */
const char *
uri_get_suffix(const char *uri)
{
34
	const char *suffix = strrchr(uri, '.');
Max Kellermann's avatar
Max Kellermann committed
35 36
	if (suffix == nullptr)
		return nullptr;
37 38 39

	++suffix;

Max Kellermann's avatar
Max Kellermann committed
40 41
	if (strpbrk(suffix, "/\\") != nullptr)
		return nullptr;
42

43
	return suffix;
44
}
45

46 47 48 49 50
static const char *
verify_uri_segment(const char *p)
{
	const char *q;

51
	unsigned dots = 0;
52
	while (*p == '.') {
53
		++p;
54 55 56
		++dots;
	}

57
	if (dots <= 2 && (*p == 0 || *p == '/'))
Max Kellermann's avatar
Max Kellermann committed
58
		return nullptr;
59 60

	q = strchr(p + 1, '/');
Max Kellermann's avatar
Max Kellermann committed
61
	return q != nullptr ? q : "";
62 63 64 65 66 67 68
}

bool
uri_safe_local(const char *uri)
{
	while (true) {
		uri = verify_uri_segment(uri);
Max Kellermann's avatar
Max Kellermann committed
69
		if (uri == nullptr)
70 71 72 73 74 75 76 77 78 79 80
			return false;

		if (*uri == 0)
			return true;

		assert(*uri == '/');

		++uri;
	}
}

81
std::string
82 83 84 85
uri_remove_auth(const char *uri)
{
	const char *auth, *slash, *at;

86
	if (memcmp(uri, "http://", 7) == 0)
87
		auth = uri + 7;
88
	else if (memcmp(uri, "https://", 8) == 0)
89 90 91
		auth = uri + 8;
	else
		/* unrecognized URI */
92
		return std::string();
93 94

	slash = strchr(auth, '/');
Max Kellermann's avatar
Max Kellermann committed
95
	if (slash == nullptr)
96 97
		slash = auth + strlen(auth);

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

	/* duplicate the full URI and then delete the auth
	   information */
105 106 107
	std::string result(uri);
	result.erase(auth - uri, at + 1 - auth);
	return result;
108
}
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

bool
uri_is_child(const char *parent, const char *child)
{
	assert(parent != nullptr);
	assert(child != nullptr);

	const size_t parent_length = strlen(parent);
	return memcmp(parent, child, parent_length) == 0 &&
		child[parent_length] == '/';
}


bool
uri_is_child_or_same(const char *parent, const char *child)
{
	return strcmp(parent, child) == 0 || uri_is_child(parent, child);
}