Metadata returns a middleware that sets gRPC metadata from the request headers.
(keys ...string)
| 24 | |
| 25 | // Metadata returns a middleware that sets gRPC metadata from the request headers. |
| 26 | func Metadata(keys ...string) MiddlewareFunc { |
| 27 | return func(next http.Handler) http.Handler { |
| 28 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 29 | ctx := r.Context() |
| 30 | header := textproto.MIMEHeader(r.Header) |
| 31 | md := make(metadata.MD) |
| 32 | for _, key := range keys { |
| 33 | if value := header.Values(key); value != nil { |
| 34 | md[strings.ToLower(key)] = value |
| 35 | } |
| 36 | } |
| 37 | if len(md) > 0 { |
| 38 | if existingMD, ok := metadata.FromIncomingContext(ctx); ok { |
| 39 | md = metadata.Join(existingMD, md) |
| 40 | } |
| 41 | ctx = metadata.NewIncomingContext(ctx, md) |
| 42 | } |
| 43 | next.ServeHTTP(w, r.WithContext(ctx)) |
| 44 | }) |
| 45 | } |
| 46 | } |