metadata.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. *
  3. * Copyright 2020 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 contains functions to set and get metadata from addresses.
  19. //
  20. // This package is experimental.
  21. package metadata
  22. import (
  23. "fmt"
  24. "strings"
  25. "google.golang.org/grpc/metadata"
  26. "google.golang.org/grpc/resolver"
  27. )
  28. type mdKeyType string
  29. const mdKey = mdKeyType("grpc.internal.address.metadata")
  30. type mdValue metadata.MD
  31. func (m mdValue) Equal(o interface{}) bool {
  32. om, ok := o.(mdValue)
  33. if !ok {
  34. return false
  35. }
  36. if len(m) != len(om) {
  37. return false
  38. }
  39. for k, v := range m {
  40. ov := om[k]
  41. if len(ov) != len(v) {
  42. return false
  43. }
  44. for i, ve := range v {
  45. if ov[i] != ve {
  46. return false
  47. }
  48. }
  49. }
  50. return true
  51. }
  52. // Get returns the metadata of addr.
  53. func Get(addr resolver.Address) metadata.MD {
  54. attrs := addr.Attributes
  55. if attrs == nil {
  56. return nil
  57. }
  58. md, _ := attrs.Value(mdKey).(mdValue)
  59. return metadata.MD(md)
  60. }
  61. // Set sets (overrides) the metadata in addr.
  62. //
  63. // When a SubConn is created with this address, the RPCs sent on it will all
  64. // have this metadata.
  65. func Set(addr resolver.Address, md metadata.MD) resolver.Address {
  66. addr.Attributes = addr.Attributes.WithValue(mdKey, mdValue(md))
  67. return addr
  68. }
  69. // Validate validates every pair in md with ValidatePair.
  70. func Validate(md metadata.MD) error {
  71. for k, vals := range md {
  72. if err := ValidatePair(k, vals...); err != nil {
  73. return err
  74. }
  75. }
  76. return nil
  77. }
  78. // hasNotPrintable return true if msg contains any characters which are not in %x20-%x7E
  79. func hasNotPrintable(msg string) bool {
  80. // for i that saving a conversion if not using for range
  81. for i := 0; i < len(msg); i++ {
  82. if msg[i] < 0x20 || msg[i] > 0x7E {
  83. return true
  84. }
  85. }
  86. return false
  87. }
  88. // ValidatePair validate a key-value pair with the following rules (the pseudo-header will be skipped) :
  89. //
  90. // - key must contain one or more characters.
  91. // - the characters in the key must be contained in [0-9 a-z _ - .].
  92. // - if the key ends with a "-bin" suffix, no validation of the corresponding value is performed.
  93. // - the characters in the every value must be printable (in [%x20-%x7E]).
  94. func ValidatePair(key string, vals ...string) error {
  95. // key should not be empty
  96. if key == "" {
  97. return fmt.Errorf("there is an empty key in the header")
  98. }
  99. // pseudo-header will be ignored
  100. if key[0] == ':' {
  101. return nil
  102. }
  103. // check key, for i that saving a conversion if not using for range
  104. for i := 0; i < len(key); i++ {
  105. r := key[i]
  106. if !(r >= 'a' && r <= 'z') && !(r >= '0' && r <= '9') && r != '.' && r != '-' && r != '_' {
  107. return fmt.Errorf("header key %q contains illegal characters not in [0-9a-z-_.]", key)
  108. }
  109. }
  110. if strings.HasSuffix(key, "-bin") {
  111. return nil
  112. }
  113. // check value
  114. for _, val := range vals {
  115. if hasNotPrintable(val) {
  116. return fmt.Errorf("header key %q contains value with non-printable ASCII characters", key)
  117. }
  118. }
  119. return nil
  120. }