This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.go
164 lines (134 loc) · 3.69 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
package rewrite
import (
"net/http"
"strconv"
)
type RewriteRule int
const (
Keep RewriteRule = iota
PrefixIfUrlRewrite
Prefix
UrlRewrite
PrefixIfContentRewrite
ContentLength
Cookie
)
type HeaderRewriter struct {
Prefix string
Rules map[string]RewriteRule
Urlrw Rewriter
Cookierw Rewriter
RewritingContent bool
}
func NewHeaderRewriter(configs ...func(cfg *Config)) *HeaderRewriter {
c := makeConfig(configs...)
return &HeaderRewriter{
Prefix: c.HeaderPrefix,
Rules: c.HeaderRules,
Urlrw: c.Defmod,
// Cookierw: c.CookieRewriter,
// RewritingContent: c.ContentRewriter != nil,
}
}
func (hrw HeaderRewriter) RewriteHeaders(headers http.Header) http.Header {
rewritten := http.Header{}
for key, _ := range headers {
newkey, newval := hrw.rewriteHeader(key, headers.Get(key))
rewritten.Add(newkey, newval)
}
return rewritten
}
func (hrw HeaderRewriter) rewriteHeader(name, value string) (string, string) {
switch hrw.Rules[name] {
case Keep:
return name, value
case UrlRewrite:
if hrw.Urlrw != nil {
return name, string(hrw.Urlrw.Rewrite([]byte(value)))
}
return name, value
case PrefixIfContentRewrite:
if hrw.RewritingContent {
return hrw.Prefix + name, value
}
return name, value
case PrefixIfUrlRewrite:
if hrw.Urlrw != nil {
return hrw.Prefix + name, value
}
return name, value
case ContentLength:
if value == "0" {
return name, value
}
// if not rewriting content, attempt to use the
// length value
if !hrw.RewritingContent {
if lenth, err := strconv.Atoi(value); err == nil {
return name, strconv.FormatInt(int64(lenth), 10)
}
}
return hrw.Prefix + name, value
case Cookie:
if hrw.Cookierw != nil {
// return self.rwinfo.cookie_rewriter.rewrite(value)
}
return name, value
case Prefix:
return hrw.Prefix + name, value
}
return name, value
}
// func (hrw *HeaderRewriter) AddCacheHeaders(headers map[string]string) {
// }
var DefaultHeaderRewriters = map[string]RewriteRule{
"Access-Control-Allow-Origin": PrefixIfUrlRewrite,
"Access-Control-Allow-Credentials": PrefixIfUrlRewrite,
"Access-Control-Expose-Headers": PrefixIfUrlRewrite,
"Access-Control-Max-Age": PrefixIfUrlRewrite,
"Access-Control-Allow-Methods": PrefixIfUrlRewrite,
"Access-Control-Allow-Headers": PrefixIfUrlRewrite,
"Accept-Patch": Keep,
"Accept-Ranges": Keep,
"Age": Prefix,
"Allow": Keep,
"Alt-Svc": Prefix,
"Cache-Control": Prefix,
"Connection": Prefix,
"Content-Base": UrlRewrite,
"Content-Disposition": Keep,
"Content-Encoding": PrefixIfContentRewrite,
"Content-Language": Keep,
"Content-Length": ContentLength,
"Content-Location": UrlRewrite,
"Content-Md5": Prefix,
"Content-Range": Keep,
"Content-Security-Policy": Prefix,
"Content-Security-Policy-Report-Only": Prefix,
"Content-Type": Keep,
"Date": Keep,
"Etag": Prefix,
"Expires": Prefix,
"Last-Modified": Prefix,
"Link": Keep,
"Location": UrlRewrite,
"P3p": Prefix,
"Pragma": Prefix,
"Proxy-Authenticate": Keep,
"Public-Key-Pins": Prefix,
"Retry-After": Prefix,
"Server": Prefix,
"Set-Cookie": Cookie,
"Strict-Transport-Security": Prefix,
"Trailer": Prefix,
"Transfer-Encoding": Prefix,
"Tk": Prefix,
"Upgrade": Prefix,
"Upgrade-Insecure-Requests": Prefix,
"Vary": Prefix,
"Via": Prefix,
"Warning": Prefix,
"Www-Authenticate": Keep,
"X-Frame-Options": Prefix,
"X-Xss-Protection": Prefix,
}