blob: 246b1aa989af8b050282f8058e8854f27464f2fa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package middleware
import (
"errors"
"net/http"
"strings"
)
var errInvalidHeaderParameter = errors.New("invalid syntax specified as header parameter")
// AddCustomHeaders adds a map of Headers to a Response
func AddCustomHeaders(w http.ResponseWriter, headers http.Header) {
for k, v := range headers {
for _, value := range v {
w.Header().Add(k, value)
}
}
}
// ParseHeaderString parses a string of key values into a map
func ParseHeaderString(customHeaders []string) (http.Header, error) {
headers := http.Header{}
for _, keyValueString := range customHeaders {
keyValue := strings.SplitN(keyValueString, ":", -1)
if len(keyValue) != 2 {
return nil, errInvalidHeaderParameter
}
key := strings.TrimSpace(keyValue[0])
value := strings.TrimSpace(keyValue[1])
headers[key] = append(headers[key], value)
}
return headers, nil
}
|