Skip to content

Commit

Permalink
Allow UrlRewriter to change protocol, i.e. https->http, and http->https
Browse files Browse the repository at this point in the history
Fixes #13114

Closes #13115.

PiperOrigin-RevId: 360172767
  • Loading branch information
Denys Kurylenko authored and copybara-github committed Mar 1, 2021
1 parent 1e258d2 commit 495ac92
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,24 @@ private ImmutableList<URL> applyRewriteRules(URL url) {
}

return rewrittenUrls.build().stream()
.map(
urlString -> {
try {
return new URL(url.getProtocol() + "://" + urlString);
} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}
})
.map(urlString -> prefixWithProtocol(urlString, url.getProtocol()))
.collect(toImmutableList());
}

/** Prefixes url with protocol if not already prefixed by {@link #REWRITABLE_SCHEMES} */
private static URL prefixWithProtocol(String url, String protocol) {
try {
for (String schemaPrefix : REWRITABLE_SCHEMES) {
if (url.startsWith(schemaPrefix + "://")) {
return new URL(url);
}
}
return new URL(protocol + "://" + url);
} catch (MalformedURLException e) {
throw new IllegalStateException(e);
}
}

@Nullable
public String getAllBlockedMessage() {
return config.getAllBlockedMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,28 @@ public void multipleAllBlockedMessage() throws Exception {
assertThat(e.getLocation()).isEqualTo(Location.fromFileLineColumn("/some/file", 3, 0));
}
}

@Test
public void rewritingUrlsAllowsProtocolRewrite() throws Exception {
String config =
""
+ "block *\n"
+ "allow mycorp.com\n"
+ "allow othercorp.com\n"
+ "rewrite bad.com/foo/(.*) http://mycorp.com/$1\n"
+ "rewrite bad.com/bar/(.*) https://othercorp.com/bar/$1\n";

UrlRewriter munger = new UrlRewriter(str -> {}, "/dev/null", new StringReader(config));

List<URL> amended =
munger.amend(
ImmutableList.of(
new URL("https://www.bad.com"),
new URL("https://bad.com/foo/bar"),
new URL("http://bad.com/bar/xyz")));

assertThat(amended)
.containsExactly(
new URL("http://mycorp.com/bar"), new URL("https://othercorp.com/bar/xyz"));
}
}

0 comments on commit 495ac92

Please sign in to comment.