gzip: Remove mimes

This commit is contained in:
Matthew Holt 2015-07-03 18:13:30 -06:00
parent 32ef35b952
commit b5579ca910
5 changed files with 2 additions and 129 deletions

View File

@ -28,7 +28,6 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
config := gzip.Config{}
pathFilter := gzip.PathFilter{make(gzip.Set)}
mimeFilter := gzip.MIMEFilter{make(gzip.Set)}
extFilter := gzip.ExtFilter{make(gzip.Set)}
// No extra args expected
@ -38,17 +37,6 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
for c.NextBlock() {
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":
exts := c.RemainingArgs()
if len(exts) == 0 {
@ -92,13 +80,10 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
config.Filters = []gzip.Filter{pathFilter}
}
// If extensions specified, use it over MIME types (if any).
// Otherwise, if specified, use MIME types.
// Then, if extensions are specified, use those to filter.
// Otherwise, use default extensions filter.
if len(extFilter.Exts) > 0 {
config.Filters = append(config.Filters, extFilter)
} else if len(mimeFilter.Types) > 0 {
config.Filters = append(config.Filters, mimeFilter)
} else {
config.Filters = append(config.Filters, gzip.DefaultExtFilter())
}

View File

@ -59,25 +59,13 @@ func TestGzip(t *testing.T) {
level 3
}
`, 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
ext .html
level 1
mimes text/html text/plain
}
gzip { not /file1
ext .htm
level 3
mimes text/html text/css
}
`, false},
}

View File

@ -3,7 +3,6 @@ package gzip
import (
"net/http"
"path"
"strings"
"github.com/mholt/caddy/middleware"
)
@ -16,7 +15,7 @@ type Filter interface {
}
// 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.
func DefaultExtFilter() ExtFilter {
@ -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.
type Set map[string]struct{}

View File

@ -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 {
r, _ := http.NewRequest("GET", url, nil)
return r

View File

@ -76,46 +76,6 @@ func TestGzipHandler(t *testing.T) {
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 {