@@ -69,35 +69,33 @@ func (dt *DigestTransport) RoundTrip(req *http.Request) (resp *http.Response, er
6969// Execute initialise the request and get a response
7070func (dr * DigestRequest ) Execute () (resp * http.Response , err error ) {
7171
72- if dr .Auth == nil {
73- var req * http.Request
74- if req , err = http .NewRequest (dr .Method , dr .Uri , bytes .NewReader ([]byte (dr .Body ))); err != nil {
75- return nil , err
76- }
77-
78- client := & http.Client {
79- Timeout : 30 * time .Second ,
80- }
81- resp , err = client .Do (req )
82-
83- if err != nil {
84- return nil , err
85- }
86- defer resp .Body .Close ()
87-
88- if resp .StatusCode == 401 {
89- return dr .executeNewDigest (resp )
90- }
91- return
72+ if dr .Auth != nil {
73+ return dr .executeExistingDigest ()
9274}
9375
94- return dr .executeExistingDigest ()
76+ var req * http.Request
77+ if req , err = http .NewRequest (dr .Method , dr .Uri , bytes .NewReader ([]byte (dr .Body ))); err != nil {
78+ return nil , err
79+ }
80+
81+ client := & http.Client {
82+ Timeout : 30 * time .Second ,
83+ }
84+ if resp , err = client .Do (req ); err != nil {
85+ return nil , err
86+ }
87+ defer resp .Body .Close ()
88+
89+ if resp .StatusCode == 401 {
90+ return dr .executeNewDigest (resp )
91+ }
92+
93+ return resp , nil
9594}
9695
97- func (dr * DigestRequest ) executeNewDigest (resp * http.Response ) (* http.Response , error ) {
96+ func (dr * DigestRequest ) executeNewDigest (resp * http.Response ) (resp2 * http.Response , err error ) {
9897var (
9998auth * authorization
100- err error
10199wa * wwwAuthenticate
102100waString string
103101)
@@ -111,45 +109,42 @@ func (dr *DigestRequest) executeNewDigest(resp *http.Response) (*http.Response,
111109if auth , err = newAuthorization (dr ); err != nil {
112110return nil , err
113111}
114- authString := auth .toString ()
115112
116- if resp , err = dr .executeRequest (authString ); err != nil {
113+ if resp2 , err = dr .executeRequest (auth . toString () ); err != nil {
117114return nil , err
118115}
119- defer resp .Body .Close ()
120116
121117dr .Auth = auth
122- return resp , nil
118+ return resp2 , nil
123119}
124120
125- func (dr * DigestRequest ) executeExistingDigest () (* http.Response , error ) {
126- var (
127- auth * authorization
128- err error
129- )
121+ func (dr * DigestRequest ) executeExistingDigest () (resp * http.Response , err error ) {
122+ var auth * authorization
130123
131124if auth , err = dr .Auth .refreshAuthorization (dr ); err != nil {
132125return nil , err
133126}
134-
135127dr .Auth = auth
136- authString := dr . Auth . toString ()
137- return dr .executeRequest (authString )
128+
129+ return dr .executeRequest (dr . Auth . toString () )
138130}
139131
140- func (dr * DigestRequest ) executeRequest (authString string ) (* http.Response , error ) {
141- var (
142- err error
143- req * http.Request
144- )
132+ func (dr * DigestRequest ) executeRequest (authString string ) (resp * http.Response , err error ) {
133+ var req * http.Request
145134
146135if req , err = http .NewRequest (dr .Method , dr .Uri , bytes .NewReader ([]byte (dr .Body ))); err != nil {
147136return nil , err
148137}
149138
150139req .Header .Add ("Authorization" , authString )
140+
151141client := & http.Client {
152142Timeout : 30 * time .Second ,
153143}
154- return client .Do (req )
144+ if resp , err = client .Do (req ); err != nil {
145+ return nil , err
146+ }
147+ defer resp .Body .Close ()
148+
149+ return resp , nil
155150}
0 commit comments