Source file
src/net/http/pattern.go
1
2
3
4
5
6
7 package http
8
9 import (
10 "errors"
11 "fmt"
12 "net/url"
13 "strings"
14 "unicode"
15 )
16
17
18
19 type pattern struct {
20 str string
21 method string
22 host string
23
24
25
26
27
28
29
30
31
32
33 segments []segment
34 loc string
35 }
36
37 func (p *pattern) String() string { return p.str }
38
39 func (p *pattern) lastSegment() segment {
40 return p.segments[len(p.segments)-1]
41 }
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 type segment struct {
62 s string
63 wild bool
64 multi bool
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 func parsePattern(s string) (_ *pattern, err error) {
85 if len(s) == 0 {
86 return nil, errors.New("empty pattern")
87 }
88 off := 0
89 defer func() {
90 if err != nil {
91 err = fmt.Errorf("at offset %d: %w", off, err)
92 }
93 }()
94
95 method, rest, found := s, "", false
96 if i := strings.IndexAny(s, " \t"); i >= 0 {
97 method, rest, found = s[:i], strings.TrimLeft(s[i+1:], " \t"), true
98 }
99 if !found {
100 rest = method
101 method = ""
102 }
103 if method != "" && !validMethod(method) {
104 return nil, fmt.Errorf("invalid method %q", method)
105 }
106 p := &pattern{str: s, method: method}
107
108 if found {
109 off = len(method) + 1
110 }
111 i := strings.IndexByte(rest, '/')
112 if i < 0 {
113 return nil, errors.New("host/path missing /")
114 }
115 p.host = rest[:i]
116 rest = rest[i:]
117 if j := strings.IndexByte(p.host, '{'); j >= 0 {
118 off += j
119 return nil, errors.New("host contains '{' (missing initial '/'?)")
120 }
121
122 off += i
123
124
125
126 if method != "" && method != "CONNECT" && rest != cleanPath(rest) {
127 return nil, errors.New("non-CONNECT pattern with unclean path can never match")
128 }
129
130 seenNames := map[string]bool{}
131 for len(rest) > 0 {
132
133 rest = rest[1:]
134 off = len(s) - len(rest)
135 if len(rest) == 0 {
136
137 p.segments = append(p.segments, segment{wild: true, multi: true})
138 break
139 }
140 i := strings.IndexByte(rest, '/')
141 if i < 0 {
142 i = len(rest)
143 }
144 var seg string
145 seg, rest = rest[:i], rest[i:]
146 if i := strings.IndexByte(seg, '{'); i < 0 {
147
148 seg = pathUnescape(seg)
149 p.segments = append(p.segments, segment{s: seg})
150 } else {
151
152 if i != 0 {
153 return nil, errors.New("bad wildcard segment (must start with '{')")
154 }
155 if seg[len(seg)-1] != '}' {
156 return nil, errors.New("bad wildcard segment (must end with '}')")
157 }
158 name := seg[1 : len(seg)-1]
159 if name == "$" {
160 if len(rest) != 0 {
161 return nil, errors.New("{$} not at end")
162 }
163 p.segments = append(p.segments, segment{s: "/"})
164 break
165 }
166 name, multi := strings.CutSuffix(name, "...")
167 if multi && len(rest) != 0 {
168 return nil, errors.New("{...} wildcard not at end")
169 }
170 if name == "" {
171 return nil, errors.New("empty wildcard")
172 }
173 if !isValidWildcardName(name) {
174 return nil, fmt.Errorf("bad wildcard name %q", name)
175 }
176 if seenNames[name] {
177 return nil, fmt.Errorf("duplicate wildcard name %q", name)
178 }
179 seenNames[name] = true
180 p.segments = append(p.segments, segment{s: name, wild: true, multi: multi})
181 }
182 }
183 return p, nil
184 }
185
186 func isValidWildcardName(s string) bool {
187 if s == "" {
188 return false
189 }
190
191 for i, c := range s {
192 if !unicode.IsLetter(c) && c != '_' && (i == 0 || !unicode.IsDigit(c)) {
193 return false
194 }
195 }
196 return true
197 }
198
199 func pathUnescape(path string) string {
200 u, err := url.PathUnescape(path)
201 if err != nil {
202
203 return path
204 }
205 return u
206 }
207
208
209 type relationship string
210
211 const (
212 equivalent relationship = "equivalent"
213 moreGeneral relationship = "moreGeneral"
214 moreSpecific relationship = "moreSpecific"
215 disjoint relationship = "disjoint"
216 overlaps relationship = "overlaps"
217 )
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 func (p1 *pattern) conflictsWith(p2 *pattern) bool {
233 if p1.host != p2.host {
234
235
236
237 return false
238 }
239 rel := p1.comparePathsAndMethods(p2)
240 return rel == equivalent || rel == overlaps
241 }
242
243 func (p1 *pattern) comparePathsAndMethods(p2 *pattern) relationship {
244 mrel := p1.compareMethods(p2)
245
246 if mrel == disjoint {
247 return disjoint
248 }
249 prel := p1.comparePaths(p2)
250 return combineRelationships(mrel, prel)
251 }
252
253
254
255
256
257
258
259
260 func (p1 *pattern) compareMethods(p2 *pattern) relationship {
261 if p1.method == p2.method {
262 return equivalent
263 }
264 if p1.method == "" {
265
266 return moreGeneral
267 }
268 if p2.method == "" {
269 return moreSpecific
270 }
271 if p1.method == "GET" && p2.method == "HEAD" {
272
273 return moreGeneral
274 }
275 if p2.method == "GET" && p1.method == "HEAD" {
276 return moreSpecific
277 }
278 return disjoint
279 }
280
281
282
283 func (p1 *pattern) comparePaths(p2 *pattern) relationship {
284
285
286 if len(p1.segments) != len(p2.segments) && !p1.lastSegment().multi && !p2.lastSegment().multi {
287 return disjoint
288 }
289
290
291 var segs1, segs2 []segment
292 rel := equivalent
293 for segs1, segs2 = p1.segments, p2.segments; len(segs1) > 0 && len(segs2) > 0; segs1, segs2 = segs1[1:], segs2[1:] {
294 rel = combineRelationships(rel, compareSegments(segs1[0], segs2[0]))
295 if rel == disjoint {
296 return rel
297 }
298 }
299
300
301
302 if len(segs1) == 0 && len(segs2) == 0 {
303 return rel
304 }
305
306
307
308 if len(segs1) < len(segs2) && p1.lastSegment().multi {
309 return combineRelationships(rel, moreGeneral)
310 }
311 if len(segs2) < len(segs1) && p2.lastSegment().multi {
312 return combineRelationships(rel, moreSpecific)
313 }
314 return disjoint
315 }
316
317
318 func compareSegments(s1, s2 segment) relationship {
319 if s1.multi && s2.multi {
320 return equivalent
321 }
322 if s1.multi {
323 return moreGeneral
324 }
325 if s2.multi {
326 return moreSpecific
327 }
328 if s1.wild && s2.wild {
329 return equivalent
330 }
331 if s1.wild {
332 if s2.s == "/" {
333
334 return disjoint
335 }
336 return moreGeneral
337 }
338 if s2.wild {
339 if s1.s == "/" {
340 return disjoint
341 }
342 return moreSpecific
343 }
344
345 if s1.s == s2.s {
346 return equivalent
347 }
348 return disjoint
349 }
350
351
352
353
354
355
356
357
358
359 func combineRelationships(r1, r2 relationship) relationship {
360 switch r1 {
361 case equivalent:
362 return r2
363 case disjoint:
364 return disjoint
365 case overlaps:
366 if r2 == disjoint {
367 return disjoint
368 }
369 return overlaps
370 case moreGeneral, moreSpecific:
371 switch r2 {
372 case equivalent:
373 return r1
374 case inverseRelationship(r1):
375 return overlaps
376 default:
377 return r2
378 }
379 default:
380 panic(fmt.Sprintf("unknown relationship %q", r1))
381 }
382 }
383
384
385
386 func inverseRelationship(r relationship) relationship {
387 switch r {
388 case moreSpecific:
389 return moreGeneral
390 case moreGeneral:
391 return moreSpecific
392 default:
393 return r
394 }
395 }
396
397
398 func isLitOrSingle(seg segment) bool {
399 if seg.wild {
400 return !seg.multi
401 }
402 return seg.s != "/"
403 }
404
405
406 func describeConflict(p1, p2 *pattern) string {
407 mrel := p1.compareMethods(p2)
408 prel := p1.comparePaths(p2)
409 rel := combineRelationships(mrel, prel)
410 if rel == equivalent {
411 return fmt.Sprintf("%s matches the same requests as %s", p1, p2)
412 }
413 if rel != overlaps {
414 panic("describeConflict called with non-conflicting patterns")
415 }
416 if prel == overlaps {
417 return fmt.Sprintf(`%[1]s and %[2]s both match some paths, like %[3]q.
418 But neither is more specific than the other.
419 %[1]s matches %[4]q, but %[2]s doesn't.
420 %[2]s matches %[5]q, but %[1]s doesn't.`,
421 p1, p2, commonPath(p1, p2), differencePath(p1, p2), differencePath(p2, p1))
422 }
423 if mrel == moreGeneral && prel == moreSpecific {
424 return fmt.Sprintf("%s matches more methods than %s, but has a more specific path pattern", p1, p2)
425 }
426 if mrel == moreSpecific && prel == moreGeneral {
427 return fmt.Sprintf("%s matches fewer methods than %s, but has a more general path pattern", p1, p2)
428 }
429 return fmt.Sprintf("bug: unexpected way for two patterns %s and %s to conflict: methods %s, paths %s", p1, p2, mrel, prel)
430 }
431
432
433 func writeMatchingPath(b *strings.Builder, segs []segment) {
434 for _, s := range segs {
435 writeSegment(b, s)
436 }
437 }
438
439 func writeSegment(b *strings.Builder, s segment) {
440 b.WriteByte('/')
441 if !s.multi && s.s != "/" {
442 b.WriteString(s.s)
443 }
444 }
445
446
447
448 func commonPath(p1, p2 *pattern) string {
449 var b strings.Builder
450 var segs1, segs2 []segment
451 for segs1, segs2 = p1.segments, p2.segments; len(segs1) > 0 && len(segs2) > 0; segs1, segs2 = segs1[1:], segs2[1:] {
452 if s1 := segs1[0]; s1.wild {
453 writeSegment(&b, segs2[0])
454 } else {
455 writeSegment(&b, s1)
456 }
457 }
458 if len(segs1) > 0 {
459 writeMatchingPath(&b, segs1)
460 } else if len(segs2) > 0 {
461 writeMatchingPath(&b, segs2)
462 }
463 return b.String()
464 }
465
466
467
468 func differencePath(p1, p2 *pattern) string {
469 var b strings.Builder
470
471 var segs1, segs2 []segment
472 for segs1, segs2 = p1.segments, p2.segments; len(segs1) > 0 && len(segs2) > 0; segs1, segs2 = segs1[1:], segs2[1:] {
473 s1 := segs1[0]
474 s2 := segs2[0]
475 if s1.multi && s2.multi {
476
477 b.WriteByte('/')
478 return b.String()
479
480 }
481 if s1.multi && !s2.multi {
482
483
484
485
486 b.WriteByte('/')
487 if s2.s == "/" {
488 if s1.s != "" {
489 b.WriteString(s1.s)
490 } else {
491 b.WriteString("x")
492 }
493 }
494 return b.String()
495 }
496 if !s1.multi && s2.multi {
497 writeSegment(&b, s1)
498 } else if s1.wild && s2.wild {
499
500
501 writeSegment(&b, s1)
502 } else if s1.wild && !s2.wild {
503
504
505
506
507 if s1.s != s2.s {
508 writeSegment(&b, s1)
509 } else {
510 b.WriteByte('/')
511 b.WriteString(s2.s + "x")
512 }
513 } else if !s1.wild && s2.wild {
514 writeSegment(&b, s1)
515 } else {
516
517
518 if s1.s != s2.s {
519 panic(fmt.Sprintf("literals differ: %q and %q", s1.s, s2.s))
520 }
521 writeSegment(&b, s1)
522 }
523 }
524 if len(segs1) > 0 {
525
526
527 writeMatchingPath(&b, segs1)
528 } else if len(segs2) > 0 {
529 writeMatchingPath(&b, segs2)
530 }
531 return b.String()
532 }
533
View as plain text