edge: factor the _redirects source pattern out for reuse
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 file changed,
+88
−66
internal/edge/redirects.go+88 −66
diff --git a/internal/edge/redirects.go b/internal/edge/redirects.go| index 51d5f9b51..1bee36fa4 100644 |
| --- a/internal/edge/redirects.go |
| +++ b/internal/edge/redirects.go |
| @@ -60,12 +60,10 @@ const ( |
| // redirectRule is one parsed line of a _redirects file. |
| type redirectRule struct { |
| - line int // 1-based line number, for logs |
| - host string // lowercased; "" matches any host |
| - segs []string // source path segments; ":name" captures one |
| - splat bool // the source ends in "*" |
| - prefix string // a URL destination's "scheme://host", never substituted |
| - to string // the destination template after prefix |
| + line int // 1-based line number, for logs |
| + pathPattern |
| + prefix string // a URL destination's "scheme://host", never substituted |
| + to string // the destination template after prefix |
| status int |
| force bool // "!": applies even when a file exists at the request path |
| } |
| @@ -153,23 +151,70 @@ func parseRedirectLine(line string) (redirectRule, error) { |
| rule.status = code |
| } |
| - src := fields[0] |
| + pp, err := parsePattern(fields[0]) |
| + if err != nil { |
| + return rule, err |
| + } |
| + rule.pathPattern = pp |
| + |
| + dst := fields[1] |
| + switch { |
| + case strings.HasPrefix(dst, "http://") || strings.HasPrefix(dst, "https://"): |
| + if !isRedirect(rule.status) { |
| + return rule, fmt.Errorf("destination %q: a %d serves a file from this site, so it takes a path, not a URL", dst, rule.status) |
| + } |
| + u, err := url.Parse(dst) |
| + if err != nil || u.Host == "" || u.User != nil { |
| + return rule, fmt.Errorf("destination %q: not a usable URL", dst) |
| + } |
| + i := len(u.Scheme) + len("://") + len(u.Host) |
| + if !strings.EqualFold(dst[:i], u.Scheme+"://"+u.Host) { |
| + return rule, fmt.Errorf("destination %q: not a usable URL", dst) |
| + } |
| + rule.prefix, rule.to = dst[:i], dst[i:] |
| + if rule.to != "" && !strings.ContainsAny(rule.to[:1], "/?#") { |
| + return rule, fmt.Errorf("destination %q: not a usable URL", dst) |
| + } |
| + case strings.HasPrefix(dst, "//") || strings.HasPrefix(dst, "/\\"): |
| + return rule, fmt.Errorf("destination %q: name another host with https://", dst) |
| + case strings.HasPrefix(dst, "/"): |
| + rule.to = dst |
| + default: |
| + return rule, fmt.Errorf("destination %q: want a path starting with / or an http(s) URL", dst) |
| + } |
| + return rule, nil |
| +} |
| + |
| +// pathPattern is the source half of a rule: the request host and path it |
| +// applies to. _redirects sources and _headers path lines share it, so the two |
| +// files read a path the same way. |
| +type pathPattern struct { |
| + host string // lowercased; "" matches any host |
| + segs []string // path segments; ":name" captures one |
| + splat bool // the pattern ends in "*" |
| +} |
| + |
| +// parsePattern parses a source field: a path, or an http(s) URL whose host |
| +// must match the request's. |
| +func parsePattern(field string) (pathPattern, error) { |
| + var pp pathPattern |
| + src := field |
| if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") { |
| u, err := url.Parse(src) |
| if err != nil || u.Host == "" || u.User != nil || u.Port() != "" || u.RawQuery != "" || u.Fragment != "" { |
| - return rule, fmt.Errorf("source %q: want a URL with only a host and a path", src) |
| + return pp, fmt.Errorf("source %q: want a URL with only a host and a path", src) |
| } |
| - rule.host = serverName(u.Hostname()) // as requestHost normalizes the request's |
| + pp.host = serverName(u.Hostname()) // as requestHost normalizes the request's |
| src = u.Path |
| if src == "" { |
| src = "/" |
| } |
| } |
| if !strings.HasPrefix(src, "/") { |
| - return rule, fmt.Errorf("source %q: want a path starting with / or an http(s) URL", fields[0]) |
| + return pp, fmt.Errorf("source %q: want a path starting with / or an http(s) URL", field) |
| } |
| if strings.ContainsAny(src, "?#") { |
| - return rule, fmt.Errorf("source %q: query strings and fragments are not matched", fields[0]) |
| + return pp, fmt.Errorf("source %q: query strings and fragments are not matched", field) |
| } |
| names := map[string]bool{} |
| trimmed := strings.Trim(src, "/") |
| @@ -177,24 +222,24 @@ func parseRedirectLine(line string) (redirectRule, error) { |
| switch { |
| case seg == "": |
| if trimmed != "" { |
| - return rule, fmt.Errorf("source %q: empty path segment", fields[0]) |
| + return pp, fmt.Errorf("source %q: empty path segment", field) |
| } |
| - case rule.splat: |
| - return rule, fmt.Errorf("source %q: * must be the last segment", fields[0]) |
| + case pp.splat: |
| + return pp, fmt.Errorf("source %q: * must be the last segment", field) |
| case seg == "*": |
| - rule.splat = true |
| + pp.splat = true |
| case strings.Contains(seg, "*"): |
| - return rule, fmt.Errorf("source %q: * must be a whole segment", fields[0]) |
| + return pp, fmt.Errorf("source %q: * must be a whole segment", field) |
| case strings.HasPrefix(seg, ":"): |
| name := seg[1:] |
| if !validPlaceholder(name) || name == "splat" { |
| - return rule, fmt.Errorf("source %q: bad placeholder %q", fields[0], seg) |
| + return pp, fmt.Errorf("source %q: bad placeholder %q", field, seg) |
| } |
| if names[name] { |
| - return rule, fmt.Errorf("source %q: placeholder %q appears twice", fields[0], seg) |
| + return pp, fmt.Errorf("source %q: placeholder %q appears twice", field, seg) |
| } |
| names[name] = true |
| - rule.segs = append(rule.segs, seg) |
| + pp.segs = append(pp.segs, seg) |
| default: |
| // Requests match decoded, as files are found, so a literal |
| // segment is decoded here to meet them: "/caf%C3%A9" matches a |
| @@ -202,44 +247,18 @@ func parseRedirectLine(line string) (redirectRule, error) { |
| // no request path has one inside a segment. |
| dec, err := url.PathUnescape(seg) |
| if err != nil || strings.Contains(dec, "/") { |
| - return rule, fmt.Errorf("source %q: bad percent-encoding in %q", fields[0], seg) |
| + return pp, fmt.Errorf("source %q: bad percent-encoding in %q", field, seg) |
| } |
| if strings.HasPrefix(dec, ":") { |
| - return rule, fmt.Errorf("source %q: %q would read as a placeholder", fields[0], seg) |
| + return pp, fmt.Errorf("source %q: %q would read as a placeholder", field, seg) |
| } |
| if dec == "." || dec == ".." { |
| - return rule, fmt.Errorf("source %q: dot segments never match", fields[0]) |
| + return pp, fmt.Errorf("source %q: dot segments never match", field) |
| } |
| - rule.segs = append(rule.segs, dec) |
| + pp.segs = append(pp.segs, dec) |
| } |
| } |
| - |
| - dst := fields[1] |
| - switch { |
| - case strings.HasPrefix(dst, "http://") || strings.HasPrefix(dst, "https://"): |
| - if !isRedirect(rule.status) { |
| - return rule, fmt.Errorf("destination %q: a %d serves a file from this site, so it takes a path, not a URL", dst, rule.status) |
| - } |
| - u, err := url.Parse(dst) |
| - if err != nil || u.Host == "" || u.User != nil { |
| - return rule, fmt.Errorf("destination %q: not a usable URL", dst) |
| - } |
| - i := len(u.Scheme) + len("://") + len(u.Host) |
| - if !strings.EqualFold(dst[:i], u.Scheme+"://"+u.Host) { |
| - return rule, fmt.Errorf("destination %q: not a usable URL", dst) |
| - } |
| - rule.prefix, rule.to = dst[:i], dst[i:] |
| - if rule.to != "" && !strings.ContainsAny(rule.to[:1], "/?#") { |
| - return rule, fmt.Errorf("destination %q: not a usable URL", dst) |
| - } |
| - case strings.HasPrefix(dst, "//") || strings.HasPrefix(dst, "/\\"): |
| - return rule, fmt.Errorf("destination %q: name another host with https://", dst) |
| - case strings.HasPrefix(dst, "/"): |
| - rule.to = dst |
| - default: |
| - return rule, fmt.Errorf("destination %q: want a path starting with / or an http(s) URL", dst) |
| - } |
| - return rule, nil |
| + return pp, nil |
| } |
| // validPlaceholder: a placeholder name is letters, digits and underscores. |
| @@ -255,17 +274,26 @@ func validPlaceholder(name string) bool { |
| return true |
| } |
| -// match reports whether the rule applies to a request for host whose cleaned |
| -// path splits into reqSegs, and returns what the source captured. |
| -func (rule *redirectRule) match(host string, reqSegs []string) (map[string]string, bool) { |
| - if rule.host != "" && rule.host != host { |
| +// pathSegs splits a cleaned request path into the segments a pattern matches |
| +// against: nil for the root. |
| +func pathSegs(p string) []string { |
| + if p == "/" { |
| + return nil |
| + } |
| + return strings.Split(p[1:], "/") |
| +} |
| + |
| +// match reports whether the pattern applies to a request for host whose |
| +// cleaned path splits into reqSegs, and returns what it captured. |
| +func (pp *pathPattern) match(host string, reqSegs []string) (map[string]string, bool) { |
| + if pp.host != "" && pp.host != host { |
| return nil, false |
| } |
| - if len(reqSegs) < len(rule.segs) || !rule.splat && len(reqSegs) != len(rule.segs) { |
| + if len(reqSegs) < len(pp.segs) || !pp.splat && len(reqSegs) != len(pp.segs) { |
| return nil, false |
| } |
| var caps map[string]string |
| - for i, seg := range rule.segs { |
| + for i, seg := range pp.segs { |
| if strings.HasPrefix(seg, ":") { |
| if caps == nil { |
| caps = map[string]string{} |
| @@ -275,11 +303,11 @@ func (rule *redirectRule) match(host string, reqSegs []string) (map[string]strin |
| return nil, false |
| } |
| } |
| - if rule.splat { |
| + if pp.splat { |
| if caps == nil { |
| caps = map[string]string{} |
| } |
| - caps["splat"] = strings.Join(reqSegs[len(rule.segs):], "/") |
| + caps["splat"] = strings.Join(reqSegs[len(pp.segs):], "/") |
| } |
| return caps, true |
| } |
| @@ -343,10 +371,7 @@ func (rs *redirectRules) resolve(host, p, rawPath, rawQuery string, exists bool) |
| if rs == nil || len(rs.rules) == 0 { |
| return redirectAction{}, false |
| } |
| - var reqSegs []string |
| - if p != "/" { |
| - reqSegs = strings.Split(p[1:], "/") |
| - } |
| + reqSegs := pathSegs(p) |
| for i := range rs.rules { |
| rule := &rs.rules[i] |
| if exists && !rule.force { |
| @@ -419,10 +444,7 @@ func (rs *redirectRules) names(host, p string) bool { |
| if rs == nil { |
| return false |
| } |
| - var reqSegs []string |
| - if p != "/" { |
| - reqSegs = strings.Split(p[1:], "/") |
| - } |
| + reqSegs := pathSegs(p) |
| rules: |
| for i := range rs.rules { |
| rule := &rs.rules[i] |