Skip to content

Commit c083846

Browse files
burningtntGlavo
andauthored
Close #2911: 下载失败时打印重定向链 (#2912)
* Support #2911. * update * update * update * update * update --------- Co-authored-by: Glavo <zjx001202@gmail.com>
1 parent 90cfe3d commit c083846

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

HMCLCore/src/main/java/org/jackhuang/hmcl/task/FetchTask.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public void execute() throws Exception {
9494
break download;
9595
}
9696

97+
List<String> redirects = null;
9798
try {
9899
beforeDownload(url);
99100

@@ -103,7 +104,9 @@ public void execute() throws Exception {
103104
if (checkETag) repository.injectConnection(conn);
104105

105106
if (conn instanceof HttpURLConnection) {
106-
conn = NetworkUtils.resolveConnection((HttpURLConnection) conn);
107+
redirects = new ArrayList<>();
108+
109+
conn = NetworkUtils.resolveConnection((HttpURLConnection) conn, redirects);
107110
int responseCode = ((HttpURLConnection) conn).getResponseCode();
108111

109112
if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
@@ -164,13 +167,13 @@ public void execute() throws Exception {
164167
} catch (FileNotFoundException ex) {
165168
failedURL = url;
166169
exception = ex;
167-
Logging.LOG.log(Level.WARNING, "Failed to download " + url + ", not found", ex);
170+
Logging.LOG.log(Level.WARNING, "Failed to download " + url + ", not found" + ((redirects == null || redirects.isEmpty()) ? "" : ", redirects: " + redirects), ex);
168171

169172
break; // we will not try this URL again
170173
} catch (IOException ex) {
171174
failedURL = url;
172175
exception = ex;
173-
Logging.LOG.log(Level.WARNING, "Failed to download " + url + ", repeat times: " + (++repeat), ex);
176+
Logging.LOG.log(Level.WARNING, "Failed to download " + url + ", repeat times: " + (++repeat) + ((redirects == null || redirects.isEmpty()) ? "" : ", redirects: " + redirects), ex);
174177
}
175178
}
176179
}

HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/NetworkUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static org.jackhuang.hmcl.util.StringUtils.*;
3030

3131
/**
32-
*
3332
* @author huangyuhui
3433
*/
3534
public final class NetworkUtils {
@@ -131,16 +130,20 @@ public static String encodeLocation(String location) {
131130
return sb.toString();
132131
}
133132

133+
public static HttpURLConnection resolveConnection(HttpURLConnection conn) throws IOException {
134+
return resolveConnection(conn, null);
135+
}
136+
134137
/**
135138
* This method is a work-around that aims to solve problem when "Location" in
136139
* stupid server's response is not encoded.
137-
*
140+
*
138141
* @see <a href="https://github.com/curl/curl/issues/473">Issue with libcurl</a>
139142
* @param conn the stupid http connection.
140143
* @return manually redirected http connection.
141144
* @throws IOException if an I/O error occurs.
142145
*/
143-
public static HttpURLConnection resolveConnection(HttpURLConnection conn) throws IOException {
146+
public static HttpURLConnection resolveConnection(HttpURLConnection conn, List<String> redirects) throws IOException {
144147
int redirect = 0;
145148
while (true) {
146149
conn.setUseCaches(false);
@@ -154,6 +157,9 @@ public static HttpURLConnection resolveConnection(HttpURLConnection conn) throws
154157
String newURL = conn.getHeaderField("Location");
155158
conn.disconnect();
156159

160+
if (redirects != null) {
161+
redirects.add(newURL);
162+
}
157163
if (redirect > 20) {
158164
throw new IOException("Too much redirects");
159165
}

0 commit comments

Comments
 (0)