-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.go
297 lines (273 loc) · 6.43 KB
/
pattern.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package ngebut
import (
"errors"
"fmt"
"net/url"
"strings"
"unicode"
)
// A pattern is something that can be matched against an HTTP request.
type pattern struct {
str string // original string
method string
host string
segments []segment
}
// A segment is a pattern piece that matches one or more path segments, or a trailing slash.
type segment struct {
s string // literal or wildcard name or "/" for "/{$}".
wild bool
multi bool // "..." wildcard
}
func parsePattern(s string) (_ *pattern, err error) {
if s == "" {
return nil, errors.New("empty pattern")
}
off := 0 // offset into string
defer func() {
if err != nil {
err = fmt.Errorf("at offset %d: %w", off, err)
}
}()
method, rest, found := strings.Cut(s, " ")
if !found {
rest = method
method = ""
}
if method != "" && !validMethod(method) {
return nil, fmt.Errorf("invalid method %q", method)
}
p := &pattern{str: s, method: method}
if found {
off = len(method) + 1
}
i := strings.IndexByte(rest, '/')
if i < 0 {
return nil, errors.New("host/path missing /")
}
p.host = rest[:i]
rest = rest[i:]
if j := strings.IndexByte(p.host, '{'); j >= 0 {
off += j
return nil, errors.New("host contains '{' (missing initial '/'?)")
}
// At this point, rest is the path.
off += i
seenNames := map[string]bool{} // remember wildcard names to catch dups
for len(rest) > 0 {
rest = rest[1:]
off = len(s) - len(rest)
if len(rest) == 0 {
p.segments = append(p.segments, segment{wild: true, multi: true})
break
}
i := strings.IndexByte(rest, '/')
if i < 0 {
i = len(rest)
}
var seg string
seg, rest = rest[:i], rest[i:]
if i := strings.IndexByte(seg, '{'); i < 0 {
seg = pathUnescape(seg)
p.segments = append(p.segments, segment{s: seg})
} else {
if i != 0 {
return nil, errors.New("bad wildcard segment (must start with '{')")
}
if seg[len(seg)-1] != '}' {
return nil, errors.New("bad wildcard segment (must end with '}')")
}
name := seg[1 : len(seg)-1]
if name == "$" {
if len(rest) != 0 {
return nil, errors.New("{$} not at end")
}
p.segments = append(p.segments, segment{s: "/"})
break
}
name, multi := strings.CutSuffix(name, "...")
if multi && len(rest) != 0 {
return nil, errors.New("{...} wildcard not at end")
}
if name == "" {
return nil, errors.New("empty wildcard")
}
if !isValidWildcardName(name) {
return nil, fmt.Errorf("bad wildcard name %q", name)
}
if seenNames[name] {
return nil, fmt.Errorf("duplicate wildcard name %q", name)
}
seenNames[name] = true
p.segments = append(p.segments, segment{s: name, wild: true, multi: multi})
}
}
return p, nil
}
func validMethod(method string) bool {
return method == "GET" || method == "POST" || method == "PUT" || method == "DELETE" || method == "HEAD" || method == "OPTIONS" || method == "PATCH"
}
func isValidWildcardName(s string) bool {
if s == "" {
return false
}
for i, c := range s {
if !unicode.IsLetter(c) && c != '_' && (i == 0 || !unicode.IsDigit(c)) {
return false
}
}
return true
}
func pathUnescape(path string) string {
u, err := url.PathUnescape(path)
if err != nil {
return path
}
return u
}
type relationship string
const (
equivalent relationship = "equivalent"
moreGeneral relationship = "moreGeneral"
moreSpecific relationship = "moreSpecific"
disjoint relationship = "disjoint"
overlaps relationship = "overlaps"
)
func (p1 *pattern) comparePathsAndMethods(p2 *pattern) relationship {
mrel := p1.compareMethods(p2)
if mrel == disjoint {
return disjoint
}
prel := p1.comparePaths(p2)
return combineRelationships(mrel, prel)
}
func (p1 *pattern) compareMethods(p2 *pattern) relationship {
if p1.method == p2.method {
return equivalent
}
if p1.method == "" {
return moreGeneral
}
if p2.method == "" {
return moreSpecific
}
if p1.method == "GET" && p2.method == "HEAD" {
return moreGeneral
}
if p2.method == "GET" && p1.method == "HEAD" {
return moreSpecific
}
return disjoint
}
func (p1 *pattern) comparePaths(p2 *pattern) relationship {
if len(p1.segments) != len(p2.segments) && !p1.lastSegment().multi && !p2.lastSegment().multi {
return disjoint
}
var segs1, segs2 []segment
rel := equivalent
for segs1, segs2 = p1.segments, p2.segments; len(segs1) > 0 && len(segs2) > 0; segs1, segs2 = segs1[1:], segs2[1:] {
rel = combineRelationships(rel, compareSegments(segs1[0], segs2[0]))
if rel == disjoint {
return rel
}
}
if len(segs1) == 0 && len(segs2) == 0 {
return rel
}
if len(segs1) < len(segs2) && p1.lastSegment().multi {
return combineRelationships(rel, moreGeneral)
}
if len(segs2) < len(segs1) && p2.lastSegment().multi {
return combineRelationships(rel, moreSpecific)
}
return disjoint
}
func compareSegments(s1, s2 segment) relationship {
if s1.multi && s2.multi {
return equivalent
}
if s1.multi {
return moreGeneral
}
if s2.multi {
return moreSpecific
}
if s1.wild && s2.wild {
return equivalent
}
if s1.wild {
if s2.s == "/" {
return disjoint
}
return moreGeneral
}
if s2.wild {
if s1.s == "/" {
return disjoint
}
return moreSpecific
}
if s1.s == s2.s {
return equivalent
}
return disjoint
}
func combineRelationships(r1, r2 relationship) relationship {
switch r1 {
case equivalent:
return r2
case disjoint:
return disjoint
case overlaps:
if r2 == disjoint {
return disjoint
}
return overlaps
case moreGeneral, moreSpecific:
switch r2 {
case equivalent:
return r1
case inverseRelationship(r1):
return overlaps
default:
return r2
}
default:
panic(fmt.Sprintf("unknown relationship %q", r1))
}
}
func inverseRelationship(r relationship) relationship {
switch r {
case moreSpecific:
return moreGeneral
case moreGeneral:
return moreSpecific
default:
return r
}
}
func (p *pattern) lastSegment() segment {
return p.segments[len(p.segments)-1]
}
func (p *pattern) conflictsWith(other *pattern) bool {
if p.method != other.method {
return false
}
if p.host != other.host {
return false
}
rel := p.comparePaths(other)
return rel == equivalent || rel == overlaps
}
func (p *pattern) match(req *Request) bool {
if p.method != "" && p.method != req.Method {
return false
}
if p.host != "" && !strings.HasPrefix(req.Host, p.host) {
return false
}
return p.matchPath(req.URL.Path)
}
func (p *pattern) matchPath(path string) bool {
return strings.HasPrefix(path, p.str) && (path == p.str || p.lastSegment().multi)
}