/
header.go
200 lines (175 loc) · 5.67 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tchannel
import (
"context"
"encoding/binary"
"io"
"strings"
"github.com/uber/tchannel-go"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/transport/tchannel/internal"
)
const (
// ErrorCodeHeaderKey is the response header key for the error code.
ErrorCodeHeaderKey = "$rpc$-error-code"
// ErrorNameHeaderKey is the response header key for the error name.
ErrorNameHeaderKey = "$rpc$-error-name"
// ErrorMessageHeaderKey is the response header key for the error message.
ErrorMessageHeaderKey = "$rpc$-error-message"
// ServiceHeaderKey is the response header key for the respond service
ServiceHeaderKey = "$rpc$-service"
)
var _reservedHeaderKeys = map[string]struct{}{
ErrorCodeHeaderKey: {},
ErrorNameHeaderKey: {},
ErrorMessageHeaderKey: {},
ServiceHeaderKey: {},
}
func isReservedHeaderKey(key string) bool {
_, ok := _reservedHeaderKeys[strings.ToLower(key)]
return ok
}
// readRequestHeaders reads headers and baggage from an incoming request.
func readRequestHeaders(
ctx context.Context,
format tchannel.Format,
getReader func() (tchannel.ArgReader, error),
) (context.Context, transport.Headers, error) {
headers, err := readHeaders(format, getReader)
if err != nil {
return ctx, headers, err
}
return ctx, headers, nil
}
// readHeaders reads headers using the given function to get the arg reader.
//
// This may be used with the Arg2Reader functions on InboundCall and
// OutboundCallResponse.
//
// If the format is JSON, the headers are expected to be JSON encoded.
//
// This function always returns a non-nil Headers object in case of success.
func readHeaders(format tchannel.Format, getReader func() (tchannel.ArgReader, error)) (transport.Headers, error) {
if format == tchannel.JSON {
// JSON is special
var headers map[string]string
err := tchannel.NewArgReader(getReader()).ReadJSON(&headers)
return transport.HeadersFromMap(headers), err
}
r, err := getReader()
if err != nil {
return transport.Headers{}, err
}
headers, err := decodeHeaders(r)
if err != nil {
return headers, err
}
return headers, r.Close()
}
var emptyMap = map[string]string{}
// writeHeaders writes the given headers using the given function to get the
// arg writer.
//
// This may be used with the Arg2Writer functions on OutboundCall and
// InboundCallResponse.
//
// If the format is JSON, the headers are JSON encoded.
func writeHeaders(format tchannel.Format, headers map[string]string, tracingBaggage map[string]string, getWriter func() (tchannel.ArgWriter, error)) error {
merged := mergeHeaders(headers, tracingBaggage)
if format == tchannel.JSON {
// JSON is special
if merged == nil {
// We want to write "{}", not "null" for empty map.
merged = emptyMap
}
return tchannel.NewArgWriter(getWriter()).WriteJSON(merged)
}
return tchannel.NewArgWriter(getWriter()).Write(encodeHeaders(merged))
}
// mergeHeaders will keep the last value if the same key appears multiple times
func mergeHeaders(m1, m2 map[string]string) map[string]string {
if len(m1) == 0 {
return m2
}
if len(m2) == 0 {
return m1
}
// merge and return
merged := make(map[string]string, len(m1)+len(m2))
for k, v := range m1 {
merged[k] = v
}
for k, v := range m2 {
merged[k] = v
}
return merged
}
// decodeHeaders decodes headers using the format:
//
// nh:2 (k~2 v~2){nh}
func decodeHeaders(r io.Reader) (transport.Headers, error) {
reader := internal.NewReader(r)
count := reader.ReadUint16()
if count == 0 {
return transport.Headers{}, reader.Err()
}
headers := transport.NewHeadersWithCapacity(int(count))
for i := 0; i < int(count) && reader.Err() == nil; i++ {
k := reader.ReadLen16String()
v := reader.ReadLen16String()
headers = headers.With(k, v)
}
return headers, reader.Err()
}
// encodeHeaders encodes headers using the format:
//
// nh:2 (k~2 v~2){nh}
func encodeHeaders(hs map[string]string) []byte {
if len(hs) == 0 {
return []byte{0, 0} // nh = 2
}
size := 2 // nh:2
for k, v := range hs {
size += len(k) + 2 // k~2
size += len(v) + 2 // v~2
}
out := make([]byte, size)
i := 2
binary.BigEndian.PutUint16(out, uint16(len(hs))) // nh:2
for k, v := range hs {
i += _putStr16(k, out[i:]) // k~2
i += _putStr16(v, out[i:]) // v~2
}
return out
}
func headerMap(hs transport.Headers, headerCase headerCase) map[string]string {
switch headerCase {
case originalHeaderCase:
return hs.OriginalItems()
default:
return hs.Items()
}
}
// _putStr16 writes the bytes `in` into `out` using the encoding `s~2`.
func _putStr16(in string, out []byte) int {
binary.BigEndian.PutUint16(out, uint16(len(in)))
return copy(out[2:], in) + 2
}