mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 05:01:28 +00:00
Merge pull request #1719 from JessonChan/err_ctrler
multiple response.WriteHeader calls
This commit is contained in:
commit
65b13eddad
@ -61,7 +61,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)
|
||||||
}
|
}
|
||||||
@ -178,6 +181,12 @@ type Response struct {
|
|||||||
Status int
|
Status int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Response) reset(rw http.ResponseWriter) {
|
||||||
|
r.ResponseWriter = rw
|
||||||
|
r.Status = 0
|
||||||
|
r.Started = 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,
|
||||||
// and sets `started` to true.
|
// and sets `started` to true.
|
||||||
// started means the response has sent out.
|
// started means the response has sent out.
|
||||||
@ -197,6 +206,10 @@ func (w *Response) Copy(buf *bytes.Buffer) (int64, error) {
|
|||||||
// WriteHeader sends an HTTP response header with status code,
|
// WriteHeader sends an HTTP response header with status code,
|
||||||
// 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 {
|
||||||
|
//prevent multiple response.WriteHeader calls
|
||||||
|
return
|
||||||
|
}
|
||||||
w.Status = code
|
w.Status = code
|
||||||
w.Started = true
|
w.Started = true
|
||||||
w.ResponseWriter.WriteHeader(code)
|
w.ResponseWriter.WriteHeader(code)
|
||||||
|
@ -261,7 +261,7 @@ func (c *Controller) Abort(code string) {
|
|||||||
|
|
||||||
// CustomAbort stops controller handler and show the error data, it's similar Aborts, but support status code and body.
|
// CustomAbort stops controller handler and show the error data, it's similar Aborts, but support status code and body.
|
||||||
func (c *Controller) CustomAbort(status int, body string) {
|
func (c *Controller) CustomAbort(status int, body string) {
|
||||||
c.Ctx.ResponseWriter.WriteHeader(status)
|
c.Ctx.Output.Status = status
|
||||||
// first panic from ErrorMaps, is is user defined error functions.
|
// first panic from ErrorMaps, is is user defined error functions.
|
||||||
if _, ok := ErrorMaps[body]; ok {
|
if _, ok := ErrorMaps[body]; ok {
|
||||||
panic(body)
|
panic(body)
|
||||||
|
@ -24,7 +24,7 @@ import (
|
|||||||
// means if the file name in configuration is project.log filesLogWriter will create project.error.log/project.debug.log
|
// means if the file name in configuration is project.log filesLogWriter will create project.error.log/project.debug.log
|
||||||
// and write the error-level logs to project.error.log and write the debug-level logs to project.debug.log
|
// and write the error-level logs to project.error.log and write the debug-level logs to project.debug.log
|
||||||
// the rotate attribute also acts like fileLogWriter
|
// the rotate attribute also acts like fileLogWriter
|
||||||
type mulitFileLogWriter struct {
|
type multiFileLogWriter struct {
|
||||||
writers [LevelDebug + 1 + 1]*fileLogWriter // the last one for fullLogWriter
|
writers [LevelDebug + 1 + 1]*fileLogWriter // the last one for fullLogWriter
|
||||||
fullLogWriter *fileLogWriter
|
fullLogWriter *fileLogWriter
|
||||||
Separate []string `json:"separate"`
|
Separate []string `json:"separate"`
|
||||||
@ -45,7 +45,7 @@ var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning
|
|||||||
// "separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"],
|
// "separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"],
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func (f *mulitFileLogWriter) Init(config string) error {
|
func (f *multiFileLogWriter) Init(config string) error {
|
||||||
writer := newFileWriter().(*fileLogWriter)
|
writer := newFileWriter().(*fileLogWriter)
|
||||||
err := writer.Init(config)
|
err := writer.Init(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -76,7 +76,7 @@ func (f *mulitFileLogWriter) Init(config string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *mulitFileLogWriter) Destroy() {
|
func (f *multiFileLogWriter) Destroy() {
|
||||||
for i := 0; i < len(f.writers); i++ {
|
for i := 0; i < len(f.writers); i++ {
|
||||||
if f.writers[i] != nil {
|
if f.writers[i] != nil {
|
||||||
f.writers[i].Destroy()
|
f.writers[i].Destroy()
|
||||||
@ -84,7 +84,7 @@ func (f *mulitFileLogWriter) Destroy() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *mulitFileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
|
func (f *multiFileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
|
||||||
if f.fullLogWriter != nil {
|
if f.fullLogWriter != nil {
|
||||||
f.fullLogWriter.WriteMsg(when, msg, level)
|
f.fullLogWriter.WriteMsg(when, msg, level)
|
||||||
}
|
}
|
||||||
@ -98,7 +98,7 @@ func (f *mulitFileLogWriter) WriteMsg(when time.Time, msg string, level int) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *mulitFileLogWriter) Flush() {
|
func (f *multiFileLogWriter) Flush() {
|
||||||
for i := 0; i < len(f.writers); i++ {
|
for i := 0; i < len(f.writers); i++ {
|
||||||
if f.writers[i] != nil {
|
if f.writers[i] != nil {
|
||||||
f.writers[i].Flush()
|
f.writers[i].Flush()
|
||||||
@ -108,9 +108,9 @@ func (f *mulitFileLogWriter) Flush() {
|
|||||||
|
|
||||||
// newFilesWriter create a FileLogWriter returning as LoggerInterface.
|
// newFilesWriter create a FileLogWriter returning as LoggerInterface.
|
||||||
func newFilesWriter() Logger {
|
func newFilesWriter() Logger {
|
||||||
return &mulitFileLogWriter{}
|
return &multiFileLogWriter{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Register("mulitfile", newFilesWriter)
|
Register("multifile", newFilesWriter)
|
||||||
}
|
}
|
@ -24,7 +24,7 @@ import (
|
|||||||
|
|
||||||
func TestFiles_1(t *testing.T) {
|
func TestFiles_1(t *testing.T) {
|
||||||
log := NewLogger(10000)
|
log := NewLogger(10000)
|
||||||
log.SetLogger("mulitfile", `{"filename":"test.log","separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"]}`)
|
log.SetLogger("multifile", `{"filename":"test.log","separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"]}`)
|
||||||
log.Debug("debug")
|
log.Debug("debug")
|
||||||
log.Informational("info")
|
log.Informational("info")
|
||||||
log.Notice("notice")
|
log.Notice("notice")
|
@ -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)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user