From c0838468b1370d8a8b4cc63918acec46be66efd4 Mon Sep 17 00:00:00 2001 From: Burning_TNT <88144530+burningtnt@users.noreply.github.com> Date: Thu, 14 Mar 2024 13:50:25 +0800 Subject: [PATCH] =?UTF-8?q?Close=20#2911:=20=E4=B8=8B=E8=BD=BD=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=97=B6=E6=89=93=E5=8D=B0=E9=87=8D=E5=AE=9A=E5=90=91?= =?UTF-8?q?=E9=93=BE=20(#2912)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Support #2911. * update * update * update * update * update --------- Co-authored-by: Glavo --- .../main/java/org/jackhuang/hmcl/task/FetchTask.java | 9 ++++++--- .../org/jackhuang/hmcl/util/io/NetworkUtils.java | 12 +++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java index 669dd59f3..18e5855c8 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java @@ -94,6 +94,7 @@ public abstract class FetchTask extends Task { break download; } + List redirects = null; try { beforeDownload(url); @@ -103,7 +104,9 @@ public abstract class FetchTask extends Task { if (checkETag) repository.injectConnection(conn); if (conn instanceof HttpURLConnection) { - conn = NetworkUtils.resolveConnection((HttpURLConnection) conn); + redirects = new ArrayList<>(); + + conn = NetworkUtils.resolveConnection((HttpURLConnection) conn, redirects); int responseCode = ((HttpURLConnection) conn).getResponseCode(); if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) { @@ -164,13 +167,13 @@ public abstract class FetchTask extends Task { } catch (FileNotFoundException ex) { failedURL = url; exception = ex; - Logging.LOG.log(Level.WARNING, "Failed to download " + url + ", not found", ex); + Logging.LOG.log(Level.WARNING, "Failed to download " + url + ", not found" + ((redirects == null || redirects.isEmpty()) ? "" : ", redirects: " + redirects), ex); break; // we will not try this URL again } catch (IOException ex) { failedURL = url; exception = ex; - Logging.LOG.log(Level.WARNING, "Failed to download " + url + ", repeat times: " + (++repeat), ex); + Logging.LOG.log(Level.WARNING, "Failed to download " + url + ", repeat times: " + (++repeat) + ((redirects == null || redirects.isEmpty()) ? "" : ", redirects: " + redirects), ex); } } } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java index 5d4e51a6c..afd352dbe 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java @@ -29,7 +29,6 @@ import static org.jackhuang.hmcl.util.Pair.pair; import static org.jackhuang.hmcl.util.StringUtils.*; /** - * * @author huangyuhui */ public final class NetworkUtils { @@ -131,16 +130,20 @@ public final class NetworkUtils { return sb.toString(); } + public static HttpURLConnection resolveConnection(HttpURLConnection conn) throws IOException { + return resolveConnection(conn, null); + } + /** * This method is a work-around that aims to solve problem when "Location" in * stupid server's response is not encoded. - * + * * @see Issue with libcurl * @param conn the stupid http connection. * @return manually redirected http connection. * @throws IOException if an I/O error occurs. */ - public static HttpURLConnection resolveConnection(HttpURLConnection conn) throws IOException { + public static HttpURLConnection resolveConnection(HttpURLConnection conn, List redirects) throws IOException { int redirect = 0; while (true) { conn.setUseCaches(false); @@ -154,6 +157,9 @@ public final class NetworkUtils { String newURL = conn.getHeaderField("Location"); conn.disconnect(); + if (redirects != null) { + redirects.add(newURL); + } if (redirect > 20) { throw new IOException("Too much redirects"); }