Botan 3.14.1
Crypto and TLS for C&
Botan::URI::Authority Class Referencefinal

#include <uri.h>

Public Types

using Host = std::variant<DNSName, IPv4Address, IPv6Address>
enum class  HostKind : uint8_t { DNS = 0 , IPv4 = 1 , IPv6 = 2 }

Public Member Functions

const Host & host () const
HostKind host_kind () const
std::string host_to_string () const
std::strong_ordering operator<=> (const Authority &other) const
bool operator== (const Authority &other) const
const std::string & original_input () const
std::optional< uint16_t > port () const
const std::optional< std::string > & userinfo () const

Static Public Member Functions

static std::optional< Authority > from_string (std::string_view raw)

Detailed Description

The optional authority component of a URI: a validated DNS name, IPv4 literal, or IPv6 literal, with an optional port.

Definition at line 31 of file uri.h.

Member Typedef Documentation

◆ Host

A validated DNS name, or a literal IPv4 or IPv6 address.

Definition at line 36 of file uri.h.

Member Enumeration Documentation

◆ HostKind

enum class Botan::URI::Authority::HostKind : uint8_t
strong

Tag for the alternative held by Host.

Enumerator
DNS 
IPv4 
IPv6 

Definition at line 41 of file uri.h.

41 : uint8_t {
42 DNS = 0,
43 IPv4 = 1,
44 IPv6 = 2,
45 };

Member Function Documentation

◆ from_string()

std::optional< URI::Authority > Botan::URI::Authority::from_string ( std::string_view raw)
static

Parse a bare authority "host[:port]" or "[ipv6][:port]". Returns nullopt for any parse failure.

Definition at line 247 of file uri.cpp.

247 {
248 if(raw.empty()) {
249 return {};
250 }
251
252 // Capture the full input now; the userinfo prefix is stripped from
253 // `raw` below, and m_raw must reflect the original
254 const std::string original_input(raw);
255
256 /*
257 RFC 3986
258 userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
259
260 Thus a unencoded '@' is not allowed inside userinfo, and the single '@' splits the
261 username from the authority. The @ being present at all is significant; an empty
262 userinfo ("https://@example.com/") is distinct from no userinfo at all.
263 */
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) {
268 return {};
269 }
270 const auto userinfo_view = raw.substr(0, first_at);
271 if(!validate_userinfo(userinfo_view)) {
272 return {};
273 }
274 userinfo = std::string(userinfo_view);
275 raw.remove_prefix(first_at + 1);
276 }
277
278 std::string_view host_view;
279 std::string_view port_str;
280 std::optional<Host> host;
281
282 if(!raw.empty() && raw.front() == '[') {
283 // Bracketed IPv6 literal.
284 const auto close = raw.find(']');
285 if(close == std::string_view::npos) {
286 return {};
287 }
288 host_view = raw.substr(1, close - 1);
289 if(host_view.empty()) {
290 return {};
291 }
292 const auto after = raw.substr(close + 1);
293 if(!after.empty()) {
294 if(after.front() != ':') {
295 return {};
296 }
297 port_str = after.substr(1);
298 }
299 auto ipv6 = IPv6Address::from_string(host_view);
300 if(!ipv6.has_value()) {
301 return {};
302 }
303 host = *ipv6;
304 } else {
305 // host[:port] with no brackets. Only one ':' is allowed (port).
306 const auto colon = raw.find(':');
307 if(colon == std::string_view::npos) {
308 host_view = raw;
309 } else {
310 host_view = raw.substr(0, colon);
311 port_str = raw.substr(colon + 1);
312
313 // Verify the `:` char is the only one that appears
314 if(port_str.find(':') != std::string::npos) {
315 return {};
316 }
317 }
318
319 if(host_view.empty()) {
320 return {};
321 }
322
323 // Technically valid per RFC 3986 but likely not something we want to support
324 if(host_view.ends_with('.')) {
325 return {};
326 }
327
328 if(auto ipv4 = IPv4Address::from_string(host_view)) {
329 host = *ipv4;
330 } else if(auto dns = DNSName::from_string(host_view)) {
331 host = std::move(*dns);
332 } else {
333 return {};
334 }
335 }
336
337 std::optional<uint16_t> port;
338
339 if(!port_str.empty()) {
340 port = parse_port(port_str);
341 if(!port.has_value()) {
342 return {};
343 }
344 }
345
346 return Authority(original_input, std::move(userinfo), std::move(*host), port);
347}
static std::optional< DNSName > from_string(std::string_view name)
Definition dns_name.cpp:134
static std::optional< IPv4Address > from_string(std::string_view str)
static std::optional< IPv6Address > from_string(std::string_view str)
const std::optional< std::string > & userinfo() const
Definition uri.h:87
const Host & host() const
Definition uri.h:56
std::optional< uint16_t > port() const
Definition uri.h:74
const std::string & original_input() const
Definition uri.h:79

