Commit b5579ca9 authored by Matthew Holt's avatar Matthew Holt

gzip: Remove mimes

parent 32ef35b9
...@@ -28,7 +28,6 @@ func gzipParse(c *Controller) ([]gzip.Config, error) { ...@@ -28,7 +28,6 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
config := gzip.Config{} config := gzip.Config{}
pathFilter := gzip.PathFilter{make(gzip.Set)} pathFilter := gzip.PathFilter{make(gzip.Set)}
mimeFilter := gzip.MIMEFilter{make(gzip.Set)}
extFilter := gzip.ExtFilter{make(gzip.Set)} extFilter := gzip.ExtFilter{make(gzip.Set)}
// No extra args expected // No extra args expected
...@@ -38,17 +37,6 @@ func gzipParse(c *Controller) ([]gzip.Config, error) { ...@@ -38,17 +37,6 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
for c.NextBlock() { for c.NextBlock() {
switch c.Val() { switch c.Val() {
case "mimes":
mimes := c.RemainingArgs()
if len(mimes) == 0 {
return configs, c.ArgErr()
}
for _, m := range mimes {
if !gzip.ValidMIME(m) {
return configs, fmt.Errorf("gzip: invalid MIME %v", m)
}
mimeFilter.Types.Add(m)
}
case "ext": case "ext":
exts := c.RemainingArgs() exts := c.RemainingArgs()
if len(exts) == 0 { if len(exts) == 0 {
...@@ -92,13 +80,10 @@ func gzipParse(c *Controller) ([]gzip.Config, error) { ...@@ -92,13 +80,10 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
config.Filters = []gzip.Filter{pathFilter} config.Filters = []gzip.Filter{pathFilter}
} }
// If extensions specified, use it over MIME types (if any). // Then, if extensions are specified, use those to filter.
// Otherwise, if specified, use MIME types.
// Otherwise, use default extensions filter. // Otherwise, use default extensions filter.
if len(extFilter.Exts) > 0 { if len(extFilter.Exts) > 0 {
config.Filters = append(config.Filters, extFilter) config.Filters = append(config.Filters, extFilter)
} else if len(mimeFilter.Types) > 0 {
config.Filters = append(config.Filters, mimeFilter)
} else { } else {
config.Filters = append(config.Filters, gzip.DefaultExtFilter()) config.Filters = append(config.Filters, gzip.DefaultExtFilter())
} }
......
...@@ -59,25 +59,13 @@ func TestGzip(t *testing.T) { ...@@ -59,25 +59,13 @@ func TestGzip(t *testing.T) {
level 3 level 3
} }
`, false}, `, false},
{`gzip { mimes text/html
}`, false},
{`gzip { mimes text/html application/json
}`, false},
{`gzip { mimes text/html application/
}`, true},
{`gzip { mimes text/html /json
}`, true},
{`gzip { mimes /json text/html
}`, true},
{`gzip { not /file {`gzip { not /file
ext .html ext .html
level 1 level 1
mimes text/html text/plain
} }
gzip { not /file1 gzip { not /file1
ext .htm ext .htm
level 3 level 3
mimes text/html text/css
} }
`, false}, `, false},
} }
......
...@@ -3,7 +3,6 @@ package gzip ...@@ -3,7 +3,6 @@ package gzip
import ( import (
"net/http" "net/http"
"path" "path"
"strings"
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
) )
...@@ -16,7 +15,7 @@ type Filter interface { ...@@ -16,7 +15,7 @@ type Filter interface {
} }
// defaultExtensions is the list of default extensions for which to enable gzipping. // defaultExtensions is the list of default extensions for which to enable gzipping.
var defaultExtensions = []string{"", ".txt", ".html", ".css", ".json", ".js", ".md", ".xml"} var defaultExtensions = []string{"", ".txt", ".htm", ".html", ".css", ".php", ".js", ".json", ".md", ".xml"}
// DefaultExtFilter creates an ExtFilter with default extensions. // DefaultExtFilter creates an ExtFilter with default extensions.
func DefaultExtFilter() ExtFilter { func DefaultExtFilter() ExtFilter {
...@@ -59,39 +58,6 @@ func (p PathFilter) ShouldCompress(r *http.Request) bool { ...@@ -59,39 +58,6 @@ func (p PathFilter) ShouldCompress(r *http.Request) bool {
}) })
} }
// MIMEFilter is Filter for request content types.
type MIMEFilter struct {
// Types is the MIME types to accept.
Types Set
}
// defaultMIMETypes is the list of default MIME types to use.
var defaultMIMETypes = []string{
"text/plain", "text/html", "text/css", "application/json", "application/javascript",
"text/x-markdown", "text/xml", "application/xml",
}
// DefaultMIMEFilter creates a MIMEFilter with default types.
func DefaultMIMEFilter() MIMEFilter {
m := MIMEFilter{Types: make(Set)}
for _, mime := range defaultMIMETypes {
m.Types.Add(mime)
}
return m
}
// ShouldCompress checks if the content type of the request
// matches any of the registered ones. It returns true if
// found and false otherwise.
func (m MIMEFilter) ShouldCompress(r *http.Request) bool {
return m.Types.Contains(r.Header.Get("Accept"))
}
func ValidMIME(mime string) bool {
s := strings.Split(mime, "/")
return len(s) == 2 && strings.TrimSpace(s[0]) != "" && strings.TrimSpace(s[1]) != ""
}
// Set stores distinct strings. // Set stores distinct strings.
type Set map[string]struct{} type Set map[string]struct{}
......
...@@ -100,32 +100,6 @@ func TestPathFilter(t *testing.T) { ...@@ -100,32 +100,6 @@ func TestPathFilter(t *testing.T) {
} }
} }
func TestMIMEFilter(t *testing.T) {
var filter Filter = DefaultMIMEFilter()
_ = filter.(MIMEFilter)
var mimes = []string{
"text/html", "text/css", "application/json",
}
for i, m := range mimes {
r := urlRequest("file" + m)
r.Header.Set("Accept", m)
if !filter.ShouldCompress(r) {
t.Errorf("Test %v: Should be valid filter", i)
}
}
mimes = []string{
"image/jpeg", "image/png",
}
filter = DefaultMIMEFilter()
for i, m := range mimes {
r := urlRequest("file" + m)
r.Header.Set("Content-Type", m)
if filter.ShouldCompress(r) {
t.Errorf("Test %v: Should not be valid filter", i)
}
}
}
func urlRequest(url string) *http.Request { func urlRequest(url string) *http.Request {
r, _ := http.NewRequest("GET", url, nil) r, _ := http.NewRequest("GET", url, nil)
return r return r
......
...@@ -76,46 +76,6 @@ func TestGzipHandler(t *testing.T) { ...@@ -76,46 +76,6 @@ func TestGzipHandler(t *testing.T) {
t.Error(err) t.Error(err)
} }
} }
gz.Configs[0].Filters[1] = DefaultMIMEFilter()
w = httptest.NewRecorder()
gz.Next = nextFunc(true)
var mimes = []string{
"text/html", "text/css", "application/json",
}
for _, m := range mimes {
url := "/file"
r, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Error(err)
}
r.Header.Set("Accept", m)
r.Header.Set("Accept-Encoding", "gzip")
_, err = gz.ServeHTTP(w, r)
if err != nil {
t.Error(err)
}
}
w = httptest.NewRecorder()
gz.Next = nextFunc(false)
mimes = []string{
"image/jpeg", "image/png",
}
for _, m := range mimes {
url := "/file"
r, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Error(err)
}
r.Header.Set("Content-Type", m)
r.Header.Set("Accept-Encoding", "gzip")
_, err = gz.ServeHTTP(w, r)
if err != nil {
t.Error(err)
}
}
} }
func nextFunc(shouldGzip bool) middleware.Handler { func nextFunc(shouldGzip bool) middleware.Handler {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment