Passed
Push — master ( b9ce7a...fa5039 )
by Stefano
02:17
created

pkg/scan/client/client.go   A

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 59
dl 0
loc 89
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F client.NewClientFromConfig 0 74 15
1
package client
2
3
import (
4
	"context"
5
	"net"
6
	"net/http"
7
	"net/http/cookiejar"
8
	"net/url"
9
	"time"
10
11
	"github.com/pkg/errors"
12
	"github.com/stefanoj3/dirstalk/pkg/scan/client/cookie"
13
	"golang.org/x/net/proxy"
14
)
15
16
func NewClientFromConfig(
0 ignored issues
show
introduced by
exported function NewClientFromConfig should have comment or be unexported
Loading history...
17
	timeoutInMilliseconds int,
18
	socks5Url *url.URL,
19
	userAgent string,
20
	useCookieJar bool,
21
	cookies []*http.Cookie,
22
	headers map[string]string,
23
	shouldCacheRequests bool,
24
	u *url.URL,
25
) (*http.Client, error) {
26
	transport := http.Transport{
27
		MaxIdleConns:          100,
28
		IdleConnTimeout:       90 * time.Second,
29
		TLSHandshakeTimeout:   10 * time.Second,
30
		ExpectContinueTimeout: 1 * time.Second,
31
	}
32
33
	c := &http.Client{
34
		Timeout:   time.Millisecond * time.Duration(timeoutInMilliseconds),
35
		Transport: &transport,
36
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
37
			return http.ErrUseLastResponse
38
		},
39
	}
40
41
	if useCookieJar {
42
		jar, err := cookiejar.New(nil)
43
		if err != nil {
44
			return nil, errors.Wrap(err, "NewClientFromConfig: failed to create cookie jar")
45
		}
46
		c.Jar = jar
47
	}
48
49
	if c.Jar != nil {
50
		c.Jar.SetCookies(u, cookies)
51
	}
52
53
	if len(cookies) > 0 && c.Jar == nil {
54
		c.Jar = cookie.NewStatelessJar(cookies)
55
	}
56
57
	if socks5Url != nil {
58
		tbDialer, err := proxy.FromURL(socks5Url, proxy.Direct)
59
		if err != nil {
60
			return nil, errors.Wrap(err, "NewClientFromConfig: failed to create socks5 proxy")
61
		}
62
63
		transport.DialContext = func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
64
			return tbDialer.Dial(network, addr)
65
		}
66
	}
67
68
	var err error
69
70
	c.Transport, err = decorateTransportWithUserAgentDecorator(c.Transport, userAgent)
71
	if err != nil {
72
		return nil, errors.Wrap(err, "NewClientFromConfig: failed to decorate transport")
73
	}
74
75
	if len(headers) > 0 {
76
		c.Transport, err = decorateTransportWithHeadersDecorator(c.Transport, headers)
77
		if err != nil {
78
			return nil, errors.Wrap(err, "NewClientFromConfig: failed to decorate transport")
79
		}
80
	}
81
82
	if shouldCacheRequests {
83
		c.Transport, err = decorateTransportWithRequestCacheDecorator(c.Transport)
84
		if err != nil {
85
			return nil, errors.Wrap(err, "NewClientFromConfig: failed to decorate transport")
86
		}
87
	}
88
89
	return c, nil
90
}
91