mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-01 21:24:23 +08:00
b471b7e835
- The mime middleware used filepath where it should arguably use path. - Changed the configuration to use a map instead of scanning an array during every request. The map is static (after configuration), so should be fine for concurrent access. - Catch duplicate extensions within a configuration and error out. - Add tests for new error case.
33 lines
769 B
Go
33 lines
769 B
Go
package mime
|
|
|
|
import (
|
|
"net/http"
|
|
"path"
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
)
|
|
|
|
// Config represent a mime config. Map from extension to mime-type.
|
|
// Note, this should be safe with concurrent read access, as this is
|
|
// not modified concurrently.
|
|
type Config map[string]string
|
|
|
|
// Mime sets Content-Type header of requests based on configurations.
|
|
type Mime struct {
|
|
Next middleware.Handler
|
|
Configs Config
|
|
}
|
|
|
|
// ServeHTTP implements the middleware.Handler interface.
|
|
func (e Mime) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
// Get a clean /-path, grab the extension
|
|
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)
|
|
}
|