2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2009-02-25 16:44:06 +01:00
|
|
|
|
2013-04-08 23:30:21 +02:00
|
|
|
#include "UriUtil.hxx"
|
2018-08-02 10:38:20 +02:00
|
|
|
#include "ASCII.hxx"
|
2020-06-28 02:14:07 +02:00
|
|
|
#include "SplitString.hxx"
|
2009-02-25 16:44:06 +01:00
|
|
|
|
2021-11-14 02:27:07 +01:00
|
|
|
#include <array>
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2020-05-01 04:25:55 +02:00
|
|
|
#include <cstring>
|
2009-02-25 16:44:06 +01:00
|
|
|
|
2020-06-28 02:14:07 +02:00
|
|
|
#include <string_view>
|
|
|
|
|
2009-12-26 02:07:44 +01:00
|
|
|
static const char *
|
2017-05-08 14:44:49 +02:00
|
|
|
verify_uri_segment(const char *p) noexcept
|
2009-12-26 02:07:44 +01:00
|
|
|
{
|
2010-02-27 18:56:47 +01:00
|
|
|
unsigned dots = 0;
|
2010-06-25 21:54:45 +02:00
|
|
|
while (*p == '.') {
|
2010-02-27 18:56:47 +01:00
|
|
|
++p;
|
2010-06-25 21:54:45 +02:00
|
|
|
++dots;
|
|
|
|
}
|
|
|
|
|
2010-02-27 18:56:47 +01:00
|
|
|
if (dots <= 2 && (*p == 0 || *p == '/'))
|
2013-04-08 23:30:21 +02:00
|
|
|
return nullptr;
|
2009-12-26 02:07:44 +01:00
|
|
|
|
2020-05-01 04:25:55 +02:00
|
|
|
const char *q = std::strchr(p + 1, '/');
|
2013-04-08 23:30:21 +02:00
|
|
|
return q != nullptr ? q : "";
|
2009-12-26 02:07:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
uri_safe_local(const char *uri) noexcept
|
2009-12-26 02:07:44 +01:00
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
uri = verify_uri_segment(uri);
|
2013-04-08 23:30:21 +02:00
|
|
|
if (uri == nullptr)
|
2009-12-26 02:07:44 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (*uri == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
assert(*uri == '/');
|
|
|
|
|
|
|
|
++uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:19:31 +02:00
|
|
|
[[gnu::pure]]
|
2015-11-06 09:43:37 +01:00
|
|
|
static const char *
|
2017-05-08 14:44:49 +02:00
|
|
|
SkipUriScheme(const char *uri) noexcept
|
2009-02-27 19:20:11 +01:00
|
|
|
{
|
2021-11-14 02:27:07 +01:00
|
|
|
static constexpr auto schemes = std::array {
|
2020-06-22 22:48:03 +02:00
|
|
|
"http://", "https://",
|
|
|
|
"ftp://",
|
2020-06-22 22:48:49 +02:00
|
|
|
"smb://",
|
2020-06-22 22:48:03 +02:00
|
|
|
};
|
|
|
|
|
2015-11-06 09:47:28 +01:00
|
|
|
for (auto scheme : schemes) {
|
2018-08-02 10:38:20 +02:00
|
|
|
auto result = StringAfterPrefixCaseASCII(uri, scheme);
|
2015-11-06 09:47:28 +01:00
|
|
|
if (result != nullptr)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2015-11-06 09:43:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2017-05-08 14:44:49 +02:00
|
|
|
uri_remove_auth(const char *uri) noexcept
|
2015-11-06 09:43:37 +01:00
|
|
|
{
|
|
|
|
const char *auth = SkipUriScheme(uri);
|
|
|
|
if (auth == nullptr)
|
2009-02-27 19:20:11 +01:00
|
|
|
/* unrecognized URI */
|
2021-11-11 11:18:46 +01:00
|
|
|
return {};
|
2009-02-27 19:20:11 +01:00
|
|
|
|
2020-05-01 04:25:55 +02:00
|
|
|
const char *slash = std::strchr(auth, '/');
|
2013-04-08 23:30:21 +02:00
|
|
|
if (slash == nullptr)
|
2009-02-27 19:20:11 +01:00
|
|
|
slash = auth + strlen(auth);
|
|
|
|
|
2021-11-14 02:27:07 +01:00
|
|
|
auto at = (const char *)std::memchr(auth, '@', slash - auth);
|
2013-04-08 23:30:21 +02:00
|
|
|
if (at == nullptr)
|
2009-02-27 19:20:11 +01:00
|
|
|
/* no auth info present, do nothing */
|
2021-11-11 11:18:46 +01:00
|
|
|
return {};
|
2009-02-27 19:20:11 +01:00
|
|
|
|
|
|
|
/* duplicate the full URI and then delete the auth
|
|
|
|
information */
|
2013-10-23 21:38:07 +02:00
|
|
|
std::string result(uri);
|
|
|
|
result.erase(auth - uri, at + 1 - auth);
|
|
|
|
return result;
|
2009-02-27 19:20:11 +01:00
|
|
|
}
|
2020-06-28 02:14:07 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|