File tree 2 files changed +39
-8
lines changed
main/java/com/google/devtools/build/lib/bazel/repository/downloader
test/java/com/google/devtools/build/lib/bazel/repository/downloader
2 files changed +39
-8
lines changed Original file line number Diff line number Diff line change @@ -187,17 +187,24 @@ private ImmutableList<URL> applyRewriteRules(URL url) {
187
187
}
188
188
189
189
return rewrittenUrls .build ().stream ()
190
- .map (
191
- urlString -> {
192
- try {
193
- return new URL (url .getProtocol () + "://" + urlString );
194
- } catch (MalformedURLException e ) {
195
- throw new IllegalStateException (e );
196
- }
197
- })
190
+ .map (urlString -> prefixWithProtocol (urlString , url .getProtocol ()))
198
191
.collect (toImmutableList ());
199
192
}
200
193
194
+ /** Prefixes url with protocol if not already prefixed by {@link #REWRITABLE_SCHEMES} */
195
+ private static URL prefixWithProtocol (String url , String protocol ) {
196
+ try {
197
+ for (String schemaPrefix : REWRITABLE_SCHEMES ) {
198
+ if (url .startsWith (schemaPrefix + "://" )) {
199
+ return new URL (url );
200
+ }
201
+ }
202
+ return new URL (protocol + "://" + url );
203
+ } catch (MalformedURLException e ) {
204
+ throw new IllegalStateException (e );
205
+ }
206
+ }
207
+
201
208
@ Nullable
202
209
public String getAllBlockedMessage () {
203
210
return config .getAllBlockedMessage ();
Original file line number Diff line number Diff line change @@ -212,4 +212,28 @@ public void multipleAllBlockedMessage() throws Exception {
212
212
assertThat (e .getLocation ()).isEqualTo (Location .fromFileLineColumn ("/some/file" , 3 , 0 ));
213
213
}
214
214
}
215
+
216
+ @ Test
217
+ public void rewritingUrlsAllowsProtocolRewrite () throws Exception {
218
+ String config =
219
+ ""
220
+ + "block *\n "
221
+ + "allow mycorp.com\n "
222
+ + "allow othercorp.com\n "
223
+ + "rewrite bad.com/foo/(.*) http://mycorp.com/$1\n "
224
+ + "rewrite bad.com/bar/(.*) https://othercorp.com/bar/$1\n " ;
225
+
226
+ UrlRewriter munger = new UrlRewriter (str -> {}, "/dev/null" , new StringReader (config ));
227
+
228
+ List <URL > amended =
229
+ munger .amend (
230
+ ImmutableList .of (
231
+ new URL ("https://www.bad.com" ),
232
+ new URL ("https://bad.com/foo/bar" ),
233
+ new URL ("http://bad.com/bar/xyz" )));
234
+
235
+ assertThat (amended )
236
+ .containsExactly (
237
+ new URL ("http://mycorp.com/bar" ), new URL ("https://othercorp.com/bar/xyz" ));
238
+ }
215
239
}
You can’t perform that action at this time.
0 commit comments