-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatching.go
More file actions
53 lines (48 loc) · 1.33 KB
/
matching.go
File metadata and controls
53 lines (48 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package gateway
import "strings"
// matchSegment matches a single segment of a domain against a segment of the pattern.
func matchSegment(valueSeg, patternSeg string) bool {
if patternSeg == "**" {
return true
}
if patternSeg == "*" {
return true
}
if len(valueSeg) != len(patternSeg) {
return false
}
for i := range valueSeg {
if patternSeg[i] != '?' && valueSeg[i] != patternSeg[i] {
return false
}
}
return true
}
// matchSegments recursively matches domain and pattern segments.
func matchSegments(valueParts, patternParts []string) bool {
if len(patternParts) == 0 {
return len(valueParts) == 0
}
if len(valueParts) == 0 {
for _, part := range patternParts {
if part != "**" {
return false
}
}
return true
}
if patternParts[0] == "**" {
// Try to match ** with the current segment and without it.
return matchSegments(valueParts, patternParts[1:]) || matchSegments(valueParts[1:], patternParts)
}
if matchSegment(valueParts[0], patternParts[0]) {
return matchSegments(valueParts[1:], patternParts[1:])
}
return false
}
// matchDomain matches the domain against the pattern.
func matchDomain(pattern, domain string) bool {
domainParts := strings.Split(domain, ".")
patternParts := strings.Split(pattern, ".")
return matchSegments(domainParts, patternParts)
}