mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-27 23:03:37 -05:00
encode: ignore flushing until after first write (#4318)
* encode: ignore flushing until after first write (fix #4314) The first write will determine if encoding has to be done and will add an Content-Encoding. Until then Flushing has to be delayed so the Content-Encoding header can be added before headers and status code is written. (A passthrough flush would write header and status code) * Update modules/caddyhttp/encode/encode.go Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
This commit is contained in:
parent
f43fd6f388
commit
4ebf100f09
1 changed files with 16 additions and 0 deletions
|
@ -182,6 +182,7 @@ type responseWriter struct {
|
||||||
buf *bytes.Buffer
|
buf *bytes.Buffer
|
||||||
config *Encode
|
config *Encode
|
||||||
statusCode int
|
statusCode int
|
||||||
|
wroteHeader bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteHeader stores the status to write when the time comes
|
// WriteHeader stores the status to write when the time comes
|
||||||
|
@ -195,6 +196,19 @@ func (enc *Encode) Match(rw *responseWriter) bool {
|
||||||
return enc.Matcher.Match(rw.statusCode, rw.Header())
|
return enc.Matcher.Match(rw.statusCode, rw.Header())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flush implements http.Flusher. It delays the actual Flush of the underlying ResponseWriterWrapper
|
||||||
|
// until headers were written.
|
||||||
|
func (rw *responseWriter) Flush() {
|
||||||
|
if !rw.wroteHeader {
|
||||||
|
// flushing the underlying ResponseWriter will write header and status code,
|
||||||
|
// but we need to delay that until we can determine if we must encode and
|
||||||
|
// therefore add the Content-Encoding header; this happens in the first call
|
||||||
|
// to rw.Write (see bug in #4314)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rw.ResponseWriterWrapper.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
// Write writes to the response. If the response qualifies,
|
// Write writes to the response. If the response qualifies,
|
||||||
// it is encoded using the encoder, which is initialized
|
// it is encoded using the encoder, which is initialized
|
||||||
// if not done so already.
|
// if not done so already.
|
||||||
|
@ -225,6 +239,7 @@ func (rw *responseWriter) Write(p []byte) (int, error) {
|
||||||
if rw.statusCode > 0 {
|
if rw.statusCode > 0 {
|
||||||
rw.ResponseWriter.WriteHeader(rw.statusCode)
|
rw.ResponseWriter.WriteHeader(rw.statusCode)
|
||||||
rw.statusCode = 0
|
rw.statusCode = 0
|
||||||
|
rw.wroteHeader = true
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
@ -271,6 +286,7 @@ func (rw *responseWriter) Close() error {
|
||||||
// that rely on If-None-Match, for example
|
// that rely on If-None-Match, for example
|
||||||
rw.ResponseWriter.WriteHeader(rw.statusCode)
|
rw.ResponseWriter.WriteHeader(rw.statusCode)
|
||||||
rw.statusCode = 0
|
rw.statusCode = 0
|
||||||
|
rw.wroteHeader = true
|
||||||
}
|
}
|
||||||
if rw.w != nil {
|
if rw.w != nil {
|
||||||
err2 := rw.w.Close()
|
err2 := rw.w.Close()
|
||||||
|
|
Loading…
Add table
Reference in a new issue