Skip to content
Open
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
17 changes: 15 additions & 2 deletions middleware/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net"
"net/http"
"strconv"
"strings"
"sync"
)
Expand Down Expand Up @@ -239,9 +240,21 @@ func (c *Compressor) selectEncoder(h http.Header, w io.Writer) (io.Writer, strin

func matchAcceptEncoding(accepted []string, encoding string) bool {
for _, v := range accepted {
if strings.Contains(v, encoding) {
return true
name, params, _ := strings.Cut(strings.TrimSpace(v), ";")
if !strings.EqualFold(strings.TrimSpace(name), encoding) {
continue
}
for _, param := range strings.Split(params, ";") {
key, value, ok := strings.Cut(strings.TrimSpace(param), "=")
if !ok || !strings.EqualFold(strings.TrimSpace(key), "q") {
continue
}
quality, err := strconv.ParseFloat(strings.TrimSpace(value), 64)
if err == nil && quality <= 0 {
return false
}
}
return true
}
return false
}
Expand Down
128 changes: 128 additions & 0 deletions middleware/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ func TestCompressor(t *testing.T) {
acceptedEncodings: []string{"nop, gzip, deflate"},
expectedEncoding: "nop",
},
{
name: "gzip rejected with q=0 returns no encoding",
path: "/gethtml",
acceptedEncodings: []string{"gzip;q=0"},
expectedEncoding: "",
},
{
name: "gzip rejected with q=0 falls back to deflate",
path: "/gethtml",
acceptedEncodings: []string{"gzip;q=0", "deflate"},
expectedEncoding: "deflate",
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -169,6 +181,122 @@ func TestCompressorWildcards(t *testing.T) {
}
}

func TestMatchAcceptEncoding(t *testing.T) {
tests := []struct {
name string
accepted []string
encoding string
want bool
}{
{
name: "exact match",
accepted: []string{"gzip"},
encoding: "gzip",
want: true,
},
{
name: "no match",
accepted: []string{"deflate"},
encoding: "gzip",
want: false,
},
{
name: "empty list",
accepted: []string{""},
encoding: "gzip",
want: false,
},
{
name: "leading space",
accepted: []string{" gzip"},
encoding: "gzip",
want: true,
},
{
name: "trailing space",
accepted: []string{"gzip "},
encoding: "gzip",
want: true,
},
{
name: "mixed case encoding",
accepted: []string{"GZip"},
encoding: "gzip",
want: true,
},
{
name: "br should not match b",
accepted: []string{"br"},
encoding: "b",
want: false,
},
{
name: "gzip should not match zip",
accepted: []string{"gzip"},
encoding: "zip",
want: false,
},
{
name: "q=0 rejected",
accepted: []string{"gzip;q=0"},
encoding: "gzip",
want: false,
},
{
name: "q=0.0 rejected",
accepted: []string{"gzip;q=0.0"},
encoding: "gzip",
want: false,
},
{
name: "q=0 with space",
accepted: []string{"gzip; q=0"},
encoding: "gzip",
want: false,
},
{
name: "mixed case q=0 rejected",
accepted: []string{"gzip; Q=0"},
encoding: "gzip",
want: false,
},
{
name: "q=0.5 accepted",
accepted: []string{"gzip;q=0.5"},
encoding: "gzip",
want: true,
},
{
name: "q=1.0 accepted",
accepted: []string{"gzip;q=1.0"},
encoding: "gzip",
want: true,
},
{
name: "match on second element",
accepted: []string{"deflate", "gzip"},
encoding: "gzip",
want: true,
},
{
name: "q=0 on second element",
accepted: []string{"deflate", "gzip;q=0"},
encoding: "gzip",
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := matchAcceptEncoding(tt.accepted, tt.encoding)
if got != tt.want {
t.Errorf("matchAcceptEncoding(%v, %q) = %v, want %v",
tt.accepted, tt.encoding, got, tt.want)
}
})
}
}

func testRequestWithAcceptedEncodings(t *testing.T, ts *httptest.Server, method, path string, encodings ...string) (*http.Response, string) {
req, err := http.NewRequest(method, ts.URL+path, nil)
if err != nil {
Expand Down