ValueFromIncomingContext returns the metadata value corresponding to the metadata key from the incoming metadata if it exists. Keys are matched in a case insensitive manner.
(ctx context.Context, key string)
| 214 | // key from the incoming metadata if it exists. Keys are matched in a case insensitive |
| 215 | // manner. |
| 216 | func ValueFromIncomingContext(ctx context.Context, key string) []string { |
| 217 | md, ok := ctx.Value(mdIncomingKey{}).(MD) |
| 218 | if !ok { |
| 219 | return nil |
| 220 | } |
| 221 | |
| 222 | if v, ok := md[key]; ok { |
| 223 | return copyOf(v) |
| 224 | } |
| 225 | for k, v := range md { |
| 226 | // Case insensitive comparison: MD is a map, and there's no guarantee |
| 227 | // that the MD attached to the context is created using our helper |
| 228 | // functions. |
| 229 | if strings.EqualFold(k, key) { |
| 230 | return copyOf(v) |
| 231 | } |
| 232 | } |
| 233 | return nil |
| 234 | } |
| 235 | |
| 236 | func copyOf(v []string) []string { |
| 237 | vals := make([]string, len(v)) |