-
Notifications
You must be signed in to change notification settings - Fork 13
/
matcher.go
178 lines (137 loc) · 3.57 KB
/
matcher.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package mux
import (
"net/http"
"regexp"
"strings"
)
const (
rankAny = iota
rankPath
rankScheme
)
// Matcher types try to match a request.
type Matcher interface {
Match(*http.Request) bool
Rank() int
}
// headerMatcher matches the request against header values.
type headerMatcher map[string]comparison
func newHeaderMatcher(pairs ...string) (headerMatcher, error) {
headers, err := convertStringsToMapString(isEvenPairs, pairs...)
if err != nil {
return nil, err
}
return headerMatcher(headers), nil
}
func (m headerMatcher) Match(r *http.Request) bool {
return matchMap(m, r.Header, true)
}
func (m headerMatcher) Rank() int {
return rankAny
}
// headerRegexMatcher matches the request against header values.
type headerRegexMatcher map[string]comparison
func newHeaderRegexMatcher(pairs ...string) (headerRegexMatcher, error) {
headers, err := convertStringsToMapRegex(isEvenPairs, pairs...)
if err != nil {
return nil, err
}
return headerRegexMatcher(headers), nil
}
func (m headerRegexMatcher) Match(r *http.Request) bool {
return matchMap(m, r.Header, true)
}
func (m headerRegexMatcher) Rank() int {
return rankAny
}
// MatcherFunc is the function signature used by custom Matchers.
type MatcherFunc func(*http.Request) bool
// Match returns the match for a given request.
func (m MatcherFunc) Match(r *http.Request) bool {
return m(r)
}
func (m MatcherFunc) Rank() int {
return rankAny
}
// schemeMatcher matches the request against URL schemes.
type schemeMatcher map[string]struct{}
func newSchemeMatcher(schemes ...string) schemeMatcher {
schemeMatcher := schemeMatcher{}
for _, v := range schemes {
schemeMatcher[strings.ToLower(v)] = struct{}{}
}
return schemeMatcher
}
func (m schemeMatcher) Match(r *http.Request) bool {
if _, found := m[r.URL.Scheme]; found {
return true
}
return false
}
func (m schemeMatcher) Rank() int {
return rankScheme
}
// pathMatcher matches the request against a URL path.
type pathMatcher string
func (m pathMatcher) Match(r *http.Request) bool {
return strings.Compare(string(m), r.URL.Path) == 0
}
func (m pathMatcher) Rank() int {
return rankPath
}
// pathWithVarsMatcher matches the request against a URL path.
type pathWithVarsMatcher struct {
regex *regexp.Regexp
}
func newPathWithVarsMatcher(path string) pathWithVarsMatcher {
Loop:
for {
switch {
case strings.Contains(path, ":number"):
path = strings.Replace(path, ":number", "([0-9]{1,})", -1)
continue
case strings.Contains(path, ":string"):
path = strings.Replace(path, ":string", "([a-zA-Z]{1,})", -1)
continue
default:
break Loop
}
}
return pathWithVarsMatcher{
regex: regexp.MustCompile(`^` + path + `$`),
}
}
func (m pathWithVarsMatcher) Rank() int {
return rankPath
}
func (m pathWithVarsMatcher) Match(r *http.Request) bool {
return m.regex.MatchString(r.URL.Path)
}
//pathWithVarsMatcher matches the request against a URL path.
type pathRegexMatcher struct {
regex *regexp.Regexp
}
func newPathRegexMatcher(path string) pathRegexMatcher {
path = strings.Replace(path, "#", "", -1)
return pathRegexMatcher{
regex: regexp.MustCompile(`^` + path + `$`),
}
}
func (m pathRegexMatcher) Match(r *http.Request) bool {
return m.regex.MatchString(r.URL.Path)
}
func (m pathRegexMatcher) Rank() int {
return rankPath
}
// Matchers implements the sort interface (len, swap, less)
// see sort.Sort (Standard Library)
type Matchers []Matcher
func (m Matchers) Len() int {
return len(m)
}
func (m Matchers) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (m Matchers) Less(i, j int) bool {
return m[i].Rank() < m[j].Rank()
}