mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-29 12:16:16 +08:00
a798e0c951
- Server types no longer need to store their own contexts; they are stored on the caddy.Instance, which means each context will be properly GC'ed when the instance is stopped. Server types should use type assertions to convert from caddy.Context to their concrete context type when they need to use it. - Pass the entire context into httpserver.GetConfig instead of only the Key field. - caddy.NewTestController now requires a server type string so it can create a controller with the proper concrete context associated with that server type. Tests still need more attention so that we can test the proper creation of startup functions, etc.
75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
package mime
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/mholt/caddy"
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterPlugin("mime", caddy.Plugin{
|
|
ServerType: "http",
|
|
Action: setup,
|
|
})
|
|
}
|
|
|
|
// setup configures a new mime middleware instance.
|
|
func setup(c *caddy.Controller) error {
|
|
configs, err := mimeParse(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
|
return Mime{Next: next, Configs: configs}
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
func mimeParse(c *caddy.Controller) (Config, error) {
|
|
configs := Config{}
|
|
|
|
for c.Next() {
|
|
// At least one extension is required
|
|
|
|
args := c.RemainingArgs()
|
|
switch len(args) {
|
|
case 2:
|
|
if err := validateExt(configs, args[0]); err != nil {
|
|
return configs, err
|
|
}
|
|
configs[args[0]] = args[1]
|
|
case 1:
|
|
return configs, c.ArgErr()
|
|
case 0:
|
|
for c.NextBlock() {
|
|
ext := c.Val()
|
|
if err := validateExt(configs, ext); err != nil {
|
|
return configs, err
|
|
}
|
|
if !c.NextArg() {
|
|
return configs, c.ArgErr()
|
|
}
|
|
configs[ext] = c.Val()
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return configs, nil
|
|
}
|
|
|
|
// validateExt checks for valid file name extension.
|
|
func validateExt(configs Config, ext string) error {
|
|
if !strings.HasPrefix(ext, ".") {
|
|
return fmt.Errorf(`mime: invalid extension "%v" (must start with dot)`, ext)
|
|
}
|
|
if _, ok := configs[ext]; ok {
|
|
return fmt.Errorf(`mime: duplicate extension "%v" found`, ext)
|
|
}
|
|
return nil
|
|
}
|