# HG changeset patch # User Bernhard Urban # Date 1412589750 -7200 # Node ID 3152f72f5cdae439bd5c70e843b36cbbb4db89c6 # Parent 98e60bdf6d0573063d69e34710ddb37b7d3ae7b4 mx: follow redirects in URLConnectionDownload util diff -r 98e60bdf6d05 -r 3152f72f5cda mxtool/URLConnectionDownload.java --- a/mxtool/URLConnectionDownload.java Mon Oct 06 11:51:32 2014 +0200 +++ b/mxtool/URLConnectionDownload.java Mon Oct 06 12:02:30 2014 +0200 @@ -81,29 +81,46 @@ for (String s : urls) { try { - System.err.println("Downloading " + s + " to " + path + proxyMsg); - URL url = new URL(s); - URLConnection conn = url.openConnection(); - // 10 second timeout to establish connection - conn.setConnectTimeout(10000); - InputStream in = conn.getInputStream(); - int size = conn.getContentLength(); - FileOutputStream out = new FileOutputStream(path); - int read = 0; - byte[] buf = new byte[8192]; - int n = 0; - while ((read = in.read(buf)) != -1) { - n += read; - if (verbose) { - long percent = ((long) n * 100 / size); - System.err.print("\r " + n + " bytes " + (size == -1 ? "" : " (" + percent + "%)")); + while (true) { + System.err.println("Downloading " + s + " to " + path + proxyMsg); + URL url = new URL(s); + URLConnection conn = url.openConnection(); + // 10 second timeout to establish connection + conn.setConnectTimeout(10000); + + if (conn instanceof HttpURLConnection) { + // HttpURLConnection per default follows redirections, + // but not if it changes the protocol (e.g. http -> + // https). While this is a sane default, in our + // situation it's okay to follow a protocol transition. + HttpURLConnection httpconn = (HttpURLConnection) conn; + switch (httpconn.getResponseCode()) { + case HttpURLConnection.HTTP_MOVED_PERM: + case HttpURLConnection.HTTP_MOVED_TEMP: + System.err.println("follow redirect..."); + s = httpconn.getHeaderField("Location"); + continue; + } } - out.write(buf, 0, read); + InputStream in = conn.getInputStream(); + int size = conn.getContentLength(); + FileOutputStream out = new FileOutputStream(path); + int read = 0; + byte[] buf = new byte[8192]; + int n = 0; + while ((read = in.read(buf)) != -1) { + n += read; + if (verbose) { + long percent = ((long) n * 100 / size); + System.err.print("\r " + n + " bytes " + (size == -1 ? "" : " (" + percent + "%)")); + } + out.write(buf, 0, read); + } + System.err.println(); + out.close(); + in.close(); + return; } - System.err.println(); - out.close(); - in.close(); - return; } catch (MalformedURLException e) { throw new Error("Error in URL " + s, e); } catch (IOException e) {