mirror of
https://github.com/datarhei/core.git
synced 2025-10-04 15:42:57 +08:00
Write header only if a valid return code is available
This commit is contained in:
@@ -31,6 +31,7 @@ type Config struct {
|
|||||||
type gzipResponseWriter struct {
|
type gzipResponseWriter struct {
|
||||||
io.Writer
|
io.Writer
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
|
wroteHeader bool
|
||||||
wroteBody bool
|
wroteBody bool
|
||||||
minLength int
|
minLength int
|
||||||
minLengthExceeded bool
|
minLengthExceeded bool
|
||||||
@@ -131,9 +132,11 @@ func NewWithConfig(config Config) echo.MiddlewareFunc {
|
|||||||
res.Writer = rw
|
res.Writer = rw
|
||||||
w.Reset(io.Discard)
|
w.Reset(io.Discard)
|
||||||
} else if !grw.minLengthExceeded {
|
} else if !grw.minLengthExceeded {
|
||||||
// Write uncompressed response
|
// If the minimum content length hasn't exceeded, write the uncompressed response
|
||||||
res.Writer = rw
|
res.Writer = rw
|
||||||
|
if grw.wroteHeader {
|
||||||
grw.ResponseWriter.WriteHeader(grw.code)
|
grw.ResponseWriter.WriteHeader(grw.code)
|
||||||
|
}
|
||||||
grw.buffer.WriteTo(rw)
|
grw.buffer.WriteTo(rw)
|
||||||
w.Reset(io.Discard)
|
w.Reset(io.Discard)
|
||||||
}
|
}
|
||||||
@@ -155,6 +158,8 @@ func (w *gzipResponseWriter) WriteHeader(code int) {
|
|||||||
}
|
}
|
||||||
w.Header().Del(echo.HeaderContentLength) // Issue #444
|
w.Header().Del(echo.HeaderContentLength) // Issue #444
|
||||||
|
|
||||||
|
w.wroteHeader = true
|
||||||
|
|
||||||
// Delay writing of the header until we know if we'll actually compress the response
|
// Delay writing of the header until we know if we'll actually compress the response
|
||||||
w.code = code
|
w.code = code
|
||||||
}
|
}
|
||||||
@@ -174,7 +179,9 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) {
|
|||||||
|
|
||||||
// The minimum length is exceeded, add Content-Encoding header and write the header
|
// The minimum length is exceeded, add Content-Encoding header and write the header
|
||||||
w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806
|
w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806
|
||||||
|
if w.wroteHeader {
|
||||||
w.ResponseWriter.WriteHeader(w.code)
|
w.ResponseWriter.WriteHeader(w.code)
|
||||||
|
}
|
||||||
|
|
||||||
return w.Writer.Write(w.buffer.Bytes())
|
return w.Writer.Write(w.buffer.Bytes())
|
||||||
} else {
|
} else {
|
||||||
@@ -186,6 +193,17 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *gzipResponseWriter) Flush() {
|
func (w *gzipResponseWriter) Flush() {
|
||||||
|
if !w.minLengthExceeded {
|
||||||
|
// Enforce compression
|
||||||
|
w.minLengthExceeded = true
|
||||||
|
w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806
|
||||||
|
if w.wroteHeader {
|
||||||
|
w.ResponseWriter.WriteHeader(w.code)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Writer.Write(w.buffer.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
w.Writer.(*gzip.Writer).Flush()
|
w.Writer.(*gzip.Writer).Flush()
|
||||||
if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
|
if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
|
||||||
flusher.Flush()
|
flusher.Flush()
|
||||||
|
Reference in New Issue
Block a user