Skip to content

Commit

Permalink
Do not override the original io.ReadCloser with the gzip reader
Browse files Browse the repository at this point in the history
The parent should handle the 'closing' of the original / given
`io.ReadCloser`. But, as we are 'creating' a new reader for
gzip-encoded content, the function doing that should be
responsible for closing the reader after all bytes are read.
To be explicit, we should set the new reader to a different
variable, and close it accordingly. Also, prior to this commit,
we did not do any checking to see if there were errors on closing.
This commit does that.
  • Loading branch information
MrSaints committed Mar 15, 2018
1 parent dc42f09 commit 1740467
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/camo/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,27 @@ func hexEncodeCSSURLs(baseURL *url.URL, hmacKey []byte, css []byte) ([]byte, err

func writeCSSWithResolvedURLs(baseURL *url.URL, contentEncoding string, hmacKey []byte, w io.Writer, r io.ReadCloser) (int64, error) {
var err error
var gr io.ReadCloser

if contentEncoding == "gzip" {
r, err = gzip.NewReader(r)
if r != nil {
defer r.Close()
}
gr, err = gzip.NewReader(r)
if err != nil {
return 0, err
}
r = gr
}

css, err := ioutil.ReadAll(r)
if err != nil {
return 0, err
}

if gr != nil {
if err := gr.Close(); err != nil {
return 0, err
}
}

resolvedCSS, err := hexEncodeCSSURLs(baseURL, hmacKey, css)
if err != nil {
return 0, err
Expand Down

0 comments on commit 1740467

Please sign in to comment.