From 1ae7ef40e099785f4c3ba1d1ad145ef3cc2ed482 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Fri, 24 Jul 2015 18:54:42 -0700 Subject: [PATCH] Don't send http error if response already committed. Signed-off-by: Vishal Rana --- README.md | 1 + context.go | 27 ++++++--------------------- echo.go | 6 +++++- response.go | 8 ++++---- website/docs/index.md | 1 + 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 2a7badfe..2c4a73b8 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ A fast and unfancy micro web framework for Golang. - HTML via templates - String - JSON + - JSONP - XML - NoContent - Redirect diff --git a/context.go b/context.go index c2ac0d00..d0a66eb2 100644 --- a/context.go +++ b/context.go @@ -115,10 +115,7 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) { } c.response.Header().Set(ContentType, TextHTMLCharsetUTF8) c.response.WriteHeader(code) - if err = c.echo.renderer.Render(c.response, name, data); err != nil { - c.response.clear() - } - return + return c.echo.renderer.Render(c.response, name, data) } // HTML formats according to a format specifier and sends HTML response with @@ -126,9 +123,7 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) { func (c *Context) HTML(code int, format string, a ...interface{}) (err error) { c.response.Header().Set(ContentType, TextHTMLCharsetUTF8) c.response.WriteHeader(code) - if _, err = fmt.Fprintf(c.response, format, a...); err != nil { - c.response.clear() - } + _, err = fmt.Fprintf(c.response, format, a...) return } @@ -137,9 +132,7 @@ func (c *Context) HTML(code int, format string, a ...interface{}) (err error) { func (c *Context) String(code int, format string, a ...interface{}) (err error) { c.response.Header().Set(ContentType, TextPlain) c.response.WriteHeader(code) - if _, err = fmt.Fprintf(c.response, format, a...); err != nil { - c.response.clear() - } + _, err = fmt.Fprintf(c.response, format, a...) return } @@ -147,10 +140,7 @@ func (c *Context) String(code int, format string, a ...interface{}) (err error) func (c *Context) JSON(code int, i interface{}) (err error) { c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8) c.response.WriteHeader(code) - if err = json.NewEncoder(c.response).Encode(i); err != nil { - c.response.clear() - } - return + return json.NewEncoder(c.response).Encode(i) } // JSONP sends a JSONP response with status code. It uses `callback` to construct @@ -159,9 +149,7 @@ func (c *Context) JSONP(code int, callback string, i interface{}) (err error) { c.response.Header().Set(ContentType, ApplicationJavaScriptCharsetUTF8) c.response.WriteHeader(code) c.response.Write([]byte(callback + "(")) - if err = json.NewEncoder(c.response).Encode(i); err != nil { - c.response.clear() - } else { + if err = json.NewEncoder(c.response).Encode(i); err == nil { c.response.Write([]byte(");")) } return @@ -172,10 +160,7 @@ func (c *Context) XML(code int, i interface{}) (err error) { c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8) c.response.WriteHeader(code) c.response.Write([]byte(xml.Header)) - if err = xml.NewEncoder(c.response).Encode(i); err != nil { - c.response.clear() - } - return + return xml.NewEncoder(c.response).Encode(i) } // NoContent sends a response with no body and a status code. diff --git a/echo.go b/echo.go index 030ae827..21ecf542 100644 --- a/echo.go +++ b/echo.go @@ -195,7 +195,11 @@ func New() (e *Echo) { if e.debug { msg = err.Error() } - http.Error(c.response, msg, code) + if !c.response.committed { + http.Error(c.response, msg, code) + } + // TODO: + // else just send the message? log? } e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler) e.SetBinder(func(r *http.Request, v interface{}) (err error) { diff --git a/response.go b/response.go index ae26a32f..715df158 100644 --- a/response.go +++ b/response.go @@ -81,7 +81,7 @@ func (r *Response) reset(w http.ResponseWriter) { r.committed = false } -func (r *Response) clear() { - r.Header().Del(ContentType) - r.committed = false -} +//func (r *Response) clear() { +// r.Header().Del(ContentType) +// r.committed = false +//} diff --git a/website/docs/index.md b/website/docs/index.md index 25ed59cf..b4a2e4c3 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -28,6 +28,7 @@ A fast and unfancy micro web framework for Golang. - HTML via templates - String - JSON + - JSONP - XML - NoContent - Redirect