Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

addons/game-day: follow a calendar link's redirects, up to https and never down to http

Justin Newman5141b2c113d8
diff --git a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Fetcher.java b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Fetcher.java
index a37308b1a..ac5fe2ae8 100644
--- a/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Fetcher.java
+++ b/addons/game-day/app/src/main/java/io/github/justinnewman/gameday/Fetcher.java
@@ -24,6 +24,7 @@ final class Fetcher {
static final int MAX_BYTES = 5 * 1024 * 1024;
static final long STALE_MS = 24 * 3_600_000L;
static final long RETRY_MS = 3_600_000L;
+ static final int MAX_REDIRECTS = 5;
private static final Instant ALL_FROM = Instant.EPOCH;
private static final Instant ALL_TO = Instant.parse("2200-01-01T00:00:00Z");
@@ -73,28 +74,60 @@ final class Fetcher {
/** Writes the body to {@code to}; returns an error to show, or null. */
private String download(String link, File to) throws IOException {
- HttpURLConnection conn = (HttpURLConnection) new URL(link).openConnection();
- try {
- conn.setConnectTimeout(connectMs);
- conn.setReadTimeout(readMs);
- conn.setInstanceFollowRedirects(true);
- conn.setRequestProperty("User-Agent", "GameDay/1 (Android TV)");
- int code = conn.getResponseCode();
- if (code != HttpURLConnection.HTTP_OK) return "Link refused (" + code + ")";
- long length = conn.getContentLengthLong();
- if (length > MAX_BYTES) return "Over 5 MB";
- try (InputStream in = conn.getInputStream(); OutputStream out = new FileOutputStream(to)) {
- byte[] buf = new byte[16 * 1024];
- long total = 0;
- for (int n = in.read(buf); n >= 0; n = in.read(buf)) {
- total += n;
- if (total > MAX_BYTES) return "Over 5 MB";
- out.write(buf, 0, n);
+ URL url = new URL(link);
+ for (int hop = 0; hop <= MAX_REDIRECTS; hop++) {
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ try {
+ conn.setConnectTimeout(connectMs);
+ conn.setReadTimeout(readMs);
+ conn.setInstanceFollowRedirects(false);
+ conn.setRequestProperty("User-Agent", "GameDay/1 (Android TV)");
+ int code = conn.getResponseCode();
+ if (code == 301 || code == 302 || code == 303 || code == 307 || code == 308) {
+ URL next = nextHop(url, conn.getHeaderField("Location"));
+ if (next == null) return "Link moved (" + code + ")";
+ url = next;
+ continue;
}
+ return body(conn, code, to);
+ } finally {
+ conn.disconnect();
+ }
+ }
+ return "Too many redirects";
+ }
+
+ /** Writes a 200's body to {@code to}; returns an error to show, or null. */
+ private static String body(HttpURLConnection conn, int code, File to) throws IOException {
+ if (code != HttpURLConnection.HTTP_OK) return "Link refused (" + code + ")";
+ if (conn.getContentLengthLong() > MAX_BYTES) return "Over 5 MB";
+ try (InputStream in = conn.getInputStream(); OutputStream out = new FileOutputStream(to)) {
+ byte[] buf = new byte[16 * 1024];
+ long total = 0;
+ for (int n = in.read(buf); n >= 0; n = in.read(buf)) {
+ total += n;
+ if (total > MAX_BYTES) return "Over 5 MB";
+ out.write(buf, 0, n);
}
+ }
+ return null;
+ }
+
+ /**
+ * Where a redirect leads, or null when it must not be followed. Android follows a redirect only within the
+ * same scheme, and most sites send http:// on to https://, so redirects are followed here: up to https from
+ * http, never down from https to http.
+ */
+ static URL nextHop(URL from, String location) {
+ if (location == null || location.trim().isEmpty()) return null;
+ try {
+ URL to = new URL(from, location.trim());
+ String scheme = to.getProtocol().toLowerCase(java.util.Locale.ROOT);
+ if (scheme.equals("https")) return to;
+ if (scheme.equals("http") && from.getProtocol().equalsIgnoreCase("http")) return to;
+ return null;
+ } catch (java.net.MalformedURLException bad) {
return null;
- } finally {
- conn.disconnect();
}
}
diff --git a/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/FetcherTest.java b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/FetcherTest.java
index 0ad07715e..91e6f3ebd 100644
--- a/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/FetcherTest.java
+++ b/addons/game-day/app/src/test/java/io/github/justinnewman/gameday/FetcherTest.java
@@ -41,10 +41,20 @@ public class FetcherTest {
while (!server.isClosed()) {
try (Socket s = server.accept()) {
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), StandardCharsets.ISO_8859_1));
+ String request = in.readLine();
for (String line = in.readLine(); line != null && !line.isEmpty(); line = in.readLine()) {
- // request line and headers: not needed
+ // headers: not needed
}
OutputStream o = s.getOutputStream();
+ // "/old" moved to "/cal.ics"; "/loop" moves to itself forever.
+ String path = request == null ? "" : request.split(" ")[1];
+ if (path.equals("/old") || path.equals("/loop")) {
+ String to = path.equals("/old") ? "/cal.ics" : "/loop";
+ o.write(("HTTP/1.0 301 Moved\r\nLocation: " + to + "\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
+ .getBytes(StandardCharsets.ISO_8859_1));
+ o.flush();
+ continue;
+ }
o.write(("HTTP/1.0 " + status + " X\r\nContent-Type: " + type + "\r\nContent-Length: " + body.length
+ "\r\nConnection: close\r\n\r\n").getBytes(StandardCharsets.ISO_8859_1));
o.write(body);
@@ -117,6 +127,33 @@ public class FetcherTest {
assertEquals("Over 5 MB", s.error);
}
+ private Calendar at(String path) {
+ String link = "http://127.0.0.1:" + server.getLocalPort() + path;
+ return new Calendar(Links.id(link), "Wild", link, Sport.byLabel("Hockey"), 12);
+ }
+
+ @Test public void redirectIsFollowed() throws Exception {
+ FetchStatus s = new Fetcher().fetch(at("/old"), tmp.newFolder(), null, NOW);
+ assertNull(s.error);
+ assertEquals(1, s.entries);
+ }
+
+ @Test public void redirectLoopGivesUp() throws Exception {
+ FetchStatus s = new Fetcher().fetch(at("/loop"), tmp.newFolder(), null, NOW);
+ assertEquals("Too many redirects", s.error);
+ }
+
+ @Test public void nextHopRules() throws Exception {
+ java.net.URL http = new java.net.URL("http://a.example/x");
+ java.net.URL https = new java.net.URL("https://a.example/x");
+ assertEquals("https://b.example/y", Fetcher.nextHop(http, "https://b.example/y").toString());
+ assertEquals("http://a.example/z", Fetcher.nextHop(http, "/z").toString());
+ assertEquals("https://a.example/z", Fetcher.nextHop(https, "/z").toString());
+ assertNull("never from https down to http", Fetcher.nextHop(https, "http://b.example/y"));
+ assertNull(Fetcher.nextHop(https, "ftp://b.example/y"));
+ assertNull(Fetcher.nextHop(https, null));
+ }
+
@Test public void noAnswerIsReported() throws Exception {
try (ServerSocket silent = new ServerSocket(0)) {
String link = "http://127.0.0.1:" + silent.getLocalPort() + "/cal.ics";