Host constructor
Creates a Host from its parts.
Throws FormatException if host is empty or contains URI brackets
(the brackets belong to the wire form only), or if port is outside
the 0-65535 range.
Implementation
Host(this.host, [this.port]) {
if (host.isEmpty) {
throw const FormatException('host cannot be empty');
}
if (host.codeUnits.contains(0x5B) || host.codeUnits.contains(0x5D)) {
throw FormatException(
'host must not include URI brackets; pass the unbracketed value',
host,
);
}
// Reject control characters, whitespace, and structural delimiters. This
// covers every host form (reg-name, IPv6, IPvFuture: none contain these)
// and, critically, blocks CR/LF injection when a host built from
// untrusted input is later serialized (e.g. into a cookie Domain).
for (var i = 0; i < host.length; i++) {
if (_isForbiddenHostChar(host.codeUnitAt(i))) {
throw FormatException('invalid character in host', host, i);
}
}
// A colon is only valid in an IP-literal (IPv6 / IPvFuture). Validating it
// here rejects a reg-name like `a:b`, which would otherwise be bracketed
// by [encode] to `[a:b]` and then fail to re-parse.
if (host.codeUnits.contains(0x3A)) {
_validateIpLiteral(host, host);
}
final p = port;
if (p != null && (p < 0 || p > 65535)) {
throw FormatException('port must be in 0-65535', p.toString());
}
}