From ea89e61e51ad45ccb1841b2c5c6a55b8abb77313 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Mon, 30 Mar 2015 16:45:37 -0400 Subject: [PATCH] follow more redirects --- core/src/processing/core/PApplet.java | 24 +++++++++++++++++------- core/todo.txt | 2 ++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 09b556204..25cb7aa49 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -6093,8 +6093,6 @@ public class PApplet implements PConstants { * Call openStream() without automatic gzip decompression. */ public InputStream createInputRaw(String filename) { - InputStream stream = null; - if (filename == null) return null; if (filename.length() == 0) { @@ -6103,19 +6101,29 @@ public class PApplet implements PConstants { return null; } - // safe to check for this as a url first. this will prevent online + // First check whether this looks like a URL. This will prevent online // access logs from being spammed with GET /sketchfolder/http://blahblah if (filename.contains(":")) { // at least smells like URL try { URL url = new URL(filename); - stream = url.openStream(); - return stream; + URLConnection conn = url.openConnection(); + HttpURLConnection httpConn = (HttpURLConnection) conn; + // Will not handle a protocol change (see below) + httpConn.setInstanceFollowRedirects(true); + int response = httpConn.getResponseCode(); + // Normally will not follow HTTPS redirects from HTTP due to security concerns + // http://stackoverflow.com/questions/1884230/java-doesnt-follow-redirect-in-urlconnection/1884427 + if (response >= 300 && response < 400) { + String newLocation = httpConn.getHeaderField("Location"); + return createInputRaw(newLocation); + } + return conn.getInputStream(); } catch (MalformedURLException mfue) { // not a url, that's fine } catch (FileNotFoundException fnfe) { - // Java 1.5 likes to throw this when URL not available. (fix for 0119) + // Added in 0119 b/c Java 1.5 throws FNFE when URL not available. // http://dev.processing.org/bugs/show_bug.cgi?id=403 } catch (IOException e) { @@ -6127,6 +6135,8 @@ public class PApplet implements PConstants { } } + InputStream stream = null; + // Moved this earlier than the getResourceAsStream() checks, because // calling getResourceAsStream() on a directory lists its contents. // http://dev.processing.org/bugs/show_bug.cgi?id=716 @@ -6157,7 +6167,7 @@ public class PApplet implements PConstants { throw new RuntimeException("This file is named " + filenameActual + " not " + filename + ". Rename the file " + - "or change your code."); + "or change your code."); } } catch (IOException e) { } } diff --git a/core/todo.txt b/core/todo.txt index d44b0991b..f025fd1e2 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -10,6 +10,8 @@ X https://github.com/processing/processing/issues/2919 X handled instead as an example X add warning message when a negative textSize() is used X https://github.com/processing/processing/issues/3110 +X loadXxxx() methods will truly follow redirects (including http -> https) +X https://github.com/processing/processing-docs/issues/218 head X Sketch window dimensions off in Java2D