snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
29
internal/middleware/headers.go
Normal file
29
internal/middleware/headers.go
Normal file
@@ -0,0 +1,29 @@
|
||||
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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user