metadata.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. // Package metadata define the structure of the metadata supported by gRPC library.
  34. package metadata // import "google.golang.org/grpc/metadata"
  35. import (
  36. "encoding/base64"
  37. "fmt"
  38. "strings"
  39. "golang.org/x/net/context"
  40. )
  41. const (
  42. binHdrSuffix = "-bin"
  43. )
  44. // encodeKeyValue encodes key and value qualified for transmission via gRPC.
  45. // Transmitting binary headers violates HTTP/2 spec.
  46. // TODO(zhaoq): Maybe check if k is ASCII also.
  47. func encodeKeyValue(k, v string) (string, string) {
  48. k = strings.ToLower(k)
  49. if strings.HasSuffix(k, binHdrSuffix) {
  50. val := base64.StdEncoding.EncodeToString([]byte(v))
  51. v = string(val)
  52. }
  53. return k, v
  54. }
  55. // DecodeKeyValue returns the original key and value corresponding to the
  56. // encoded data in k, v.
  57. func DecodeKeyValue(k, v string) (string, string, error) {
  58. if !strings.HasSuffix(k, binHdrSuffix) {
  59. return k, v, nil
  60. }
  61. val, err := base64.StdEncoding.DecodeString(v)
  62. if err != nil {
  63. return "", "", err
  64. }
  65. return k, string(val), nil
  66. }
  67. // MD is a mapping from metadata keys to values. Users should use the following
  68. // two convenience functions New and Pairs to generate MD.
  69. type MD map[string][]string
  70. // New creates a MD from given key-value map.
  71. func New(m map[string]string) MD {
  72. md := MD{}
  73. for k, v := range m {
  74. key, val := encodeKeyValue(k, v)
  75. md[key] = append(md[key], val)
  76. }
  77. return md
  78. }
  79. // Pairs returns an MD formed by the mapping of key, value ...
  80. // Pairs panics if len(kv) is odd.
  81. func Pairs(kv ...string) MD {
  82. if len(kv)%2 == 1 {
  83. panic(fmt.Sprintf("metadata: Pairs got the odd number of input pairs for metadata: %d", len(kv)))
  84. }
  85. md := MD{}
  86. var k string
  87. for i, s := range kv {
  88. if i%2 == 0 {
  89. k = s
  90. continue
  91. }
  92. key, val := encodeKeyValue(k, s)
  93. md[key] = append(md[key], val)
  94. }
  95. return md
  96. }
  97. // Len returns the number of items in md.
  98. func (md MD) Len() int {
  99. return len(md)
  100. }
  101. // Copy returns a copy of md.
  102. func (md MD) Copy() MD {
  103. out := MD{}
  104. for k, v := range md {
  105. for _, i := range v {
  106. out[k] = append(out[k], i)
  107. }
  108. }
  109. return out
  110. }
  111. type mdKey struct{}
  112. // NewContext creates a new context with md attached.
  113. func NewContext(ctx context.Context, md MD) context.Context {
  114. return context.WithValue(ctx, mdKey{}, md)
  115. }
  116. // FromContext returns the MD in ctx if it exists.
  117. func FromContext(ctx context.Context) (md MD, ok bool) {
  118. md, ok = ctx.Value(mdKey{}).(MD)
  119. return
  120. }