From d575022e7f98391bca61ec8e70d01b7b61d5efd5 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Thu, 22 Aug 2024 17:44:04 +0100 Subject: [PATCH] JAL-4449 Changed HttpUtils.followConnection(conn0) to return conn0 unused when there is not a redirect from http to https, otherwise to return a new unused connection directly to the new https URL --- src/jalview/util/HttpUtils.java | 71 +++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/src/jalview/util/HttpUtils.java b/src/jalview/util/HttpUtils.java index ec35948..1d9f8d0 100644 --- a/src/jalview/util/HttpUtils.java +++ b/src/jalview/util/HttpUtils.java @@ -30,6 +30,8 @@ import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; +import jalview.bin.Console; + public class HttpUtils { public final static String JALVIEWSCHEMEPREFIX = "jalview"; @@ -129,7 +131,9 @@ public class HttpUtils } /** - * wrapper to follow a URL connection ALLOWING redirects from http to https + * wrapper to return a new HttpURLConnection to a new URL when there is a + * redirect from http to https, otherwise return the unused original + * HttpURLConnection * * @param HttpURLConnection * conn0 @@ -139,34 +143,54 @@ public class HttpUtils throws IOException { URL url = conn0.getURL(); - if (url == null) + // we are only checking for a redirect from http to https otherwise the java + // connection will follow when called (if not unset) + if (url == null || !"http".equals(url.getProtocol()) + || !conn0.getInstanceFollowRedirects()) { - return null; + return conn0; } - HttpURLConnection conn = null; - int response = conn0.getResponseCode(); - boolean followed = false; - if (response >= 300 && response < 400 && conn0.getFollowRedirects()) + + // check the response code + HttpURLConnection checkConn = (HttpURLConnection) url.openConnection(); + httpURLConnectionCopyAttributes(conn0, checkConn); + + boolean redirectToHttps = false; + int response = checkConn.getResponseCode(); + checkConn.disconnect(); + if (response >= 300 && response < 400) { // we are only checking for a redirect from http to https - if ("http".equals(url.getProtocol())) + URL loc = new URL(conn0.getHeaderField("Location")); + if (loc != null && "https".equals(loc.getProtocol())) { - 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; - } + redirectToHttps = true; + url = loc; } } - return followed && conn != null ? conn : conn0; + + if (!redirectToHttps) + { + return conn0; + } + + // We want to return an HttpURLConnection to the new https URL that is + // unconnected in case further manipulation of the request is required + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + httpURLConnectionCopyAttributes(conn0, conn); + return conn; + } + + private static void httpURLConnectionCopyAttributes( + HttpURLConnection conn0, HttpURLConnection conn1) + throws ProtocolException + { + conn1.setRequestMethod(conn0.getRequestMethod()); + conn1.setDoInput(conn0.getDoInput()); + conn1.setUseCaches(conn0.getUseCaches()); + conn1.setConnectTimeout(conn0.getConnectTimeout()); + conn1.setReadTimeout(conn0.getReadTimeout()); + conn1.setInstanceFollowRedirects(conn0.getInstanceFollowRedirects()); } /** @@ -180,8 +204,11 @@ public class HttpUtils { if (url == null) { + Console.debug("HttpUtils.openConnection(url) called with null url"); return null; } + Console.debug("HttpUtils.openConnection(url) called with url=" + + url.toString()); URLConnection conn = null; String protocol = url.getProtocol(); if ("http".equals(protocol) || "https".equals(protocol)) -- 1.7.10.2