/
mux.go
148 lines (133 loc) · 3.3 KB
/
mux.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
package netkit
import (
"net/http"
"net/url"
"strings"
)
// serve mux
type ServeMux struct {
routes map[string][]*Handler
}
// return new serve mux instance
func NewServeMux() *ServeMux {
return &ServeMux{make(map[string][]*Handler)}
}
// match requests against registered handlers, serve http
func (self *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, h := range self.routes[r.Method] {
if params, ok := h.parse(r.URL.Path); ok {
if len(params) > 0 {
r.URL.RawQuery = url.Values(params).Encode() + "&" + r.URL.RawQuery
}
h.ServeHTTP(w, r)
return
}
}
allowed := make([]string, 0, len(self.routes))
for method, routes := range self.routes {
if method == r.Method {
continue
}
for _, h := range routes {
if _, ok := h.parse(r.URL.Path); ok {
allowed = append(allowed, method)
}
}
}
if len(allowed) == 0 {
//http.NotFound(w, r)
http.Redirect(w, r, "/error/404", 303)
return
}
w.Header().Add("Allow", strings.Join(allowed, ", "))
//http.Error(w, "Method Not Allowed", 405)
http.Redirect(w, r, "/error/405", 303)
}
// register and http handler for a paticular method and path
func (self *ServeMux) Route(method, path string, h http.Handler) {
self.routes[method] = append(self.routes[method], &Handler{path, h})
n := len(path)
if n > 0 && path[n-1] == '/' {
self.Route(method, path[:n-1], http.RedirectHandler(path, 301))
}
}
// wrapper for Route to allow use of handler functions
func (self *ServeMux) RouteFunc(method, path string, h http.HandlerFunc) {
self.Route(method, path, h)
}
// register forwarder handler
func (self *ServeMux) Forward(path, newpath string) {
self.Route("GET", path, http.RedirectHandler(newpath, 301))
}
// register static file server handler
func (self *ServeMux) Static(path, folder string) {
n := len(path)
if n > 0 && path[n-1] != '/' {
path = path + "/"
}
h := http.StripPrefix(path, http.FileServer(http.Dir(folder)))
self.Route("GET", path, h)
}
// route
type Handler struct {
path string
http.Handler
}
// parse registered pattern
func (self *Handler) parse(path string) (url.Values, bool) {
p := make(url.Values)
var i, j int
for i < len(path) {
switch {
case j >= len(self.path):
if self.path != "/" && len(self.path) > 0 && self.path[len(self.path)-1] == '/' {
return p, true
}
return nil, false
case self.path[j] == ':':
var name, val string
var nextc byte
name, nextc, j = match(self.path, isBoth, j+1)
val, _, i = match(path, byteParse(nextc), i)
p.Add(":"+name, val)
case path[i] == self.path[j]:
i++
j++
default:
return nil, false
}
}
if j != len(self.path) {
return nil, false
}
return p, true
}
// match path with registered handler
func match(s string, f func(byte) bool, i int) (matched string, next byte, j int) {
j = i
for j < len(s) && f(s[j]) {
j++
}
if j < len(s) {
next = s[j]
}
return s[i:j], next, j
}
// determine type of byte
func byteParse(b byte) func(byte) bool {
return func(c byte) bool {
return c != b && c != '/'
}
}
// test for alpha byte
func isAlpha(ch byte) bool {
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_'
}
// test for numerical byte
func isDigit(ch byte) bool {
return '0' <= ch && ch <= '9'
}
// test for alpha or numerical byte
func isBoth(ch byte) bool {
return isAlpha(ch) || isDigit(ch)
}