-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
255 lines (218 loc) · 6.37 KB
/
tree.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
package httprouter
import (
"net/http"
"strings"
"unicode"
)
type treeNode struct {
pathFragment string
static []*treeNode
variable *treeNode
wildcard *treeNode
handlers *methodHandlers
}
func (this *treeNode) Add(route Route) error {
if route.AllowedMethods == MethodNone {
return ErrUnknownMethod
}
if len(route.Path) == 0 {
if this.handlers == nil {
this.handlers = &methodHandlers{}
}
return this.handlers.Add(route.AllowedMethods, route.Handler)
}
if route.Path[0] == '/' {
route.Path = route.Path[1:]
} else {
return ErrMalformedPath
}
var pathFragmentForChildNode string
slashIndex := strings.Index(route.Path, "/")
if slashIndex == 0 {
return ErrMalformedPath // first character is a slash, that means the URL provided looks something like this: /path/to//document (note the double slash)
} else if slashIndex == -1 {
pathFragmentForChildNode = route.Path
} else {
pathFragmentForChildNode = route.Path[0:slashIndex]
}
if !hasOnlyAllowedCharacters(pathFragmentForChildNode) {
return ErrInvalidCharacters
} else if strings.HasPrefix(pathFragmentForChildNode, "*") {
return this.addWildcard(route, pathFragmentForChildNode)
} else if strings.HasPrefix(pathFragmentForChildNode, ":") {
return this.addVariable(route, pathFragmentForChildNode)
} else {
return this.addStatic(route, pathFragmentForChildNode)
}
}
func (this *treeNode) addWildcard(route Route, pathFragment string) error {
if this.wildcard == nil {
this.wildcard = &treeNode{pathFragment: pathFragment}
}
if len(route.Path) > 1 {
return ErrInvalidWildcard // must only be "*"
}
route.Path = ""
return this.wildcard.Add(route)
}
func (this *treeNode) addVariable(route Route, pathFragment string) error {
if this.variable == nil {
this.variable = &treeNode{pathFragment: pathFragment}
}
route.Path = route.Path[len(pathFragment):]
return this.variable.Add(route)
}
func (this *treeNode) addStatic(route Route, pathFragment string) (err error) {
route.Path = route.Path[len(pathFragment):]
for _, staticChild := range this.static {
if staticChild.pathFragment == pathFragment {
return staticChild.Add(route)
}
}
staticChild := &treeNode{pathFragment: pathFragment}
if err = staticChild.Add(route); err != nil {
return err
}
this.static = append(this.static, staticChild)
return nil
}
func hasOnlyAllowedCharacters(input string) bool {
for index, value := range input {
if unicode.IsLetter(value) {
continue // TODO: ASCII only (a-z A-Z)
} else if unicode.IsDigit(value) {
continue // TODO: ASCII only (0-9)
} else if value == '.' || value == '-' || value == '_' {
continue
} else if index == 0 && (value == '*' || value == ':') {
continue
}
return false
}
return true
}
func (this *treeNode) Resolve(method, incomingPath string) (http.Handler, bool) {
if len(incomingPath) == 0 {
if this.handlers == nil {
return nil, false
} else {
return this.handlers.Resolve(method), true
}
}
if incomingPath[0] == '/' {
incomingPath = incomingPath[1:]
}
var handler http.Handler
var staticResourceExists, variableResourceExists bool
var pathFragment = parsePathFragment(incomingPath)
for _, staticChild := range this.static {
if pathFragment != staticChild.pathFragment {
continue
}
// the path fragment DOES match...
remainingPath := incomingPath[len(pathFragment):]
if handler, staticResourceExists = staticChild.Resolve(method, remainingPath); handler != nil {
return handler, staticResourceExists
}
break // don't bother checking any more of siblings of the static child, they don't match
}
if this.variable != nil {
remainingPath := incomingPath[len(pathFragment):]
if handler, variableResourceExists = this.variable.Resolve(method, remainingPath); handler != nil {
return handler, variableResourceExists
}
}
if this.wildcard != nil {
return this.wildcard.Resolve(method, "")
}
return nil, staticResourceExists || variableResourceExists
}
func parsePathFragment(value string) string {
if index := strings.Index(value, "/"); index == -1 {
return value
} else {
return value[0:index]
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type methodHandlers struct {
Get http.Handler
Head http.Handler
Post http.Handler
Put http.Handler
Delete http.Handler
Connect http.Handler
Options http.Handler
Trace http.Handler
Patch http.Handler
}
func (this *methodHandlers) Add(allowed Method, handler http.Handler) error {
if allowed&MethodGet == MethodGet && this.Get != nil {
return ErrRouteExists
} else if allowed&MethodGet == MethodGet {
this.Get = handler
}
if allowed&MethodHead == MethodHead && this.Head != nil {
return ErrRouteExists
} else if allowed&MethodHead == MethodHead {
this.Head = handler
}
if allowed&MethodPost == MethodPost && this.Post != nil {
return ErrRouteExists
} else if allowed&MethodPost == MethodPost {
this.Post = handler
}
if allowed&MethodPut == MethodPut && this.Put != nil {
return ErrRouteExists
} else if allowed&MethodPut == MethodPut {
this.Put = handler
}
if allowed&MethodDelete == MethodDelete && this.Delete != nil {
return ErrRouteExists
} else if allowed&MethodDelete == MethodDelete {
this.Delete = handler
}
if allowed&MethodConnect == MethodConnect && this.Connect != nil {
return ErrRouteExists
} else if allowed&MethodConnect == MethodConnect {
this.Connect = handler
}
if allowed&MethodOptions == MethodOptions && this.Options != nil {
return ErrRouteExists
} else if allowed&MethodOptions == MethodOptions {
this.Options = handler
}
if allowed&MethodTrace == MethodTrace && this.Trace != nil {
return ErrRouteExists
} else if allowed&MethodTrace == MethodTrace {
this.Trace = handler
}
if allowed&MethodPatch == MethodPatch && this.Patch != nil {
return ErrRouteExists
} else if allowed&MethodPatch == MethodPatch {
this.Patch = handler
}
return nil
}
func (this *methodHandlers) Resolve(method string) http.Handler {
switch method {
case http.MethodGet:
return this.Get
case http.MethodHead:
return this.Head
case http.MethodPost:
return this.Post
case http.MethodPut:
return this.Put
case http.MethodDelete:
return this.Delete
case http.MethodConnect:
return this.Connect
case http.MethodOptions:
return this.Options
case http.MethodTrace:
return this.Trace
default:
return this.Patch
}
}