mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-02 03:25:31 +08:00
Merge pull request #682 from weingart/mime_fix
mime: Use a map, and error on duplicate extensions.
This commit is contained in:
commit
f14cdcc436
|
@ -20,8 +20,8 @@ func Mime(c *Controller) (middleware.Middleware, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mimeParse(c *Controller) ([]mime.Config, error) {
|
func mimeParse(c *Controller) (mime.Config, error) {
|
||||||
var configs []mime.Config
|
configs := mime.Config{}
|
||||||
|
|
||||||
for c.Next() {
|
for c.Next() {
|
||||||
// At least one extension is required
|
// At least one extension is required
|
||||||
|
@ -29,22 +29,22 @@ func mimeParse(c *Controller) ([]mime.Config, error) {
|
||||||
args := c.RemainingArgs()
|
args := c.RemainingArgs()
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
case 2:
|
case 2:
|
||||||
if err := validateExt(args[0]); err != nil {
|
if err := validateExt(configs, args[0]); err != nil {
|
||||||
return configs, err
|
return configs, err
|
||||||
}
|
}
|
||||||
configs = append(configs, mime.Config{Ext: args[0], ContentType: args[1]})
|
configs[args[0]] = args[1]
|
||||||
case 1:
|
case 1:
|
||||||
return configs, c.ArgErr()
|
return configs, c.ArgErr()
|
||||||
case 0:
|
case 0:
|
||||||
for c.NextBlock() {
|
for c.NextBlock() {
|
||||||
ext := c.Val()
|
ext := c.Val()
|
||||||
if err := validateExt(ext); err != nil {
|
if err := validateExt(configs, ext); err != nil {
|
||||||
return configs, err
|
return configs, err
|
||||||
}
|
}
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return configs, c.ArgErr()
|
return configs, c.ArgErr()
|
||||||
}
|
}
|
||||||
configs = append(configs, mime.Config{Ext: ext, ContentType: c.Val()})
|
configs[ext] = c.Val()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,9 +54,12 @@ func mimeParse(c *Controller) ([]mime.Config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateExt checks for valid file name extension.
|
// validateExt checks for valid file name extension.
|
||||||
func validateExt(ext string) error {
|
func validateExt(configs mime.Config, ext string) error {
|
||||||
if !strings.HasPrefix(ext, ".") {
|
if !strings.HasPrefix(ext, ".") {
|
||||||
return fmt.Errorf(`mime: invalid extension "%v" (must start with dot)`, 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,11 @@ func TestMime(t *testing.T) {
|
||||||
.html text/html
|
.html text/html
|
||||||
.txt text/plain
|
.txt text/plain
|
||||||
} `, false},
|
} `, false},
|
||||||
|
{`mime {
|
||||||
|
.foo text/foo
|
||||||
|
.bar text/bar
|
||||||
|
.foo text/foobar
|
||||||
|
} `, true},
|
||||||
{`mime { .html text/html } `, false},
|
{`mime { .html text/html } `, false},
|
||||||
{`mime { .html
|
{`mime { .html
|
||||||
} `, true},
|
} `, true},
|
||||||
|
|
|
@ -2,40 +2,31 @@ package mime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path"
|
||||||
|
|
||||||
"github.com/mholt/caddy/middleware"
|
"github.com/mholt/caddy/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config represent a mime config.
|
// Config represent a mime config. Map from extension to mime-type.
|
||||||
type Config struct {
|
// Note, this should be safe with concurrent read access, as this is
|
||||||
Ext string
|
// not modified concurrently.
|
||||||
ContentType string
|
type Config map[string]string
|
||||||
}
|
|
||||||
|
|
||||||
// SetContent sets the Content-Type header of the request if the request path
|
|
||||||
// is supported.
|
|
||||||
func (c Config) SetContent(w http.ResponseWriter, r *http.Request) bool {
|
|
||||||
ext := filepath.Ext(r.URL.Path)
|
|
||||||
if ext != c.Ext {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", c.ContentType)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mime sets Content-Type header of requests based on configurations.
|
// Mime sets Content-Type header of requests based on configurations.
|
||||||
type Mime struct {
|
type Mime struct {
|
||||||
Next middleware.Handler
|
Next middleware.Handler
|
||||||
Configs []Config
|
Configs Config
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP implements the middleware.Handler interface.
|
// ServeHTTP implements the middleware.Handler interface.
|
||||||
func (e Mime) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
func (e Mime) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
for _, c := range e.Configs {
|
|
||||||
if ok := c.SetContent(w, r); ok {
|
// Get a clean /-path, grab the extension
|
||||||
break
|
ext := path.Ext(path.Clean(r.URL.Path))
|
||||||
}
|
|
||||||
|
if contentType, ok := e.Configs[ext]; ok {
|
||||||
|
w.Header().Set("Content-Type", contentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.Next.ServeHTTP(w, r)
|
return e.Next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,18 +11,13 @@ import (
|
||||||
|
|
||||||
func TestMimeHandler(t *testing.T) {
|
func TestMimeHandler(t *testing.T) {
|
||||||
|
|
||||||
mimes := map[string]string{
|
mimes := Config{
|
||||||
".html": "text/html",
|
".html": "text/html",
|
||||||
".txt": "text/plain",
|
".txt": "text/plain",
|
||||||
".swf": "application/x-shockwave-flash",
|
".swf": "application/x-shockwave-flash",
|
||||||
}
|
}
|
||||||
|
|
||||||
var configs []Config
|
m := Mime{Configs: mimes}
|
||||||
for ext, contentType := range mimes {
|
|
||||||
configs = append(configs, Config{Ext: ext, ContentType: contentType})
|
|
||||||
}
|
|
||||||
|
|
||||||
m := Mime{Configs: configs}
|
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
exts := []string{
|
exts := []string{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user