Skip to content

Commit 9009d40

Browse files
author
Michael Hoisie
committed
Store routes in a vector instead of a map. This gives the correct order of evaluation.
1 parent f3c6f2e commit 9009d40

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

status.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ var statusText = map[int]string{
1010
http.StatusContinue: "Continue",
1111
http.StatusSwitchingProtocols: "Switching Protocols",
1212

13-
http.StatusOK: "OK",
14-
http.StatusCreated: "Created",
15-
http.StatusAccepted: "Accepted",
13+
http.StatusOK: "OK",
14+
http.StatusCreated: "Created",
15+
http.StatusAccepted: "Accepted",
1616
http.StatusNonAuthoritativeInfo: "Non-Authoritative Information",
17-
http.StatusNoContent: "No Content",
18-
http.StatusResetContent: "Reset Content",
19-
http.StatusPartialContent: "Partial Content",
17+
http.StatusNoContent: "No Content",
18+
http.StatusResetContent: "Reset Content",
19+
http.StatusPartialContent: "Partial Content",
2020

2121
http.StatusMultipleChoices: "Multiple Choices",
2222
http.StatusMovedPermanently: "Moved Permanently",
@@ -26,29 +26,29 @@ var statusText = map[int]string{
2626
http.StatusUseProxy: "Use Proxy",
2727
http.StatusTemporaryRedirect: "Temporary Redirect",
2828

29-
http.StatusBadRequest: "Bad Request",
30-
http.StatusUnauthorized: "Unauthorized",
31-
http.StatusPaymentRequired: "Payment Required",
32-
http.StatusForbidden: "Forbidden",
33-
http.StatusNotFound: "Not Found",
34-
http.StatusMethodNotAllowed: "Method Not Allowed",
35-
http.StatusNotAcceptable: "Not Acceptable",
36-
http.StatusProxyAuthRequired: "Proxy Authentication Required",
37-
http.StatusRequestTimeout: "Request Timeout",
38-
http.StatusConflict: "Conflict",
39-
http.StatusGone: "Gone",
40-
http.StatusLengthRequired: "Length Required",
41-
http.StatusPreconditionFailed: "Precondition Failed",
42-
http.StatusRequestEntityTooLarge: "Request Entity Too Large",
43-
http.StatusRequestURITooLong: "Request URI Too Long",
44-
http.StatusUnsupportedMediaType: "Unsupported Media Type",
29+
http.StatusBadRequest: "Bad Request",
30+
http.StatusUnauthorized: "Unauthorized",
31+
http.StatusPaymentRequired: "Payment Required",
32+
http.StatusForbidden: "Forbidden",
33+
http.StatusNotFound: "Not Found",
34+
http.StatusMethodNotAllowed: "Method Not Allowed",
35+
http.StatusNotAcceptable: "Not Acceptable",
36+
http.StatusProxyAuthRequired: "Proxy Authentication Required",
37+
http.StatusRequestTimeout: "Request Timeout",
38+
http.StatusConflict: "Conflict",
39+
http.StatusGone: "Gone",
40+
http.StatusLengthRequired: "Length Required",
41+
http.StatusPreconditionFailed: "Precondition Failed",
42+
http.StatusRequestEntityTooLarge: "Request Entity Too Large",
43+
http.StatusRequestURITooLong: "Request URI Too Long",
44+
http.StatusUnsupportedMediaType: "Unsupported Media Type",
4545
http.StatusRequestedRangeNotSatisfiable: "Requested Range Not Satisfiable",
46-
http.StatusExpectationFailed: "Expectation Failed",
46+
http.StatusExpectationFailed: "Expectation Failed",
4747

48-
http.StatusInternalServerError: "Internal Server Error",
49-
http.StatusNotImplemented: "Not Implemented",
50-
http.StatusBadGateway: "Bad Gateway",
51-
http.StatusServiceUnavailable: "Service Unavailable",
52-
http.StatusGatewayTimeout: "Gateway Timeout",
48+
http.StatusInternalServerError: "Internal Server Error",
49+
http.StatusNotImplemented: "Not Implemented",
50+
http.StatusBadGateway: "Bad Gateway",
51+
http.StatusServiceUnavailable: "Service Unavailable",
52+
http.StatusGatewayTimeout: "Gateway Timeout",
5353
http.StatusHTTPVersionNotSupported: "HTTP Version Not Supported",
5454
}

web.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ type route struct {
165165
handler *reflect.FuncValue
166166
}
167167

168-
var routes = make(map[*regexp.Regexp]route)
168+
var routes vector.Vector
169169

170170
func addRoute(r string, method string, handler interface{}) {
171171
cr, err := regexp.Compile(r)
@@ -174,7 +174,7 @@ func addRoute(r string, method string, handler interface{}) {
174174
return
175175
}
176176
fv := reflect.NewValue(handler).(*reflect.FuncValue)
177-
routes[cr] = route{r, cr, method, fv}
177+
routes.Push(route{r, cr, method, fv})
178178
}
179179

180180
type httpConn struct {
@@ -250,7 +250,9 @@ func routeHandler(req *Request, c conn) {
250250
ctx.SetHeader("Content-Type", "text/html; charset=utf-8", true)
251251
ctx.SetHeader("Server", "web.go", true)
252252

253-
for cr, route := range routes {
253+
for i := 0; i < routes.Len(); i++ {
254+
route := routes.At(i).(route)
255+
cr := route.cr
254256
//if the methods don't match, skip this handler (except HEAD can be used in place of GET)
255257
if req.Method != route.method && !(req.Method == "HEAD" && route.method == "GET") {
256258
continue
@@ -311,13 +313,13 @@ func routeHandler(req *Request, c conn) {
311313

312314
return
313315
}
314-
316+
315317
//try to serve index.html
316318
if indexPath := path.Join(staticDir, "index.html"); requestPath == "/" && fileExists(indexPath) {
317-
serveFile(&ctx, indexPath)
318-
return
319+
serveFile(&ctx, indexPath)
320+
return
319321
}
320-
322+
321323
ctx.Abort(404, "Page not found")
322324
}
323325

0 commit comments

Comments
 (0)