Browse Source
Add ability to set arbitrary headers
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.pull/81/head
3 changed files with 61 additions and 0 deletions
-
27headers.go
-
16server.go
-
18server_test.go
@ -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 |
|||
} |
Reference in new issue
xxxxxxxxxx