Skip to content

Commit

Permalink
Merge pull request #258 from mtojek/fix_compress2
Browse files Browse the repository at this point in the history
Fix: nil pointer dereference when writing using closed compressor
  • Loading branch information
emicklei committed Nov 25, 2015
2 parents 362c6e6 + 6e887cf commit 7b748ee
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (c *CompressingResponseWriter) WriteHeader(status int) {
// Write is part of http.ResponseWriter interface
// It is passed through the compressor
func (c *CompressingResponseWriter) Write(bytes []byte) (int, error) {
if c.isCompressorClosed() {
return -1, errors.New("Compressing error: tried to write data using closed compressor")
}
return c.compressor.Write(bytes)
}

Expand All @@ -45,7 +48,11 @@ func (c *CompressingResponseWriter) CloseNotify() <-chan bool {
}

// Close the underlying compressor
func (c *CompressingResponseWriter) Close() {
func (c *CompressingResponseWriter) Close() error {
if c.isCompressorClosed() {
return errors.New("Compressing error: tried to close already closed compressor")
}

c.compressor.Close()
if ENCODING_GZIP == c.encoding {
currentCompressorProvider.ReleaseGzipWriter(c.compressor.(*gzip.Writer))
Expand All @@ -55,6 +62,11 @@ func (c *CompressingResponseWriter) Close() {
}
// gc hint needed?
c.compressor = nil
return nil
}

func (c *CompressingResponseWriter) isCompressorClosed() bool {
return nil == c.compressor
}

// WantsCompressedResponse reads the Accept-Encoding header to see if and which encoding is requested.
Expand Down

0 comments on commit 7b748ee

Please sign in to comment.