dvNormaliseHost function

String? dvNormaliseHost(
  1. String? raw
)

raw as a host name: lowercase, with no port and no trailing dot, the brackets taken off an IPv6 literal. Null for an empty or non-ASCII host (internationalised names arrive as punycode; anything else is refused rather than mapped).

Implementation

String? dvNormaliseHost(String? raw) {
  if (raw == null) return null;
  String host = raw.trim().toLowerCase();
  if (host.isEmpty || !_ascii(host)) return null;
  if (host.startsWith('[')) {
    final int close = host.indexOf(']');
    if (close < 0) return null;
    host = host.substring(1, close);
  } else {
    final int colon = host.indexOf(':');
    // One colon is host:port. More than one unbracketed is a bare IPv6
    // address, which has no port to strip.
    if (colon >= 0 && host.indexOf(':', colon + 1) < 0) {
      host = host.substring(0, colon);
    }
  }
  while (host.endsWith('.')) {
    host = host.substring(0, host.length - 1);
  }
  return host.isEmpty ? null : host;
}