forked from gitter-badger/api-3
/
bytecode_runner.go
85 lines (75 loc) · 1.34 KB
/
bytecode_runner.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
package web
import "net/http"
type routeMachine struct {
sm stateMachine
routes []route
}
func matchRoute(route route, m method, ms *method, r *http.Request, c *C) bool {
if !route.pattern.Match(r, c) {
return false
}
*ms |= route.method
if route.method&m != 0 {
route.pattern.Run(r, c)
return true
}
return false
}
func (rm routeMachine) route(c *C, w http.ResponseWriter, r *http.Request) (method, *route) {
m := httpMethod(r.Method)
var methods method
p := r.URL.Path
if len(rm.sm) == 0 {
return methods, nil
}
var i int
for {
sm := rm.sm[i].mode
if sm&smSetCursor != 0 {
si := rm.sm[i].i
p = r.URL.Path[si:]
i++
continue
}
length := int(sm & smLengthMask)
match := false
if length <= len(p) {
bs := rm.sm[i].bs
switch length {
case 3:
if p[2] != bs[2] {
break
}
fallthrough
case 2:
if p[1] != bs[1] {
break
}
fallthrough
case 1:
if p[0] != bs[0] {
break
}
fallthrough
case 0:
p = p[length:]
match = true
}
}
if match && sm&smRoute != 0 {
si := rm.sm[i].i
if matchRoute(rm.routes[si], m, &methods, r, c) {
return 0, &rm.routes[si]
}
i++
} else if match != (sm&smJumpOnMatch == 0) {
if sm&smFail != 0 {
return methods, nil
}
i = int(rm.sm[i].i)
} else {
i++
}
}
return methods, nil
}