-
Notifications
You must be signed in to change notification settings - Fork 60
/
http_headers.go
91 lines (73 loc) · 2.58 KB
/
http_headers.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
package backend
import (
"fmt"
"net/http"
"net/textproto"
"strings"
)
const (
// OAuthIdentityTokenHeaderName the header name used for forwarding
// OAuth Identity access token.
OAuthIdentityTokenHeaderName = "Authorization"
// OAuthIdentityIDTokenHeaderName the header name used for forwarding
// OAuth Identity ID token.
OAuthIdentityIDTokenHeaderName = "X-Id-Token"
// CookiesHeaderName the header name used for forwarding
// cookies.
CookiesHeaderName = "Cookie"
httpHeaderPrefix = "http_"
)
// ForwardHTTPHeaders interface marking that forward of HTTP headers is supported.
type ForwardHTTPHeaders interface {
// SetHTTPHeader sets the header entries associated with key to the
// single element value. It replaces any existing values
// associated with key. The key is case insensitive; it is
// canonicalized by textproto.CanonicalMIMEHeaderKey.
SetHTTPHeader(key, value string)
// DeleteHTTPHeader deletes the values associated with key.
// The key is case insensitive; it is canonicalized by
// CanonicalHeaderKey.
DeleteHTTPHeader(key string)
// GetHTTPHeader gets the first value associated with the given key. If
// there are no values associated with the key, Get returns "".
// It is case insensitive; textproto.CanonicalMIMEHeaderKey is
// used to canonicalize the provided key. Get assumes that all
// keys are stored in canonical form.
GetHTTPHeader(key string) string
// GetHTTPHeaders returns HTTP headers.
GetHTTPHeaders() http.Header
}
func setHTTPHeaderInStringMap(headers map[string]string, key string, value string) {
if headers == nil {
headers = map[string]string{}
}
headers[fmt.Sprintf("%s%s", httpHeaderPrefix, key)] = value
}
func getHTTPHeadersFromStringMap(headers map[string]string) http.Header {
httpHeaders := http.Header{}
for k, v := range headers {
if textproto.CanonicalMIMEHeaderKey(k) == OAuthIdentityTokenHeaderName {
httpHeaders.Set(k, v)
}
if textproto.CanonicalMIMEHeaderKey(k) == OAuthIdentityIDTokenHeaderName {
httpHeaders.Set(k, v)
}
if textproto.CanonicalMIMEHeaderKey(k) == CookiesHeaderName {
httpHeaders.Set(k, v)
}
if strings.HasPrefix(k, httpHeaderPrefix) {
hKey := strings.TrimPrefix(k, httpHeaderPrefix)
httpHeaders.Set(hKey, v)
}
}
return httpHeaders
}
func deleteHTTPHeaderInStringMap(headers map[string]string, key string) {
for k := range headers {
if textproto.CanonicalMIMEHeaderKey(k) == textproto.CanonicalMIMEHeaderKey(key) ||
textproto.CanonicalMIMEHeaderKey(k) == textproto.CanonicalMIMEHeaderKey(fmt.Sprintf("%s%s", httpHeaderPrefix, key)) {
delete(headers, k)
break
}
}
}