Allow matching multiple subdomains in wildcards

Previously, this method would match only hosts of the form:

    user.host.com

This changeset allows matches on hosts of the form:

    user.org.host.com
    user.organization.com.host.com

This will potentially be the pattern that tangled.org uses for its hosted
instance of git-pages.

Signed-off-by: oppiliappan <me@oppi.li>
This commit is contained in:
oppiliappan
2025-11-16 05:56:12 +00:00
parent 5da56a1b94
commit 779f705d5c

View File

@@ -34,10 +34,27 @@ func (pattern *WildcardPattern) GetHost() string {
// corresponding to the * in the domain pattern.
func (pattern *WildcardPattern) Matches(host string) (string, bool) {
hostParts := strings.Split(host, ".")
if len(hostParts) != 1+len(pattern.Domain) || !slices.Equal(hostParts[1:], pattern.Domain) {
hostLen := len(hostParts)
patternLen := len(pattern.Domain)
// host must have at least one more part than the pattern domain
if hostLen <= patternLen {
return "", false
}
return hostParts[0], true
// break the host parts into <subdomain parts> and <domain parts>
mid := hostLen - patternLen
prefix := hostParts[:mid]
suffix := hostParts[mid:]
// check if the suffix matches the domain
if !slices.Equal(suffix, pattern.Domain) {
return "", false
}
// return all the subdomain parts
subdomain := strings.Join(prefix, ".")
return subdomain, true
}
func (pattern *WildcardPattern) IsFallbackFor(host string) bool {