This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrest_proxy.go
467 lines (381 loc) · 11.3 KB
/
rest_proxy.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package edge
import (
"github.com/ronaksoft/rony"
"sort"
"strings"
)
/*
Creation Time: 2021 - Jul - 02
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
type RestHandler func(conn rony.RestConn, ctx *DispatchCtx) error
type RestProxy interface {
ClientMessage(conn rony.RestConn, ctx *DispatchCtx) error
ServerMessage(conn rony.RestConn, ctx *DispatchCtx) error
}
type restProxy struct {
cm RestHandler
sm RestHandler
}
func (r *restProxy) ClientMessage(conn rony.RestConn, ctx *DispatchCtx) error {
return r.cm(conn, ctx)
}
func (r *restProxy) ServerMessage(conn rony.RestConn, ctx *DispatchCtx) error {
return r.sm(conn, ctx)
}
func NewRestProxy(onClientMessage, onServerMessage RestHandler) *restProxy {
return &restProxy{
cm: onClientMessage,
sm: onServerMessage,
}
}
// restMux help to provide RESTFull wrappers around RPC handlers.
type restMux struct {
routes map[string]*trie
}
func (hp *restMux) Set(method, path string, f RestProxy) {
method = strings.ToUpper(method)
if hp.routes == nil {
hp.routes = make(map[string]*trie)
}
if _, ok := hp.routes[method]; !ok {
hp.routes[method] = &trie{
root: newTrieNode(),
hasRootWildcard: false,
}
}
hp.routes[method].Insert(path, WithTag(method), WithProxyFactory(f))
}
func (hp *restMux) Search(conn rony.RestConn) (string, RestProxy) {
r := hp.routes[strings.ToUpper(conn.Method())]
if r == nil {
return "", nil
}
n := r.Search(conn.Path(), conn)
if n == nil {
return "", nil
}
return n.key, n.Proxy
}
const (
// paramStart is the character, as a string, which a path pattern starts to define its named parameter.
paramStart = ":"
// wildcardParamStart is the character, as a string, which a path pattern starts to define
// its named parameter for wildcards. It allows everything else after that path prefix
// but the trie checks for static paths and named parameters before that in order to
// support everything that other implementations do not, and if nothing else found then it tries to
//find the closest wildcard path(super and unique).
wildcardParamStart = "*"
)
// trie contains the main logic for adding and searching nodes for path segments.
// It supports wildcard and named path parameters.
// trie supports very coblex and useful path patterns for routes.
// The trie checks for static paths(path without : or *) and named parameters before that in order to
// support everything that other implementations do not, and if nothing else found then it tries
// to find the closest wildcard path(super and unique).
type trie struct {
root *trieNode
// if true then it will handle any path if not other parent wildcard exists,
// so even 404 (on http services) is up to it, see trie#Insert.
hasRootWildcard bool
hasRootSlash bool
}
// InsertOption is just a function which accepts a pointer to a trieNode which can
// alt its `Handler`, `Tag` and `Data` fields.
// See `WithHandler`, `WithTag` and `WithData`.
type InsertOption func(*trieNode)
// WithProxyFactory sets the node's `Handler` field (useful for HTTP).
func WithProxyFactory(proxy RestProxy) InsertOption {
if proxy == nil {
panic("muxie/WithProxy: empty handler")
}
return func(n *trieNode) {
if n.Proxy == nil {
n.Proxy = proxy
}
}
}
// WithTag sets the node's `Tag` field (maybe useful for HTTP).
func WithTag(tag string) InsertOption {
return func(n *trieNode) {
if n.Tag == "" {
n.Tag = tag
}
}
}
// Insert adds a node to the trie.
func (t *trie) Insert(pattern string, options ...InsertOption) {
if pattern == "" {
panic("muxie/trie#Insert: empty pattern")
}
n := t.insert(pattern, "", nil, nil)
for _, opt := range options {
opt(n)
}
}
const (
pathSep = "/"
pathSepB = '/'
)
func slowPathSplit(path string) []string {
if path == pathSep {
return []string{pathSep}
}
// remove last sep if any.
if path[len(path)-1] == pathSepB {
path = path[:len(path)-1]
}
return strings.Split(path, pathSep)[1:]
}
func resolveStaticPart(key string) string {
i := strings.Index(key, paramStart)
if i == -1 {
i = strings.Index(key, wildcardParamStart)
}
if i == -1 {
i = len(key)
}
return key[:i]
}
func (t *trie) insert(key, tag string, optionalData interface{}, proxy RestProxy) *trieNode {
input := slowPathSplit(key)
n := t.root
if key == pathSep {
t.hasRootSlash = true
}
var paramKeys []string
for _, s := range input {
c := s[0]
if isParam, isWildcard := c == paramStart[0], c == wildcardParamStart[0]; isParam || isWildcard {
n.hasDynamicChild = true
paramKeys = append(paramKeys, s[1:]) // without : or *.
// if node has already a wildcard, don't force a value, check for true only.
if isParam {
n.childNamedParameter = true
s = paramStart
}
if isWildcard {
n.childWildcardParameter = true
s = wildcardParamStart
if t.root == n {
t.hasRootWildcard = true
}
}
}
if !n.hasChild(s) {
child := newTrieNode()
n.addChild(s, child)
}
n = n.getChild(s)
}
n.Tag = tag
n.Proxy = proxy
n.Data = optionalData
n.paramKeys = paramKeys
n.key = key
n.staticKey = resolveStaticPart(key)
n.end = true
return n
}
// ParamsSetter is the interface which should be implemented by the
// params writer for `search` in order to store the found named path parameters, if any.
type ParamsSetter interface {
Set(string, interface{})
}
// Search is the most important part of the trie.
// It will try to find the responsible node for a specific query (or a request path for HTTP endpoints).
//
// Search supports searching for static paths(path without : or *) and paths that contain
// named parameters or wildcards.
// Priority as:
// 1. static paths
// 2. named parameters with ":"
// 3. wildcards
// 4. closest wildcard if not found, if any
// 5. root wildcard
func (t *trie) Search(q string, params ParamsSetter) *trieNode {
end := len(q)
if end == 0 || (end == 1 && q[0] == pathSepB) {
// fixes only root wildcard but no / registered at.
if t.hasRootSlash {
return t.root.getChild(pathSep)
} else if t.hasRootWildcard {
// no need to going through setting parameters, this one has not but it is wildcard.
return t.root.getChild(wildcardParamStart)
}
return nil
}
n := t.root
start := 1
i := 1
var paramValues []string
for {
if i == end || q[i] == pathSepB {
if child := n.getChild(q[start:i]); child != nil {
n = child
} else if n.childNamedParameter { // && n.childWildcardParameter == false {
n = n.getChild(paramStart)
if ln := len(paramValues); cap(paramValues) > ln {
paramValues = paramValues[:ln+1]
paramValues[ln] = q[start:i]
} else {
paramValues = append(paramValues, q[start:i])
}
} else if n.childWildcardParameter {
n = n.getChild(wildcardParamStart)
if ln := len(paramValues); cap(paramValues) > ln {
paramValues = paramValues[:ln+1]
paramValues[ln] = q[start:]
} else {
paramValues = append(paramValues, q[start:])
}
break
} else {
n = n.findClosestParentWildcardNode()
if n != nil {
// means that it has :param/static and *wildcard, we go trhough the :param
// but the next path segment is not the /static, so go back to *wildcard
// instead of not found.
//
// Fixes:
// /hello/*p
// /hello/:p1/static/:p2
// req: http://localhost:8080/hello/dsadsa/static/dsadsa => found
// req: http://localhost:8080/hello/dsadsa => but not found!
// and
// /second/wild/*p
// /second/wild/static/otherstatic/
// req: /second/wild/static/otherstatic/random => but not found!
params.Set(n.paramKeys[0], q[len(n.staticKey):])
return n
}
return nil
}
if i == end {
break
}
i++
start = i
continue
}
i++
}
if n == nil || !n.end {
if n != nil { // we need it on both places, on last segment (below) or on the first unnknown (above).
if n = n.findClosestParentWildcardNode(); n != nil {
params.Set(n.paramKeys[0], q[len(n.staticKey):])
return n
}
}
if t.hasRootWildcard {
// that's the case for root wildcard, tests are passing
// even without it but stick with it for reference.
// Note ote that something like:
// Routes: /other2/*myparam and /other2/static
// Reqs: /other2/staticed will be handled
// by the /other2/*myparam and not the root wildcard (see above), which is what we want.
n = t.root.getChild(wildcardParamStart)
params.Set(n.paramKeys[0], q[1:])
return n
}
return nil
}
for i, paramValue := range paramValues {
if len(n.paramKeys) > i {
params.Set(n.paramKeys[i], paramValue)
}
}
return n
}
// trieNode is the trie's node which path patterns with their data like an HTTP handler are saved to.
// See `trie` too.
type trieNode struct {
parent *trieNode
children map[string]*trieNode
hasDynamicChild bool // does one of the children contains a parameter or wildcard?
childNamedParameter bool // is the child a named parameter (single segmnet)
childWildcardParameter bool // or it is a wildcard (can be more than one path segments) ?
paramKeys []string // the param keys without : or *.
end bool // it is a complete node, here we stop, and we can say that the node is valid.
key string // if end == true then key is filled with the original value of the insertion's key.
// if key != "" && its parent has childWildcardParameter == true,
// we need it to track the static part for the closest-wildcard's parameter storage.
staticKey string
// insert main data relative to http and a tag for things like route names.
Proxy RestProxy
Tag string
// other insert data.
Data interface{}
}
// newTrieNode returns a new, empty, trieNode.
func newTrieNode() *trieNode {
n := new(trieNode)
return n
}
func (n *trieNode) addChild(s string, child *trieNode) {
if n.children == nil {
n.children = make(map[string]*trieNode)
}
if _, exists := n.children[s]; exists {
return
}
child.parent = n
n.children[s] = child
}
func (n *trieNode) getChild(s string) *trieNode {
if n.children == nil {
return nil
}
return n.children[s]
}
func (n *trieNode) hasChild(s string) bool {
return n.getChild(s) != nil
}
func (n *trieNode) findClosestParentWildcardNode() *trieNode {
n = n.parent
for n != nil {
if n.childWildcardParameter {
return n.getChild(wildcardParamStart)
}
n = n.parent
}
return nil
}
// keysSorter is the type definition for the sorting logic
// that caller can pass on `GetKeys` and `Autocomplete`.
type keysSorter = func(list []string) func(i, j int) bool
// Keys returns this node's key (if it's a final path segment)
// and its children's node's key. The "sorter" can be optionally used to sort the result.
func (n *trieNode) Keys(sorter keysSorter) (list []string) {
if n == nil {
return
}
if n.end {
list = append(list, n.key)
}
if n.children != nil {
for _, child := range n.children {
list = append(list, child.Keys(sorter)...)
}
}
if sorter != nil {
sort.Slice(list, sorter(list))
}
return
}
// Parent returns the parent of that node, can return nil if this is the root node.
func (n *trieNode) Parent() *trieNode {
return n.parent
}
// String returns the key, which is the path pattern for the HTTP Mux.
func (n *trieNode) String() string {
return n.key
}
// IsEnd returns true if this trieNode is a final path, has a key.
func (n *trieNode) IsEnd() bool {
return n.end
}