Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.

Commit 41469e9

Browse files
authored
Merge pull request #13 from nickysemenza/allow-setting-custom-http-client
enable passing in a http client, for customizing TLS config and timeouts
2 parents a94f932 + ea9536d commit 41469e9

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

digest_auth_client.go

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import (
99
)
1010

1111
type DigestRequest struct {
12-
Body string
13-
Method string
14-
Password string
15-
URI string
16-
Username string
17-
Header http.Header
18-
Auth *authorization
19-
Wa *wwwAuthenticate
20-
CertVal bool
21-
Timeout time.Duration
12+
Body string
13+
Method string
14+
Password string
15+
URI string
16+
Username string
17+
Header http.Header
18+
Auth *authorization
19+
Wa *wwwAuthenticate
20+
CertVal bool
21+
HTTPClient *http.Client
2222
}
2323

2424
type DigestTransport struct {
@@ -45,6 +45,23 @@ func NewTransport(username, password string) DigestTransport {
4545
return dt
4646
}
4747

48+
func (dr *DigestRequest) getHTTPClient() *http.Client {
49+
if dr.HTTPClient != nil {
50+
return dr.HTTPClient
51+
}
52+
tlsConfig := tls.Config{}
53+
if !dr.CertVal {
54+
tlsConfig.InsecureSkipVerify = true
55+
}
56+
57+
return &http.Client{
58+
Timeout: 30 * time.Second,
59+
Transport: &http.Transport{
60+
TLSClientConfig: &tlsConfig,
61+
},
62+
}
63+
}
64+
4865
// UpdateRequest is called when you want to reuse an existing
4966
// DigestRequest connection with new request information
5067
func (dr *DigestRequest) UpdateRequest(username, password, method, uri, body string) *DigestRequest {
@@ -89,16 +106,7 @@ func (dr *DigestRequest) Execute() (resp *http.Response, err error) {
89106
}
90107
req.Header = dr.Header
91108

92-
client := &http.Client{
93-
Timeout: dr.Timeout,
94-
}
95-
96-
if !dr.CertVal {
97-
tr := &http.Transport{
98-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
99-
}
100-
client.Transport = tr
101-
}
109+
client := dr.getHTTPClient()
102110

103111
if resp, err = client.Do(req); err != nil {
104112
return nil, err
@@ -160,16 +168,6 @@ func (dr *DigestRequest) executeRequest(authString string) (resp *http.Response,
160168
req.Header = dr.Header
161169
req.Header.Add("Authorization", authString)
162170

163-
client := &http.Client{
164-
Timeout: dr.Timeout,
165-
}
166-
167-
if !dr.CertVal {
168-
tr := &http.Transport{
169-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
170-
}
171-
client.Transport = tr
172-
}
173-
171+
client := dr.getHTTPClient()
174172
return client.Do(req)
175173
}

0 commit comments

Comments
 (0)