blob: d884c6ac6c4f81db1fe878861978a12168166fbe [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
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
20package thrift
21
22import (
23 "context"
24)
25
26// See https://godoc.org/context#WithValue on why do we need the unexported typedefs.
27type responseHelperKey struct{}
28
29// TResponseHelper defines a object with a set of helper functions that can be
30// retrieved from the context object passed into server handler functions.
31//
32// Use GetResponseHelper to retrieve the injected TResponseHelper implementation
33// from the context object.
34//
35// The zero value of TResponseHelper is valid with all helper functions being
36// no-op.
37type TResponseHelper struct {
38 // THeader related functions
39 *THeaderResponseHelper
40}
41
42// THeaderResponseHelper defines THeader related TResponseHelper functions.
43//
44// The zero value of *THeaderResponseHelper is valid with all helper functions
45// being no-op.
46type THeaderResponseHelper struct {
47 proto *THeaderProtocol
48}
49
50// NewTHeaderResponseHelper creates a new THeaderResponseHelper from the
51// underlying TProtocol.
52func NewTHeaderResponseHelper(proto TProtocol) *THeaderResponseHelper {
53 if hp, ok := proto.(*THeaderProtocol); ok {
54 return &THeaderResponseHelper{
55 proto: hp,
56 }
57 }
58 return nil
59}
60
61// SetHeader sets a response header.
62//
63// It's no-op if the underlying protocol/transport does not support THeader.
64func (h *THeaderResponseHelper) SetHeader(key, value string) {
65 if h != nil && h.proto != nil {
66 h.proto.SetWriteHeader(key, value)
67 }
68}
69
70// ClearHeaders clears all the response headers previously set.
71//
72// It's no-op if the underlying protocol/transport does not support THeader.
73func (h *THeaderResponseHelper) ClearHeaders() {
74 if h != nil && h.proto != nil {
75 h.proto.ClearWriteHeaders()
76 }
77}
78
79// GetResponseHelper retrieves the TResponseHelper implementation injected into
80// the context object.
81//
82// If no helper was found in the context object, a nop helper with ok == false
83// will be returned.
84func GetResponseHelper(ctx context.Context) (helper TResponseHelper, ok bool) {
85 if v := ctx.Value(responseHelperKey{}); v != nil {
86 helper, ok = v.(TResponseHelper)
87 }
88 return
89}
90
91// SetResponseHelper injects TResponseHelper into the context object.
92func SetResponseHelper(ctx context.Context, helper TResponseHelper) context.Context {
93 return context.WithValue(ctx, responseHelperKey{}, helper)
94}