getDomain function

String? getDomain(
  1. String suffix,
  2. String hostname,
  3. Options options
)

Implementation

String? getDomain(String suffix, String hostname, Options options) {
  // Check if `hostname` ends with a member of `validHosts`.
  if (options.validHosts != null) {
    final validHosts = options.validHosts;
    for (final vhost in validHosts!) {
      if (shareSameDomainSuffix(hostname, vhost)) {
        return vhost;
      }
    }
  }

  int numberOfLeadingDots = 0;
  if (hostname.startsWith('.')) {
    while (numberOfLeadingDots < hostname.length &&
        hostname[numberOfLeadingDots] == '.') {
      numberOfLeadingDots += 1;
    }
  }

  // If `hostname` is a valid public suffix, then there is no domain to return.
  // Since we already know that `getPublicSuffix` returns a suffix of `hostname`
  // there is no need to perform a string comparison and we only compare the
  // size.
  if (suffix.length == hostname.length - numberOfLeadingDots) {
    return null;
  }

  // To extract the general domain, we start by identifying the public suffix
  // (if any), then consider the domain to be the public suffix with one added
  // level of depth. (e.g.: if hostname is `not.evil.co.uk` and public suffix:
  // `co.uk`, then we take one more level: `evil`, giving the final result:
  // `evil.co.uk`).
  return extractDomainWithSuffix(hostname, suffix);
}