mpd/src/util/UriExtract.cxx

133 lines
2.5 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: BSD-2-Clause
// author: Max Kellermann <max.kellermann@gmail.com>
2019-08-09 15:54:13 +02:00
#include "UriExtract.hxx"
#include "CharUtil.hxx"
#include "StringSplit.hxx"
2019-08-09 15:54:13 +02:00
#include <cstring>
2019-08-09 15:54:13 +02:00
static constexpr bool
IsValidSchemeStart(char ch)
{
return IsLowerAlphaASCII(ch);
}
static constexpr bool
IsValidSchemeChar(char ch)
{
return IsLowerAlphaASCII(ch) || IsDigitASCII(ch) ||
ch == '+' || ch == '.' || ch == '-';
}
2021-10-13 11:28:04 +02:00
[[gnu::pure]]
2019-08-09 15:54:13 +02:00
static bool
IsValidScheme(std::string_view p) noexcept
2019-08-09 15:54:13 +02:00
{
if (p.empty() || !IsValidSchemeStart(p.front()))
return false;
for (size_t i = 1; i < p.size(); ++i)
2019-08-09 15:54:13 +02:00
if (!IsValidSchemeChar(p[i]))
return false;
return true;
}
/**
* Return the URI part after the scheme specification (and after the
* double slash).
*/
2021-10-13 11:28:04 +02:00
[[gnu::pure]]
static std::string_view
uri_after_scheme(std::string_view uri) noexcept
2019-08-09 15:54:13 +02:00
{
if (uri.length() > 2 &&
uri[0] == '/' && uri[1] == '/' && uri[2] != '/')
return uri.substr(2);
auto colon = uri.find(':');
if (colon == std::string_view::npos ||
!IsValidScheme(uri.substr(0, colon)))
return {};
uri = uri.substr(colon + 1);
if (uri[0] != '/' || uri[1] != '/')
return {};
return uri.substr(2);
2019-08-09 15:54:13 +02:00
}
bool
uri_has_scheme(std::string_view uri) noexcept
2019-08-09 15:54:13 +02:00
{
return !uri_get_scheme(uri).empty();
2019-08-09 15:54:13 +02:00
}
std::string_view
uri_get_scheme(std::string_view uri) noexcept
2019-08-09 15:54:13 +02:00
{
auto end = uri.find("://");
if (end == std::string_view::npos)
return {};
2019-08-09 15:54:13 +02:00
return uri.substr(0, end);
2019-08-09 15:54:13 +02:00
}
bool
uri_is_relative_path(const char *uri) noexcept
{
return !uri_has_scheme(uri) && *uri != '/';
}
std::string_view
uri_get_path(std::string_view uri) noexcept
2019-08-09 15:54:13 +02:00
{
auto ap = uri_after_scheme(uri);
if (ap.data() != nullptr) {
auto slash = ap.find('/');
if (slash == std::string_view::npos)
return {};
return ap.substr(slash);
}
2019-08-09 15:54:13 +02:00
return uri;
}
2021-10-13 11:28:04 +02:00
[[gnu::pure]]
static std::string_view
UriWithoutQueryString(std::string_view uri) noexcept
{
return Split(uri, '?').first;
}
2019-08-09 15:54:13 +02:00
/* suffixes should be ascii only characters */
std::string_view
uri_get_suffix(std::string_view _uri) noexcept
2019-08-09 15:54:13 +02:00
{
const auto uri = UriWithoutQueryString(_uri);
const auto dot = uri.rfind('.');
if (dot == uri.npos || dot == 0 ||
uri[dot - 1] == '/' || uri[dot - 1] == '\\')
return {};
2019-08-09 15:54:13 +02:00
auto suffix = uri.substr(dot + 1);
if (suffix.find('/') != suffix.npos ||
suffix.find('\\') != suffix.npos)
/* this was not the last path segment */
return {};
2019-08-09 15:54:13 +02:00
return suffix;
2019-08-09 15:54:13 +02:00
}
const char *
uri_get_fragment(const char *uri) noexcept
{
const char *fragment = std::strchr(uri, '#');
if (fragment == nullptr)
return nullptr;
return fragment + 1;
}