fork of whitequark.org/git-pages with mods for tangled

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>

oppi.li 779f705d 5da56a1b

verified
Changed files
+19 -2
src
+19 -2
src/wildcard.go
··· 34 34 // corresponding to the * in the domain pattern. 35 35 func (pattern *WildcardPattern) Matches(host string) (string, bool) { 36 36 hostParts := strings.Split(host, ".") 37 - if len(hostParts) != 1+len(pattern.Domain) || !slices.Equal(hostParts[1:], pattern.Domain) { 37 + hostLen := len(hostParts) 38 + patternLen := len(pattern.Domain) 39 + 40 + // host must have at least one more part than the pattern domain 41 + if hostLen <= patternLen { 42 + return "", false 43 + } 44 + 45 + // break the host parts into <subdomain parts> and <domain parts> 46 + mid := hostLen - patternLen 47 + prefix := hostParts[:mid] 48 + suffix := hostParts[mid:] 49 + 50 + // check if the suffix matches the domain 51 + if !slices.Equal(suffix, pattern.Domain) { 38 52 return "", false 39 53 } 40 - return hostParts[0], true 54 + 55 + // return all the subdomain parts 56 + subdomain := strings.Join(prefix, ".") 57 + return subdomain, true 41 58 } 42 59 43 60 func (pattern *WildcardPattern) IsFallbackFor(host string) bool {