FromOutgoingContext returns the outgoing metadata in ctx if it exists. All keys in the returned MD are lowercase.
(ctx context.Context)
| 258 | // |
| 259 | // All keys in the returned MD are lowercase. |
| 260 | func FromOutgoingContext(ctx context.Context) (MD, bool) { |
| 261 | raw, ok := ctx.Value(mdOutgoingKey{}).(rawMD) |
| 262 | if !ok { |
| 263 | return nil, false |
| 264 | } |
| 265 | |
| 266 | mdSize := len(raw.md) |
| 267 | for i := range raw.added { |
| 268 | mdSize += len(raw.added[i]) / 2 |
| 269 | } |
| 270 | |
| 271 | out := make(MD, mdSize) |
| 272 | for k, v := range raw.md { |
| 273 | // We need to manually convert all keys to lower case, because MD is a |
| 274 | // map, and there's no guarantee that the MD attached to the context is |
| 275 | // created using our helper functions. |
| 276 | key := strings.ToLower(k) |
| 277 | out[key] = copyOf(v) |
| 278 | } |
| 279 | for _, added := range raw.added { |
| 280 | if len(added)%2 == 1 { |
| 281 | panic(fmt.Sprintf("metadata: FromOutgoingContext got an odd number of input pairs for metadata: %d", len(added))) |
| 282 | } |
| 283 | |
| 284 | for i := 0; i < len(added); i += 2 { |
| 285 | key := strings.ToLower(added[i]) |
| 286 | out[key] = append(out[key], added[i+1]) |
| 287 | } |
| 288 | } |
| 289 | return out, ok |
| 290 | } |
| 291 | |
| 292 | type rawMD struct { |
| 293 | md MD |