Add ability to set arbitrary headers

This is useful if you want to add headers for things like HTTP Strict
Transport Security or HTTP Public Key Pinning.
This commit is contained in:
mutantmonkey 2016-06-03 22:49:01 -07:00
parent 1f3bc4bfea
commit 39bb999db6
3 changed files with 61 additions and 0 deletions

27
headers.go Normal file
View file

@ -0,0 +1,27 @@
package main
import (
"net/http"
"strings"
)
type addheaders struct {
h http.Handler
headers []string
}
func (a addheaders) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, header := range a.headers {
headerSplit := strings.SplitN(header, ": ", 2)
w.Header().Add(headerSplit[0], headerSplit[1])
}
a.h.ServeHTTP(w, r)
}
func AddHeaders(headers []string) func(http.Handler) http.Handler {
fn := func(h http.Handler) http.Handler {
return addheaders{h, headers}
}
return fn
}