Skip to content

Commit

Permalink
Dropped Response#Writer/SetWriter functions
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed Dec 22, 2016
1 parent d9a6052 commit 869cdcd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
7 changes: 4 additions & 3 deletions middleware/compress.go
Expand Up @@ -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)
}
Expand Down
28 changes: 9 additions & 19 deletions response.go
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 869cdcd

Please sign in to comment.