github.com/xmidt-org/webpa-common@v1.11.9/xhttp/staticHeaders.go (about) 1 package xhttp 2 3 import ( 4 "net/http" 5 "net/textproto" 6 ) 7 8 // StaticHeaders returns an Alice-style constructor that emits a static set of headers 9 // into every response. If the set of headers is empty, the constructor does no 10 // decoration. 11 func StaticHeaders(extra http.Header) func(http.Handler) http.Handler { 12 if len(extra) > 0 { 13 // preprocess the header keys, so that we do this just once. 14 // this also allows the header to be read in from sources that do not use 15 // the http.Header methods, such as unmarshaled JSON. 16 preprocessed := make(http.Header, len(extra)) 17 for k, v := range extra { 18 preprocessed[textproto.CanonicalMIMEHeaderKey(k)] = v 19 } 20 21 extra = preprocessed 22 return func(next http.Handler) http.Handler { 23 return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { 24 header := response.Header() 25 for k, v := range extra { 26 header[k] = v 27 } 28 29 next.ServeHTTP(response, request) 30 }) 31 } 32 } 33 34 return func(next http.Handler) http.Handler { 35 return next 36 } 37 }