Commit abf22909 authored by Matthew Holt's avatar Matthew Holt

gzip: Make it gzip again

parent a86e16dd
......@@ -31,7 +31,7 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
mimeFilter := gzip.MIMEFilter{make(gzip.Set)}
extFilter := gzip.ExtFilter{make(gzip.Set)}
// no extra args expected
// No extra args expected
if len(c.RemainingArgs()) > 0 {
return configs, c.ArgErr()
}
......@@ -45,7 +45,7 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
}
for _, m := range mimes {
if !gzip.ValidMIME(m) {
return configs, fmt.Errorf("Invalid MIME %v.", m)
return configs, fmt.Errorf("gzip: invalid MIME %v", m)
}
mimeFilter.Types.Add(m)
}
......@@ -56,7 +56,7 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
}
for _, e := range exts {
if !strings.HasPrefix(e, ".") {
return configs, fmt.Errorf(`Invalid extension %v. Should start with "."`, e)
return configs, fmt.Errorf(`gzip: invalid extension "%v" (must start with dot)`, e)
}
extFilter.Exts.Add(e)
}
......@@ -66,14 +66,13 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
return configs, c.ArgErr()
}
for _, p := range paths {
if p == "/" {
return configs, fmt.Errorf(`gzip: cannot exclude path "/" - remove directive entirely instead`)
}
if !strings.HasPrefix(p, "/") {
return configs, fmt.Errorf(`Invalid path %v. Should start with "/"`, p)
return configs, fmt.Errorf(`gzip: invalid path "%v" (must start with /)`, p)
}
pathFilter.IgnoredPaths.Add(p)
// Warn user if / is used
if p == "/" {
fmt.Println("Warning: Paths ignored by gzip includes wildcard(/). No request will be gzipped.\nRemoving gzip directive from Caddyfile is preferred if this is intended.")
}
}
case "level":
if !c.NextArg() {
......@@ -88,22 +87,20 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
config.Filters = []gzip.Filter{}
// if ignored paths are specified, put in front to filter with path first
// If ignored paths are specified, put in front to filter with path first
if len(pathFilter.IgnoredPaths) > 0 {
config.Filters = []gzip.Filter{pathFilter}
}
// if mime types are specified, use it and ignore extensions
if len(mimeFilter.Types) > 0 {
config.Filters = append(config.Filters, mimeFilter)
// if extensions are specified, use it
} else if len(extFilter.Exts) > 0 {
// If extensions specified, use it over MIME types (if any).
// Otherwise, if specified, use MIME types.
// Otherwise, use default extensions filter.
if len(extFilter.Exts) > 0 {
config.Filters = append(config.Filters, extFilter)
// neither is specified, use default mime types
} else if len(mimeFilter.Types) > 0 {
config.Filters = append(config.Filters, mimeFilter)
} else {
config.Filters = append(config.Filters, gzip.DefaultMIMEFilter())
config.Filters = append(config.Filters, gzip.DefaultExtFilter())
}
configs = append(configs, config)
......
......@@ -15,6 +15,18 @@ type Filter interface {
ShouldCompress(*http.Request) bool
}
// defaultExtensions is the list of default extensions for which to enable gzipping.
var defaultExtensions = []string{"", ".txt", ".html", ".css", ".json", ".js", ".md", ".xml"}
// DefaultExtFilter creates an ExtFilter with default extensions.
func DefaultExtFilter() ExtFilter {
m := ExtFilter{Exts: make(Set)}
for _, extension := range defaultExtensions {
m.Exts.Add(extension)
}
return m
}
// ExtFilter is Filter for file name extensions.
type ExtFilter struct {
// Exts is the file name extensions to accept
......@@ -72,7 +84,7 @@ func DefaultMIMEFilter() MIMEFilter {
// 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("Content-Type"))
return m.Types.Contains(r.Header.Get("Accept"))
}
func ValidMIME(mime string) bool {
......
......@@ -36,8 +36,7 @@ func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
outer:
for _, c := range g.Configs {
// Check filters to determine if gzipping is permitted for this
// request
// Check filters to determine if gzipping is permitted for this request
for _, filter := range c.Filters {
if !filter.ShouldCompress(r) {
continue outer
......
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