Files
upstream-mirror b352a9a30d
Some checks failed
test / Lint (push) Failing after 3s
test / Test with Go 1.26.8 (push) Failing after 2s
snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:17:21 +05:00

30 lines
762 B
Go

package middleware
import (
"context"
"net/http"
)
type contextHeadersKey struct{}
// GetHeadersFromContext returns http.Header from context.
func GetHeadersFromContext(ctx context.Context) (http.Header, bool) {
if val := ctx.Value(contextHeadersKey{}); val != nil {
values, ok := val.(http.Header)
return values, ok
}
return nil, false
}
func SetHeadersToContext(ctx context.Context, h http.Header) context.Context {
return context.WithValue(ctx, contextHeadersKey{}, h)
}
// HeadersToContext puts HTTP headers to request context.
func HeadersToContext(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(SetHeadersToContext(r.Context(), r.Header))
h.ServeHTTP(w, r)
})
}