Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 133 additions & 15 deletions middleware/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type gzipResponseWriter struct {
wroteBody bool
minLength int
minLengthExceeded bool
passthrough bool
buffer *bytes.Buffer
code int
}
Expand Down Expand Up @@ -91,7 +92,8 @@ func (config GzipConfig) ToMiddleware() (echo.MiddlewareFunc, error) {

res := c.Response()
res.Header().Add(echo.HeaderVary, echo.HeaderAcceptEncoding)
if strings.Contains(c.Request().Header.Get(echo.HeaderAcceptEncoding), gzipScheme) {
var succeeded bool
if acceptsGzip(c.Request().Header.Values(echo.HeaderAcceptEncoding)) {
i := pool.Get()
w, ok := i.(*gzip.Writer)
if !ok {
Expand All @@ -113,9 +115,16 @@ func (config GzipConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
// There are different reasons for cases when we have not yet written response to the client and now need to do so.
// a) handler response had only response code and no response body (ala 404 or redirects etc). Response code need to be written now.
// b) body is shorter than our minimum length threshold and being buffered currently and needs to be written
if !grw.wroteBody {
if res.Header().Get(echo.HeaderContentEncoding) == gzipScheme {
res.Header().Del(echo.HeaderContentEncoding)
// Responses already encoded by the handler pass through unchanged.
if grw.passthrough || (succeeded && grw.passThroughIfEncoded()) {
c.SetResponse(rw)
w.Reset(io.Discard)
} else if !grw.wroteBody && !grw.minLengthExceeded {
// Nothing was compressed, so no coding applies to the body that follows.
res.Header().Del(echo.HeaderContentEncoding)
if !succeeded {
// The error handler writes its own body.
res.Header().Del(echo.HeaderContentLength)
}
if grw.wroteHeader {
rw.WriteHeader(grw.code)
Expand All @@ -139,21 +148,31 @@ func (config GzipConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
pool.Put(w)
}()
}
return next(c)
err := next(c)
succeeded = err == nil
return err
}
}, nil
}

func (w *gzipResponseWriter) WriteHeader(code int) {
w.Header().Del(echo.HeaderContentLength) // Issue #444
if w.passthrough {
w.ResponseWriter.WriteHeader(code)
return
}

w.wroteHeader = true

// Delay writing of the header until we know if we'll actually compress the response
w.code = code
w.passThroughIfEncoded()
}

func (w *gzipResponseWriter) Write(b []byte) (int, error) {
if w.passThroughIfEncoded() {
return w.ResponseWriter.Write(b)
}

if w.Header().Get(echo.HeaderContentType) == "" {
w.Header().Set(echo.HeaderContentType, http.DetectContentType(b))
}
Expand All @@ -166,12 +185,13 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) {
w.minLengthExceeded = true

// The minimum length is exceeded, add Content-Encoding header and write the header
w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806
if w.wroteHeader {
w.ResponseWriter.WriteHeader(w.code)
}
w.startCompression()

return w.Writer.Write(w.buffer.Bytes())
if _, err := w.Writer.Write(w.buffer.Bytes()); err != nil {
return 0, err
}
// Report only the caller's bytes; the buffer also holds earlier writes.
return len(b), nil
}

return n, err
Expand All @@ -181,13 +201,23 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) {
}

func (w *gzipResponseWriter) Flush() {
if w.passThroughIfEncoded() {
if !w.wroteHeader {
// Commit through echo.Response so the error handler sees the response as written.
w.wroteHeader = true
w.code = http.StatusOK
if r, err := echo.UnwrapResponse(w.ResponseWriter); err == nil && !r.Committed {
r.WriteHeader(http.StatusOK)
}
}
_ = http.NewResponseController(w.ResponseWriter).Flush()
return
}

if !w.minLengthExceeded {
// Enforce compression because we will not know how much more data will come
w.minLengthExceeded = true
w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806
if w.wroteHeader {
w.ResponseWriter.WriteHeader(w.code)
}
w.startCompression()

_, _ = w.Writer.Write(w.buffer.Bytes())
}
Expand All @@ -198,6 +228,46 @@ func (w *gzipResponseWriter) Flush() {
_ = http.NewResponseController(w.ResponseWriter).Flush()
}

// startCompression commits gzip headers once the response is known to be compressed.
func (w *gzipResponseWriter) startCompression() {
w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806
w.Header().Del(echo.HeaderContentLength) // Issue #444
if w.wroteHeader {
w.ResponseWriter.WriteHeader(w.code)
}
}

// passThroughIfEncoded switches to the underlying writer before gzip handles the body.
func (w *gzipResponseWriter) passThroughIfEncoded() bool {
if w.passthrough {
return true
}
if w.wroteBody || w.minLengthExceeded {
return false
}
if !hasContentCoding(w.Header().Values(echo.HeaderContentEncoding)) {
return false
}
w.passthrough = true
if w.wroteHeader {
w.ResponseWriter.WriteHeader(w.code)
}
return true
}

// hasContentCoding reports whether Content-Encoding lists any coding other than identity.
func hasContentCoding(headers []string) bool {
for _, header := range headers {
for coding := range strings.SplitSeq(header, ",") {
coding = strings.TrimSpace(coding)
if coding != "" && !strings.EqualFold(coding, "identity") {
return true
}
}
}
return false
}

func (w *gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return http.NewResponseController(w.ResponseWriter).Hijack()
}
Expand Down Expand Up @@ -233,3 +303,51 @@ func bufferPool() sync.Pool {
},
}
}

func acceptsGzip(headers []string) bool {
var gzipAllowed, wildcardAllowed, wildcardRefused bool
for _, header := range headers {
for entry := range strings.SplitSeq(header, ",") {
coding, weight, hasWeight := strings.Cut(entry, ";")
coding = strings.TrimSpace(coding)
switch {
case strings.EqualFold(coding, gzipScheme), strings.EqualFold(coding, "x-gzip"):
// RFC 9110, section 8.4.1.3: x-gzip is equivalent to gzip.
// An explicit refusal or invalid weight takes precedence over
// wildcards and over any duplicate entry allowing gzip.
if hasWeight && !positiveQualityValue(weight) {
return false
}
gzipAllowed = true
case coding == "*":
if hasWeight && !positiveQualityValue(weight) {
wildcardRefused = true
} else {
wildcardAllowed = true
}
}
}
}
return gzipAllowed || (wildcardAllowed && !wildcardRefused)
}

// positiveQualityValue matches the qvalue grammar in RFC 9110, section 12.4.2.
func positiveQualityValue(weight string) bool {
name, value, ok := strings.Cut(strings.TrimSpace(weight), "=")
if !ok || !strings.EqualFold(name, "q") {
return false
}
whole, fraction, _ := strings.Cut(value, ".")
if (whole != "0" && whole != "1") || len(fraction) > 3 {
return false
}
positive := whole == "1"
for i := range len(fraction) {
digit := fraction[i]
if digit < '0' || digit > '9' || (whole == "1" && digit != '0') {
return false
}
positive = positive || digit != '0'
}
return positive
}
100 changes: 100 additions & 0 deletions middleware/compress_proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors

package middleware

import (
"bytes"
"compress/gzip"
"compress/zlib"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"testing"

"github.com/labstack/echo/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGzip_PreservesProxyContentEncoding(t *testing.T) {
tests := []struct {
name string
method string
minLength int
emptyEncodingField bool
}{
{"get", http.MethodGet, 0, false},
{"get above threshold", http.MethodGet, 8, false},
{"get below threshold", http.MethodGet, 4096, false},
{"head", http.MethodHead, 0, false},
{"empty first encoding field", http.MethodGet, 0, true},
}
payload := []byte("encoded upstream response")
for _, encoding := range []string{"gzip", "deflate"} {
var encoded bytes.Buffer
var compressor io.WriteCloser
if encoding == "gzip" {
compressor = gzip.NewWriter(&encoded)
} else {
compressor = zlib.NewWriter(&encoded)
}
_, err := compressor.Write(payload)
require.NoError(t, err)
require.NoError(t, compressor.Close())
wire := encoded.Bytes()

for _, tt := range tests {
t.Run(encoding+"/"+tt.name, func(t *testing.T) {
encodings := []string{encoding}
if tt.emptyEncodingField {
encodings = []string{"", encoding}
}
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, tt.method, r.Method)
for _, value := range encodings {
w.Header().Add(echo.HeaderContentEncoding, value)
}
w.Header().Set(echo.HeaderContentType, echo.MIMETextPlain)
w.Header().Set(echo.HeaderContentLength, strconv.Itoa(len(wire)))
w.WriteHeader(http.StatusCreated)
if r.Method != http.MethodHead {
_, _ = w.Write(wire)
}
}))
defer upstream.Close()
target, err := url.Parse(upstream.URL)
require.NoError(t, err)

e := echo.New()
e.Use(GzipWithConfig(GzipConfig{MinLength: tt.minLength}))
e.Use(Proxy(NewRoundRobinBalancer([]*ProxyTarget{{URL: target}})))
proxy := httptest.NewServer(e)
defer proxy.Close()

transport := &http.Transport{DisableCompression: true}
defer transport.CloseIdleConnections()
client := &http.Client{Transport: transport}
req, err := http.NewRequest(tt.method, proxy.URL, nil)
require.NoError(t, err)
req.Header.Set(echo.HeaderAcceptEncoding, "gzip, deflate")
res, err := client.Do(req)
require.NoError(t, err)
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
require.NoError(t, err)

assert.Equal(t, http.StatusCreated, res.StatusCode)
assert.Equal(t, encodings, res.Header.Values(echo.HeaderContentEncoding))
assert.Equal(t, int64(len(wire)), res.ContentLength)
if tt.method == http.MethodHead {
assert.Empty(t, body)
} else {
assert.Equal(t, wire, body, "proxy must preserve the upstream encoded bytes")
}
})
}
}
}
Loading
Loading