/
pat.go
302 lines (246 loc) · 8.21 KB
/
pat.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
298
299
300
301
302
/*
Package pat is a URL-matching domain-specific language for Goji.
Quick Reference
The following table gives an overview of the language this package accepts. See
the subsequent sections for a more detailed explanation of what each pattern
does.
Pattern Matches Does Not Match
/ / /hello
/hello /hello /hi
/hello/
/user/:name /user/carl /user/carl/photos
/user/alice /user/carl/
/user/
/:file.:ext /data.json /.json
/info.txt /data.
/data.tar.gz /data.json/download
/user/* /user/ /user
/user/carl
/user/carl/photos
Static Paths
Most URL paths may be specified directly: the pattern "/hello" matches URLs with
precisely that path ("/hello/", for instance, is treated as distinct).
Note that this package operates on raw (i.e., escaped) paths (see the
documentation for net/url.URL.EscapedPath). In order to match a character that
can appear escaped in a URL path, use its percent-encoded form.
Named Matches
Named matches allow URL paths to contain any value in a particular path segment.
Such matches are denoted by a leading ":", for example ":name" in the rule
"/user/:name", and permit any non-empty value in that position. For instance, in
the previous "/user/:name" example, the path "/user/carl" is matched, while
"/user/" or "/user/carl/" (note the trailing slash) are not matched. Pat rules
can contain any number of named matches.
Named matches set URL variables by comparing pattern names to the segments they
matched. In our "/user/:name" example, a request for "/user/carl" would bind the
"name" variable to the value "carl". Use the Param function to extract these
variables from the request context. Variable names in a single pattern must be
unique.
Matches are ordinarily delimited by slashes ("/"), but several other characters
are accepted as delimiters (with slightly different semantics): the period
("."), semicolon (";"), and comma (",") characters. For instance, given the
pattern "/:file.:ext", the request "/data.json" would match, binding "file" to
"data" and "ext" to "json". Note that these special characters are treated
slightly differently than slashes: the above pattern also matches the path
"/data.tar.gz", with "ext" getting set to "tar.gz"; and the pattern "/:file"
matches names with dots in them (like "data.json").
Prefix Matches
Pat can also match prefixes of routes using wildcards. Prefix wildcard routes
end with "/*", and match just the path segments preceding the asterisk. For
instance, the pattern "/user/*" will match "/user/" and "/user/carl/photos" but
not "/user" (note the lack of a trailing slash).
The unmatched suffix, including the leading slash ("/"), are placed into the
request context, which allows subsequent routing (e.g., a subrouter) to continue
from where this pattern left off. For instance, in the "/user/*" pattern from
above, a request for "/user/carl/photos" will consume the "/user" prefix,
leaving the path "/carl/photos" for subsequent patterns to handle. A subrouter
pattern for "/:name/photos" would match this remaining path segment, for
instance.
*/
package pat
import (
"net/http"
"regexp"
"sort"
"strings"
"goji.io/pattern"
"golang.org/x/net/context"
)
type patNames []struct {
name pattern.Variable
idx int
}
func (p patNames) Len() int {
return len(p)
}
func (p patNames) Less(i, j int) bool {
return p[i].name < p[j].name
}
func (p patNames) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
/*
Pattern implements goji.Pattern using a path-matching domain specific language.
See the package documentation for more information about the semantics of this
object.
*/
type Pattern struct {
raw string
methods map[string]struct{}
// These are parallel arrays of each pattern string (sans ":"), the
// breaks each expect afterwords (used to support e.g., "." dividers),
// and the string literals in between every pattern. There is always one
// more literal than pattern, and they are interleaved like this:
// <literal> <pattern> <literal> <pattern> <literal> etc...
pats patNames
breaks []byte
literals []string
wildcard bool
}
// "Break characters" are characters that can end patterns. They are not allowed
// to appear in pattern names. "/" was chosen because it is the standard path
// separator, and "." was chosen because it often delimits file extensions. ";"
// and "," were chosen because Section 3.3 of RFC 3986 suggests their use.
const bc = "/.;,"
var patternRe = regexp.MustCompile(`[` + bc + `]:([^` + bc + `]+)`)
/*
New returns a new Pattern from the given Pat route. See the package
documentation for more information about what syntax is accepted by this
function.
*/
func New(pat string) *Pattern {
p := &Pattern{raw: pat}
if strings.HasSuffix(pat, "/*") {
pat = pat[:len(pat)-1]
p.wildcard = true
}
matches := patternRe.FindAllStringSubmatchIndex(pat, -1)
numMatches := len(matches)
p.pats = make(patNames, numMatches)
p.breaks = make([]byte, numMatches)
p.literals = make([]string, numMatches+1)
n := 0
for i, match := range matches {
a, b := match[2], match[3]
p.literals[i] = pat[n : a-1] // Need to leave off the colon
p.pats[i].name = pattern.Variable(pat[a:b])
p.pats[i].idx = i
if b == len(pat) {
p.breaks[i] = '/'
} else {
p.breaks[i] = pat[b]
}
n = b
}
p.literals[numMatches] = pat[n:]
sort.Sort(p.pats)
return p
}
func newWithMethods(pat string, methods ...string) *Pattern {
p := New(pat)
methodSet := make(map[string]struct{}, len(methods))
for _, method := range methods {
methodSet[method] = struct{}{}
}
p.methods = methodSet
return p
}
/*
Match runs the Pat pattern on the given request, returning a non-nil context if
the request matches the request.
This function satisfies goji.Pattern.
*/
func (p *Pattern) Match(ctx context.Context, r *http.Request) context.Context {
if p.methods != nil {
if _, ok := p.methods[r.Method]; !ok {
return nil
}
}
// Check Path
path := pattern.Path(ctx)
var scratch []string
if p.wildcard {
scratch = make([]string, len(p.pats)+1)
} else if len(p.pats) > 0 {
scratch = make([]string, len(p.pats))
}
for i := range p.pats {
sli := p.literals[i]
if !strings.HasPrefix(path, sli) {
return nil
}
path = path[len(sli):]
m := 0
bc := p.breaks[i]
for ; m < len(path); m++ {
if path[m] == bc || path[m] == '/' {
break
}
}
if m == 0 {
// Empty strings are not matches, otherwise routes like
// "/:foo" would match the path "/"
return nil
}
scratch[i] = path[:m]
path = path[m:]
}
// There's exactly one more literal than pat.
tail := p.literals[len(p.pats)]
if p.wildcard {
if !strings.HasPrefix(path, tail) {
return nil
}
scratch[len(p.pats)] = path[len(tail)-1:]
} else if path != tail {
return nil
}
for i := range p.pats {
var err error
scratch[i], err = unescape(scratch[i])
if err != nil {
// If we encounter an encoding error here, there's
// really not much we can do about it with our current
// API, and I'm not really interested in supporting
// clients that misencode URLs anyways.
return nil
}
}
return &match{ctx, p, scratch}
}
/*
PathPrefix returns a string prefix that the Paths of all requests that this
Pattern accepts must contain.
This function satisfies goji's PathPrefix Pattern optimization.
*/
func (p *Pattern) PathPrefix() string {
return p.literals[0]
}
/*
HTTPMethods returns a set of HTTP methods that all requests that this
Pattern matches must be in, or nil if it's not possible to determine
which HTTP methods might be matched.
This function satisfies goji's HTTPMethods Pattern optimization.
*/
func (p *Pattern) HTTPMethods() map[string]struct{} {
return p.methods
}
/*
String returns the pattern string that was used to create this Pattern.
*/
func (p *Pattern) String() string {
return p.raw
}
/*
Param returns the bound parameter with the given name. For instance, given the
route:
/user/:name
and the URL Path:
/user/carl
a call to Param(ctx, "name") would return the string "carl". It is the caller's
responsibility to ensure that the variable has been bound. Attempts to access
variables that have not been set (or which have been invalidly set) are
considered programmer errors and will trigger a panic.
*/
func Param(ctx context.Context, name string) string {
return ctx.Value(pattern.Variable(name)).(string)
}