diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2019-06-18 11:13:12 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2019-06-18 11:13:12 -0600 |
commit | 6706c9225a8dcb976785bdf2c35eb151d54ac18c (patch) | |
tree | 1e278a1261e826d0bda3c924bd179aabd0af2e92 /modules/caddyhttp/encode | |
parent | 5137859e47678aae81e178ca7d164f9e2b4e3121 (diff) |
Implement templates handler; various minor cleanups and bug fixes
Diffstat (limited to 'modules/caddyhttp/encode')
-rw-r--r-- | modules/caddyhttp/encode/encode.go | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/modules/caddyhttp/encode/encode.go b/modules/caddyhttp/encode/encode.go index cf658e3..e20667f 100644 --- a/modules/caddyhttp/encode/encode.go +++ b/modules/caddyhttp/encode/encode.go @@ -148,7 +148,7 @@ func (rw *responseWriter) Write(p []byte) (int, error) { return n, err } -// init should be called once we know we are writing an encoded response. +// init should be called before we write a response, if rw.buf is not nil. func (rw *responseWriter) init() { if rw.Header().Get("Content-Encoding") == "" && rw.buf.Len() >= rw.config.MinLength { rw.w = rw.config.writerPools[rw.encodingName].Get().(Encoder) @@ -164,7 +164,13 @@ func (rw *responseWriter) init() { // deallocates any active resources. func (rw *responseWriter) Close() error { var err error - if rw.buf != nil { + // only attempt to write the remaining buffered response + // if there are any bytes left to write; otherwise, if + // the handler above us returned an error without writing + // anything, we'd write to the response when we instead + // should simply let the error propagate back down; this + // is why the check for rw.buf.Len() > 0 is crucial + if rw.buf != nil && rw.buf.Len() > 0 { rw.init() p := rw.buf.Bytes() defer func() { @@ -280,7 +286,7 @@ const defaultMinLength = 512 // Interface guards var ( - _ caddy.Provisioner = (*Encode)(nil) + _ caddy.Provisioner = (*Encode)(nil) _ caddyhttp.MiddlewareHandler = (*Encode)(nil) _ caddyhttp.HTTPInterfaces = (*responseWriter)(nil) ) |