1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-02 15:43:28 +00:00

beego/context: Fix ignored Header in case SetStatus has been called before

This commit is contained in:
Christoph Portmann 2014-07-08 23:20:55 +03:00
parent fefd8ddb5b
commit 3bb4d6f013
2 changed files with 13 additions and 1 deletions

View File

@ -71,6 +71,14 @@ func (output *BeegoOutput) Body(content []byte) {
} else { } else {
output.Header("Content-Length", strconv.Itoa(len(content))) output.Header("Content-Length", strconv.Itoa(len(content)))
} }
// Write status code if it has been set manually
// Set it to 0 afterwards to prevent "multiple response.WriteHeader calls"
if output.Status != 0 {
output.Context.ResponseWriter.WriteHeader(output.Status)
output.Status = 0
}
output_writer.Write(content) output_writer.Write(content)
switch output_writer.(type) { switch output_writer.(type) {
case *gzip.Writer: case *gzip.Writer:
@ -270,7 +278,6 @@ func (output *BeegoOutput) ContentType(ext string) {
// SetStatus sets response status code. // SetStatus sets response status code.
// It writes response header directly. // It writes response header directly.
func (output *BeegoOutput) SetStatus(status int) { func (output *BeegoOutput) SetStatus(status int) {
output.Context.ResponseWriter.WriteHeader(status)
output.Status = status output.Status = status
} }

View File

@ -762,6 +762,11 @@ Admin:
Info("beego:" + r.URL.Path + " 404" + " +" + timeend.String()) Info("beego:" + r.URL.Path + " 404" + " +" + timeend.String())
} }
} }
// Call WriteHeader if status code has been set changed
if context.Output.Status != 0 {
w.writer.WriteHeader(context.Output.Status)
}
} }
func (p *ControllerRegistor) recoverPanic(rw http.ResponseWriter, r *http.Request) { func (p *ControllerRegistor) recoverPanic(rw http.ResponseWriter, r *http.Request) {