From 446b2b29ab21ccca74cc3adf94ccbf4351ec193f Mon Sep 17 00:00:00 2001 From: mmukahhal-tibco Date: Fri, 22 Nov 2024 23:14:26 +0000 Subject: [PATCH] fix content encoding not parsing comma separated lists of encodings --- middleware/content_encoding.go | 11 +++++++---- middleware/content_encoding_test.go | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/middleware/content_encoding.go b/middleware/content_encoding.go index e0b9ccc0..79580f59 100644 --- a/middleware/content_encoding.go +++ b/middleware/content_encoding.go @@ -21,10 +21,13 @@ func AllowContentEncoding(contentEncoding ...string) func(next http.Handler) htt return } // All encodings in the request must be allowed - for _, encoding := range requestEncodings { - if _, ok := allowedEncodings[strings.TrimSpace(strings.ToLower(encoding))]; !ok { - w.WriteHeader(http.StatusUnsupportedMediaType) - return + for _, encodingList := range requestEncodings { + encodings := strings.Split(encodingList, ",") + for _, encoding := range encodings { + if _, ok := allowedEncodings[strings.TrimSpace(strings.ToLower(encoding))]; !ok { + w.WriteHeader(http.StatusUnsupportedMediaType) + return + } } } next.ServeHTTP(w, r) diff --git a/middleware/content_encoding_test.go b/middleware/content_encoding_test.go index e23a708f..8045f6e7 100644 --- a/middleware/content_encoding_test.go +++ b/middleware/content_encoding_test.go @@ -49,11 +49,21 @@ func TestContentEncodingMiddleware(t *testing.T) { encodings: []string{"deflate", "gzip"}, expectedStatus: 200, }, + { + name: "Support for deflate and gzip encoding", + encodings: []string{"deflate, gzip"}, + expectedStatus: 200, + }, { name: "No support for deflate and br encoding", encodings: []string{"deflate", "br"}, expectedStatus: 415, }, + { + name: "No support for deflate and br encoding", + encodings: []string{"deflate, br"}, + expectedStatus: 415, + }, } for _, tt := range tests {