-
Notifications
You must be signed in to change notification settings - Fork 25
/
client.go
62 lines (49 loc) · 1.36 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
53
54
55
56
57
58
59
60
61
62
package http
//go:generate mockgen -destination=../http_mock/client_mock.go -package=http_mock . Client
import (
"errors"
"fmt"
"github.com/valyala/fasthttp"
"strings"
)
// ErrTimeout is returned when the crawling failed because of timeout issue
var ErrTimeout = errors.New("timeout has occurred")
// Client is an HTTP client
type Client interface {
// Get the corresponding URL
// this methods follows redirections
Get(URL string) (Response, error)
}
type client struct {
c *fasthttp.Client
}
// NewFastHTTPClient create a new Client using fasthttp.Client as backend
func NewFastHTTPClient(c *fasthttp.Client) Client {
return &client{c: c}
}
func (c *client) Get(URL string) (Response, error) {
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)
req.SetRequestURI(URL)
if err := c.c.Do(req, resp); err != nil {
// TODO better
if strings.Contains(err.Error(), "unknown error TTL expired") {
return nil, ErrTimeout
}
return nil, err
}
switch code := resp.StatusCode(); {
case code > 302:
return nil, fmt.Errorf("non-managed error code %d", code)
// follow redirect
case code == 301 || code == 302:
if location := string(resp.Header.Peek("Location")); location != "" {
return c.Get(location)
}
}
r := &response{}
resp.CopyTo(&r.raw)
return r, nil
}