Source file src/net/http/pattern.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Patterns for ServeMux routing.
     6  
     7  package http
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"net/url"
    13  	"strings"
    14  	"unicode"
    15  )
    16  
    17  // A pattern is something that can be matched against an HTTP request.
    18  // It has an optional method, an optional host, and a path.
    19  type pattern struct {
    20  	str    string // original string
    21  	method string
    22  	host   string
    23  	// The representation of a path differs from the surface syntax, which
    24  	// simplifies most algorithms.
    25  	//
    26  	// Paths ending in '/' are represented with an anonymous "..." wildcard.
    27  	// For example, the path "a/" is represented as a literal segment "a" followed
    28  	// by a segment with multi==true.
    29  	//
    30  	// Paths ending in "{$}" are represented with the literal segment "/".
    31  	// For example, the path "a/{$}" is represented as a literal segment "a" followed
    32  	// by a literal segment "/".
    33  	segments []segment
    34  	loc      string // source location of registering call, for helpful messages
    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  // A segment is a pattern piece that matches one or more path segments, or
    44  // a trailing slash.
    45  //
    46  // If wild is false, it matches a literal segment, or, if s == "/", a trailing slash.
    47  // Examples:
    48  //
    49  //	"a" => segment{s: "a"}
    50  //	"/{$}" => segment{s: "/"}
    51  //
    52  // If wild is true and multi is false, it matches a single path segment.
    53  // Example:
    54  //
    55  //	"{x}" => segment{s: "x", wild: true}
    56  //
    57  // If both wild and multi are true, it matches all remaining path segments.
    58  // Example:
    59  //
    60  //	"{rest...}" => segment{s: "rest", wild: true, multi: true}
    61  type segment struct {
    62  	s     string // literal or wildcard name or "/" for "/{$}".
    63  	wild  bool
    64  	multi bool // "..." wildcard
    65  }
    66  
    67  // parsePattern parses a string into a Pattern.
    68  // The string's syntax is
    69  //
    70  //	[METHOD] [HOST]/[PATH]
    71  //
    72  // where:
    73  //   - METHOD is an HTTP method
    74  //   - HOST is a hostname
    75  //   - PATH consists of slash-separated segments, where each segment is either
    76  //     a literal or a wildcard of the form "{name}", "{name...}", or "{$}".
    77  //
    78  // METHOD, HOST and PATH are all optional; that is, the string can be "/".
    79  // If METHOD is present, it must be followed by a single space.
    80  // Wildcard names must be valid Go identifiers.
    81  // The "{$}" and "{name...}" wildcard must occur at the end of PATH.
    82  // PATH may end with a '/'.
    83  // Wildcard names in a path must be distinct.
    84  func parsePattern(s string) (_ *pattern, err error) {
    85  	if len(s) == 0 {
    86  		return nil, errors.New("empty pattern")
    87  	}
    88  	off := 0 // offset into string
    89  	defer func() {
    90  		if err != nil {
    91  			err = fmt.Errorf("at offset %d: %w", off, err)
    92  		}
    93  	}()
    94  
    95  	method, rest, found := strings.Cut(s, " ")
    96  	if !found {
    97  		rest = method
    98  		method = ""
    99  	}
   100  	if method != "" && !validMethod(method) {
   101  		return nil, fmt.Errorf("invalid method %q", method)
   102  	}
   103  	p := &pattern{str: s, method: method}
   104  
   105  	if found {
   106  		off = len(method) + 1
   107  	}
   108  	i := strings.IndexByte(rest, '/')
   109  	if i < 0 {
   110  		return nil, errors.New("host/path missing /")
   111  	}
   112  	p.host = rest[:i]
   113  	rest = rest[i:]
   114  	if j := strings.IndexByte(p.host, '{'); j >= 0 {
   115  		off += j
   116  		return nil, errors.New("host contains '{' (missing initial '/'?)")
   117  	}
   118  	// At this point, rest is the path.
   119  	off += i
   120  
   121  	// An unclean path with a method that is not CONNECT can never match,
   122  	// because paths are cleaned before matching.
   123  	if method != "" && method != "CONNECT" && rest != cleanPath(rest) {
   124  		return nil, errors.New("non-CONNECT pattern with unclean path can never match")
   125  	}
   126  
   127  	seenNames := map[string]bool{} // remember wildcard names to catch dups
   128  	for len(rest) > 0 {
   129  		// Invariant: rest[0] == '/'.
   130  		rest = rest[1:]
   131  		off = len(s) - len(rest)
   132  		if len(rest) == 0 {
   133  			// Trailing slash.
   134  			p.segments = append(p.segments, segment{wild: true, multi: true})
   135  			break
   136  		}
   137  		i := strings.IndexByte(rest, '/')
   138  		if i < 0 {
   139  			i = len(rest)
   140  		}
   141  		var seg string
   142  		seg, rest = rest[:i], rest[i:]
   143  		if i := strings.IndexByte(seg, '{'); i < 0 {
   144  			// Literal.
   145  			seg = pathUnescape(seg)
   146  			p.segments = append(p.segments, segment{s: seg})
   147  		} else {
   148  			// Wildcard.
   149  			if i != 0 {
   150  				return nil, errors.New("bad wildcard segment (must start with '{')")
   151  			}
   152  			if seg[len(seg)-1] != '}' {
   153  				return nil, errors.New("bad wildcard segment (must end with '}')")
   154  			}
   155  			name := seg[1 : len(seg)-1]
   156  			if name == "$" {
   157  				if len(rest) != 0 {
   158  					return nil, errors.New("{$} not at end")
   159  				}
   160  				p.segments = append(p.segments, segment{s: "/"})
   161  				break
   162  			}
   163  			name, multi := strings.CutSuffix(name, "...")
   164  			if multi && len(rest) != 0 {
   165  				return nil, errors.New("{...} wildcard not at end")
   166  			}
   167  			if name == "" {
   168  				return nil, errors.New("empty wildcard")
   169  			}
   170  			if !isValidWildcardName(name) {
   171  				return nil, fmt.Errorf("bad wildcard name %q", name)
   172  			}
   173  			if seenNames[name] {
   174  				return nil, fmt.Errorf("duplicate wildcard name %q", name)
   175  			}
   176  			seenNames[name] = true
   177  			p.segments = append(p.segments, segment{s: name, wild: true, multi: multi})
   178  		}
   179  	}
   180  	return p, nil
   181  }
   182  
   183  func isValidWildcardName(s string) bool {
   184  	if s == "" {
   185  		return false
   186  	}
   187  	// Valid Go identifier.
   188  	for i, c := range s {
   189  		if !unicode.IsLetter(c) && c != '_' && (i == 0 || !unicode.IsDigit(c)) {
   190  			return false
   191  		}
   192  	}
   193  	return true
   194  }
   195  
   196  func pathUnescape(path string) string {
   197  	u, err := url.PathUnescape(path)
   198  	if err != nil {
   199  		// Invalidly escaped path; use the original
   200  		return path
   201  	}
   202  	return u
   203  }
   204  
   205  // relationship is a relationship between two patterns, p1 and p2.
   206  type relationship string
   207  
   208  const (
   209  	equivalent   relationship = "equivalent"   // both match the same requests
   210  	moreGeneral  relationship = "moreGeneral"  // p1 matches everything p2 does & more
   211  	moreSpecific relationship = "moreSpecific" // p2 matches everything p1 does & more
   212  	disjoint     relationship = "disjoint"     // there is no request that both match
   213  	overlaps     relationship = "overlaps"     // there is a request that both match, but neither is more specific
   214  )
   215  
   216  // conflictsWith reports whether p1 conflicts with p2, that is, whether
   217  // there is a request that both match but where neither is higher precedence
   218  // than the other.
   219  //
   220  //	Precedence is defined by two rules:
   221  //	1. Patterns with a host win over patterns without a host.
   222  //	2. Patterns whose method and path is more specific win. One pattern is more
   223  //	   specific than another if the second matches all the (method, path) pairs
   224  //	   of the first and more.
   225  //
   226  // If rule 1 doesn't apply, then two patterns conflict if their relationship
   227  // is either equivalence (they match the same set of requests) or overlap
   228  // (they both match some requests, but neither is more specific than the other).
   229  func (p1 *pattern) conflictsWith(p2 *pattern) bool {
   230  	if p1.host != p2.host {
   231  		// Either one host is empty and the other isn't, in which case the
   232  		// one with the host wins by rule 1, or neither host is empty
   233  		// and they differ, so they won't match the same paths.
   234  		return false
   235  	}
   236  	rel := p1.comparePathsAndMethods(p2)
   237  	return rel == equivalent || rel == overlaps
   238  }
   239  
   240  func (p1 *pattern) comparePathsAndMethods(p2 *pattern) relationship {
   241  	mrel := p1.compareMethods(p2)
   242  	// Optimization: avoid a call to comparePaths.
   243  	if mrel == disjoint {
   244  		return disjoint
   245  	}
   246  	prel := p1.comparePaths(p2)
   247  	return combineRelationships(mrel, prel)
   248  }
   249  
   250  // compareMethods determines the relationship between the method
   251  // part of patterns p1 and p2.
   252  //
   253  // A method can either be empty, "GET", or something else.
   254  // The empty string matches any method, so it is the most general.
   255  // "GET" matches both GET and HEAD.
   256  // Anything else matches only itself.
   257  func (p1 *pattern) compareMethods(p2 *pattern) relationship {
   258  	if p1.method == p2.method {
   259  		return equivalent
   260  	}
   261  	if p1.method == "" {
   262  		// p1 matches any method, but p2 does not, so p1 is more general.
   263  		return moreGeneral
   264  	}
   265  	if p2.method == "" {
   266  		return moreSpecific
   267  	}
   268  	if p1.method == "GET" && p2.method == "HEAD" {
   269  		// p1 matches GET and HEAD; p2 matches only HEAD.
   270  		return moreGeneral
   271  	}
   272  	if p2.method == "GET" && p1.method == "HEAD" {
   273  		return moreSpecific
   274  	}
   275  	return disjoint
   276  }
   277  
   278  // comparePaths determines the relationship between the path
   279  // part of two patterns.
   280  func (p1 *pattern) comparePaths(p2 *pattern) relationship {
   281  	// Optimization: if a path pattern doesn't end in a multi ("...") wildcard, then it
   282  	// can only match paths with the same number of segments.
   283  	if len(p1.segments) != len(p2.segments) && !p1.lastSegment().multi && !p2.lastSegment().multi {
   284  		return disjoint
   285  	}
   286  
   287  	// Consider corresponding segments in the two path patterns.
   288  	var segs1, segs2 []segment
   289  	rel := equivalent
   290  	for segs1, segs2 = p1.segments, p2.segments; len(segs1) > 0 && len(segs2) > 0; segs1, segs2 = segs1[1:], segs2[1:] {
   291  		rel = combineRelationships(rel, compareSegments(segs1[0], segs2[0]))
   292  		if rel == disjoint {
   293  			return rel
   294  		}
   295  	}
   296  	// We've reached the end of the corresponding segments of the patterns.
   297  	// If they have the same number of segments, then we've already determined
   298  	// their relationship.
   299  	if len(segs1) == 0 && len(segs2) == 0 {
   300  		return rel
   301  	}
   302  	// Otherwise, the only way they could fail to be disjoint is if the shorter
   303  	// pattern ends in a multi. In that case, that multi is more general
   304  	// than the remainder of the longer pattern, so combine those two relationships.
   305  	if len(segs1) < len(segs2) && p1.lastSegment().multi {
   306  		return combineRelationships(rel, moreGeneral)
   307  	}
   308  	if len(segs2) < len(segs1) && p2.lastSegment().multi {
   309  		return combineRelationships(rel, moreSpecific)
   310  	}
   311  	return disjoint
   312  }
   313  
   314  // compareSegments determines the relationship between two segments.
   315  func compareSegments(s1, s2 segment) relationship {
   316  	if s1.multi && s2.multi {
   317  		return equivalent
   318  	}
   319  	if s1.multi {
   320  		return moreGeneral
   321  	}
   322  	if s2.multi {
   323  		return moreSpecific
   324  	}
   325  	if s1.wild && s2.wild {
   326  		return equivalent
   327  	}
   328  	if s1.wild {
   329  		if s2.s == "/" {
   330  			// A single wildcard doesn't match a trailing slash.
   331  			return disjoint
   332  		}
   333  		return moreGeneral
   334  	}
   335  	if s2.wild {
   336  		if s1.s == "/" {
   337  			return disjoint
   338  		}
   339  		return moreSpecific
   340  	}
   341  	// Both literals.
   342  	if s1.s == s2.s {
   343  		return equivalent
   344  	}
   345  	return disjoint
   346  }
   347  
   348  // combineRelationships determines the overall relationship of two patterns
   349  // given the relationships of a partition of the patterns into two parts.
   350  //
   351  // For example, if p1 is more general than p2 in one way but equivalent
   352  // in the other, then it is more general overall.
   353  //
   354  // Or if p1 is more general in one way and more specific in the other, then
   355  // they overlap.
   356  func combineRelationships(r1, r2 relationship) relationship {
   357  	switch r1 {
   358  	case equivalent:
   359  		return r2
   360  	case disjoint:
   361  		return disjoint
   362  	case overlaps:
   363  		if r2 == disjoint {
   364  			return disjoint
   365  		}
   366  		return overlaps
   367  	case moreGeneral, moreSpecific:
   368  		switch r2 {
   369  		case equivalent:
   370  			return r1
   371  		case inverseRelationship(r1):
   372  			return overlaps
   373  		default:
   374  			return r2
   375  		}
   376  	default:
   377  		panic(fmt.Sprintf("unknown relationship %q", r1))
   378  	}
   379  }
   380  
   381  // If p1 has relationship `r` to p2, then
   382  // p2 has inverseRelationship(r) to p1.
   383  func inverseRelationship(r relationship) relationship {
   384  	switch r {
   385  	case moreSpecific:
   386  		return moreGeneral
   387  	case moreGeneral:
   388  		return moreSpecific
   389  	default:
   390  		return r
   391  	}
   392  }
   393  
   394  // isLitOrSingle reports whether the segment is a non-dollar literal or a single wildcard.
   395  func isLitOrSingle(seg segment) bool {
   396  	if seg.wild {
   397  		return !seg.multi
   398  	}
   399  	return seg.s != "/"
   400  }
   401  
   402  // describeConflict returns an explanation of why two patterns conflict.
   403  func describeConflict(p1, p2 *pattern) string {
   404  	mrel := p1.compareMethods(p2)
   405  	prel := p1.comparePaths(p2)
   406  	rel := combineRelationships(mrel, prel)
   407  	if rel == equivalent {
   408  		return fmt.Sprintf("%s matches the same requests as %s", p1, p2)
   409  	}
   410  	if rel != overlaps {
   411  		panic("describeConflict called with non-conflicting patterns")
   412  	}
   413  	if prel == overlaps {
   414  		return fmt.Sprintf(`%[1]s and %[2]s both match some paths, like %[3]q.
   415  But neither is more specific than the other.
   416  %[1]s matches %[4]q, but %[2]s doesn't.
   417  %[2]s matches %[5]q, but %[1]s doesn't.`,
   418  			p1, p2, commonPath(p1, p2), differencePath(p1, p2), differencePath(p2, p1))
   419  	}
   420  	if mrel == moreGeneral && prel == moreSpecific {
   421  		return fmt.Sprintf("%s matches more methods than %s, but has a more specific path pattern", p1, p2)
   422  	}
   423  	if mrel == moreSpecific && prel == moreGeneral {
   424  		return fmt.Sprintf("%s matches fewer methods than %s, but has a more general path pattern", p1, p2)
   425  	}
   426  	return fmt.Sprintf("bug: unexpected way for two patterns %s and %s to conflict: methods %s, paths %s", p1, p2, mrel, prel)
   427  }
   428  
   429  // writeMatchingPath writes to b a path that matches the segments.
   430  func writeMatchingPath(b *strings.Builder, segs []segment) {
   431  	for _, s := range segs {
   432  		writeSegment(b, s)
   433  	}
   434  }
   435  
   436  func writeSegment(b *strings.Builder, s segment) {
   437  	b.WriteByte('/')
   438  	if !s.multi && s.s != "/" {
   439  		b.WriteString(s.s)
   440  	}
   441  }
   442  
   443  // commonPath returns a path that both p1 and p2 match.
   444  // It assumes there is such a path.
   445  func commonPath(p1, p2 *pattern) string {
   446  	var b strings.Builder
   447  	var segs1, segs2 []segment
   448  	for segs1, segs2 = p1.segments, p2.segments; len(segs1) > 0 && len(segs2) > 0; segs1, segs2 = segs1[1:], segs2[1:] {
   449  		if s1 := segs1[0]; s1.wild {
   450  			writeSegment(&b, segs2[0])
   451  		} else {
   452  			writeSegment(&b, s1)
   453  		}
   454  	}
   455  	if len(segs1) > 0 {
   456  		writeMatchingPath(&b, segs1)
   457  	} else if len(segs2) > 0 {
   458  		writeMatchingPath(&b, segs2)
   459  	}
   460  	return b.String()
   461  }
   462  
   463  // differencePath returns a path that p1 matches and p2 doesn't.
   464  // It assumes there is such a path.
   465  func differencePath(p1, p2 *pattern) string {
   466  	var b strings.Builder
   467  
   468  	var segs1, segs2 []segment
   469  	for segs1, segs2 = p1.segments, p2.segments; len(segs1) > 0 && len(segs2) > 0; segs1, segs2 = segs1[1:], segs2[1:] {
   470  		s1 := segs1[0]
   471  		s2 := segs2[0]
   472  		if s1.multi && s2.multi {
   473  			// From here the patterns match the same paths, so we must have found a difference earlier.
   474  			b.WriteByte('/')
   475  			return b.String()
   476  
   477  		}
   478  		if s1.multi && !s2.multi {
   479  			// s1 ends in a "..." wildcard but s2 does not.
   480  			// A trailing slash will distinguish them, unless s2 ends in "{$}",
   481  			// in which case any segment will do; prefer the wildcard name if
   482  			// it has one.
   483  			b.WriteByte('/')
   484  			if s2.s == "/" {
   485  				if s1.s != "" {
   486  					b.WriteString(s1.s)
   487  				} else {
   488  					b.WriteString("x")
   489  				}
   490  			}
   491  			return b.String()
   492  		}
   493  		if !s1.multi && s2.multi {
   494  			writeSegment(&b, s1)
   495  		} else if s1.wild && s2.wild {
   496  			// Both patterns will match whatever we put here; use
   497  			// the first wildcard name.
   498  			writeSegment(&b, s1)
   499  		} else if s1.wild && !s2.wild {
   500  			// s1 is a wildcard, s2 is a literal.
   501  			// Any segment other than s2.s will work.
   502  			// Prefer the wildcard name, but if it's the same as the literal,
   503  			// tweak the literal.
   504  			if s1.s != s2.s {
   505  				writeSegment(&b, s1)
   506  			} else {
   507  				b.WriteByte('/')
   508  				b.WriteString(s2.s + "x")
   509  			}
   510  		} else if !s1.wild && s2.wild {
   511  			writeSegment(&b, s1)
   512  		} else {
   513  			// Both are literals. A precondition of this function is that the
   514  			// patterns overlap, so they must be the same literal. Use it.
   515  			if s1.s != s2.s {
   516  				panic(fmt.Sprintf("literals differ: %q and %q", s1.s, s2.s))
   517  			}
   518  			writeSegment(&b, s1)
   519  		}
   520  	}
   521  	if len(segs1) > 0 {
   522  		// p1 is longer than p2, and p2 does not end in a multi.
   523  		// Anything that matches the rest of p1 will do.
   524  		writeMatchingPath(&b, segs1)
   525  	} else if len(segs2) > 0 {
   526  		writeMatchingPath(&b, segs2)
   527  	}
   528  	return b.String()
   529  }
   530  

View as plain text