header_context.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package thrift
  20. import (
  21. "context"
  22. )
  23. // See https://pkg.go.dev/context#WithValue on why do we need the unexported typedefs.
  24. type (
  25. headerKey string
  26. headerKeyList int
  27. )
  28. // Values for headerKeyList.
  29. const (
  30. headerKeyListRead headerKeyList = iota
  31. headerKeyListWrite
  32. )
  33. // SetHeader sets a header in the context.
  34. func SetHeader(ctx context.Context, key, value string) context.Context {
  35. return context.WithValue(
  36. ctx,
  37. headerKey(key),
  38. value,
  39. )
  40. }
  41. // UnsetHeader unsets a previously set header in the context.
  42. func UnsetHeader(ctx context.Context, key string) context.Context {
  43. return context.WithValue(
  44. ctx,
  45. headerKey(key),
  46. nil,
  47. )
  48. }
  49. // GetHeader returns a value of the given header from the context.
  50. func GetHeader(ctx context.Context, key string) (value string, ok bool) {
  51. if v := ctx.Value(headerKey(key)); v != nil {
  52. value, ok = v.(string)
  53. }
  54. return
  55. }
  56. // SetReadHeaderList sets the key list of read THeaders in the context.
  57. func SetReadHeaderList(ctx context.Context, keys []string) context.Context {
  58. return context.WithValue(
  59. ctx,
  60. headerKeyListRead,
  61. keys,
  62. )
  63. }
  64. // GetReadHeaderList returns the key list of read THeaders from the context.
  65. func GetReadHeaderList(ctx context.Context) []string {
  66. if v := ctx.Value(headerKeyListRead); v != nil {
  67. if value, ok := v.([]string); ok {
  68. return value
  69. }
  70. }
  71. return nil
  72. }
  73. // SetWriteHeaderList sets the key list of THeaders to write in the context.
  74. func SetWriteHeaderList(ctx context.Context, keys []string) context.Context {
  75. return context.WithValue(
  76. ctx,
  77. headerKeyListWrite,
  78. keys,
  79. )
  80. }
  81. // GetWriteHeaderList returns the key list of THeaders to write from the context.
  82. func GetWriteHeaderList(ctx context.Context) []string {
  83. if v := ctx.Value(headerKeyListWrite); v != nil {
  84. if value, ok := v.([]string); ok {
  85. return value
  86. }
  87. }
  88. return nil
  89. }
  90. // AddReadTHeaderToContext adds the whole THeader headers into context.
  91. func AddReadTHeaderToContext(ctx context.Context, headers THeaderMap) context.Context {
  92. keys := make([]string, 0, len(headers))
  93. for key, value := range headers {
  94. ctx = SetHeader(ctx, key, value)
  95. keys = append(keys, key)
  96. }
  97. return SetReadHeaderList(ctx, keys)
  98. }