-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.go
More file actions
238 lines (207 loc) · 5.21 KB
/
server.go
File metadata and controls
238 lines (207 loc) · 5.21 KB
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
package shift
import (
"net/http"
"sort"
"strings"
)
type Server struct {
muxes [9]multiplexer // Muxes for default HTTP methods.
muxIndices []int // Indices of non-nil muxes. This index is useful to skip <nil> muxes.
customMuxes map[string]multiplexer // Muxes for custom HTTP methods.
config *Config
}
func (svr *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.RawPath
if path == "" {
path = r.URL.Path
}
var mux multiplexer
if idx := methodIndex(r.Method); idx >= 0 {
mux = svr.muxes[idx]
} else {
mux = svr.customMuxes[r.Method]
}
if mux == nil {
if svr.config.handleMethodNotAllowed {
svr.handleMethodNotAllowed(path, r.Method, w)
}
svr.config.notFoundHandler(w, r)
return
}
handler, ps, template := mux.find(path)
if handler != nil {
_ = handler(w, r, Route{
Params: newParams(ps), // ps could be <nil> as well, but that's okay!
Path: template,
})
return
}
// Look with/without trailing slash.
if svr.config.trailingSlashMatch.behavior != behaviorSkip {
var clean string
if len(path) > 0 && path[len(path)-1] == '/' {
clean = path[:len(path)-1]
} else {
clean = path + "/"
}
handler, ps, template = mux.find(clean)
if handler != nil {
switch svr.config.trailingSlashMatch.behavior {
case behaviorRedirect:
r.URL.Path = clean
http.Redirect(w, r, r.URL.String(), svr.config.trailingSlashMatch.code)
return
case behaviorExecute:
r.URL.Path = clean
_ = handler(w, r, Route{
Params: newParams(ps), // ps could be <nil> here too, but that's okay!
Path: template,
})
return
}
}
}
// Correct the path and do a case-insensitive search...
if svr.config.pathCorrectionMatch.behavior != behaviorSkip {
clean := cleanPath(path)
handler, ps, template, matchedPath := mux.findCaseInsensitive(clean, svr.config.pathCorrectionMatch.behavior == behaviorExecute)
if handler != nil {
switch svr.config.pathCorrectionMatch.behavior {
case behaviorRedirect:
r.URL.Path = matchedPath
http.Redirect(w, r, r.URL.String(), svr.config.pathCorrectionMatch.code)
return
case behaviorExecute:
_ = handler(w, r, Route{
Params: newParams(ps), // ps could be <nil> here too, but that's okay!
Path: template,
})
return
}
}
}
// Look for allowed methods.
if svr.config.handleMethodNotAllowed {
svr.handleMethodNotAllowed(path, r.Method, w)
}
svr.config.notFoundHandler(w, r)
return
}
func (svr *Server) populateRoutes(byMethods map[string]*methodInfo) {
for method, info := range byMethods {
var mux multiplexer
total := len(info.logs) // Total routes in the method.
staticPercentage := float64(info.staticRoutes) / float64(total) * 100
// Determine mux variant.
if staticPercentage == 100 {
mux = newStaticMux()
} else if staticPercentage >= 30 {
mux = newHybridMux()
} else {
mux = newRadixMux()
}
// Register routes.
for _, log := range info.logs {
mux.add(log.path, log.static, log.handler)
}
// Store mux.
if idx := methodIndex(method); idx >= 0 {
svr.muxes[idx] = mux
// Store indices of active muxes in ascending order.
svr.muxIndices = append(svr.muxIndices, idx)
sort.Slice(svr.muxIndices, func(i, j int) bool {
return svr.muxIndices[i] < svr.muxIndices[j]
})
} else {
if svr.customMuxes == nil {
svr.customMuxes = make(map[string]multiplexer)
}
svr.customMuxes[method] = mux
}
}
}
func (svr *Server) handleMethodNotAllowed(path string, method string, w http.ResponseWriter) {
allowed := svr.allowedHeader(path, method)
if len(allowed) > 0 {
w.Header().Add("Allow", allowed)
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func (svr *Server) allowedHeader(path string, skipMethod string) string {
var allowed strings.Builder
skipped := false
if skipMethodIdx := methodIndex(skipMethod); skipMethodIdx != -1 {
for _, idx := range svr.muxIndices {
if !skipped && idx == skipMethodIdx {
skipped = true
continue
}
if handler, _, _ := svr.muxes[idx].find(path); handler != nil {
if allowed.Len() != 0 {
allowed.WriteString(", ")
}
allowed.WriteString(methodString(idx))
}
}
}
for method, mux := range svr.customMuxes {
if !skipped && method == skipMethod {
continue
}
if handler, _, _ := mux.find(path); handler != nil {
if allowed.Len() != 0 {
allowed.WriteString(", ")
}
allowed.WriteString(method)
}
}
return allowed.String()
}
func methodIndex(method string) int {
switch method {
case http.MethodGet:
return 0
case http.MethodPost:
return 1
case http.MethodPut:
return 2
case http.MethodPatch:
return 3
case http.MethodDelete:
return 4
case http.MethodHead:
return 5
case http.MethodOptions:
return 6
case http.MethodTrace:
return 7
case http.MethodConnect:
return 8
default:
return -1
}
}
func methodString(idx int) string {
switch idx {
case 0:
return http.MethodGet
case 1:
return http.MethodPost
case 2:
return http.MethodPut
case 3:
return http.MethodPatch
case 4:
return http.MethodDelete
case 5:
return http.MethodHead
case 6:
return http.MethodOptions
case 7:
return http.MethodTrace
case 8:
return http.MethodConnect
default:
return ""
}
}