Fixes NoMethod / NoRoute handlers

This commit is contained in:
Manu Mtz-Almeida 2015-03-25 16:53:58 +01:00
parent 48fec0650d
commit aa9fad5ad8

27
gin.go
View File

@ -6,7 +6,6 @@ package gin
import ( import (
"html/template" "html/template"
"math"
"net/http" "net/http"
"sync" "sync"
@ -15,7 +14,6 @@ import (
) )
const ( const (
AbortIndex = math.MaxInt8 / 2
MIMEJSON = "application/json" MIMEJSON = "application/json"
MIMEHTML = "text/html" MIMEHTML = "text/html"
MIMEXML = "application/xml" MIMEXML = "application/xml"
@ -31,14 +29,15 @@ type (
// Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middlewares. // Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middlewares.
Engine struct { Engine struct {
*RouterGroup *RouterGroup
HTMLRender render.Render HTMLRender render.Render
Default404Body []byte Default404Body []byte
Default405Body []byte Default405Body []byte
pool sync.Pool pool sync.Pool
allNoRouteNoMethod []HandlerFunc allNoRoute []HandlerFunc
noRoute []HandlerFunc allNoMethod []HandlerFunc
noMethod []HandlerFunc noRoute []HandlerFunc
router *httprouter.Router noMethod []HandlerFunc
router *httprouter.Router
} }
) )
@ -115,15 +114,15 @@ func (engine *Engine) Use(middlewares ...HandlerFunc) {
} }
func (engine *Engine) rebuild404Handlers() { func (engine *Engine) rebuild404Handlers() {
engine.allNoRouteNoMethod = engine.combineHandlers(engine.noRoute) engine.allNoRoute = engine.combineHandlers(engine.noRoute)
} }
func (engine *Engine) rebuild405Handlers() { func (engine *Engine) rebuild405Handlers() {
engine.allNoRouteNoMethod = engine.combineHandlers(engine.noMethod) engine.allNoMethod = engine.combineHandlers(engine.noMethod)
} }
func (engine *Engine) handle404(w http.ResponseWriter, req *http.Request) { func (engine *Engine) handle404(w http.ResponseWriter, req *http.Request) {
c := engine.createContext(w, req, nil, engine.allNoRouteNoMethod) c := engine.createContext(w, req, nil, engine.allNoRoute)
// set 404 by default, useful for logging // set 404 by default, useful for logging
c.Writer.WriteHeader(404) c.Writer.WriteHeader(404)
c.Next() c.Next()
@ -138,7 +137,7 @@ func (engine *Engine) handle404(w http.ResponseWriter, req *http.Request) {
} }
func (engine *Engine) handle405(w http.ResponseWriter, req *http.Request) { func (engine *Engine) handle405(w http.ResponseWriter, req *http.Request) {
c := engine.createContext(w, req, nil, engine.allNoRouteNoMethod) c := engine.createContext(w, req, nil, engine.allNoMethod)
// set 405 by default, useful for logging // set 405 by default, useful for logging
c.Writer.WriteHeader(405) c.Writer.WriteHeader(405)
c.Next() c.Next()