From 4588812d246172750dcdfd383701edc93a5e6f91 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Mon, 6 Jul 2015 06:36:48 +0100 Subject: [PATCH] Gzip: Fix wildcard extension bug. --- config/setup/gzip.go | 2 +- config/setup/gzip_test.go | 5 +++++ middleware/gzip/filter.go | 4 ++-- middleware/gzip/filter_test.go | 7 +++++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/config/setup/gzip.go b/config/setup/gzip.go index 933ca592..e78d374a 100644 --- a/config/setup/gzip.go +++ b/config/setup/gzip.go @@ -43,7 +43,7 @@ func gzipParse(c *Controller) ([]gzip.Config, error) { return configs, c.ArgErr() } for _, e := range exts { - if !strings.HasPrefix(e, ".") { + if !strings.HasPrefix(e, ".") && e != gzip.ExtWildCard { return configs, fmt.Errorf(`gzip: invalid extension "%v" (must start with dot)`, e) } extFilter.Exts.Add(e) diff --git a/config/setup/gzip_test.go b/config/setup/gzip_test.go index 30843e6b..22d01d7a 100644 --- a/config/setup/gzip_test.go +++ b/config/setup/gzip_test.go @@ -68,6 +68,11 @@ func TestGzip(t *testing.T) { level 3 } `, false}, + {`gzip { not /file + ext * + level 1 + } + `, false}, } for i, test := range tests { c := NewTestController(test.input) diff --git a/middleware/gzip/filter.go b/middleware/gzip/filter.go index e490722b..ceac660e 100644 --- a/middleware/gzip/filter.go +++ b/middleware/gzip/filter.go @@ -33,14 +33,14 @@ type ExtFilter struct { } // extWildCard is the wildcard for extensions. -const extWildCard = "*" +const ExtWildCard = "*" // ShouldCompress checks if the request file extension matches any // of the registered extensions. It returns true if the extension is // found and false otherwise. func (e ExtFilter) ShouldCompress(r *http.Request) bool { ext := path.Ext(r.URL.Path) - return e.Exts.Contains(extWildCard) || e.Exts.Contains(ext) + return e.Exts.Contains(ExtWildCard) || e.Exts.Contains(ext) } // PathFilter is Filter for request path. diff --git a/middleware/gzip/filter_test.go b/middleware/gzip/filter_test.go index 9cc4b8ba..f6537b9e 100644 --- a/middleware/gzip/filter_test.go +++ b/middleware/gzip/filter_test.go @@ -73,6 +73,13 @@ func TestExtFilter(t *testing.T) { t.Errorf("Test %v: Should not be valid filter", i) } } + filter.(ExtFilter).Exts.Add(ExtWildCard) + for i, e := range exts { + r := urlRequest("file" + e) + if !filter.ShouldCompress(r) { + t.Errorf("Test %v: Should be valid filter. Wildcard used.", i) + } + } } func TestPathFilter(t *testing.T) {