From 3db7223d52500057751bcff180b4ee88d30f1380 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Mon, 13 May 2024 17:12:49 +0100 Subject: [PATCH] JAL-1054 Fix wrappers for following http->https redirects --- src/jalview/bin/Cache.java | 2 +- src/jalview/util/HttpUtils.java | 86 ++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/jalview/bin/Cache.java b/src/jalview/bin/Cache.java index 136e2f7..48a0a58 100755 --- a/src/jalview/bin/Cache.java +++ b/src/jalview/bin/Cache.java @@ -498,7 +498,7 @@ public class Cache if (authorDetails != null) { URL localJarFileURL = new URL(authorDetails); - InputStream in = HttpUtils.openStream(localJarFileURL); + InputStream in = localJarFileURL.openStream(); applicationProperties.load(in); in.close(); } diff --git a/src/jalview/util/HttpUtils.java b/src/jalview/util/HttpUtils.java index 6ecfab2..9fef54e 100644 --- a/src/jalview/util/HttpUtils.java +++ b/src/jalview/util/HttpUtils.java @@ -105,71 +105,72 @@ public class HttpUtils /** * wrapper to follow a URL connection ALLOWING redirects from http to https * - * @param URL - * url + * @param HttpURLConnection + * conn0 * @return HttpUrlConnection conn */ - public static HttpURLConnection openConnection(URL url) throws IOException + public static HttpURLConnection followConnection(HttpURLConnection conn0) + throws IOException { + URL url = conn0.getURL(); if (url == null) { return null; } HttpURLConnection conn = null; - if (url != null && url.getProtocol().startsWith("http")) + int response = conn0.getResponseCode(); + boolean followed = false; + if (response >= 300 && response < 400 && conn0.getFollowRedirects()) { - HttpURLConnection conn0 = (HttpURLConnection) url.openConnection(); - if (conn0 != null) - { - conn = HttpUtils.followConnection(conn0); - } - else + // we are only checking for a redirect from http to https + if ("http".equals(url.getProtocol())) { - conn = conn0; + URL loc = new URL(conn0.getHeaderField("Location")); + if (loc != null && "https".equals(loc.getProtocol())) + { + conn = (HttpURLConnection) loc.openConnection(); + conn.setRequestMethod(conn0.getRequestMethod()); + conn.setDoInput(conn0.getDoInput()); + conn.setUseCaches(conn0.getUseCaches()); + conn.setConnectTimeout(conn0.getConnectTimeout()); + conn.setReadTimeout(conn0.getReadTimeout()); + conn.setInstanceFollowRedirects( + conn0.getInstanceFollowRedirects()); + followed = true; + } } } - return conn; + return followed && conn != null ? conn : conn0; } /** * wrapper to follow a URL connection ALLOWING redirects from http to https * - * @param HttpURLConnection - * conn0 + * @param URL + * url * @return HttpUrlConnection conn */ - public static HttpURLConnection followConnection(HttpURLConnection conn0) - throws IOException + public static HttpURLConnection openConnection(URL url) throws IOException { - HttpURLConnection newConn = null; - URL url = conn0.getURL(); if (url == null) { return null; } - int response = conn0.getResponseCode(); - boolean followed = false; - if (response >= 300 && response < 400 && conn0.getFollowRedirects()) + HttpURLConnection conn = null; + String protocol = url.getProtocol(); + if ("http".equals(protocol) || "https".equals(protocol)) { - // we are only checking for a redirect from http to https - if ("http".equals(url.getProtocol())) + HttpURLConnection conn0 = (HttpURLConnection) url.openConnection(); + if (conn0 != null) { - URL loc = new URL(conn0.getHeaderField("Location")); - if (loc != null && "https".equals(loc.getProtocol())) - { - newConn = (HttpURLConnection) loc.openConnection(); - newConn.setRequestMethod(conn0.getRequestMethod()); - newConn.setDoInput(conn0.getDoInput()); - newConn.setUseCaches(conn0.getUseCaches()); - newConn.setConnectTimeout(conn0.getConnectTimeout()); - newConn.setReadTimeout(conn0.getReadTimeout()); - newConn.setInstanceFollowRedirects( - conn0.getInstanceFollowRedirects()); - followed = true; - } + conn = HttpUtils.followConnection(conn0); + } + else + { + conn = conn0; } } - return followed ? newConn : conn0; + return conn; } /** @@ -182,8 +183,13 @@ public class HttpUtils */ public static InputStream openStream(URL url) throws IOException { + if (url == null) + { + return null; + } InputStream is = null; - if (url != null && url.getProtocol().startsWith("http")) + String protocol = url.getProtocol(); + if ("http".equals(protocol) || "https".equals(protocol)) { HttpURLConnection conn = HttpUtils .followConnection((HttpURLConnection) url.openConnection()); @@ -192,6 +198,10 @@ public class HttpUtils is = conn.getInputStream(); } } + else + { + is = url.openStream(); + } return is; } -- 1.7.10.2