use 'trees.get()' method to find root of the tree for the given HTTP method

This commit is contained in:
biningo 2021-04-10 10:48:49 +08:00
parent 51c7d001e0
commit 2affe8b208

72
gin.go
View File

@ -472,50 +472,46 @@ 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 root := engine.trees.get(httpMethod)
for i, tl := 0, len(t); i < tl; i++ { if root == nil {
if t[i].method != httpMethod { if engine.HandleMethodNotAllowed {
continue for _, tree := range engine.trees {
} if tree.method == httpMethod {
root := t[i].root continue
// Find route in tree }
value := root.getValue(rPath, c.params, unescape) if value := tree.root.getValue(rPath, nil, unescape); value.handlers != nil {
if value.params != nil { c.handlers = engine.allNoMethod
c.Params = *value.params serveError(c, http.StatusMethodNotAllowed, default405Body)
} return
if value.handlers != nil { }
c.handlers = value.handlers
c.fullPath = value.fullPath
c.Next()
c.writermem.WriteHeaderNow()
return
}
if httpMethod != "CONNECT" && rPath != "/" {
if value.tsr && engine.RedirectTrailingSlash {
redirectTrailingSlash(c)
return
}
if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) {
return
} }
} }
break c.handlers = engine.allNoRoute
serveError(c, http.StatusNotFound, default404Body)
return
} }
if engine.HandleMethodNotAllowed { // Find route in tree
for _, tree := range engine.trees { value := root.getValue(rPath, c.params, unescape)
if tree.method == httpMethod { if value.params != nil {
continue c.Params = *value.params
} }
if value := tree.root.getValue(rPath, nil, unescape); value.handlers != nil { if value.handlers != nil {
c.handlers = engine.allNoMethod c.handlers = value.handlers
serveError(c, http.StatusMethodNotAllowed, default405Body) c.fullPath = value.fullPath
return c.Next()
} c.writermem.WriteHeaderNow()
return
}
if httpMethod != "CONNECT" && rPath != "/" {
if value.tsr && engine.RedirectTrailingSlash {
redirectTrailingSlash(c)
return
}
if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) {
return
} }
} }
c.handlers = engine.allNoRoute
serveError(c, http.StatusNotFound, default404Body)
} }
var mimePlain = []string{MIMEPlain} var mimePlain = []string{MIMEPlain}