-
Notifications
You must be signed in to change notification settings - Fork 117
/
header.go
200 lines (175 loc) · 5.57 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
package gateway
import (
"context"
"fmt"
"net/http"
"net/textproto"
"strings"
"google.golang.org/grpc/metadata"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
)
const XForwardedFor = "X-Forwarded-For"
// GetGeoHeaders returns a slice of x-geo- headers.
func GetGeoHeaders() []string {
return []string{
"x-geo-org",
"x-geo-country-code",
"x-geo-country-name",
"x-geo-region-code",
"x-geo-region-name",
"x-geo-city-name",
"x-geo-postal-code",
"x-geo-latitude",
"x-geo-longitude",
}
}
// GetXB3Headers returns a slice of x-b3- headers.
func GetXB3Headers() []string {
return []string{
"x-b3-traceid",
"x-b3-parentspanid",
"x-b3-spanid",
"x-b3-sampled",
}
}
// Header returns first value for a given key if it exists in gRPC metadata
// from incoming or outcoming context, otherwise returns (nil, false)
//
// Calls HeaderN(ctx, key, 1)
//
// Provided key is converted to lowercase (see grpc/metadata.New).
// If key is not found the prefix "grpcgateway-" is added to the key and
// key is being searched once again.
func Header(ctx context.Context, key string) (string, bool) {
if l, ok := HeaderN(ctx, key, 1); ok {
return l[0], ok
}
return "", false
}
// HeaderN returns first n values for a given key if it exists in gRPC metadata
// from incoming or outcoming context, otherwise returns (nil, false)
//
// If n < 0 all values for a given key will be returned
// If n > 0 at least n values will be returned, or (nil, false)
// If n == 0 result is (nil, false)
//
// Provided key is converted to lowercase (see grpc/metadata.New).
// If key is not found the prefix "grpcgateway-" is added to the key and
// key is being searched once again.
func HeaderN(ctx context.Context, key string, n int) (val []string, found bool) {
if n == 0 {
return
}
if smd, ok := runtime.ServerMetadataFromContext(ctx); ok {
ctx = metadata.NewIncomingContext(ctx, smd.HeaderMD)
}
imd, iok := metadata.FromIncomingContext(ctx)
omd, ook := metadata.FromOutgoingContext(ctx)
md := metadata.Join(imd, omd)
if !iok && !ook {
return nil, false
}
key = strings.ToLower(key)
if v, ok := md[key]; ok {
val = append(val, v...)
found = true
}
// If md contains 'key' and 'runtime.MetadataPrefix + key'
// collect them all
key = runtime.MetadataPrefix + key
if v, ok := md[key]; ok {
val = append(val, v...)
found = true
}
switch {
case !found:
return
case n < 0 || len(val) == n:
return
case len(val) < n:
return nil, false
default:
return val[:n], found
}
}
// PrefixOutgoingHeaderMatcher discards all grpc header metadata.
func PrefixOutgoingHeaderMatcher(key string) (string, bool) {
return "", false
}
func handleForwardResponseServerMetadata(matcher runtime.HeaderMatcherFunc, w http.ResponseWriter, md runtime.ServerMetadata) {
for k, vs := range md.HeaderMD {
if h, ok := matcher(k); ok {
for _, v := range vs {
w.Header().Add(h, v)
}
}
}
}
func handleForwardResponseTrailerHeader(w http.ResponseWriter, md runtime.ServerMetadata) {
for k := range md.TrailerMD {
if strings.HasPrefix(k, "error-") || strings.HasPrefix(k, "success-") {
continue
}
tKey := textproto.CanonicalMIMEHeaderKey(fmt.Sprintf("%s%s", runtime.MetadataTrailerPrefix, k))
w.Header().Add("Trailer", tKey)
}
}
func handleForwardResponseTrailer(w http.ResponseWriter, md runtime.ServerMetadata) {
for k, vs := range md.TrailerMD {
tKey := fmt.Sprintf("%s%s", runtime.MetadataTrailerPrefix, k)
for _, v := range vs {
w.Header().Add(tKey, v)
}
}
}
// GeoIPHeaderMatcher X-Geo-* headers are set of geo metadata from MaxMind DB injected on ingress nginx
func GeoIPHeaderMatcher() runtime.HeaderMatcherFunc {
return ExtendedDefaultHeaderMatcher(GetGeoHeaders()...)
}
// RequestIDHeaderMatcher request id header contains unique identifier for request
func RequestIDHeaderMatcher() runtime.HeaderMatcherFunc {
return ExtendedDefaultHeaderMatcher("request-id")
}
// TracingHeaderMatcher tracing headers
func TracingHeaderMatcher() runtime.HeaderMatcherFunc {
return ExtendedDefaultHeaderMatcher(GetXB3Headers()...)
}
// AtlasDefaultHeaderMatcher func used to add all headers used by atlas-app-toolkit
// This function also passes through all the headers that runtime.DefaultHeaderMatcher handles.
// AtlasDefaultHeaderMatcher can be used as a Incoming/Outgoing header matcher.
func AtlasDefaultHeaderMatcher() func(string) (string, bool) {
return ChainHeaderMatcher(
GeoIPHeaderMatcher(),
RequestIDHeaderMatcher(),
TracingHeaderMatcher(),
)
}
// ExtendedDefaultHeaderMatcher func is used to add custom headers to be matched
// from incoming http requests, If this returns true the header will be added to grpc context.
// This function also passes through all the headers that runtime.DefaultHeaderMatcher handles.
func ExtendedDefaultHeaderMatcher(headerNames ...string) func(string) (string, bool) {
customHeaders := map[string]bool{}
for _, name := range headerNames {
customHeaders[strings.ToLower(name)] = true
}
return func(headerName string) (string, bool) {
if key, ok := runtime.DefaultHeaderMatcher(headerName); ok {
return key, ok
}
_, ok := customHeaders[strings.ToLower(headerName)]
return headerName, ok
}
}
// ChainHeaderMatcher func is used to build chain on header matcher funcitons
// this function can be used as incoming or outgoing header matcher
// keep in mind that gRPC metadata treat as case insensitive strings
func ChainHeaderMatcher(matchers ...runtime.HeaderMatcherFunc) runtime.HeaderMatcherFunc {
return func(h string) (string, bool) {
for _, m := range matchers {
if k, allow := m(h); allow {
return k, allow
}
}
return "", false
}
}