-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
basicauth_client.go
92 lines (75 loc) · 2.05 KB
/
basicauth_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
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
package crawler
import (
"net/http"
"net/url"
"time"
)
const (
// HTTP client timeout in seconds.
clientTimeOut = 10
)
type BasicAuthClient struct {
Options *ClientOptions
client *http.Client
}
type ClientOptions struct {
UserAgent string
BasicAuth bool
BasicAuthDomains []string
AuthUser string
AuthPass string
}
func NewClient(options *ClientOptions) *BasicAuthClient {
httpClient := &http.Client{
Timeout: clientTimeOut * time.Second,
CheckRedirect: func(r *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
return &BasicAuthClient{
client: httpClient,
Options: options,
}
}
// Makes a request with the specified method.
func (c *BasicAuthClient) request(method, u string) (*http.Response, error) {
req, err := http.NewRequest(method, u, nil)
if err != nil {
return &http.Response{}, err
}
domain, err := url.Parse(u)
if err != nil {
return &http.Response{}, err
}
if c.Options.BasicAuth && c.isBasicAuthDomain(domain.Host) {
req.SetBasicAuth(c.Options.AuthUser, c.Options.AuthPass)
}
return c.Do(req)
}
// Returns true if the domain exists in the BasicAutDomains slice.
func (c *BasicAuthClient) isBasicAuthDomain(domain string) bool {
for _, element := range c.Options.BasicAuthDomains {
if element == domain {
return true
}
}
return false
}
// Makes a GET request to an URL and returns the http response or an error.
func (c *BasicAuthClient) Get(u string) (*http.Response, error) {
return c.request(http.MethodGet, u)
}
// Makes a HEAD request to an URL and returns the http response or an error.
func (c *BasicAuthClient) Head(u string) (*http.Response, error) {
return c.request(http.MethodHead, u)
}
// Does a request and returns its response and error.
// It sets the client's User-Agent as well as the BasicAuth details if they are available.
func (c *BasicAuthClient) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", c.Options.UserAgent)
resp, err := c.client.Do(req)
if err != nil {
return resp, err
}
return resp, nil
}