2016-06-05 22:51:56 -05:00
|
|
|
// Package header provides middleware that appends headers to
|
|
|
|
// requests based on a set of configuration rules that define
|
|
|
|
// which routes receive which headers.
|
|
|
|
package header
|
|
|
|
|
|
|
|
import (
|
2016-10-10 02:33:36 -05:00
|
|
|
"bufio"
|
|
|
|
"net"
|
2016-06-05 22:51:56 -05:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2017-02-17 11:25:22 -05:00
|
|
|
"errors"
|
2016-06-05 22:51:56 -05:00
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Headers is middleware that adds headers to the responses
|
|
|
|
// for requests matching a certain path.
|
|
|
|
type Headers struct {
|
|
|
|
Next httpserver.Handler
|
|
|
|
Rules []Rule
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implements the httpserver.Handler interface and serves requests,
|
|
|
|
// setting headers on the response according to the configured rules.
|
|
|
|
func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
replacer := httpserver.NewReplacer(r, nil, "")
|
2017-02-17 11:25:22 -05:00
|
|
|
rww := &responseWriterWrapper{ResponseWriter: w}
|
2016-06-05 22:51:56 -05:00
|
|
|
for _, rule := range h.Rules {
|
|
|
|
if httpserver.Path(r.URL.Path).Matches(rule.Path) {
|
2016-11-03 13:24:26 -05:00
|
|
|
for name := range rule.Headers {
|
|
|
|
|
2016-07-20 16:23:55 -05:00
|
|
|
// One can either delete a header, add multiple values to a header, or simply
|
|
|
|
// set a header.
|
2016-11-03 13:24:26 -05:00
|
|
|
|
|
|
|
if strings.HasPrefix(name, "-") {
|
|
|
|
rww.delHeader(strings.TrimLeft(name, "-"))
|
|
|
|
} else if strings.HasPrefix(name, "+") {
|
|
|
|
for _, value := range rule.Headers[name] {
|
|
|
|
rww.Header().Add(strings.TrimLeft(name, "+"), replacer.Replace(value))
|
|
|
|
}
|
2016-06-05 22:51:56 -05:00
|
|
|
} else {
|
2016-11-03 13:24:26 -05:00
|
|
|
for _, value := range rule.Headers[name] {
|
|
|
|
rww.Header().Set(name, replacer.Replace(value))
|
|
|
|
}
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 02:35:13 -05:00
|
|
|
return h.Next.ServeHTTP(rww, r)
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Rule groups a slice of HTTP headers by a URL pattern.
|
|
|
|
Rule struct {
|
|
|
|
Path string
|
2016-11-03 13:24:26 -05:00
|
|
|
Headers http.Header
|
2016-06-05 22:51:56 -05:00
|
|
|
}
|
|
|
|
)
|
2016-09-27 02:35:13 -05:00
|
|
|
|
|
|
|
// headerOperation represents an operation on the header
|
|
|
|
type headerOperation func(http.Header)
|
|
|
|
|
|
|
|
// responseWriterWrapper wraps the real ResponseWriter.
|
|
|
|
// It defers header operations until writeHeader
|
|
|
|
type responseWriterWrapper struct {
|
2017-02-17 11:25:22 -05:00
|
|
|
http.ResponseWriter
|
2016-09-27 02:35:13 -05:00
|
|
|
ops []headerOperation
|
|
|
|
wroteHeader bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rww *responseWriterWrapper) Header() http.Header {
|
2017-02-17 11:25:22 -05:00
|
|
|
return rww.ResponseWriter.Header()
|
2016-09-27 02:35:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rww *responseWriterWrapper) Write(d []byte) (int, error) {
|
|
|
|
if !rww.wroteHeader {
|
|
|
|
rww.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2017-02-17 11:25:22 -05:00
|
|
|
return rww.ResponseWriter.Write(d)
|
2016-09-27 02:35:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rww *responseWriterWrapper) WriteHeader(status int) {
|
|
|
|
if rww.wroteHeader {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rww.wroteHeader = true
|
|
|
|
// capture the original headers
|
|
|
|
h := rww.Header()
|
|
|
|
|
|
|
|
// perform our revisions
|
|
|
|
for _, op := range rww.ops {
|
|
|
|
op(h)
|
|
|
|
}
|
|
|
|
|
2017-02-17 11:25:22 -05:00
|
|
|
rww.ResponseWriter.WriteHeader(status)
|
2016-09-27 02:35:13 -05:00
|
|
|
}
|
|
|
|
|
2016-11-01 23:08:02 -05:00
|
|
|
// delHeader deletes the existing header according to the key
|
|
|
|
// Also it will delete that header added later.
|
2016-09-27 02:35:13 -05:00
|
|
|
func (rww *responseWriterWrapper) delHeader(key string) {
|
2016-11-01 23:08:02 -05:00
|
|
|
// remove the existing one if any
|
|
|
|
rww.Header().Del(key)
|
2016-09-27 02:35:13 -05:00
|
|
|
|
2016-11-01 23:08:02 -05:00
|
|
|
// register a future deletion
|
2016-09-27 02:35:13 -05:00
|
|
|
rww.ops = append(rww.ops, func(h http.Header) {
|
2016-11-01 23:08:02 -05:00
|
|
|
h.Del(key)
|
2016-09-27 02:35:13 -05:00
|
|
|
})
|
|
|
|
}
|
2016-10-10 02:33:36 -05:00
|
|
|
|
|
|
|
// Hijack implements http.Hijacker. It simply wraps the underlying
|
|
|
|
// ResponseWriter's Hijack method if there is one, or returns an error.
|
|
|
|
func (rww *responseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
2017-02-17 11:25:22 -05:00
|
|
|
if hj, ok := rww.ResponseWriter.(http.Hijacker); ok {
|
2016-10-10 02:33:36 -05:00
|
|
|
return hj.Hijack()
|
|
|
|
}
|
2017-02-17 11:25:22 -05:00
|
|
|
return nil, nil, httpserver.NonHijackerError{Underlying: rww.ResponseWriter}
|
2016-10-10 02:33:36 -05:00
|
|
|
}
|
2016-11-02 09:13:05 -05:00
|
|
|
|
|
|
|
// Flush implements http.Flusher. It simply wraps the underlying
|
|
|
|
// ResponseWriter's Flush method if there is one, or panics.
|
|
|
|
func (rww *responseWriterWrapper) Flush() {
|
2017-02-17 11:25:22 -05:00
|
|
|
if f, ok := rww.ResponseWriter.(http.Flusher); ok {
|
2016-11-02 09:13:05 -05:00
|
|
|
f.Flush()
|
|
|
|
} else {
|
2017-02-17 11:25:22 -05:00
|
|
|
panic(httpserver.NonFlusherError{Underlying: rww.ResponseWriter}) // should be recovered at the beginning of middleware stack
|
2016-11-02 09:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseNotify implements http.CloseNotifier.
|
|
|
|
// It just inherits the underlying ResponseWriter's CloseNotify method.
|
|
|
|
// It panics if the underlying ResponseWriter is not a CloseNotifier.
|
|
|
|
func (rww *responseWriterWrapper) CloseNotify() <-chan bool {
|
2017-02-17 11:25:22 -05:00
|
|
|
if cn, ok := rww.ResponseWriter.(http.CloseNotifier); ok {
|
2016-11-02 09:13:05 -05:00
|
|
|
return cn.CloseNotify()
|
|
|
|
}
|
2017-02-17 11:25:22 -05:00
|
|
|
panic(httpserver.NonCloseNotifierError{Underlying: rww.ResponseWriter})
|
2016-11-02 09:13:05 -05:00
|
|
|
}
|
2017-02-17 11:25:22 -05:00
|
|
|
|
|
|
|
func (rww *responseWriterWrapper) Push(target string, opts *http.PushOptions) error {
|
|
|
|
if pusher, hasPusher := rww.ResponseWriter.(http.Pusher); hasPusher {
|
|
|
|
return pusher.Push(target, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("push is unavailable (probably chained http.ResponseWriter does not implement http.Pusher)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface guards
|
|
|
|
var _ http.Pusher = (*responseWriterWrapper)(nil)
|
|
|
|
var _ http.Flusher = (*responseWriterWrapper)(nil)
|
|
|
|
var _ http.CloseNotifier = (*responseWriterWrapper)(nil)
|
|
|
|
var _ http.Hijacker = (*responseWriterWrapper)(nil)
|