-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
414 lines (349 loc) · 8.86 KB
/
routes.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright 2017 The Prizem Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package routerstore
import (
"errors"
"regexp"
"strings"
)
// HTTP method string values
const (
CONNECT = "CONNECT"
DELETE = "DELETE"
GET = "GET"
HEAD = "HEAD"
OPTIONS = "OPTIONS"
PATCH = "PATCH"
POST = "POST"
PUT = "PUT"
TRACE = "TRACE"
)
var (
// ErrNotFound denotes that a route for a given request path was not found.
ErrNotFound = errors.New("Route not found")
// ErrBadSyntax denotes that an invalid pattern was passed.
ErrBadSyntax = errors.New("Path contained invalid syntax")
// ErrWildcardMisplaced denotes that a wildcard was encountered before the end of the pattern.
ErrWildcardMisplaced = errors.New("Wildcard must be at the end of the path")
)
type (
route struct {
// Static routes
indices []string
static []*route
staticMap map[string]*route
// Regex/Variable routes
variables []*variableRoute
variable *route
// Result fields
data interface{}
paramNames []string
// Wildcard flag
wildcard bool
}
variableRoute struct {
expr string
route *route
regex *regexp.Regexp
}
// RouteMux stores root level routes per HTTP method.
RouteMux struct {
methods map[string]*route
}
// Param encapsulates a name/value pair.
Param struct {
Name string
Value string
}
// Result encapsulates the details and path parameters returned by the Details method.
Result struct {
Data interface{}
Params []Param
params [10]Param // internal array that initially backs Params to prevent allocations
}
)
// New creates a new RouteMux
func New() *RouteMux {
return &RouteMux{
methods: make(map[string]*route, 10),
}
}
// GET adds a new route for GET requests.
func (m *RouteMux) GET(pattern string, details interface{}) error {
return m.AddRoute(GET, pattern, details)
}
// PUT adds a new route for PUT requests.
func (m *RouteMux) PUT(pattern string, details interface{}) error {
return m.AddRoute(PUT, pattern, details)
}
// DELETE adds a new route for DELETE requests.
func (m *RouteMux) DELETE(pattern string, details interface{}) error {
return m.AddRoute(DELETE, pattern, details)
}
// PATCH adds a new route for PATCH requests.
func (m *RouteMux) PATCH(pattern string, details interface{}) error {
return m.AddRoute(PATCH, pattern, details)
}
// POST adds a new route for POST requests.
func (m *RouteMux) POST(pattern string, details interface{}) error {
return m.AddRoute(POST, pattern, details)
}
// AddRoute adds a new route to that stores to the provided data.
func (m *RouteMux) AddRoute(method string, pattern string, data interface{}) error {
// Remove leading and trailing slashes and split the url into sections.
l := len(pattern)
for l > 0 && pattern[0] == '/' {
pattern = pattern[1:]
l--
}
for l > 0 && pattern[l-1] == '/' {
pattern = pattern[:l-1]
l--
}
// Initialize methods map, if needed.
if m.methods == nil {
m.methods = make(map[string]*route, 10)
}
// Get root route from method map.
r, ok := m.methods[method]
if !ok {
r = &route{}
m.methods[method] = r
}
if l == 0 {
r.data = data
return nil
}
parts := strings.Split(pattern, "/")
// Check for misplaced wildcard parts.
for i, part := range parts {
if part == "*" && i != len(parts)-1 {
return ErrWildcardMisplaced
}
}
// Create a slice to capture path parameter names.
var paramNames = make([]string, 0, 10)
walk:
for _, part := range parts {
if len(part) == 0 {
return ErrBadSyntax
}
// Find params that start with ":" and create variable routes.
if part[0] == ':' {
// Variable part
expr := ""
// A user may choose to override the defult expression
// similar to expressjs: ‘/user/:id([0-9]+)’
if index := strings.Index(part, "("); index != -1 {
expr = part[index:]
part = part[:index]
}
paramNames = append(paramNames, part[1:])
if expr == "" {
// No custom regexp defined.
if r.variable != nil {
r = r.variable
continue walk
}
// Set the non-regexp variable route.
next := &route{}
r.variable = next
r = next
} else {
// Find existing regexp.
for _, v := range r.variables {
if v.expr == expr {
r = v.route
continue walk
}
}
// Compile the new expression.
regex, regexErr := regexp.Compile(expr)
if regexErr != nil {
return regexErr
}
// Create the new variable route.
next := &variableRoute{
expr: expr,
route: &route{},
regex: regex,
}
// Initialize the variable routes slice, if needed.
if r.variables == nil {
r.variables = make([]*variableRoute, 0, 1)
}
r.variables = append(r.variables, next)
r = next.route
}
} else if part == "*" {
// Wildcard part
r.wildcard = true
} else {
// Static part
var next *route
// If non-nil, use the static map to find an existing route.
if r.staticMap != nil {
next = r.staticMap[part]
if next == nil {
next = &route{}
}
r.staticMap[part] = next
} else {
// Initialize the parallel slices, if needed.
if r.indices == nil {
r.indices = make([]string, 0, 10)
r.static = make([]*route, 0, 10)
}
// Find existing static route.
for i, v := range r.indices {
if v == part {
next = r.static[i]
break
}
}
// A new route must be created.
if next == nil {
next = &route{}
// Is the count of static routes enough to warrant a map instead of a slice.
if len(r.indices) >= 5 {
// Convert the parallel slices to a map.
r.staticMap = make(map[string]*route, 25)
for i, v := range r.indices {
r.staticMap[v] = r.static[i]
}
r.staticMap[part] = next
// Allow slices to be GC'd
r.indices = nil
r.static = nil
} else {
r.indices = append(r.indices, part)
r.static = append(r.static, next)
}
}
}
r = next
}
}
// Set the data and parameter names.
r.data = data
r.paramNames = paramNames
return nil
}
// Match tries to find a match for the provided method and request path. If a match is found,
// details and path paramater values are set in result.
func (m *RouteMux) Match(method string, requestPath string, result *Result) error {
result.Data = nil
result.Params = result.params[:0]
// Get the root route from the methods map.
r, ok := m.methods[method]
if !ok {
return ErrNotFound
}
// Remove leading and trailing slashes from the request path.
l := len(requestPath)
for l > 0 && requestPath[0] == '/' {
requestPath = requestPath[1:]
l--
}
for l > 0 && requestPath[l-1] == '/' {
requestPath = requestPath[:l-1]
l--
}
if l == 0 {
result.Data = r.data
return nil
}
wildcard := false
var wildcardResult Result
var wildcardPath string
pathloop:
for {
// If this is a wildcard route, make copies of the state in case child routes don't match.
if r.wildcard {
wildcard = true
wildcardResult.Params = result.Params
wildcardResult.Data = r.data
wildcardPath = requestPath
}
// Extract next path part.
part := requestPath
index := strings.IndexByte(requestPath, '/')
if index > 0 {
part = part[0:index]
requestPath = requestPath[index+1:]
}
// Test static routes slice for a match.
for i, value := range r.indices {
if value == part {
r = r.static[i]
if index < 0 {
result.Data = r.data
break pathloop
}
continue pathloop
}
}
// Test static routes map for a match.
if r.staticMap != nil {
sr, ok := r.staticMap[part]
if ok {
r = sr
if index < 0 {
result.Data = r.data
break pathloop
}
continue
}
}
// Default to variable route but test regexp routes.
next := r.variable
for _, varRoute := range r.variables {
if varRoute.regex.MatchString(part) {
next = varRoute.route
break
}
}
// Not found, break out.
if next == nil {
break pathloop
}
// Append the path parameter value.
result.Params = append(result.Params, Param{
// Name is set below when the route is identified
Value: part,
})
r = next
// Break out if this is the last path part.
if index < 0 {
result.Data = r.data
break pathloop
}
}
// Revert result to wildcard if the path did not match a more specific route.
if result.Data == nil && wildcard {
*result = wildcardResult
result.Params = append(result.Params, Param{
Name: "*",
Value: wildcardPath,
})
}
// If a match was found, set all the path parameter names and return successfully.
if result.Data != nil {
for i, name := range r.paramNames {
result.Params[i].Name = name
}
return nil
}
// No match was found.
return ErrNotFound
}
// Param returns the path parameter value for a given name.
func (r *Result) Param(name string) string {
for _, p := range r.Params {
if p.Name == name {
return p.Value
}
}
return ""
}