1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 17:40:55 +00:00

the net/http should set header first,the set http status code and then write the content

This commit is contained in:
JessonChan 2016-02-24 11:47:55 +08:00
parent 76d69b6e51
commit 8c37e76503
2 changed files with 20 additions and 7 deletions

View File

@ -59,7 +59,10 @@ type Context struct {
// Reset init Context, BeegoInput and BeegoOutput // Reset init Context, BeegoInput and BeegoOutput
func (ctx *Context) Reset(rw http.ResponseWriter, r *http.Request) { func (ctx *Context) Reset(rw http.ResponseWriter, r *http.Request) {
ctx.Request = r ctx.Request = r
ctx.ResponseWriter = &Response{rw, false, 0} if ctx.ResponseWriter == nil {
ctx.ResponseWriter = &Response{}
}
ctx.ResponseWriter.reset(rw)
ctx.Input.Reset(ctx) ctx.Input.Reset(ctx)
ctx.Output.Reset(ctx) ctx.Output.Reset(ctx)
} }
@ -174,6 +177,14 @@ type Response struct {
http.ResponseWriter http.ResponseWriter
Started bool Started bool
Status int Status int
wroteHeader bool
}
func (r *Response) reset(rw http.ResponseWriter) {
r.ResponseWriter = rw
r.Status = 0
r.Started = false
r.wroteHeader = false
} }
// Write writes the data to the connection as part of an HTTP reply, // Write writes the data to the connection as part of an HTTP reply,
@ -181,6 +192,11 @@ type Response struct {
// started means the response has sent out. // started means the response has sent out.
func (w *Response) Write(p []byte) (int, error) { func (w *Response) Write(p []byte) (int, error) {
w.Started = true w.Started = true
if !w.wroteHeader {
w.ResponseWriter.WriteHeader(w.Status)
//prevent multiple response.WriteHeader calls
w.wroteHeader = true
}
return w.ResponseWriter.Write(p) return w.ResponseWriter.Write(p)
} }
@ -188,12 +204,10 @@ func (w *Response) Write(p []byte) (int, error) {
// and sets `started` to true. // and sets `started` to true.
func (w *Response) WriteHeader(code int) { func (w *Response) WriteHeader(code int) {
if w.Status > 0 { if w.Status > 0 {
//prevent multiple response.WriteHeader calls
return return
} }
w.Status = code w.Status = code
w.Started = true w.Started = true
w.ResponseWriter.WriteHeader(code)
} }
// Hijack hijacker for http // Hijack hijacker for http

View File

@ -607,6 +607,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
) )
context := p.pool.Get().(*beecontext.Context) context := p.pool.Get().(*beecontext.Context)
context.Reset(rw, r) context.Reset(rw, r)
defer p.pool.Put(context) defer p.pool.Put(context)
defer p.recoverPanic(context) defer p.recoverPanic(context)
@ -616,8 +617,6 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
context.Output.Header("Server", BConfig.ServerName) context.Output.Header("Server", BConfig.ServerName)
} }
context.Output.Header("Content-Type", "text/html; charset=utf-8")
var urlPath string var urlPath string
if !BConfig.RouterCaseSensitive { if !BConfig.RouterCaseSensitive {
urlPath = strings.ToLower(r.URL.Path) urlPath = strings.ToLower(r.URL.Path)