+19
-2
src/wildcard.go
+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 {