From 3399fbefedfcd12e834fd81a992a8d7536302287 Mon Sep 17 00:00:00 2001 From: Zkitefly Date: Fri, 23 Aug 2024 01:48:14 +0000 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=A4=B1=E8=B4=A5=E6=97=B6?= =?UTF-8?q?=E6=89=93=E5=8D=B0=E9=87=8D=E5=AE=9A=E5=90=91=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/tungsten/fclcore/task/FetchTask.java | 9 ++++++--- .../java/com/tungsten/fclcore/util/io/NetworkUtils.java | 9 ++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/FCLCore/src/main/java/com/tungsten/fclcore/task/FetchTask.java b/FCLCore/src/main/java/com/tungsten/fclcore/task/FetchTask.java index 366f363f..3abed86e 100644 --- a/FCLCore/src/main/java/com/tungsten/fclcore/task/FetchTask.java +++ b/FCLCore/src/main/java/com/tungsten/fclcore/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/FCLCore/src/main/java/com/tungsten/fclcore/util/io/NetworkUtils.java b/FCLCore/src/main/java/com/tungsten/fclcore/util/io/NetworkUtils.java index 850f40d8..9ba2cd53 100644 --- a/FCLCore/src/main/java/com/tungsten/fclcore/util/io/NetworkUtils.java +++ b/FCLCore/src/main/java/com/tungsten/fclcore/util/io/NetworkUtils.java @@ -144,6 +144,10 @@ 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. @@ -153,7 +157,7 @@ public final class NetworkUtils { * @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) { @@ -168,6 +172,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"); }