2015-05-04 12:04:17 -05:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
2015-06-06 18:58:37 -05:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2015-05-04 12:04:17 -05:00
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
"github.com/mholt/caddy/middleware/gzip"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Gzip configures a new gzip middleware instance.
|
|
|
|
func Gzip(c *Controller) (middleware.Middleware, error) {
|
2015-06-06 18:58:37 -05:00
|
|
|
configs, err := gzipParse(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-04 12:04:17 -05:00
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
2015-06-06 18:58:37 -05:00
|
|
|
return gzip.Gzip{Next: next, Configs: configs}
|
2015-05-04 12:04:17 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2015-06-06 18:58:37 -05:00
|
|
|
|
|
|
|
func gzipParse(c *Controller) ([]gzip.Config, error) {
|
|
|
|
var configs []gzip.Config
|
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
config := gzip.Config{}
|
|
|
|
|
|
|
|
pathFilter := gzip.PathFilter{make(gzip.Set)}
|
2015-06-10 15:59:08 -05:00
|
|
|
extFilter := gzip.ExtFilter{make(gzip.Set)}
|
2015-06-06 18:58:37 -05:00
|
|
|
|
2015-07-01 19:56:30 -05:00
|
|
|
// No extra args expected
|
2015-06-06 18:58:37 -05:00
|
|
|
if len(c.RemainingArgs()) > 0 {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "ext":
|
|
|
|
exts := c.RemainingArgs()
|
|
|
|
if len(exts) == 0 {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
for _, e := range exts {
|
2015-07-06 00:36:48 -05:00
|
|
|
if !strings.HasPrefix(e, ".") && e != gzip.ExtWildCard {
|
2015-07-01 19:56:30 -05:00
|
|
|
return configs, fmt.Errorf(`gzip: invalid extension "%v" (must start with dot)`, e)
|
2015-06-06 18:58:37 -05:00
|
|
|
}
|
|
|
|
extFilter.Exts.Add(e)
|
|
|
|
}
|
|
|
|
case "not":
|
|
|
|
paths := c.RemainingArgs()
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
for _, p := range paths {
|
2015-07-01 19:56:30 -05:00
|
|
|
if p == "/" {
|
|
|
|
return configs, fmt.Errorf(`gzip: cannot exclude path "/" - remove directive entirely instead`)
|
|
|
|
}
|
2015-06-06 18:58:37 -05:00
|
|
|
if !strings.HasPrefix(p, "/") {
|
2015-07-01 19:56:30 -05:00
|
|
|
return configs, fmt.Errorf(`gzip: invalid path "%v" (must start with /)`, p)
|
2015-06-06 18:58:37 -05:00
|
|
|
}
|
|
|
|
pathFilter.IgnoredPaths.Add(p)
|
|
|
|
}
|
|
|
|
case "level":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
level, _ := strconv.Atoi(c.Val())
|
|
|
|
config.Level = level
|
|
|
|
default:
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:59:08 -05:00
|
|
|
config.Filters = []gzip.Filter{}
|
|
|
|
|
2015-07-01 19:56:30 -05:00
|
|
|
// If ignored paths are specified, put in front to filter with path first
|
2015-06-10 15:59:08 -05:00
|
|
|
if len(pathFilter.IgnoredPaths) > 0 {
|
|
|
|
config.Filters = []gzip.Filter{pathFilter}
|
|
|
|
}
|
|
|
|
|
2015-07-03 19:13:30 -05:00
|
|
|
// Then, if extensions are specified, use those to filter.
|
2015-07-01 19:56:30 -05:00
|
|
|
// Otherwise, use default extensions filter.
|
|
|
|
if len(extFilter.Exts) > 0 {
|
2015-06-10 15:59:08 -05:00
|
|
|
config.Filters = append(config.Filters, extFilter)
|
|
|
|
} else {
|
2015-07-01 19:56:30 -05:00
|
|
|
config.Filters = append(config.Filters, gzip.DefaultExtFilter())
|
2015-06-10 15:59:08 -05:00
|
|
|
}
|
2015-06-06 18:58:37 -05:00
|
|
|
|
|
|
|
configs = append(configs, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
return configs, nil
|
|
|
|
}
|