-
Notifications
You must be signed in to change notification settings - Fork 4
/
transport.go
61 lines (49 loc) · 1.43 KB
/
transport.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
package warc
import (
"crypto/tls"
"net/http"
"time"
gzip "github.com/klauspost/compress/gzip"
)
type customTransport struct {
t http.Transport
decompressBody bool
}
func (t *customTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
req = req.Clone(req.Context())
req.Header.Set("Accept-Encoding", "gzip")
resp, err = t.t.RoundTrip(req)
if err != nil {
return resp, err
}
// if the client have been created with decompressBody = true,
// we decompress the resp.Body if we received a compressed body
if t.decompressBody {
switch resp.Header.Get("Content-Encoding") {
case "gzip":
resp.Body, err = gzip.NewReader(resp.Body)
}
}
return
}
func newCustomTransport(dialer *customDialer, decompressBody bool, TLSHandshakeTimeout time.Duration) (t *customTransport, err error) {
t = new(customTransport)
t.t = http.Transport{
// configure HTTP transport
Dial: dialer.CustomDial,
DialTLS: dialer.CustomDialTLS,
// disable keep alive
MaxConnsPerHost: 0,
IdleConnTimeout: -1,
TLSHandshakeTimeout: TLSHandshakeTimeout,
ExpectContinueTimeout: 5 * time.Second,
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
DisableCompression: true,
ForceAttemptHTTP2: false,
MaxIdleConns: -1,
MaxIdleConnsPerHost: -1,
DisableKeepAlives: true,
}
t.decompressBody = decompressBody
return t, nil
}