forked from julienschmidt/httprouter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
281 lines (251 loc) · 8.55 KB
/
router.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
// Copyright 2023 Christian Stewart <[email protected]>
// Copyright 2013 Julien Schmidt. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
// Package pathrouter is a trie based high performance path request router.
//
// The router matches incoming requests by the request path.
// If a handle is registered for the path, it is called with the path params.
//
// The registered path, against which the router matches incoming requests, can
// contain two types of parameters:
//
// Syntax Type
// :name named parameter
// *name catch-all parameter
//
// Named parameters are dynamic path segments. They match anything until the
// next '/' or the path end:
//
// Path: /blog/:category/:post
//
// Requests:
// /blog/go/request-routers match: category="go", post="request-routers"
// /blog/go/request-routers/ no match, but the router would redirect
// /blog/go/ no match
// /blog/go/request-routers/comments no match
//
// Catch-all parameters match anything until the path end, including the
// directory index (the '/' before the catch-all). Since they match anything
// until the end, catch-all parameters must always be the final path element.
//
// Path: /files/*filepath
//
// Requests:
// /files/ match: filepath="/"
// /files/LICENSE match: filepath="/LICENSE"
// /files/templates/article.html match: filepath="/templates/article.html"
// /files no match, but the router would redirect
//
// The value of parameters is saved as a slice of the Param struct, consisting
// each of a key and a value. The slice is passed to the Handle func as a third
// parameter.
// There are two ways to retrieve the value of a parameter:
//
// // by the name of the parameter
// user := ps.ByName("user") // defined by :user or *user
//
// // by the index of the parameter. This way you can also get the name (key)
// thirdKey := ps[2].Key // the name of the 3rd parameter
// thirdValue := ps[2].Value // the value of the 3rd parameter
package pathrouter
import (
"context"
"sync"
)
// Handle is a function that can be registered to a route to handle requests.
// Returns if the request was handled and any error.
// If false, nil is returned, assumes the function has returned "not found."
type Handle[W any] func(ctx context.Context, reqPath string, p Params, rw W) (bool, error)
// Param is a single URL parameter, consisting of a key and a value.
type Param struct {
Key string
Value string
}
// Params is a Param-slice, as returned by the router.
// The slice is ordered, the first URL parameter is also the first slice value.
// It is therefore safe to read values by the index.
type Params []Param
// ByName returns the value of the first Param which key matches the given name.
// If no matching Param is found, an empty string is returned.
func (ps Params) ByName(name string) string {
for _, p := range ps {
if p.Key == name {
return p.Value
}
}
return ""
}
// RouterConfig are optional configuration parameters for the Router.
type RouterConfig[W any] struct {
// Enables automatic redirection if the current route can't be matched but a
// handler for the path with (without) the trailing slash exists.
// For example if /foo/ is requested but a route only exists for /foo, the
// client is redirected to /foo
RedirectTrailingSlash bool
// RedirectFixedPath configures the router to fix the current request path, if no
// handle is registered for it.
// First superfluous path elements like ../ or // are removed.
// Afterwards the router does a case-insensitive lookup of the cleaned path.
// If a handle can be found for this route, the router makes a redirection
// to the corrected path.
// For example /FOO and /..//Foo could be redirected to /foo.
// RedirectTrailingSlash is independent of this option.
RedirectFixedPath bool
// NotFound is called when no matching route is found.
NotFound Handle[W]
// Function to handle panics recovered from handlers.
// If nil, no recover() will be called (panics will throw).
// The fourth parameter is the error from recover().
PanicHandler func(ctx context.Context, reqPath string, rw W, panicErr interface{})
}
// Router can be used to dispatch URL requests to different handler functions
// via configurable routes. The request and response types are generic.
//
// R is the request type.
// W is the response writer type.
type Router[W any] struct {
conf RouterConfig[W]
tree *node[W]
paramsPool sync.Pool
maxParams uint16
}
// DefaultConfig returns the default configuration if none is specified.
// Path auto-correction, including trailing slashes, is enabled by default.
func DefaultConfig[W any]() RouterConfig[W] {
return RouterConfig[W]{
RedirectTrailingSlash: true,
RedirectFixedPath: true,
}
}
// New returns a new Router with default configuration.
// Path auto-correction, including trailing slashes, is enabled by default.
func New[W any]() *Router[W] {
return NewWithConfig(DefaultConfig[W]())
}
// NewWithConfig constructs a new Router with the given config.
//
// All configuration values are defaulted to false.
func NewWithConfig[W any](conf RouterConfig[W]) *Router[W] {
return &Router[W]{
conf: conf,
}
}
func (r *Router[W]) getParams() *Params {
ps, _ := r.paramsPool.Get().(*Params)
*ps = (*ps)[0:0] // reset slice
return ps
}
func (r *Router[W]) putParams(ps *Params) {
if ps != nil {
r.paramsPool.Put(ps)
}
}
// AddHandler registers a new request handle with the given path.
func (r *Router[W]) AddHandler(path string, handle Handle[W]) {
if handle == nil {
return
}
if len(path) == 0 {
path = "/"
} else if path[0] != '/' {
path = "/" + path
}
var varsCount uint16
root := r.tree
if root == nil {
root = new(node[W])
r.tree = root
}
root.addRoute(path, handle)
// Update maxParams
if paramsCount := countParams(path); paramsCount+varsCount > r.maxParams {
r.maxParams = paramsCount + varsCount
}
// Lazy-init paramsPool alloc func
if r.paramsPool.New == nil && r.maxParams > 0 {
r.paramsPool.New = func() interface{} {
ps := make(Params, 0, r.maxParams)
return &ps
}
}
}
// recoverPanic recovers from a panic while processing a path.
func (r *Router[W]) recoverPanic(ctx context.Context, reqPath string, wr W) {
defer func() {
// if the panic handler panics, just give up :)
_ = recover()
}()
if rcv := recover(); rcv != nil && r.conf.PanicHandler != nil {
r.conf.PanicHandler(ctx, reqPath, wr, rcv)
}
}
// LookupPath allows the manual lookup of a handler with a path.
// This is e.g. useful to build a framework around this router.
// If the path was found, it returns the handle function and the path parameter
// values. Otherwise the third return value indicates whether a redirection to
// the same path with an extra / without the trailing slash should be performed.
func (r *Router[W]) LookupPath(path string) (Handle[W], Params, bool) {
if root := r.tree; root != nil {
handle, ps, tsr := root.getValue(path, r.getParams)
if handle == nil {
r.putParams(ps)
return nil, nil, tsr
}
if ps == nil {
return handle, nil, tsr
}
return handle, *ps, tsr
}
return nil, nil, false
}
// Serve serves a request with the router.
// Returns if the request was handled and any error.
// Note: if the error handler is set, may return true even if not found.
func (r *Router[W]) Serve(ctx context.Context, reqPath string, wr W) (bool, error) {
if r.conf.PanicHandler != nil {
defer r.recoverPanic(ctx, reqPath, wr)
}
if reqPath == "" {
reqPath = "/"
}
if root := r.tree; root != nil {
if handle, ps, tsr := root.getValue(reqPath, r.getParams); handle != nil {
var params Params
if ps != nil {
params = *ps
defer r.putParams(ps)
}
found, handlerErr := handle(ctx, reqPath, params, wr)
if found || handlerErr != nil {
return found, handlerErr
}
} else if reqPath != "/" && reqPath != "" {
if tsr && r.conf.RedirectTrailingSlash {
if len(reqPath) > 1 && reqPath[len(reqPath)-1] == '/' {
reqPath = reqPath[:len(reqPath)-1]
} else {
reqPath = reqPath + "/"
}
return r.Serve(ctx, reqPath, wr)
}
// Try to fix the request path
if r.conf.RedirectFixedPath {
fixedPath, fixedFound := root.findCaseInsensitivePath(
CleanPath(reqPath),
r.conf.RedirectTrailingSlash,
)
if fixedFound {
reqPath = fixedPath
return r.Serve(ctx, reqPath, wr)
}
}
}
}
// not found
if r.conf.NotFound == nil {
return false, nil
}
return r.conf.NotFound(ctx, reqPath, nil, wr)
}