From 2a793fabff3fefc42e99a43a7a26eee2fb98badf Mon Sep 17 00:00:00 2001
From: Sergey Matveev <stargrave@stargrave.org>
Date: Wed, 8 Sep 2021 22:39:02 +0300
Subject: [PATCH] Set BasicAuth before request

---
 trip.go | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/trip.go b/trip.go
index fa34445..a2e2565 100644
--- 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"))
-- 
2.50.0