response_helper.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 responseHelperKey struct{}
  25. // TResponseHelper defines a object with a set of helper functions that can be
  26. // retrieved from the context object passed into server handler functions.
  27. //
  28. // Use GetResponseHelper to retrieve the injected TResponseHelper implementation
  29. // from the context object.
  30. //
  31. // The zero value of TResponseHelper is valid with all helper functions being
  32. // no-op.
  33. type TResponseHelper struct {
  34. // THeader related functions
  35. *THeaderResponseHelper
  36. }
  37. // THeaderResponseHelper defines THeader related TResponseHelper functions.
  38. //
  39. // The zero value of *THeaderResponseHelper is valid with all helper functions
  40. // being no-op.
  41. type THeaderResponseHelper struct {
  42. proto *THeaderProtocol
  43. }
  44. // NewTHeaderResponseHelper creates a new THeaderResponseHelper from the
  45. // underlying TProtocol.
  46. func NewTHeaderResponseHelper(proto TProtocol) *THeaderResponseHelper {
  47. if hp, ok := proto.(*THeaderProtocol); ok {
  48. return &THeaderResponseHelper{
  49. proto: hp,
  50. }
  51. }
  52. return nil
  53. }
  54. // SetHeader sets a response header.
  55. //
  56. // It's no-op if the underlying protocol/transport does not support THeader.
  57. func (h *THeaderResponseHelper) SetHeader(key, value string) {
  58. if h != nil && h.proto != nil {
  59. h.proto.SetWriteHeader(key, value)
  60. }
  61. }
  62. // ClearHeaders clears all the response headers previously set.
  63. //
  64. // It's no-op if the underlying protocol/transport does not support THeader.
  65. func (h *THeaderResponseHelper) ClearHeaders() {
  66. if h != nil && h.proto != nil {
  67. h.proto.ClearWriteHeaders()
  68. }
  69. }
  70. // GetResponseHelper retrieves the TResponseHelper implementation injected into
  71. // the context object.
  72. //
  73. // If no helper was found in the context object, a nop helper with ok == false
  74. // will be returned.
  75. func GetResponseHelper(ctx context.Context) (helper TResponseHelper, ok bool) {
  76. if v := ctx.Value(responseHelperKey{}); v != nil {
  77. helper, ok = v.(TResponseHelper)
  78. }
  79. return
  80. }
  81. // SetResponseHelper injects TResponseHelper into the context object.
  82. func SetResponseHelper(ctx context.Context, helper TResponseHelper) context.Context {
  83. return context.WithValue(ctx, responseHelperKey{}, helper)
  84. }