metadata.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. *
  3. * Copyright 2014 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package metadata define the structure of the metadata supported by gRPC library.
  19. // Please refer to https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
  20. // for more information about custom-metadata.
  21. package metadata // import "google.golang.org/grpc/metadata"
  22. import (
  23. "fmt"
  24. "strings"
  25. "golang.org/x/net/context"
  26. )
  27. // DecodeKeyValue returns k, v, nil. It is deprecated and should not be used.
  28. func DecodeKeyValue(k, v string) (string, string, error) {
  29. return k, v, nil
  30. }
  31. // MD is a mapping from metadata keys to values. Users should use the following
  32. // two convenience functions New and Pairs to generate MD.
  33. type MD map[string][]string
  34. // New creates an MD from a given key-value map.
  35. //
  36. // Only the following ASCII characters are allowed in keys:
  37. // - digits: 0-9
  38. // - uppercase letters: A-Z (normalized to lower)
  39. // - lowercase letters: a-z
  40. // - special characters: -_.
  41. // Uppercase letters are automatically converted to lowercase.
  42. //
  43. // Keys beginning with "grpc-" are reserved for grpc-internal use only and may
  44. // result in errors if set in metadata.
  45. func New(m map[string]string) MD {
  46. md := MD{}
  47. for k, val := range m {
  48. key := strings.ToLower(k)
  49. md[key] = append(md[key], val)
  50. }
  51. return md
  52. }
  53. // Pairs returns an MD formed by the mapping of key, value ...
  54. // Pairs panics if len(kv) is odd.
  55. //
  56. // Only the following ASCII characters are allowed in keys:
  57. // - digits: 0-9
  58. // - uppercase letters: A-Z (normalized to lower)
  59. // - lowercase letters: a-z
  60. // - special characters: -_.
  61. // Uppercase letters are automatically converted to lowercase.
  62. //
  63. // Keys beginning with "grpc-" are reserved for grpc-internal use only and may
  64. // result in errors if set in metadata.
  65. func Pairs(kv ...string) MD {
  66. if len(kv)%2 == 1 {
  67. panic(fmt.Sprintf("metadata: Pairs got the odd number of input pairs for metadata: %d", len(kv)))
  68. }
  69. md := MD{}
  70. var key string
  71. for i, s := range kv {
  72. if i%2 == 0 {
  73. key = strings.ToLower(s)
  74. continue
  75. }
  76. md[key] = append(md[key], s)
  77. }
  78. return md
  79. }
  80. // Len returns the number of items in md.
  81. func (md MD) Len() int {
  82. return len(md)
  83. }
  84. // Copy returns a copy of md.
  85. func (md MD) Copy() MD {
  86. return Join(md)
  87. }
  88. // Join joins any number of mds into a single MD.
  89. // The order of values for each key is determined by the order in which
  90. // the mds containing those values are presented to Join.
  91. func Join(mds ...MD) MD {
  92. out := MD{}
  93. for _, md := range mds {
  94. for k, v := range md {
  95. out[k] = append(out[k], v...)
  96. }
  97. }
  98. return out
  99. }
  100. type mdIncomingKey struct{}
  101. type mdOutgoingKey struct{}
  102. // NewIncomingContext creates a new context with incoming md attached.
  103. func NewIncomingContext(ctx context.Context, md MD) context.Context {
  104. return context.WithValue(ctx, mdIncomingKey{}, md)
  105. }
  106. // NewOutgoingContext creates a new context with outgoing md attached. If used
  107. // in conjunction with AppendToOutgoingContext, NewOutgoingContext will
  108. // overwrite any previously-appended metadata.
  109. func NewOutgoingContext(ctx context.Context, md MD) context.Context {
  110. return context.WithValue(ctx, mdOutgoingKey{}, rawMD{md: md})
  111. }
  112. // AppendToOutgoingContext returns a new context with the provided kv merged
  113. // with any existing metadata in the context. Please refer to the
  114. // documentation of Pairs for a description of kv.
  115. func AppendToOutgoingContext(ctx context.Context, kv ...string) context.Context {
  116. if len(kv)%2 == 1 {
  117. panic(fmt.Sprintf("metadata: AppendToOutgoingContext got an odd number of input pairs for metadata: %d", len(kv)))
  118. }
  119. md, _ := ctx.Value(mdOutgoingKey{}).(rawMD)
  120. added := make([][]string, len(md.added)+1)
  121. copy(added, md.added)
  122. added[len(added)-1] = make([]string, len(kv))
  123. copy(added[len(added)-1], kv)
  124. return context.WithValue(ctx, mdOutgoingKey{}, rawMD{md: md.md, added: added})
  125. }
  126. // FromIncomingContext returns the incoming metadata in ctx if it exists. The
  127. // returned MD should not be modified. Writing to it may cause races.
  128. // Modification should be made to copies of the returned MD.
  129. func FromIncomingContext(ctx context.Context) (md MD, ok bool) {
  130. md, ok = ctx.Value(mdIncomingKey{}).(MD)
  131. return
  132. }
  133. // FromOutgoingContextRaw returns the un-merged, intermediary contents
  134. // of rawMD. Remember to perform strings.ToLower on the keys. The returned
  135. // MD should not be modified. Writing to it may cause races. Modification
  136. // should be made to copies of the returned MD.
  137. //
  138. // This is intended for gRPC-internal use ONLY.
  139. func FromOutgoingContextRaw(ctx context.Context) (MD, [][]string, bool) {
  140. raw, ok := ctx.Value(mdOutgoingKey{}).(rawMD)
  141. if !ok {
  142. return nil, nil, false
  143. }
  144. return raw.md, raw.added, true
  145. }
  146. // FromOutgoingContext returns the outgoing metadata in ctx if it exists. The
  147. // returned MD should not be modified. Writing to it may cause races.
  148. // Modification should be made to copies of the returned MD.
  149. func FromOutgoingContext(ctx context.Context) (MD, bool) {
  150. raw, ok := ctx.Value(mdOutgoingKey{}).(rawMD)
  151. if !ok {
  152. return nil, false
  153. }
  154. mds := make([]MD, 0, len(raw.added)+1)
  155. mds = append(mds, raw.md)
  156. for _, vv := range raw.added {
  157. mds = append(mds, Pairs(vv...))
  158. }
  159. return Join(mds...), ok
  160. }
  161. type rawMD struct {
  162. md MD
  163. added [][]string
  164. }