blob: 0189ad346fd1c2b4e26850af12f4f8659cd863b7 [file] [log] [blame]
Jonathan Hartf86817b2018-08-17 10:35:54 -07001// Copyright 2012 The Gorilla 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
5package mux
6
7import (
8 "bytes"
9 "fmt"
10 "net/http"
11 "net/url"
12 "regexp"
13 "strconv"
14 "strings"
15)
16
17// newRouteRegexp parses a route template and returns a routeRegexp,
18// used to match a host, a path or a query string.
19//
20// It will extract named variables, assemble a regexp to be matched, create
21// a "reverse" template to build URLs and compile regexps to validate variable
22// values used in URL building.
23//
24// Previously we accepted only Python-like identifiers for variable
25// names ([a-zA-Z_][a-zA-Z0-9_]*), but currently the only restriction is that
26// name and pattern can't be empty, and names can't contain a colon.
27func newRouteRegexp(tpl string, matchHost, matchPrefix, matchQuery, strictSlash, useEncodedPath bool) (*routeRegexp, error) {
28 // Check if it is well-formed.
29 idxs, errBraces := braceIndices(tpl)
30 if errBraces != nil {
31 return nil, errBraces
32 }
33 // Backup the original.
34 template := tpl
35 // Now let's parse it.
36 defaultPattern := "[^/]+"
37 if matchQuery {
38 defaultPattern = "[^?&]*"
39 } else if matchHost {
40 defaultPattern = "[^.]+"
41 matchPrefix = false
42 }
43 // Only match strict slash if not matching
44 if matchPrefix || matchHost || matchQuery {
45 strictSlash = false
46 }
47 // Set a flag for strictSlash.
48 endSlash := false
49 if strictSlash && strings.HasSuffix(tpl, "/") {
50 tpl = tpl[:len(tpl)-1]
51 endSlash = true
52 }
53 varsN := make([]string, len(idxs)/2)
54 varsR := make([]*regexp.Regexp, len(idxs)/2)
55 pattern := bytes.NewBufferString("")
56 pattern.WriteByte('^')
57 reverse := bytes.NewBufferString("")
58 var end int
59 var err error
60 for i := 0; i < len(idxs); i += 2 {
61 // Set all values we are interested in.
62 raw := tpl[end:idxs[i]]
63 end = idxs[i+1]
64 parts := strings.SplitN(tpl[idxs[i]+1:end-1], ":", 2)
65 name := parts[0]
66 patt := defaultPattern
67 if len(parts) == 2 {
68 patt = parts[1]
69 }
70 // Name or pattern can't be empty.
71 if name == "" || patt == "" {
72 return nil, fmt.Errorf("mux: missing name or pattern in %q",
73 tpl[idxs[i]:end])
74 }
75 // Build the regexp pattern.
76 fmt.Fprintf(pattern, "%s(?P<%s>%s)", regexp.QuoteMeta(raw), varGroupName(i/2), patt)
77
78 // Build the reverse template.
79 fmt.Fprintf(reverse, "%s%%s", raw)
80
81 // Append variable name and compiled pattern.
82 varsN[i/2] = name
83 varsR[i/2], err = regexp.Compile(fmt.Sprintf("^%s$", patt))
84 if err != nil {
85 return nil, err
86 }
87 }
88 // Add the remaining.
89 raw := tpl[end:]
90 pattern.WriteString(regexp.QuoteMeta(raw))
91 if strictSlash {
92 pattern.WriteString("[/]?")
93 }
94 if matchQuery {
95 // Add the default pattern if the query value is empty
96 if queryVal := strings.SplitN(template, "=", 2)[1]; queryVal == "" {
97 pattern.WriteString(defaultPattern)
98 }
99 }
100 if !matchPrefix {
101 pattern.WriteByte('$')
102 }
103 reverse.WriteString(raw)
104 if endSlash {
105 reverse.WriteByte('/')
106 }
107 // Compile full regexp.
108 reg, errCompile := regexp.Compile(pattern.String())
109 if errCompile != nil {
110 return nil, errCompile
111 }
112
113 // Check for capturing groups which used to work in older versions
114 if reg.NumSubexp() != len(idxs)/2 {
115 panic(fmt.Sprintf("route %s contains capture groups in its regexp. ", template) +
116 "Only non-capturing groups are accepted: e.g. (?:pattern) instead of (pattern)")
117 }
118
119 // Done!
120 return &routeRegexp{
121 template: template,
122 matchHost: matchHost,
123 matchQuery: matchQuery,
124 strictSlash: strictSlash,
125 useEncodedPath: useEncodedPath,
126 regexp: reg,
127 reverse: reverse.String(),
128 varsN: varsN,
129 varsR: varsR,
130 }, nil
131}
132
133// routeRegexp stores a regexp to match a host or path and information to
134// collect and validate route variables.
135type routeRegexp struct {
136 // The unmodified template.
137 template string
138 // True for host match, false for path or query string match.
139 matchHost bool
140 // True for query string match, false for path and host match.
141 matchQuery bool
142 // The strictSlash value defined on the route, but disabled if PathPrefix was used.
143 strictSlash bool
144 // Determines whether to use encoded path from getPath function or unencoded
145 // req.URL.Path for path matching
146 useEncodedPath bool
147 // Expanded regexp.
148 regexp *regexp.Regexp
149 // Reverse template.
150 reverse string
151 // Variable names.
152 varsN []string
153 // Variable regexps (validators).
154 varsR []*regexp.Regexp
155}
156
157// Match matches the regexp against the URL host or path.
158func (r *routeRegexp) Match(req *http.Request, match *RouteMatch) bool {
159 if !r.matchHost {
160 if r.matchQuery {
161 return r.matchQueryString(req)
162 }
163 path := req.URL.Path
164 if r.useEncodedPath {
165 path = getPath(req)
166 }
167 return r.regexp.MatchString(path)
168 }
169
170 return r.regexp.MatchString(getHost(req))
171}
172
173// url builds a URL part using the given values.
174func (r *routeRegexp) url(values map[string]string) (string, error) {
175 urlValues := make([]interface{}, len(r.varsN))
176 for k, v := range r.varsN {
177 value, ok := values[v]
178 if !ok {
179 return "", fmt.Errorf("mux: missing route variable %q", v)
180 }
181 urlValues[k] = value
182 }
183 rv := fmt.Sprintf(r.reverse, urlValues...)
184 if !r.regexp.MatchString(rv) {
185 // The URL is checked against the full regexp, instead of checking
186 // individual variables. This is faster but to provide a good error
187 // message, we check individual regexps if the URL doesn't match.
188 for k, v := range r.varsN {
189 if !r.varsR[k].MatchString(values[v]) {
190 return "", fmt.Errorf(
191 "mux: variable %q doesn't match, expected %q", values[v],
192 r.varsR[k].String())
193 }
194 }
195 }
196 return rv, nil
197}
198
199// getURLQuery returns a single query parameter from a request URL.
200// For a URL with foo=bar&baz=ding, we return only the relevant key
201// value pair for the routeRegexp.
202func (r *routeRegexp) getURLQuery(req *http.Request) string {
203 if !r.matchQuery {
204 return ""
205 }
206 templateKey := strings.SplitN(r.template, "=", 2)[0]
207 for key, vals := range req.URL.Query() {
208 if key == templateKey && len(vals) > 0 {
209 return key + "=" + vals[0]
210 }
211 }
212 return ""
213}
214
215func (r *routeRegexp) matchQueryString(req *http.Request) bool {
216 return r.regexp.MatchString(r.getURLQuery(req))
217}
218
219// braceIndices returns the first level curly brace indices from a string.
220// It returns an error in case of unbalanced braces.
221func braceIndices(s string) ([]int, error) {
222 var level, idx int
223 var idxs []int
224 for i := 0; i < len(s); i++ {
225 switch s[i] {
226 case '{':
227 if level++; level == 1 {
228 idx = i
229 }
230 case '}':
231 if level--; level == 0 {
232 idxs = append(idxs, idx, i+1)
233 } else if level < 0 {
234 return nil, fmt.Errorf("mux: unbalanced braces in %q", s)
235 }
236 }
237 }
238 if level != 0 {
239 return nil, fmt.Errorf("mux: unbalanced braces in %q", s)
240 }
241 return idxs, nil
242}
243
244// varGroupName builds a capturing group name for the indexed variable.
245func varGroupName(idx int) string {
246 return "v" + strconv.Itoa(idx)
247}
248
249// ----------------------------------------------------------------------------
250// routeRegexpGroup
251// ----------------------------------------------------------------------------
252
253// routeRegexpGroup groups the route matchers that carry variables.
254type routeRegexpGroup struct {
255 host *routeRegexp
256 path *routeRegexp
257 queries []*routeRegexp
258}
259
260// setMatch extracts the variables from the URL once a route matches.
261func (v *routeRegexpGroup) setMatch(req *http.Request, m *RouteMatch, r *Route) {
262 // Store host variables.
263 if v.host != nil {
264 host := getHost(req)
265 matches := v.host.regexp.FindStringSubmatchIndex(host)
266 if len(matches) > 0 {
267 extractVars(host, matches, v.host.varsN, m.Vars)
268 }
269 }
270 path := req.URL.Path
271 if r.useEncodedPath {
272 path = getPath(req)
273 }
274 // Store path variables.
275 if v.path != nil {
276 matches := v.path.regexp.FindStringSubmatchIndex(path)
277 if len(matches) > 0 {
278 extractVars(path, matches, v.path.varsN, m.Vars)
279 // Check if we should redirect.
280 if v.path.strictSlash {
281 p1 := strings.HasSuffix(path, "/")
282 p2 := strings.HasSuffix(v.path.template, "/")
283 if p1 != p2 {
284 u, _ := url.Parse(req.URL.String())
285 if p1 {
286 u.Path = u.Path[:len(u.Path)-1]
287 } else {
288 u.Path += "/"
289 }
290 m.Handler = http.RedirectHandler(u.String(), 301)
291 }
292 }
293 }
294 }
295 // Store query string variables.
296 for _, q := range v.queries {
297 queryURL := q.getURLQuery(req)
298 matches := q.regexp.FindStringSubmatchIndex(queryURL)
299 if len(matches) > 0 {
300 extractVars(queryURL, matches, q.varsN, m.Vars)
301 }
302 }
303}
304
305// getHost tries its best to return the request host.
306func getHost(r *http.Request) string {
307 if r.URL.IsAbs() {
308 return r.URL.Host
309 }
310 host := r.Host
311 // Slice off any port information.
312 if i := strings.Index(host, ":"); i != -1 {
313 host = host[:i]
314 }
315 return host
316
317}
318
319func extractVars(input string, matches []int, names []string, output map[string]string) {
320 for i, name := range names {
321 output[name] = input[matches[2*i+2]:matches[2*i+3]]
322 }
323}