forked from nkanaev/yarr
/
client.go
52 lines (46 loc) · 1 KB
/
client.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
package worker
import (
"net"
"net/http"
"time"
)
type Client struct {
httpClient *http.Client
userAgent string
}
func (c *Client) get(url string) (*http.Response, error) {
return c.getConditional(url, "", "")
}
func (c *Client) getConditional(url, lastModified, etag string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", c.userAgent)
if lastModified != "" {
req.Header.Set("If-Modified-Since", lastModified)
}
if etag != "" {
req.Header.Set("If-None-Match", etag)
}
return c.httpClient.Do(req)
}
var client *Client
func init() {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
}).DialContext,
DisableKeepAlives: true,
TLSHandshakeTimeout: time.Second * 10,
}
httpClient := &http.Client{
Timeout: time.Second * 30,
Transport: transport,
}
client = &Client{
httpClient: httpClient,
userAgent: "Yarr/1.0",
}
}