|
@@ -349,38 +349,40 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
|
|
// Find root of the tree for the given HTTP method
|
|
// Find root of the tree for the given HTTP method
|
|
|
t := engine.trees
|
|
t := engine.trees
|
|
|
for i, tl := 0, len(t); i < tl; i++ {
|
|
for i, tl := 0, len(t); i < tl; i++ {
|
|
|
- if t[i].method == httpMethod {
|
|
|
|
|
- root := t[i].root
|
|
|
|
|
- // Find route in tree
|
|
|
|
|
- handlers, params, tsr := root.getValue(path, c.Params, unescape)
|
|
|
|
|
- if handlers != nil {
|
|
|
|
|
- c.handlers = handlers
|
|
|
|
|
- c.Params = params
|
|
|
|
|
- c.Next()
|
|
|
|
|
- c.writermem.WriteHeaderNow()
|
|
|
|
|
|
|
+ if t[i].method != httpMethod {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ root := t[i].root
|
|
|
|
|
+ // Find route in tree
|
|
|
|
|
+ handlers, params, tsr := root.getValue(path, c.Params, unescape)
|
|
|
|
|
+ if handlers != nil {
|
|
|
|
|
+ c.handlers = handlers
|
|
|
|
|
+ c.Params = params
|
|
|
|
|
+ c.Next()
|
|
|
|
|
+ c.writermem.WriteHeaderNow()
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ if httpMethod != "CONNECT" && path != "/" {
|
|
|
|
|
+ if tsr && engine.RedirectTrailingSlash {
|
|
|
|
|
+ redirectTrailingSlash(c)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- if httpMethod != "CONNECT" && path != "/" {
|
|
|
|
|
- if tsr && engine.RedirectTrailingSlash {
|
|
|
|
|
- redirectTrailingSlash(c)
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
- if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) {
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) {
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
- break
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+ break
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if engine.HandleMethodNotAllowed {
|
|
if engine.HandleMethodNotAllowed {
|
|
|
for _, tree := range engine.trees {
|
|
for _, tree := range engine.trees {
|
|
|
- if tree.method != httpMethod {
|
|
|
|
|
- if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {
|
|
|
|
|
- c.handlers = engine.allNoMethod
|
|
|
|
|
- serveError(c, http.StatusMethodNotAllowed, default405Body)
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if tree.method == httpMethod {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {
|
|
|
|
|
+ c.handlers = engine.allNoMethod
|
|
|
|
|
+ serveError(c, http.StatusMethodNotAllowed, default405Body)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -393,14 +395,16 @@ var mimePlain = []string{MIMEPlain}
|
|
|
func serveError(c *Context, code int, defaultMessage []byte) {
|
|
func serveError(c *Context, code int, defaultMessage []byte) {
|
|
|
c.writermem.status = code
|
|
c.writermem.status = code
|
|
|
c.Next()
|
|
c.Next()
|
|
|
- if !c.writermem.Written() {
|
|
|
|
|
- if c.writermem.Status() == code {
|
|
|
|
|
- c.writermem.Header()["Content-Type"] = mimePlain
|
|
|
|
|
- c.Writer.Write(defaultMessage)
|
|
|
|
|
- } else {
|
|
|
|
|
- c.writermem.WriteHeaderNow()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if c.writermem.Written() {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ if c.writermem.Status() == code {
|
|
|
|
|
+ c.writermem.Header()["Content-Type"] = mimePlain
|
|
|
|
|
+ c.Writer.Write(defaultMessage)
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
+ c.writermem.WriteHeaderNow()
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func redirectTrailingSlash(c *Context) {
|
|
func redirectTrailingSlash(c *Context) {
|
|
@@ -411,10 +415,9 @@ func redirectTrailingSlash(c *Context) {
|
|
|
code = http.StatusTemporaryRedirect
|
|
code = http.StatusTemporaryRedirect
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ req.URL.Path = path + "/"
|
|
|
if length := len(path); length > 1 && path[length-1] == '/' {
|
|
if length := len(path); length > 1 && path[length-1] == '/' {
|
|
|
req.URL.Path = path[:length-1]
|
|
req.URL.Path = path[:length-1]
|
|
|
- } else {
|
|
|
|
|
- req.URL.Path = path + "/"
|
|
|
|
|
}
|
|
}
|
|
|
debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())
|
|
debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())
|
|
|
http.Redirect(c.Writer, req, req.URL.String(), code)
|
|
http.Redirect(c.Writer, req, req.URL.String(), code)
|