1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Dropped Response#Writer/SetWriter functions

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-12-21 18:56:35 -08:00
parent d9a6052c67
commit 869cdcd19a
2 changed files with 13 additions and 22 deletions

View File

@ -67,23 +67,24 @@ func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
res := c.Response()
res.Header().Add(echo.HeaderVary, echo.HeaderAcceptEncoding)
if strings.Contains(c.Request().Header.Get(echo.HeaderAcceptEncoding), gzipScheme) {
rw := res.Writer()
rw := res.Writer
w, err := gzip.NewWriterLevel(rw, config.Level)
if err != nil {
return err
}
defer func() {
if res.Size == 0 {
// We have to reset response to it's pristine state when
// nothing is written to body or error is returned.
// See issue #424, #407.
res.SetWriter(rw)
res.Writer = rw
w.Reset(ioutil.Discard)
}
w.Close()
}()
grw := &gzipResponseWriter{Writer: w, ResponseWriter: rw}
res.SetWriter(grw)
res.Writer = grw
}
return next(c)
}

View File

@ -11,7 +11,7 @@ type (
// by an HTTP handler to construct an HTTP response.
// See: https://golang.org/pkg/net/http/#ResponseWriter
Response struct {
writer http.ResponseWriter
Writer http.ResponseWriter
Status int
Size int64
Committed bool
@ -21,17 +21,7 @@ type (
// NewResponse creates a new instance of Response.
func NewResponse(w http.ResponseWriter, e *Echo) (r *Response) {
return &Response{writer: w, echo: e}
}
// SetWriter sets the http.ResponseWriter instance for this Response.
func (r *Response) SetWriter(w http.ResponseWriter) {
r.writer = w
}
// Writer returns the http.ResponseWriter instance for this Response.
func (r *Response) Writer() http.ResponseWriter {
return r.writer
return &Response{Writer: w, echo: e}
}
// Header returns the header map for the writer that will be sent by
@ -41,7 +31,7 @@ func (r *Response) Writer() http.ResponseWriter {
// To suppress implicit response headers, set their value to nil.
// Example: https://golang.org/pkg/net/http/#example_ResponseWriter_trailers
func (r *Response) Header() http.Header {
return r.writer.Header()
return r.Writer.Header()
}
// WriteHeader sends an HTTP response header with status code. If WriteHeader is
@ -54,7 +44,7 @@ func (r *Response) WriteHeader(code int) {
return
}
r.Status = code
r.writer.WriteHeader(code)
r.Writer.WriteHeader(code)
r.Committed = true
}
@ -63,7 +53,7 @@ func (r *Response) Write(b []byte) (n int, err error) {
if !r.Committed {
r.WriteHeader(http.StatusOK)
}
n, err = r.writer.Write(b)
n, err = r.Writer.Write(b)
r.Size += int64(n)
return
}
@ -72,14 +62,14 @@ func (r *Response) Write(b []byte) (n int, err error) {
// buffered data to the client.
// See [http.Flusher](https://golang.org/pkg/net/http/#Flusher)
func (r *Response) Flush() {
r.writer.(http.Flusher).Flush()
r.Writer.(http.Flusher).Flush()
}
// Hijack implements the http.Hijacker interface to allow an HTTP handler to
// take over the connection.
// See [http.Hijacker](https://golang.org/pkg/net/http/#Hijacker)
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return r.writer.(http.Hijacker).Hijack()
return r.Writer.(http.Hijacker).Hijack()
}
// CloseNotify implements the http.CloseNotifier interface to allow detecting
@ -88,11 +78,11 @@ func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
// client has disconnected before the response is ready.
// See [http.CloseNotifier](https://golang.org/pkg/net/http/#CloseNotifier)
func (r *Response) CloseNotify() <-chan bool {
return r.writer.(http.CloseNotifier).CloseNotify()
return r.Writer.(http.CloseNotifier).CloseNotify()
}
func (r *Response) reset(w http.ResponseWriter) {
r.writer = w
r.Writer = w
r.Size = 0
r.Status = http.StatusOK
r.Committed = false