2323import  build .bazel .remote .execution .v2 .RequestMetadata ;
2424import  com .google .common .annotations .VisibleForTesting ;
2525import  com .google .common .base .Strings ;
26+ import  com .google .common .collect .ImmutableSet ;
2627import  com .google .devtools .build .lib .bazel .repository .downloader .Checksum ;
2728import  com .google .devtools .build .lib .bazel .repository .downloader .Downloader ;
2829import  com .google .devtools .build .lib .bazel .repository .downloader .HashOutputStream ;
@@ -171,7 +172,7 @@ static FetchBlobRequest newFetchBlobRequest(
171172      requestBuilder .addQualifiers (
172173          Qualifier .newBuilder ()
173174              .setName (QUALIFIER_AUTH_HEADERS )
174-               .setValue (authHeadersJson (authHeaders ))
175+               .setValue (authHeadersJson (urls ,  authHeaders ))
175176              .build ());
176177    }
177178
@@ -197,15 +198,24 @@ private OutputStream newOutputStream(
197198    return  out ;
198199  }
199200
200-   private  static  String  authHeadersJson (Map <URI , Map <String , String >> authHeaders ) {
201+   private  static  String  authHeadersJson (
202+       List <URL > urls , Map <URI , Map <String , String >> authHeaders ) {
203+     ImmutableSet <String > hostSet  =
204+         urls .stream ().map (URL ::getHost ).collect (ImmutableSet .toImmutableSet ());
201205    Map <String , JsonObject > subObjects  = new  TreeMap <>();
202206    for  (Map .Entry <URI , Map <String , String >> entry  : authHeaders .entrySet ()) {
207+       URI  uri  = entry .getKey ();
208+       // Only add headers that are relevant to the hosts. 
209+       if  (!hostSet .contains (uri .getHost ())) {
210+         continue ;
211+       }
212+ 
203213      JsonObject  subObject  = new  JsonObject ();
204214      Map <String , String > orderedHeaders  = new  TreeMap <>(entry .getValue ());
205215      for  (Map .Entry <String , String > subEntry  : orderedHeaders .entrySet ()) {
206216        subObject .addProperty (subEntry .getKey (), subEntry .getValue ());
207217      }
208-       subObjects .put (entry . getKey () .toString (), subObject );
218+       subObjects .put (uri .toString (), subObject );
209219    }
210220
211221    JsonObject  authHeadersJson  = new  JsonObject ();
0 commit comments