Skip to content
Closed
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
29 changes: 13 additions & 16 deletions middleware/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import (
)

var defaultCompressibleContentTypes = []string{
"text/html",
"text/css",
"text/plain",
"text/javascript",
"text/*",
"application/javascript",
"application/x-javascript",
"application/json",
"application/xml",
"application/atom+xml",
"application/rss+xml",
"image/svg+xml",
Expand Down Expand Up @@ -66,19 +64,18 @@ func NewCompressor(level int, types ...string) *Compressor {
// provided, use the default list.
allowedTypes := make(map[string]struct{})
allowedWildcards := make(map[string]struct{})
if len(types) > 0 {
for _, t := range types {
if strings.Contains(strings.TrimSuffix(t, "/*"), "*") {
panic(fmt.Sprintf("middleware/compress: Unsupported content-type wildcard pattern '%s'. Only '/*' supported", t))
}
if strings.HasSuffix(t, "/*") {
allowedWildcards[strings.TrimSuffix(t, "/*")] = struct{}{}
} else {
allowedTypes[t] = struct{}{}
}

if len(types) == 0 {
types = defaultCompressibleContentTypes
}

for _, t := range types {
if strings.Contains(strings.TrimSuffix(t, "/*"), "*") {
panic(fmt.Sprintf("middleware/compress: Unsupported content-type wildcard pattern '%s'. Only '/*' supported", t))
}
} else {
for _, t := range defaultCompressibleContentTypes {
if strings.HasSuffix(t, "/*") {
allowedWildcards[strings.TrimSuffix(t, "/*")] = struct{}{}
} else {
allowedTypes[t] = struct{}{}
}
}
Expand Down
3 changes: 2 additions & 1 deletion middleware/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ func TestCompressorWildcards(t *testing.T) {
}{
{
name: "defaults",
typesCount: 10,
typesCount: 7,
wcCount: 1,
},
{
name: "no wildcard",
Expand Down