forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
236 lines (215 loc) · 4.08 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
package echo
import "net/http"
type (
router struct {
trees map[string]*node
echo *Echo
}
node struct {
label byte
prefix string
parent *node
edges edges
handler HandlerFunc
echo *Echo
}
edges []*node
param struct {
Name string
Value string
}
Params []param
)
func NewRouter(e *Echo) (r *router) {
r = &router{
trees: make(map[string]*node),
echo: e,
}
for _, m := range methods {
r.trees[m] = &node{
prefix: "",
edges: edges{},
}
}
return
}
func (r *router) Add(method, path string, h HandlerFunc, echo *Echo) {
for i, l := 0, len(path); i < l; i++ {
if path[i] == ':' {
r.insert(method, path[:i], nil, echo)
for ; i < l && path[i] != '/'; i++ {
}
if i == l {
r.insert(method, path[:i], h, echo)
return
}
r.insert(method, path[:i], nil, echo)
} else if path[i] == '*' {
r.insert(method, path[:i], nil, echo)
r.insert(method, path[:l], h, echo)
}
}
r.insert(method, path, h, echo)
}
func (r *router) insert(method, path string, h HandlerFunc, echo *Echo) {
cn := r.trees[method] // Current node as root
search := path
for {
sl := len(search)
pl := len(cn.prefix)
l := lcp(search, cn.prefix)
if l == 0 {
// At root node
cn.label = search[0]
cn.prefix = search
if h != nil {
cn.handler = h
cn.echo = echo
}
} else if l < pl {
// Split node
n := newNode(cn.prefix[l:], cn, cn.edges, cn.handler, cn.echo)
cn.edges = edges{n} // Add to parent
// Reset parent node
cn.label = cn.prefix[0]
cn.prefix = cn.prefix[:l]
cn.handler = nil
cn.echo = nil
if l == sl {
// At parent node
cn.handler = h
cn.echo = echo
} else {
// Create child node
n = newNode(search[l:], cn, edges{}, h, echo)
cn.edges = append(cn.edges, n)
}
} else if l < sl {
search = search[l:]
e := cn.findEdge(search[0])
if e != nil {
// Go deeper
cn = e
continue
}
// Create child node
n := newNode(search, cn, edges{}, h, echo)
cn.edges = append(cn.edges, n)
} else {
// Node already exists
if h != nil {
cn.handler = h
cn.echo = echo
}
}
return
}
}
func newNode(pfx string, p *node, e edges, h HandlerFunc, echo *Echo) (n *node) {
n = &node{
label: pfx[0],
prefix: pfx,
parent: p,
edges: e,
handler: h,
echo: echo,
}
return
}
func (n *node) findEdge(l byte) *node {
for _, e := range n.edges {
if e.label == l {
return e
}
}
return nil
}
// Length of longest common prefix
func lcp(a, b string) (i int) {
max := len(a)
l := len(b)
if l < max {
max = l
}
for ; i < max && a[i] == b[i]; i++ {
}
return
}
func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *Echo) {
cn := r.trees[method] // Current node as root
search := path
n := 0 // Param count
// Search order static > param > catch-all
for {
if search == "" || search == cn.prefix {
// Found
h = cn.handler
echo = cn.echo
return
}
var e *node
pl := len(cn.prefix)
l := lcp(search, cn.prefix)
if l == pl {
search = search[l:]
} else if l < pl {
if cn.label != ':' {
goto Up
}
}
// Static node
e = cn.findEdge(search[0])
if e != nil {
cn = e
continue
}
// Param node
Param:
e = cn.findEdge(':')
if e != nil {
cn = e
i, l := 0, len(search)
for ; i < l && search[i] != '/'; i++ {
}
params[n].Name = cn.prefix[1:]
params[n].Value = search[:i]
n++
search = search[i:]
continue
}
// Catch-all node
e = cn.findEdge('*')
if e != nil {
cn = e
p := params[:n+1]
p[n].Name = "_name"
p[n].Value = search
search = "" // End search
continue
}
Up:
tn := cn // Save current node
cn = cn.parent
if cn == nil {
// Not found
return
}
// Search upwards
if l == pl {
// Reset search
search = tn.prefix + search
}
goto Param
}
}
func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
c := r.echo.pool.Get().(*Context)
h, _ := r.Find(req.Method, req.URL.Path, c.params)
c.Response.Writer = w
if h != nil {
h(c)
} else {
r.echo.notFoundHandler(c)
}
r.echo.pool.Put(c)
}