From 779f705d5cabc7662b0dd2e18a82cbb978c9f86a Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Sun, 16 Nov 2025 05:56:12 +0000 Subject: [PATCH] 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 --- src/wildcard.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/wildcard.go b/src/wildcard.go index 64033b0..4c1a179 100644 --- a/src/wildcard.go +++ b/src/wildcard.go @@ -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 and + 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 {