DomainRootInfo resolveDomainRoot()

in httpclient5/src/main/java/org/apache/hc/client5/http/psl/PublicSuffixMatcher.java [167:211]


    DomainRootInfo resolveDomainRoot(final String domain, final DomainType expectedType) {
        String segment = domain;
        String result = null;
        while (segment != null) {
            // An exception rule takes priority over any other matching rule.
            final String key = segment;
            final DomainType exceptionRule = findEntry(exceptions, key);
            if (match(exceptionRule, expectedType)) {
                return new DomainRootInfo(segment, key, exceptionRule);
            }
            final DomainType domainRule = findEntry(rules, key);
            if (match(domainRule, expectedType)) {
                return new DomainRootInfo(result, key, domainRule);
            }

            final int nextdot = segment.indexOf('.');
            final String nextSegment = nextdot != -1 ? segment.substring(nextdot + 1) : null;

            // look for wildcard entries
            final String wildcardKey = (nextSegment == null) ? "*" : "*." + nextSegment;
            final DomainType wildcardDomainRule = findEntry(rules, wildcardKey);
            if (match(wildcardDomainRule, expectedType)) {
                return new DomainRootInfo(result, wildcardKey, wildcardDomainRule);
            }

            // If we're out of segments, and we're not looking for a specific type of entry,
            // apply the default `*` rule.
            // This wildcard rule means any final segment in a domain is a public suffix,
            // so the current `result` is the desired public suffix plus 1
            if (nextSegment == null && (expectedType == null || expectedType == DomainType.UNKNOWN)) {
                return new DomainRootInfo(result, null, null);
            }

            result = segment;
            segment = nextSegment;
        }

        // If no expectations then this result is good.
        if (expectedType == null || expectedType == DomainType.UNKNOWN) {
            return new DomainRootInfo(result, null, null);
        }

        // If we did have expectations apparently there was no match
        return null;
    }