9#include <botan/assert.h>
10#include <botan/internal/charset.h>
11#include <botan/internal/parsing.h>
17std::optional<uint16_t> parse_port(std::string_view s) {
19 if(
const auto port =
parse_u16(s,
true)) {
28bool is_valid_percent_escape(
char c1,
char c2) {
29 const auto is_hex_digit = [](
char c) {
30 return (c >=
'0' && c <=
'9') || (c >=
'a' && c <=
'f') || (c >=
'A' && c <=
'F');
33 if(!is_hex_digit(c1) || !is_hex_digit(c2)) {
38 if(c1 ==
'0' && c2 ==
'0') {
45bool validate_path_query_fragment(std::string_view tail) {
59 enum class State : uint8_t { Path, Query, Fragment };
60 State state = State::Path;
62 for(
size_t i = 0; i < tail.size(); ++i) {
63 const char c = tail[i];
65 if(i + 2 >= tail.size() || !is_valid_percent_escape(tail[i + 1], tail[i + 2])) {
73 if(state == State::Path) {
80 if(state == State::Fragment) {
83 state = State::Fragment;
86 if(!is_pchar_or_slash(c)) {
93bool validate_userinfo(std::string_view userinfo) {
96 for(
size_t i = 0; i < userinfo.size(); ++i) {
97 const char c = userinfo[i];
99 if(i + 2 >= userinfo.size() || !is_valid_percent_escape(userinfo[i + 1], userinfo[i + 2])) {
105 if(!is_valid_userinfo_char(c)) {
116 const bool other_has_authority = other.
raw_authority().has_value();
118 return std::tie(m_scheme, has_authority, m_authority, m_path, m_query, m_fragment) <=>
120 other.m_scheme, other_has_authority, other.m_authority, other.m_path, other.m_query, other.m_fragment);
125 m_authority == other.m_authority && m_path == other.m_path && m_query == other.m_query &&
126 m_fragment == other.m_fragment;
130 const auto colon = m_raw.find(
':');
133 const size_t rest_offset = colon + 1;
134 if(m_raw.size() < rest_offset + 2 || m_raw[rest_offset] !=
'/' || m_raw[rest_offset + 1] !=
'/') {
138 const size_t authority_start = rest_offset + 2;
139 const auto authority_end = m_raw.find_first_of(
"/?#", authority_start);
140 const size_t authority_len =
141 (authority_end == std::string::npos) ? std::string::npos : authority_end - authority_start;
142 return std::string_view(m_raw).substr(authority_start, authority_len);
155 return std::tie(m_userinfo, m_host, m_port) <=> std::tie(other.m_userinfo, other.m_host, other.m_port);
159 return m_userinfo == other.m_userinfo && m_host == other.m_host && m_port == other.m_port;
173 const auto is_ascii_alpha = [](
char c) ->
bool {
return (c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z'); };
176 if(!is_ascii_alpha(raw.front())) {
182 while(i < raw.size() && is_scheme_cont_char(raw[i])) {
186 if(i >= raw.size() || raw[i] !=
':') {
193 auto rest = raw.substr(i + 1);
195 std::optional<Authority> parsed_authority;
196 std::string_view path_query_fragment;
198 if(rest.starts_with(
"//")) {
199 rest.remove_prefix(2);
203 const auto end = rest.find_first_of(
"/?#");
204 const auto authority = (end == std::string_view::npos) ? rest : rest.substr(0, end);
205 path_query_fragment = (end == std::string_view::npos) ? std::string_view{} : rest.substr(end);
210 if(!parsed_authority.has_value()) {
215 path_query_fragment = rest;
219 if(!validate_path_query_fragment(path_query_fragment)) {
226 const auto hash = path_query_fragment.find(
'#');
227 const auto pre_fragment =
228 (hash == std::string_view::npos) ? path_query_fragment : path_query_fragment.substr(0, hash);
229 std::optional<std::string>
fragment;
230 if(hash != std::string_view::npos) {
231 fragment = std::string(path_query_fragment.substr(hash + 1));
234 const auto qmark = pre_fragment.find(
'?');
235 const auto path = (qmark == std::string_view::npos) ? pre_fragment : pre_fragment.substr(0, qmark);
236 std::optional<std::string>
query;
237 if(qmark != std::string_view::npos) {
238 query = std::string(pre_fragment.substr(qmark + 1));
243 std::string(raw),
scheme, std::move(parsed_authority), std::string(
path), std::move(
query), std::move(
fragment));
264 std::optional<std::string>
userinfo;
265 const auto first_at = raw.find(
'@');
266 if(first_at != std::string_view::npos) {
267 if(raw.find(
'@', first_at + 1) != std::string_view::npos) {
270 const auto userinfo_view = raw.substr(0, first_at);
271 if(!validate_userinfo(userinfo_view)) {
274 userinfo = std::string(userinfo_view);
275 raw.remove_prefix(first_at + 1);
278 std::string_view host_view;
279 std::string_view port_str;
280 std::optional<Host>
host;
282 if(!raw.empty() && raw.front() ==
'[') {
284 const auto close = raw.find(
']');
285 if(close == std::string_view::npos) {
288 host_view = raw.substr(1, close - 1);
289 if(host_view.empty()) {
292 const auto after = raw.substr(close + 1);
294 if(after.front() !=
':') {
297 port_str = after.substr(1);
300 if(!ipv6.has_value()) {
306 const auto colon = raw.find(
':');
307 if(colon == std::string_view::npos) {
310 host_view = raw.substr(0, colon);
311 port_str = raw.substr(colon + 1);
314 if(port_str.find(
':') != std::string::npos) {
319 if(host_view.empty()) {
324 if(host_view.ends_with(
'.')) {
331 host = std::move(*dns);
337 std::optional<uint16_t>
port;
339 if(!port_str.empty()) {
340 port = parse_port(port_str);
341 if(!
port.has_value()) {
350 return std::visit([](
const auto& h) -> std::string {
return h.to_string(); }, m_host);
354 if(std::holds_alternative<DNSName>(m_host)) {
355 return HostKind::DNS;
356 }
else if(std::holds_alternative<IPv4Address>(m_host)) {
357 return HostKind::IPv4;
358 }
else if(std::holds_alternative<IPv6Address>(m_host)) {
359 return HostKind::IPv6;
367 std::vector<URI> results;
371 for(
const auto& uri : uris) {
372 if(uri.scheme() == normalized_scheme && uri.authority().has_value()) {
373 results.push_back(uri);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_UNREACHABLE()
static constexpr CharacterValidityTable alpha_numeric_plus(std::string_view extras)
static std::optional< DNSName > from_string(std::string_view name)
static std::optional< IPv4Address > from_string(std::string_view str)
static std::optional< IPv6Address > from_string(std::string_view str)
std::strong_ordering operator<=>(const Authority &other) const
const std::optional< std::string > & userinfo() const
static std::optional< Authority > from_string(std::string_view raw)
const Host & host() const
bool operator==(const Authority &other) const
std::optional< uint16_t > port() const
HostKind host_kind() const
std::string host_to_string() const
const std::string & original_input() const
const std::optional< std::string > & fragment() const
static std::optional< URI > from_string(std::string_view raw)
const std::string & scheme() const
static std::vector< URI > filter_scheme(std::string_view scheme, std::span< const URI > uris)
std::strong_ordering operator<=>(const URI &other) const
const std::optional< std::string > & query() const
const std::string & original_input() const
const std::optional< Authority > & authority() const
std::optional< std::string_view > raw_authority() const
bool operator==(const URI &other) const
const std::string & path() const
std::string tolower_string(std::string_view str)
std::optional< uint16_t > parse_u16(std::string_view input, bool require_canonical)