/
cleanhttp.go
89 lines (69 loc) · 1.96 KB
/
cleanhttp.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
package cleanhttp
import (
"net/url"
"strings"
http "github.com/bogdanfinn/fhttp"
tls_client "github.com/bogdanfinn/tls-client"
)
// create http client and return *CleanHttp. Take *Config as params.
func NewCleanHTTPClient(config *Config) (*CleanHTTP, error) {
options := []tls_client.HttpClientOption{
tls_client.WithTimeoutSeconds(20),
tls_client.WithClientProfile(GetTLSProfile()),
tls_client.WithInsecureSkipVerify(),
tls_client.WithCookieJar(tls_client.NewCookieJar()),
}
if config.Proxy != "" {
options = append(options, tls_client.WithProxyUrl(config.Proxy))
}
client, err := tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
if err != nil {
return nil, err
}
return &CleanHTTP{
Config: config,
TLSClient: client,
Cookies: nil,
}, nil
}
// Do request and return *http.Response, Take RequestOption in params.
func (c *CleanHTTP) Do(request RequestOption) (*http.Response, error) {
if request.Header == nil {
request.Header = c.GetDefaultHeader()
}
/* Give http 400*/
/*
if request.Body != nil {
len, e := CalculateContentLength(request.Body)
if e == nil {
request.Header.Add("content-length", strconv.Itoa(int(len)))
}
}*/
req, err := http.NewRequest(request.Method, request.URL, request.Body)
if err != nil {
return nil, err
}
req.Header = request.Header
resp, err := c.TLSClient.Do(req)
if err != nil {
return nil, err
}
u, _ := url.Parse(request.URL) //nolint: errcheck
cooks := c.TLSClient.GetCookies(u)
c.Cookies = append(c.Cookies, cooks...)
return resp, nil
}
// Take all cookies from client and return them as a header format string.
func (c *CleanHTTP) FormatCookies() string { // fuck cookiejar ok ?
var builder strings.Builder
lastIndex := len(c.Cookies) - 1
for i, cookie := range c.Cookies {
builder.WriteString(cookie.Name)
builder.WriteString("=")
builder.WriteString(cookie.Value)
if i != lastIndex {
builder.WriteString("; ")
}
}
return builder.String()
}