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) { ...@@ -31,7 +31,7 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
mimeFilter := gzip.MIMEFilter{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
if len(c.RemainingArgs()) > 0 { if len(c.RemainingArgs()) > 0 {
return configs, c.ArgErr() return configs, c.ArgErr()
} }
...@@ -45,7 +45,7 @@ func gzipParse(c *Controller) ([]gzip.Config, error) { ...@@ -45,7 +45,7 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
} }
for _, m := range mimes { for _, m := range mimes {
if !gzip.ValidMIME(m) { 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) mimeFilter.Types.Add(m)
} }
...@@ -56,7 +56,7 @@ func gzipParse(c *Controller) ([]gzip.Config, error) { ...@@ -56,7 +56,7 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
} }
for _, e := range exts { for _, e := range exts {
if !strings.HasPrefix(e, ".") { 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) extFilter.Exts.Add(e)
} }
...@@ -66,14 +66,13 @@ func gzipParse(c *Controller) ([]gzip.Config, error) { ...@@ -66,14 +66,13 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
return configs, c.ArgErr() return configs, c.ArgErr()
} }
for _, p := range paths { for _, p := range paths {
if p == "/" {
return configs, fmt.Errorf(`gzip: cannot exclude path "/" - remove directive entirely instead`)
}
if !strings.HasPrefix(p, "/") { 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) 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": case "level":
if !c.NextArg() { if !c.NextArg() {
...@@ -88,22 +87,20 @@ func gzipParse(c *Controller) ([]gzip.Config, error) { ...@@ -88,22 +87,20 @@ func gzipParse(c *Controller) ([]gzip.Config, error) {
config.Filters = []gzip.Filter{} 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 { if len(pathFilter.IgnoredPaths) > 0 {
config.Filters = []gzip.Filter{pathFilter} config.Filters = []gzip.Filter{pathFilter}
} }
// if mime types are specified, use it and ignore extensions // If extensions specified, use it over MIME types (if any).
if len(mimeFilter.Types) > 0 { // Otherwise, if specified, use MIME types.
config.Filters = append(config.Filters, mimeFilter) // Otherwise, use default extensions filter.
if len(extFilter.Exts) > 0 {
// if extensions are specified, use it
} else if len(extFilter.Exts) > 0 {
config.Filters = append(config.Filters, extFilter) config.Filters = append(config.Filters, extFilter)
} else if len(mimeFilter.Types) > 0 {
// neither is specified, use default mime types config.Filters = append(config.Filters, mimeFilter)
} else { } else {
config.Filters = append(config.Filters, gzip.DefaultMIMEFilter()) config.Filters = append(config.Filters, gzip.DefaultExtFilter())
} }
configs = append(configs, config) configs = append(configs, config)
......
...@@ -15,6 +15,18 @@ type Filter interface { ...@@ -15,6 +15,18 @@ type Filter interface {
ShouldCompress(*http.Request) bool 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. // ExtFilter is Filter for file name extensions.
type ExtFilter struct { type ExtFilter struct {
// Exts is the file name extensions to accept // Exts is the file name extensions to accept
...@@ -72,7 +84,7 @@ func DefaultMIMEFilter() MIMEFilter { ...@@ -72,7 +84,7 @@ func DefaultMIMEFilter() MIMEFilter {
// matches any of the registered ones. It returns true if // matches any of the registered ones. It returns true if
// found and false otherwise. // found and false otherwise.
func (m MIMEFilter) ShouldCompress(r *http.Request) bool { 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 { func ValidMIME(mime string) bool {
......
...@@ -36,8 +36,7 @@ func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { ...@@ -36,8 +36,7 @@ func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
outer: outer:
for _, c := range g.Configs { for _, c := range g.Configs {
// Check filters to determine if gzipping is permitted for this // Check filters to determine if gzipping is permitted for this request
// request
for _, filter := range c.Filters { for _, filter := range c.Filters {
if !filter.ShouldCompress(r) { if !filter.ShouldCompress(r) {
continue outer 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