References Botan::DNSName::from_string(), Botan::IPv4Address::from_string(), Botan::IPv6Address::from_string(), host(), original_input(), Botan::URI::original_input(), port(), and userinfo().

Referenced by Botan::URI::from_string(), and Botan::OS::open_socket_udp().

◆ host()

const Host & Botan::URI::Authority::host ( ) const
inline

Parsed host: a DNS name, an IPv4 literal, or an IPv6 literal.

Definition at line 56 of file uri.h.

56{ return m_host; }

Referenced by from_string().

◆ host_kind()

URI::Authority::HostKind Botan::URI::Authority::host_kind ( ) const

Which alternative of host() is held.

Definition at line 353 of file uri.cpp.

353 {
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;
360 } else {
362 }
363}
#define BOTAN_ASSERT_UNREACHABLE()
Definition assert.h:173

References BOTAN_ASSERT_UNREACHABLE.

◆ host_to_string()

std::string Botan::URI::Authority::host_to_string ( ) const

The host as a string: DNS names and dotted-IPv4 literals are returned verbatim; IPv6 literals are returned without surrounding brackets. Lowercased for DNS / IPv4; the IPv6 form is whatever IPv6Address::to_string produces.

Definition at line 349 of file uri.cpp.

349 {
350 return std::visit([](const auto& h) -> std::string { return h.to_string(); }, m_host);
351}

◆ operator<=>()

std::strong_ordering Botan::URI::Authority::operator<=> ( const Authority & other) const

Order two authorities

Parameters
otherthe authority to compare against
Returns
the ordering of this authority relative to other

Definition at line 145 of file uri.cpp.

145 =>(const URI::Authority& other) const {
146 /*
147 Userinfo is compared without normalization; RFC 3986 6.2.2.1:
148 When a URI uses components of the generic syntax, the component
149 syntax equivalence rules always apply; namely, that the scheme
150 and host are case-insensitive and therefore should be normalized
151 to lowercase. ... The other generic syntax components are assumed
152 to be case-sensitive unless specifically defined otherwise by the
153 scheme.
154 */
155 return std::tie(m_userinfo, m_host, m_port) <=> std::tie(other.m_userinfo, other.m_host, other.m_port);
156}

◆ operator==()

bool Botan::URI::Authority::operator== ( const Authority & other) const

Compare two authorities

Parameters
otherthe authority to compare against
Returns
true if the two authorities are equal

Definition at line 158 of file uri.cpp.

158 {
159 return m_userinfo == other.m_userinfo && m_host == other.m_host && m_port == other.m_port;
160}

◆ original_input()

const std::string & Botan::URI::Authority::original_input ( ) const
inline

The original input that was parsed

Definition at line 79 of file uri.h.

79{ return m_raw; }

Referenced by from_string().

◆ port()

std::optional< uint16_t > Botan::URI::Authority::port ( ) const
inline

Port if present; nullopt otherwise.

Definition at line 74 of file uri.h.

74{ return m_port; }

Referenced by from_string().

◆ userinfo()

const std::optional< std::string > & Botan::URI::Authority::userinfo ( ) const
inline

The userinfo component, preserved verbatim (no case normalization or pct-decoding) and compared verbatim for identity. nullopt if no "@" was present; present-but-empty (e.g. "https://@example.com/") is distinguished from absent.

Definition at line 87 of file uri.h.

87{ return m_userinfo; }

Referenced by from_string().


The documentation for this class was generated from the following files: