mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-24 16:41:18 +08:00
36 lines
933 B
Go
36 lines
933 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/mholt/caddy/config"
|
|
)
|
|
|
|
// Headers is middleware that adds headers to the responses
|
|
// for requests matching a certain path.
|
|
func Headers(headers []config.Headers) Middleware {
|
|
return func(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
for _, rule := range headers {
|
|
if pathsMatch(r.URL.Path, rule.Url) {
|
|
for _, header := range rule.Headers {
|
|
w.Header().Set(header.Name, header.Value)
|
|
}
|
|
}
|
|
}
|
|
next(w, r)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Returns whether or not p1 and p2 are matching
|
|
// paths. This can be defined a number of ways
|
|
// and it is not for sure yet how to match URL/path
|
|
// strings. It may be a prefix match or a full
|
|
// string match, it may strip trailing slashes.
|
|
// Until the software hits 1.0, this will be in flux.
|
|
func pathsMatch(p1, p2 string) bool {
|
|
return strings.HasPrefix(p1, p2)
|
|
}
|