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
|
/********************************
*** Multiplexer for Go ***
*** Bone is under MIT license ***
*** Code by CodingFerret ***
*** github.com/go-zoo ***
*********************************/
package bone
import (
"net/http"
"net/url"
"strings"
)
func (m *Mux) ListenAndServe(port string) error {
return http.ListenAndServe(port, m)
}
func (m *Mux) parse(rw http.ResponseWriter, req *http.Request) bool {
for _, r := range m.Routes[req.Method] {
ok := r.parse(rw, req)
if ok {
return true
}
}
// If no HEAD method, default to GET
if req.Method == "HEAD" {
for _, r := range m.Routes["GET"] {
ok := r.parse(rw, req)
if ok {
return true
}
}
}
return false
}
// StaticRoute check if the request path is for Static route
func (m *Mux) staticRoute(rw http.ResponseWriter, req *http.Request) bool {
for _, s := range m.Routes[static] {
if len(req.URL.Path) >= s.Size {
if req.URL.Path[:s.Size] == s.Path {
s.Handler.ServeHTTP(rw, req)
return true
}
}
}
return false
}
// HandleNotFound handle when a request does not match a registered handler.
func (m *Mux) HandleNotFound(rw http.ResponseWriter, req *http.Request) {
if m.notFound != nil {
m.notFound.ServeHTTP(rw, req)
} else {
http.NotFound(rw, req)
}
}
// Check if the path don't end with a /
func (m *Mux) validate(rw http.ResponseWriter, req *http.Request) bool {
plen := len(req.URL.Path)
if plen > 1 && req.URL.Path[plen-1:] == "/" {
cleanURL(&req.URL.Path)
rw.Header().Set("Location", req.URL.String())
rw.WriteHeader(http.StatusFound)
return true
}
// Retry to find a route that match
return m.parse(rw, req)
}
func valid(path string) bool {
plen := len(path)
if plen > 1 && path[plen-1:] == "/" {
return false
}
return true
}
// Clean url path
func cleanURL(url *string) {
ulen := len((*url))
if ulen > 1 {
if (*url)[ulen-1:] == "/" {
*url = (*url)[:ulen-1]
cleanURL(url)
}
}
}
// GetValue return the key value, of the current *http.Request
func GetValue(req *http.Request, key string) string {
return GetAllValues(req)[key]
}
// GetRequestRoute returns the route of given Request
func (m *Mux) GetRequestRoute(req *http.Request) string {
cleanURL(&req.URL.Path)
for _, r := range m.Routes[req.Method] {
if r.Atts != 0 {
if r.Atts&SUB != 0 {
return r.Handler.(*Mux).GetRequestRoute(req)
}
if r.Match(req) {
return r.Path
}
}
if req.URL.Path == r.Path {
return r.Path
}
}
for _, s := range m.Routes[static] {
if len(req.URL.Path) >= s.Size {
if req.URL.Path[:s.Size] == s.Path {
return s.Path
}
}
}
return "NotFound"
}
// GetQuery return the key value, of the current *http.Request query
func GetQuery(req *http.Request, key string) []string {
if ok, value := extractQueries(req); ok {
return value[key]
}
return nil
}
// GetAllQueries return all queries of the current *http.Request
func GetAllQueries(req *http.Request) map[string][]string {
if ok, values := extractQueries(req); ok {
return values
}
return nil
}
func extractQueries(req *http.Request) (bool, map[string][]string) {
if q, err := url.ParseQuery(req.URL.RawQuery); err == nil {
var queries = make(map[string][]string)
for k, v := range q {
for _, item := range v {
values := strings.Split(item, ",")
queries[k] = append(queries[k], values...)
}
}
return true, queries
}
return false, nil
}
func (m *Mux) otherMethods(rw http.ResponseWriter, req *http.Request) bool {
for _, met := range method {
if met != req.Method {
for _, r := range m.Routes[met] {
ok := r.exists(rw, req)
if ok {
rw.WriteHeader(http.StatusMethodNotAllowed)
return true
}
}
}
}
return false
}
|