package xtwirp
import (
"context"
"net/http"
"github.com/twitchtv/twirp"
)
type twirpHeaders struct{}
func withHeaders(ctx context.Context, h http.Header) context.Context {
return context.WithValue(ctx, twirpHeaders{}, h)
}
func GetHeaders(ctx context.Context) http.Header {
if h, ok := twirp.HTTPRequestHeaders(ctx); ok {
return h
}
h, _ := ctx.Value(twirpHeaders{}).(http.Header)
return h
}
func PassHeadersHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = withHeaders(ctx, r.Header)
r = r.WithContext(ctx)
h.ServeHTTP(w, r)
})
}