14
14
15
15
package com .google .devtools .build .lib .bazel .repository .downloader ;
16
16
17
+ import com .google .auth .Credentials ;
17
18
import com .google .common .annotations .VisibleForTesting ;
18
19
import com .google .common .base .Function ;
19
20
import com .google .common .base .Optional ;
20
21
import com .google .common .base .Preconditions ;
21
22
import com .google .common .collect .ImmutableList ;
22
23
import com .google .common .collect .ImmutableMap ;
23
24
import com .google .devtools .build .lib .analysis .BlazeVersionInfo ;
25
+ import com .google .devtools .build .lib .authandtls .StaticCredentials ;
24
26
import com .google .devtools .build .lib .concurrent .ThreadSafety .ThreadSafe ;
25
27
import com .google .devtools .build .lib .events .Event ;
26
28
import com .google .devtools .build .lib .events .EventHandler ;
27
29
import java .io .IOException ;
28
30
import java .io .InputStream ;
29
31
import java .io .InterruptedIOException ;
30
- import java .net .URI ;
31
32
import java .net .URISyntaxException ;
32
33
import java .net .URL ;
33
34
import java .net .URLConnection ;
@@ -74,7 +75,7 @@ final class HttpConnectorMultiplexer {
74
75
}
75
76
76
77
public HttpStream connect (URL url , Optional <Checksum > checksum ) throws IOException {
77
- return connect (url , checksum , ImmutableMap . of () , Optional .absent ());
78
+ return connect (url , checksum , StaticCredentials . EMPTY , Optional .absent ());
78
79
}
79
80
80
81
/**
@@ -87,25 +88,22 @@ public HttpStream connect(URL url, Optional<Checksum> checksum) throws IOExcepti
87
88
*
88
89
* @param url the URL to conenct to. can be: file, http, or https
89
90
* @param checksum checksum lazily checked on entire payload, or empty to disable
90
- * @param authHeaders the authentication headers
91
+ * @param credentials the credentials
91
92
* @param type extension, e.g. "tar.gz" to force on downloaded filename, or empty to not do this
92
93
* @return an {@link InputStream} of response payload
93
94
* @throws IOException if all mirrors are down and contains suppressed exception of each attempt
94
95
* @throws InterruptedIOException if current thread is being cast into oblivion
95
96
* @throws IllegalArgumentException if {@code urls} is empty or has an unsupported protocol
96
97
*/
97
98
public HttpStream connect (
98
- URL url ,
99
- Optional <Checksum > checksum ,
100
- Map <URI , Map <String , List <String >>> authHeaders ,
101
- Optional <String > type )
99
+ URL url , Optional <Checksum > checksum , Credentials credentials , Optional <String > type )
102
100
throws IOException {
103
101
Preconditions .checkArgument (HttpUtils .isUrlSupportedByDownloader (url ));
104
102
if (Thread .interrupted ()) {
105
103
throw new InterruptedIOException ();
106
104
}
107
105
Function <URL , ImmutableMap <String , List <String >>> headerFunction =
108
- getHeaderFunction (REQUEST_HEADERS , authHeaders );
106
+ getHeaderFunction (REQUEST_HEADERS , credentials );
109
107
URLConnection connection = connector .connect (url , headerFunction );
110
108
return httpStreamFactory .create (
111
109
connection ,
@@ -127,21 +125,20 @@ public HttpStream connect(
127
125
128
126
@ VisibleForTesting
129
127
static Function <URL , ImmutableMap <String , List <String >>> getHeaderFunction (
130
- Map <String , List <String >> baseHeaders ,
131
- Map <URI , Map <String , List <String >>> additionalHeaders ) {
128
+ Map <String , List <String >> baseHeaders , Credentials credentials ) {
129
+ Preconditions .checkNotNull (baseHeaders );
130
+ Preconditions .checkNotNull (credentials );
131
+
132
132
return url -> {
133
- ImmutableMap <String , List <String >> headers = ImmutableMap . copyOf (baseHeaders );
133
+ Map <String , List <String >> headers = new HashMap <> (baseHeaders );
134
134
try {
135
- if (additionalHeaders .containsKey (url .toURI ())) {
136
- Map <String , List <String >> newHeaders = new HashMap <>(headers );
137
- newHeaders .putAll (additionalHeaders .get (url .toURI ()));
138
- headers = ImmutableMap .copyOf (newHeaders );
139
- }
140
- } catch (URISyntaxException e ) {
141
- // If we can't convert the URL to a URI (because it is syntactically malformed), still try
142
- // to do the connection, not adding authentication information as we cannot look it up.
135
+ headers .putAll (credentials .getRequestMetadata (url .toURI ()));
136
+ } catch (URISyntaxException | IOException e ) {
137
+ // If we can't convert the URL to a URI (because it is syntactically malformed), or fetching
138
+ // credentials fails for any other reason, still try to do the connection, not adding
139
+ // authentication information as we cannot look it up.
143
140
}
144
- return headers ;
141
+ return ImmutableMap . copyOf ( headers ) ;
145
142
};
146
143
}
147
144
}
0 commit comments