MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
HostPort.hpp
Go to the documentation of this file.
1#pragma once
2
4
5//=============================================================================
6// Network addresses written as text
7//=============================================================================
8
9/**
10 * @struct HostPort
11 * @brief A host and a port, as written in "host:port".
12 */
13struct HostPort {
14 std::string host;
15 uint16_t port {};
16};
17
18/**
19 * @brief Read a port number, 1 to 65535.
20 *
21 * Digits only, with no sign, no spaces and no leading or trailing text. Zero is
22 * refused, since it means "any port" to the system and names nothing to connect to.
23 *
24 * @param text Candidate text.
25 * @return The port, or nothing when the text is not one.
26 */
27[[nodiscard]] constexpr std::optional<uint16_t> parse_port(std::string_view text) noexcept
28{
29 if (text.empty() || text.size() > 5) {
30 return std::nullopt;
31 }
32
33 uint32_t value = 0;
34 for (const char c : text) {
35 if (c < '0' || c > '9') {
36 return std::nullopt;
37 }
38 value = value * 10U + static_cast<uint32_t>(c - '0');
39 }
40
41 if (value == 0 || value > 65535U) {
42 return std::nullopt;
43 }
44 return static_cast<uint16_t>(value);
45}
46
47/**
48 * @brief Read "host:port", where the host is a name, an IPv4 address or a bracketed IPv6 address.
49 *
50 * The split is at the last colon, so an IPv6 address must be bracketed: "[::1]:9000".
51 * The host is returned without its brackets. A host with a space or a control
52 * character, an empty host, or a missing or invalid port refuses the whole text.
53 *
54 * @param text Candidate text.
55 * @return The host and port, or nothing when the text is not one.
56 */
57[[nodiscard]] inline std::optional<HostPort> parse_host_port(std::string_view text)
58{
59 const size_t colon = text.rfind(':');
60 if (colon == std::string_view::npos) {
61 return std::nullopt;
62 }
63
64 std::string_view host = text.substr(0, colon);
65 const auto port = parse_port(text.substr(colon + 1));
66 if (!port) {
67 return std::nullopt;
68 }
69
70 if (host.size() >= 2 && host.front() == '[' && host.back() == ']') {
71 host = host.substr(1, host.size() - 2);
72 } else if (host.find(':') != std::string_view::npos) {
73 return std::nullopt;
74 }
75
76 if (host.empty()) {
77 return std::nullopt;
78 }
79 for (const char c : host) {
80 if (static_cast<unsigned char>(c) <= ' ' || c == 0x7F) {
81 return std::nullopt;
82 }
83 }
84
85 return HostPort { .host = std::string(host), .port = *port };
86}
87
88} // namespace MayaFlux::Transitive::Protocol
std::optional< HostPort > parse_host_port(std::string_view text)
Read "host:port", where the host is a name, an IPv4 address or a bracketed IPv6 address.
Definition HostPort.hpp:57
constexpr std::optional< uint16_t > parse_port(std::string_view text) noexcept
Read a port number, 1 to 65535.
Definition HostPort.hpp:27
A host and a port, as written in "host:port".
Definition HostPort.hpp:13