/
rootroute.go
238 lines (202 loc) · 4.21 KB
/
rootroute.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
// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package websocket
import (
"bytes"
"errors"
"fmt"
"net/http"
"strings"
)
const (
pathQuerySep = "?"
pathSep = '/'
pathParamPrefix = ':'
)
// List of route error values.
var (
ErrRouteInvMethod = errors.New("invalid method")
ErrRouteInvTarget = errors.New("invalid target")
ErrRouteDupParam = errors.New("duplicate parameter on route")
)
type rootRoute struct {
methodDelete *route
methodGet *route
methodPatch *route
methodPost *route
methodPut *route
}
// newRootRoute create and initialize each route's method with path "/" and
// nil handler.
func newRootRoute() (root *rootRoute) {
root = &rootRoute{
methodDelete: &route{
name: "/",
handler: nil,
isParam: false,
},
methodGet: &route{
name: "/",
handler: nil,
isParam: false,
},
methodPatch: &route{
name: "/",
handler: nil,
isParam: false,
},
methodPost: &route{
name: "/",
handler: nil,
isParam: false,
},
methodPut: &route{
name: "/",
handler: nil,
isParam: false,
},
}
return
}
func (root *rootRoute) getParent(method string) *route {
switch method {
case http.MethodDelete:
return root.methodDelete
case http.MethodGet:
return root.methodGet
case http.MethodPatch:
return root.methodPatch
case http.MethodPost:
return root.methodPost
case http.MethodPut:
return root.methodPut
}
return nil
}
// add new route handler by method and target.
//
// The method parameter is one of HTTP method that is allowed: DELETE, GET,
// PATCH, POST, or PUT.
// The target parameter is absolute path, MUST start with slash "/", and can
// contains parameter by prefixing it with colon ":". For example,
// "/book/:id", will be parsed into,
//
// {
// name:"book",
// childs: []*route{{
// name:"id",
// isParam:true,
// }}
// }`
func (root *rootRoute) add(method, target string, handler RouteHandler) (err error) {
var logp = `add`
if target[0] != pathSep {
return fmt.Errorf(`%s: %w`, logp, ErrRouteInvTarget)
}
method = strings.ToUpper(method)
var (
parent = root.getParent(method)
bb *bytes.Buffer
x int
started bool
isParam bool
)
if parent == nil {
return fmt.Errorf(`%s: %w`, logp, ErrRouteInvMethod)
}
bb = _bbPool.Get().(*bytes.Buffer)
bb.Reset()
started = true
for x = 1; x < len(target); x++ {
if started && target[x] == pathParamPrefix {
isParam = true
started = false
continue
}
if target[x] != pathSep {
_ = bb.WriteByte(target[x])
continue
}
if bb.Len() == 0 {
started = true
isParam = false
continue
}
parent, err = parent.addChild(isParam, bb.String())
if err != nil {
goto out
}
bb.Reset()
started = true
isParam = false
}
if bb.Len() > 0 {
parent, err = parent.addChild(isParam, bb.String())
if err != nil {
goto out
}
}
parent.handler = handler
out:
_bbPool.Put(bb)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
return nil
}
// get the route parameters values and their handler.
func (root *rootRoute) get(method, target string) (params targetParam, handler RouteHandler) {
if len(method) == 0 || len(target) == 0 {
return nil, nil
}
if target[0] != pathSep {
return nil, nil
}
method = strings.ToUpper(method)
var (
parent = root.getParent(method)
child *route
bb *bytes.Buffer
x int
)
if parent == nil {
return nil, nil
}
bb = _bbPool.Get().(*bytes.Buffer)
bb.Reset()
params = make(targetParam)
for x = 1; x < len(target); x++ {
if target[x] != pathSep {
_ = bb.WriteByte(target[x])
continue
}
child = parent.getChild(false, bb.String())
if child == nil {
child = parent.getChildAsParam()
if child == nil {
params = nil
goto out
}
params[child.name] = bb.String()
}
parent = child
bb.Reset()
}
if bb.Len() > 0 {
child = parent.getChild(false, bb.String())
if child == nil {
child = parent.getChildAsParam()
if child == nil {
params = nil
goto out
}
params[child.name] = bb.String()
}
parent = child
}
handler = parent.handler
out:
_bbPool.Put(bb)
return params, handler
}