-
Notifications
You must be signed in to change notification settings - Fork 25
/
header.go
320 lines (272 loc) · 8.37 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package azuretls
import (
http "github.com/Noooste/fhttp"
"net/url"
"sort"
"strings"
)
// OrderedHeaders is a slice of headers.
type OrderedHeaders [][]string
// PHeader is a slice of pseudo headers.
type PHeader []string
// HeaderOrder is a slice of header names.
type HeaderOrder []string
// GetDefaultPseudoHeaders returns the default pseudo headers.
func GetDefaultPseudoHeaders() PHeader {
return []string{Method, Authority, Scheme, Path}
}
// Clone returns a copy of the header.
func (oh *OrderedHeaders) Clone() OrderedHeaders {
if oh == nil {
return nil
}
var clone = make(OrderedHeaders, 0, len(*oh))
for _, header := range *oh {
var fieldClone = make([]string, 0, len(header))
for _, field := range header {
fieldClone = append(fieldClone, field)
}
clone = append(clone, fieldClone)
}
return clone
}
// Add adds the value to the field.
// It appends to any existing values associated with the field.
func (oh *OrderedHeaders) Add(field string, value ...string) {
field = http.CanonicalHeaderKey(field)
for i, c := range *oh {
if http.CanonicalHeaderKey(c[0]) == field {
(*oh)[i] = append((*oh)[i], value...)
}
}
}
// Set sets the field to the given value.
// It replaces any existing values associated with the field.
func (oh *OrderedHeaders) Set(field string, value ...string) {
field = http.CanonicalHeaderKey(field)
newList := append([]string{field}, value...)
for i, c := range *oh {
if http.CanonicalHeaderKey(c[0]) == field {
(*oh)[i] = newList
return
}
}
*oh = append(*oh, newList)
}
// Get returns the first value associated with the given field.
// If the field is not present, it returns an empty string.
func (oh *OrderedHeaders) Get(field string) string {
field = http.CanonicalHeaderKey(field)
for _, c := range *oh {
if http.CanonicalHeaderKey(c[0]) == field {
return strings.Join(c[1:], "; ")
}
}
return ""
}
// Remove removes the first instance of the field from the header.
// If the field is not present, it does nothing.
// Deprecated: Use Del instead.
func (oh *OrderedHeaders) Remove(field string) OrderedHeaders {
return oh.Del(http.CanonicalHeaderKey(field))
}
// Del removes the first instance of the field from the header.
// If the field is not present, it does nothing.
func (oh *OrderedHeaders) Del(field string) OrderedHeaders {
var index = -1
field = http.CanonicalHeaderKey(field)
for i := 0; i < len(*oh); i++ {
if http.CanonicalHeaderKey((*oh)[i][0]) == field {
index = i
}
}
if index != -1 {
return append((*oh)[:index], (*oh)[index+1:]...)
}
return *oh
}
func (oh *OrderedHeaders) ToHeader() http.Header {
var result = make(http.Header, len(*oh))
var order = make([]string, 0, len(*oh))
for _, header := range *oh {
if len(header) == 0 {
continue
}
var key = http.CanonicalHeaderKey(header[0])
if result.Get(key) != "" {
for _, v := range header[1:] {
result.Add(key, v)
}
} else {
order = append(order, key)
if len(header) == 1 {
continue
}
result.Set(key, header[1])
for _, v := range header[2:] {
result.Add(key, v)
}
}
}
return result
}
//gocyclo:ignore
func (r *Request) formatHeader() {
var setUserAgent = true
if r.OrderedHeaders != nil && len(r.OrderedHeaders) > 0 {
r.HttpRequest.Header = make(http.Header, len(r.OrderedHeaders)+2) // +2 for http.HeaderOrderKey and http.PHeaderOrderKey
r.HttpRequest.Header[http.HeaderOrderKey] = make([]string, 0, len(r.OrderedHeaders))
for _, el := range r.OrderedHeaders {
if len(el) == 0 || !shouldCopyHeaderOnRedirect(el[0], r.HttpRequest.URL, r.HttpRequest.URL) {
continue
}
var key = strings.ToLower(el[0])
r.HttpRequest.Header[http.HeaderOrderKey] = append(r.HttpRequest.Header[http.HeaderOrderKey], key)
if len(el) == 1 {
// skip empty header value, the key indicates the order
continue
}
if _, ok := r.HttpRequest.Header[key]; !ok {
if setUserAgent && http.CanonicalHeaderKey(key) == "User-Agent" {
setUserAgent = false
}
r.HttpRequest.Header.Set(key, el[1])
}
for _, v := range el[2:] {
r.HttpRequest.Header.Add(key, v)
}
}
} else if r.Header != nil && len(r.Header) > 0 {
r.HttpRequest.Header = r.Header
if r.HeaderOrder != nil && len(r.HeaderOrder) > 0 {
if v, ok := r.Header[http.HeaderOrderKey]; ok {
r.Header[http.HeaderOrderKey] = append(v, r.HeaderOrder...)
} else {
r.Header[http.HeaderOrderKey] = r.HeaderOrder
}
}
for k := range r.Header {
if http.CanonicalHeaderKey(k) == "User-Agent" {
setUserAgent = false
break
}
}
} else {
r.HttpRequest.Header = make(http.Header, 4)
}
if setUserAgent {
if r.ua == "" {
r.ua = defaultUserAgent
}
r.HttpRequest.Header.Set("User-Agent", r.ua)
}
if r.PHeader != nil {
for i, el := range r.PHeader {
if el[0] != ':' {
r.PHeader[i] = ":" + el
}
}
r.HttpRequest.Header[http.PHeaderOrderKey] = r.PHeader[:]
} else {
switch r.browser {
case Firefox:
r.HttpRequest.Header[http.PHeaderOrderKey] = []string{Method, Path, Authority, Scheme}
case Ios:
r.HttpRequest.Header[http.PHeaderOrderKey] = []string{Method, Scheme, Path, Authority}
default: //chrome sub products
r.HttpRequest.Header[http.PHeaderOrderKey] = GetDefaultPseudoHeaders()
}
}
}
func shouldCopyHeaderOnRedirect(headerKey string, initial, dest *url.URL) bool {
switch http.CanonicalHeaderKey(headerKey) {
case "Authorization", "Www-Authenticate", "Cookie", "Cookie2":
// Permit sending auth/cookie headers from "foo.com"
// to "sub.foo.com".
// Note that we don't send all cookies to subdomains
// automatically. This function is only used for
// Cookies set explicitly on the initial outgoing
// client request. Cookies automatically added via the
// CookieJar mechanism continue to follow each
// cookie's scope as set by Set-Cookie. But for
// outgoing requests with the Cookie header set
// directly, we don't know their scope, so we assume
// it's for *.domain.com.
ihost := getHost(initial)
dhost := getHost(dest)
return isDomainOrSubdomain(dhost, ihost)
}
// All other headers are copied:
return true
}
// makeHeadersCopier makes a function that copies headers from the
// initial Request, ireq. For every redirect, this function must be called
// so that it can copy headers into the upcoming Request.
func (s *Session) makeHeadersCopier(ireq *Request) func(*Request) {
// The headers to copy are from the very initial request.
// We use a closured callback to keep a reference to these original headers.
var (
ireqhdr = ireq.Header
icookies map[string][]*http.Cookie
)
var header = ireq.Header
if ireq.OrderedHeaders != nil {
header = ireq.OrderedHeaders.ToHeader()
}
if s.CookieJar != nil && header.Get("Cookie") != "" {
icookies = make(map[string][]*http.Cookie)
for _, c := range http.ReadCookies(ireq.Header, "") {
icookies[c.Name] = append(icookies[c.Name], c)
}
}
preq := ireq // The previous request
return func(req *Request) {
// If Jar is present and there was some initial cookies provided
// via the request header, then we may need to alter the initial
// cookies as we follow redirects since each redirect may end up
// modifying a pre-existing cookie.
//
// Since cookies already set in the request header do not contain
// information about the original domain and path, the logic below
// assumes any new set cookies override the original cookie
// regardless of domain or path.
//
// See https://golang.org/issue/17494
if s.CookieJar != nil && icookies != nil {
var changed bool
resp := req.Response // The response that caused the upcoming redirect
for k := range resp.Cookies {
if _, ok := icookies[k]; ok {
delete(icookies, k)
changed = true
}
}
if changed {
ireqhdr.Del("Cookie")
var ss []string
for _, cs := range icookies {
for _, c := range cs {
ss = append(ss, c.Name+"="+c.Value)
}
}
sort.Strings(ss) // Ensure deterministic headers
ireqhdr.Set("Cookie", strings.Join(ss, "; "))
}
}
// Copy the initial request's Header Values
// (at least the safe ones).
for k, vv := range ireqhdr {
if shouldCopyHeaderOnRedirect(k, preq.parsedUrl, req.parsedUrl) {
if req.OrderedHeaders != nil {
req.OrderedHeaders.Add(k, vv...)
} else {
if req.Header == nil {
req.Header = make(http.Header)
}
req.Header[k] = vv
}
}
}
preq = req // Update previous Request with the current request
}
}