@@ -20,7 +20,6 @@ var mutex = &sync.Mutex{}
2020// ProxyRequestHandler intercepts requests to CodeArtifact and add the Authorization header + correct Host header
2121func ProxyRequestHandler (p * httputil.ReverseProxy ) func (http.ResponseWriter , * http.Request ) {
2222 return func (w http.ResponseWriter , r * http.Request ) {
23-
2423 mutex .Lock ()
2524 // Store the original host header for each request
2625 originalUrlResolver [r .RemoteAddr ] = r .URL
@@ -32,7 +31,6 @@ func ProxyRequestHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *ht
3231 } else {
3332 originalUrlResolver [r .RemoteAddr ].Scheme = "http"
3433 }
35- mutex .Unlock ()
3634
3735 // Override the Host header with the CodeArtifact Host
3836 u , _ := url .Parse (CodeArtifactAuthInfo .Url )
@@ -44,6 +42,7 @@ func ProxyRequestHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *ht
4442 log .Printf ("REQ: %s %s \" %s\" \" %s\" " , r .RemoteAddr , r .Method , r .URL .RequestURI (), r .UserAgent ())
4543
4644 log .Printf ("Sending request to %s%s" , strings .Trim (CodeArtifactAuthInfo .Url , "/" ), r .URL .RequestURI ())
45+ mutex .Unlock ()
4746
4847 p .ServeHTTP (w , r )
4948 }
@@ -59,10 +58,10 @@ func ProxyResponseHandler() func(*http.Response) error {
5958 mutex .Lock ()
6059 originalUrl := originalUrlResolver [r .Request .RemoteAddr ]
6160 delete (originalUrlResolver , r .Request .RemoteAddr )
62- mutex .Unlock ()
6361
6462 u , _ := url .Parse (CodeArtifactAuthInfo .Url )
6563 hostname := u .Host + ":443"
64+ mutex .Unlock ()
6665
6766 // Rewrite the 301 to point from CodeArtifact URL to the proxy instead..
6867 if r .StatusCode == 301 || r .StatusCode == 302 {
@@ -97,11 +96,13 @@ func ProxyResponseHandler() func(*http.Response) error {
9796 oldContentResponse , _ := ioutil .ReadAll (body )
9897 oldContentResponseStr := string (oldContentResponse )
9998
99+ mutex .Lock ()
100100 resolvedHostname := strings .Replace (CodeArtifactAuthInfo .Url , u .Host , hostname , - 1 )
101101 newUrl := fmt .Sprintf ("%s://%s/" , originalUrl .Scheme , originalUrl .Host )
102102
103103 newResponseContent := strings .Replace (oldContentResponseStr , resolvedHostname , newUrl , - 1 )
104104 newResponseContent = strings .Replace (newResponseContent , CodeArtifactAuthInfo .Url , newUrl , - 1 )
105+ mutex .Unlock ()
105106
106107 r .Body = ioutil .NopCloser (strings .NewReader (newResponseContent ))
107108 r .ContentLength = int64 (len (newResponseContent ))
0 commit comments