-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
349 lines (319 loc) · 7.5 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
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
package brouter
import (
"bytes"
"github.com/bysir-zl/bygo/util"
"github.com/valyala/fasthttp"
"regexp"
"sync"
)
const (
type_nomal = iota // 普通的无任何参数 直接判断是否相等
type_reg // 正则 如 id/(id:\d+?)/
type_tang // 语法糖 如 bong/*/
type_param // 有参数 但不是正则 如 /id/(id)/
)
const (
status_notfound = iota
status_methodnotallow
status_matched
)
type path struct {
path []byte // the full path
reg *regexp.Regexp // compiled path reg
regNames [][]byte // name with $1 $2
nodes []*Node
types int
method []byte
}
type Router struct {
root *Node
paths []path
handler404 Handler
handler405 Handler
}
// return self
func (p *Router) Any(path string, h ...Handler) *Node {
return p.root.Any(path, h...)
}
func (p *Router) Get(path string, h ...Handler) *Node {
return p.root.Get(path, h...)
}
func (p *Router) Post(path string, h ...Handler) *Node {
return p.root.Post(path, h...)
}
func (p *Router) Put(path string, h ...Handler) *Node {
return p.root.Put(path, h...)
}
func (p *Router) Delete(path string, h ...Handler) *Node {
return p.root.Delete(path, h...)
}
func (p *Router) Option(path string, h ...Handler) *Node {
return p.root.Option(path, h...)
}
func (p *Router) Head(path string, h ...Handler) *Node {
return p.root.Head(path, h...)
}
func (p *Router) Controller(path string, c interface{}) *Node {
return p.root.Controller(path, c)
}
// set 404 handler
func (p *Router) When404(h Handler) {
p.handler404 = h
}
// set 404 handler
func (p *Router) When405(h Handler) {
p.handler405 = h
}
func (p *Router) Use(h ...Handler) *Node {
return p.root.Use(h...)
}
func (p *Router) UseToChild(h ...Handler) *Node {
return p.root.UseToChild(h...)
}
// return child
func (p *Router) Group(path string, funs ...func(node *Node)) *Node {
return p.root.Group(path, funs...)
}
var pool sync.Pool
func (p *Router) Init() func(ctx *fasthttp.RequestCtx) {
pool.New = func() interface{} {
return &Context{}
}
p.paths = parse(p.root)
f := func(ctx *fasthttp.RequestCtx) {
c := pool.Get().(*Context)
c.RequestCtx = ctx
url := ctx.Path()
status, nodes, keys, values := match(url, ctx.Method(), p)
switch status {
case status_notfound:
when404(c)
case status_methodnotallow:
when405(c)
case status_matched:
c.rKeys = keys
c.rValues = values
run(nodes, c)
}
pool.Put(c)
}
return f
}
func New() *Router {
r := Router{}
node := &Node{
name: util.S2B("root"),
child: []*Node{},
path: []byte{},
}
r.root = node
r.handler404 = when404
r.handler405 = when405
return &r
}
// init
// parse full paths from router's node
func parse(root *Node) []path {
p([]byte{}, root, []*Node{})
pps := []path{}
for urlString, nodes := range paths {
url := util.S2B(urlString)
methodAurl := bytes.Split(url, []byte("::"))
url = methodAurl[1]
method := methodAurl[0]
var regNames [][]byte
var reg *regexp.Regexp
types := type_nomal
// 有 : 就表示是正则路由
if bytes.Contains(url, []byte{':'}) {
// 找出名字 (id:\d+?)
cname := regexp.MustCompile(`\((.+?):.+?\)`)
names := cname.FindAllSubmatch(url, -1)
regNames = [][]byte{}
for _, v := range names {
n := v[1]
regNames = append(regNames, n)
}
// 找出真正的正则表达式
c := regexp.MustCompile(`\(.+?:(.+?)\)`)
url2 := c.ReplaceAll(url, []byte("($1)"))
// 加上前后界限
// 加上? 因为 ^/v1/c/(.*?)/?$ 当无参数时(url: /v1/c/)也能匹配到
s := "^" + util.B2S(url2) + "?$"
//log.Print(s)
reg = regexp.MustCompile(s)
types = type_reg
} else if bytes.Contains(url, []byte{'('}) {
types = type_param
} else if lurl := len(url); lurl > 2 && url[lurl - 2] == '*' {
types = type_tang
}
p := path{
path: url,
types: types,
nodes: nodes,
method: method,
reg: reg,
regNames: regNames,
}
pps = append(pps, p)
}
return pps
}
var paths = map[string][]*Node{}
func p(url []byte, node *Node, path []*Node) {
url = append(url, node.path...)
path = append(path, node)
if node.child != nil && len(node.child) != 0 {
for i := range node.child {
p(url, node.child[i], path)
}
} else {
if url[len(url) - 1] != '/' {
url = append(url, '/')
}
u := string(node.method) + "::" + string(url)
paths[u] = path
}
}
func when404(c *Context) {
c.WriteString("404 not found")
c.SetStatusCode(404)
}
func when405(c *Context) {
c.WriteString("405 method not allowed")
c.SetStatusCode(405)
}
func run(nodes []*Node, c *Context) {
abort := false
for _, node := range nodes {
for _, handle := range node.handlers {
if c.abort {
abort = true
break
}
handle(c)
}
if abort {
break
}
}
}
// match router
func match(url []byte, method []byte, router *Router) (status int, nodes []*Node, keys [][]byte, values [][]byte) {
if len(url) == 0 {
url = []byte{'/'}
} else if url[len(url) - 1] != '/' {
url = append(url, '/')
}
for _, path := range router.paths {
status, keys, values := isMatched(path, url, method)
if status != 0 {
return status, path.nodes, keys, values
}
}
return
}
var rk = []byte{')'}
var lk = []byte{'('}
// match one url and return url params
func isMatched(path path, url []byte, method []byte) (status int, keys, values [][]byte) {
urlBase := path.path
switch path.types {
case type_tang:
// 语法糖
// 匹配 user/*/ => user/a/b/c/ => [0:a,1:b,2:c]
lbase := len(urlBase)
if bytes.Equal(url[:lbase - 2], urlBase[:lbase - 2]) {
status = status_methodnotallow
if path.method[0] == 'A' || bytes.Equal(method, path.method) {
status = status_matched
}
if len(url) == lbase - 2 {
return
}
kvs := bytes.Split(url[lbase - 2:len(url) - 1], []byte{'/'})
lkvs := len(kvs)
keys = make([][]byte, lkvs)
values = make([][]byte, lkvs)
for i := 0; i < lkvs; i++ {
//params[strconv.Itoa(i)] = v
keys[i] = []byte{byte(i + 49)}
values[i] = kvs[i]
}
return
}
case type_nomal:
if bytes.Equal(urlBase, url) {
status = status_methodnotallow
if path.method[0] == 'A' || bytes.Equal(method, path.method) {
status = status_matched
}
return
}
case type_param:
// user/(id)/
// user/123/
us := bytes.Split(urlBase, lk)
lus := len(us)
//ok = true
for i := 0; i < lus; i++ {
u := us[i]
if i == 0 {
// user/
if bytes.Index(url, u) != 0 {
status = 0
return
}
keys = make([][]byte, lus - 1)
values = make([][]byte, lus - 1)
// 132/
url = url[len(u):]
} else {
nameAsp := bytes.Split(u, rk)
// id
//name := nameAsp[0]
// /
//sp := nameAsp[1]
value := bytes.Split(url, nameAsp[1])
keys[i - 1] = nameAsp[0]
values[i - 1] = value[0]
if len(value[1]) == 0 {
// is end of url
if i == lus - 1 {
status = status_methodnotallow
if path.method[0] == 'A' || bytes.Equal(method, path.method) {
status = status_matched
}
return
} else {
// router url is not end, but url is end .
// has not enough params
status = 0
return
}
}
url = url[len(value[0]) + 1:]
}
}
return
case type_reg:
vs := path.reg.FindAllSubmatch(url, -1)
if len(vs) == 1 {
status = status_methodnotallow
if path.method[0] == 'A' || bytes.Equal(method, path.method) {
status = status_matched
}
k := vs[0][1:]
lk := len(k)
keys = make([][]byte, lk)
values = make([][]byte, lk)
for i := 0; i < lk; i++ {
keys[i] = path.regNames[i]
values[i] = k[i]
}
}
return
}
return
}