combine var and use tmp var (#1108)

This commit is contained in:
田欧 2017-09-28 09:54:37 -05:00 committed by Bo-Yi Wu
parent a8c53949e5
commit f7376f7c7f
1 changed files with 27 additions and 21 deletions

48
gin.go
View File

@ -20,9 +20,11 @@ const (
defaultMultipartMemory = 32 << 20 // 32 MB defaultMultipartMemory = 32 << 20 // 32 MB
) )
var default404Body = []byte("404 page not found") var (
var default405Body = []byte("405 method not allowed") default404Body = []byte("404 page not found")
var defaultAppEngine bool default405Body = []byte("405 method not allowed")
defaultAppEngine bool
)
type HandlerFunc func(*Context) type HandlerFunc func(*Context)
type HandlersChain []HandlerFunc type HandlersChain []HandlerFunc
@ -91,6 +93,7 @@ type Engine struct {
// If enabled, the url.RawPath will be used to find parameters. // If enabled, the url.RawPath will be used to find parameters.
UseRawPath bool UseRawPath bool
// If true, the path value will be unescaped. // If true, the path value will be unescaped.
// If UseRawPath is false (by default), the UnescapePathValues effectively is true, // If UseRawPath is false (by default), the UnescapePathValues effectively is true,
// as url.Path gonna be used, which is already unescaped. // as url.Path gonna be used, which is already unescaped.
@ -161,13 +164,16 @@ func (engine *Engine) SecureJsonPrefix(prefix string) *Engine {
} }
func (engine *Engine) LoadHTMLGlob(pattern string) { func (engine *Engine) LoadHTMLGlob(pattern string) {
left := engine.delims.Left
right := engine.delims.Right
if IsDebugging() { if IsDebugging() {
debugPrintLoadTemplate(template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseGlob(pattern))) debugPrintLoadTemplate(template.Must(template.New("").Delims(left, right).Funcs(engine.FuncMap).ParseGlob(pattern)))
engine.HTMLRender = render.HTMLDebug{Glob: pattern, FuncMap: engine.FuncMap, Delims: engine.delims} engine.HTMLRender = render.HTMLDebug{Glob: pattern, FuncMap: engine.FuncMap, Delims: engine.delims}
return return
} }
templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseGlob(pattern)) templ := template.Must(template.New("").Delims(left, right).Funcs(engine.FuncMap).ParseGlob(pattern))
engine.SetHTMLTemplate(templ) engine.SetHTMLTemplate(templ)
} }
@ -322,12 +328,12 @@ func (engine *Engine) HandleContext(c *Context) {
engine.pool.Put(c) engine.pool.Put(c)
} }
func (engine *Engine) handleHTTPRequest(context *Context) { func (engine *Engine) handleHTTPRequest(c *Context) {
httpMethod := context.Request.Method httpMethod := c.Request.Method
path := context.Request.URL.Path path := c.Request.URL.Path
unescape := false unescape := false
if engine.UseRawPath && len(context.Request.URL.RawPath) > 0 { if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {
path = context.Request.URL.RawPath path = c.Request.URL.RawPath
unescape = engine.UnescapePathValues unescape = engine.UnescapePathValues
} }
@ -337,20 +343,20 @@ func (engine *Engine) handleHTTPRequest(context *Context) {
if t[i].method == httpMethod { if t[i].method == httpMethod {
root := t[i].root root := t[i].root
// Find route in tree // Find route in tree
handlers, params, tsr := root.getValue(path, context.Params, unescape) handlers, params, tsr := root.getValue(path, c.Params, unescape)
if handlers != nil { if handlers != nil {
context.handlers = handlers c.handlers = handlers
context.Params = params c.Params = params
context.Next() c.Next()
context.writermem.WriteHeaderNow() c.writermem.WriteHeaderNow()
return return
} }
if httpMethod != "CONNECT" && path != "/" { if httpMethod != "CONNECT" && path != "/" {
if tsr && engine.RedirectTrailingSlash { if tsr && engine.RedirectTrailingSlash {
redirectTrailingSlash(context) redirectTrailingSlash(c)
return return
} }
if engine.RedirectFixedPath && redirectFixedPath(context, root, engine.RedirectFixedPath) { if engine.RedirectFixedPath && redirectFixedPath(c, root, engine.RedirectFixedPath) {
return return
} }
} }
@ -362,15 +368,15 @@ func (engine *Engine) handleHTTPRequest(context *Context) {
for _, tree := range engine.trees { for _, tree := range engine.trees {
if tree.method != httpMethod { if tree.method != httpMethod {
if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil { if handlers, _, _ := tree.root.getValue(path, nil, unescape); handlers != nil {
context.handlers = engine.allNoMethod c.handlers = engine.allNoMethod
serveError(context, 405, default405Body) serveError(c, 405, default405Body)
return return
} }
} }
} }
} }
context.handlers = engine.allNoRoute c.handlers = engine.allNoRoute
serveError(context, 404, default404Body) serveError(c, 404, default404Body)
} }
var mimePlain = []string{MIMEPlain} var mimePlain = []string{MIMEPlain}