From 273b36c9fc96f2ff955a218fa0d147aa5fd29c54 Mon Sep 17 00:00:00 2001 From: Evan Teran Date: Wed, 12 Jun 2024 10:46:38 -0400 Subject: [PATCH 1/3] changed the NewCompressor logic to be support wildcards in the default set like it already did for a custom list by reusing that logic made defaultCompressibleContentTypes contain "text/*" as all text mime types are arguably very compressible added application/xml to defaultCompressibleContentTypes since it is also a common XML mime type --- middleware/compress.go | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/middleware/compress.go b/middleware/compress.go index 7ba95fd5..93497496 100644 --- a/middleware/compress.go +++ b/middleware/compress.go @@ -15,13 +15,12 @@ import ( ) var defaultCompressibleContentTypes = []string{ - "text/html", - "text/css", - "text/plain", + "text/*", "text/javascript", "application/javascript", "application/x-javascript", "application/json", + "application/xml", "application/atom+xml", "application/rss+xml", "image/svg+xml", @@ -66,19 +65,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{}{} } } From 112baec442e98685bdb13e1063217b945f571a6d Mon Sep 17 00:00:00 2001 From: Evan Teran Date: Wed, 12 Jun 2024 10:52:48 -0400 Subject: [PATCH 2/3] removing accidental redundancy in the default list --- middleware/compress.go | 1 - 1 file changed, 1 deletion(-) diff --git a/middleware/compress.go b/middleware/compress.go index 93497496..a1baa57e 100644 --- a/middleware/compress.go +++ b/middleware/compress.go @@ -16,7 +16,6 @@ import ( var defaultCompressibleContentTypes = []string{ "text/*", - "text/javascript", "application/javascript", "application/x-javascript", "application/json", From 1dd724ba802f0f8fc9c390c551ffbf7729b29532 Mon Sep 17 00:00:00 2001 From: Evan Teran Date: Fri, 28 Jun 2024 12:33:43 -0400 Subject: [PATCH 3/3] updating tests to account for new rules and expectations 7 regular rules and 1 wildcard in the default ruleset --- middleware/compress_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/middleware/compress_test.go b/middleware/compress_test.go index 992f1d40..2fd35b04 100644 --- a/middleware/compress_test.go +++ b/middleware/compress_test.go @@ -140,7 +140,8 @@ func TestCompressorWildcards(t *testing.T) { }{ { name: "defaults", - typesCount: 10, + typesCount: 7, + wcCount: 1, }, { name: "no wildcard",