]> Sergey Matveev's repositories - tofuproxy.git/commitdiff
Set BasicAuth before request
authorSergey Matveev <stargrave@stargrave.org>
Wed, 8 Sep 2021 19:39:02 +0000 (22:39 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 8 Sep 2021 19:39:02 +0000 (22:39 +0300)
trip.go

diff --git a/trip.go b/trip.go
index fa344457f97721c532c28c6c2b2ca15599dddd1e..a2e2565047626294254055d3ef3ab2f4a6a76391 100644 (file)
--- a/trip.go
+++ b/trip.go
@@ -73,6 +73,14 @@ func roundTrip(w http.ResponseWriter, req *http.Request) {
 
        reqFlags := []string{}
        unauthorized := false
+
+       authCacheM.Lock()
+       if creds, ok := authCache[req.URL.Host]; ok {
+               req.SetBasicAuth(creds[0], creds[1])
+               unauthorized = true
+       }
+       authCacheM.Unlock()
+
 Retry:
        resp, err := transport.RoundTrip(req)
        if err != nil {
@@ -88,11 +96,6 @@ Retry:
                        delete(authCache, req.URL.Host)
                } else {
                        unauthorized = true
-                       if creds, ok := authCache[req.URL.Host]; ok {
-                               authCacheM.Unlock()
-                               req.SetBasicAuth(creds[0], creds[1])
-                               goto Retry
-                       }
                }
                fifos.SinkOther <- fmt.Sprintf("%s\tauthorization required", req.URL.Host)
                user, pass, err := authDialog(host, resp.Header.Get("WWW-Authenticate"))