Use ioutil.Discard instead for unneeded bytes.

This commit is contained in:
Abiola Ibrahim 2015-12-09 18:44:25 +01:00
parent 23631cfaca
commit a44d59f1e5
2 changed files with 5 additions and 8 deletions

View file

@ -3,10 +3,10 @@
package gzip package gzip
import ( import (
"bytes"
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
@ -49,12 +49,9 @@ outer:
r.Header.Del("Accept-Encoding") r.Header.Del("Accept-Encoding")
// gzipWriter modifies underlying writer at init, // gzipWriter modifies underlying writer at init,
// use a buffer instead to leave ResponseWriter in // use a discard writer instead to leave ResponseWriter in
// original form. // original form.
var buf = &bytes.Buffer{} gzipWriter, err := newWriter(c, ioutil.Discard)
defer buf.Reset()
gzipWriter, err := newWriter(c, buf)
if err != nil { if err != nil {
// should not happen // should not happen
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@ -65,7 +62,7 @@ outer:
var rw http.ResponseWriter var rw http.ResponseWriter
// if no response filter is used // if no response filter is used
if len(c.ResponseFilters) == 0 { if len(c.ResponseFilters) == 0 {
// replace buffer with ResponseWriter // replace discard writer with ResponseWriter
gzipWriter.Reset(w) gzipWriter.Reset(w)
rw = gz rw = gz
} else { } else {

View file

@ -52,7 +52,7 @@ func (r *ResponseFilterWriter) WriteHeader(code int) {
} }
if r.shouldCompress { if r.shouldCompress {
// replace buffer with ResponseWriter // replace discard writer with ResponseWriter
if gzWriter, ok := r.gzipResponseWriter.Writer.(*gzip.Writer); ok { if gzWriter, ok := r.gzipResponseWriter.Writer.(*gzip.Writer); ok {
gzWriter.Reset(r.ResponseWriter) gzWriter.Reset(r.ResponseWriter)
} }