Skip to content

Commit e8f7508

Browse files
Improve DownloadTask redirect handling. Added explicit redirect following for HTTP 301, 302, 303, 307, and 308 response codes. Implemented manual redirect following with recursion limit to ensure reliable downloads even when underlying HTTP connection implementation doesn't properly follow redirects. Added better error handling, logging, and resource cleanup.
1 parent 0505e28 commit e8f7508

File tree

2 files changed

+66
-180
lines changed

2 files changed

+66
-180
lines changed

RemixedDungeon/src/main/java/com/nyrds/util/DownloadTask.java

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
public class DownloadTask implements Runnable {
1414

15+
private static final int MAX_REDIRECTS = 5;
16+
1517
private final DownloadStateListener m_listener;
1618
private final String m_url;
1719
private final String m_downloadTo;
@@ -29,43 +31,73 @@ public void run() {
2931
m_listener.DownloadProgress(m_url, 0);
3032

3133
try {
32-
URL url = new URL(m_url);
33-
File file = new File(m_downloadTo);
34-
35-
HttpURLConnection ucon = HttpConnectionFactory.create(url);
36-
ucon.setReadTimeout(10000);
37-
ucon.setInstanceFollowRedirects(true);
38-
ucon.connect();
39-
40-
int repCode = ucon.getResponseCode();
41-
42-
if (repCode == HttpURLConnection.HTTP_OK) {
43-
m_listener.DownloadProgress(m_url, 0);
44-
int bytesTotal = ucon.getContentLength();
45-
46-
GLog.debug("bytes in file: " + bytesTotal);
47-
48-
try (InputStream is = ucon.getInputStream();
49-
FileOutputStream fos = new FileOutputStream(file)) {
50-
byte[] buffer = new byte[1024 * 128];
51-
int count;
52-
int bytesDownloaded = 0;
53-
54-
while ((count = is.read(buffer)) != -1) {
55-
fos.write(buffer, 0, count);
56-
bytesDownloaded += count;
57-
m_listener.DownloadProgress(m_url, bytesDownloaded);
58-
Thread.yield();
59-
}
60-
}
34+
result = downloadFile(m_url, m_downloadTo, 0);
35+
} catch (Exception e) {
36+
EventCollector.logException(new ModError("Downloading", e));
37+
}
6138

62-
result = true;
63-
}
39+
m_listener.DownloadComplete(m_url, result);
40+
}
6441

65-
} catch (Exception e) {
66-
EventCollector.logException(new ModError("Downloading",e));
42+
private boolean downloadFile(String urlStr, String downloadTo, int redirectCount) throws Exception {
43+
if (redirectCount > MAX_REDIRECTS) {
44+
GLog.debug("Too many redirects, aborting download");
45+
return false;
46+
}
47+
48+
URL url = new URL(urlStr);
49+
File file = new File(downloadTo);
50+
51+
HttpURLConnection ucon = HttpConnectionFactory.create(url);
52+
ucon.setReadTimeout(10000);
53+
ucon.setInstanceFollowRedirects(true);
54+
ucon.setConnectTimeout(10000);
55+
ucon.connect();
56+
57+
int repCode = ucon.getResponseCode();
58+
GLog.debug("HTTP response code: " + repCode + " for URL: " + urlStr);
59+
60+
// Handle redirects manually if needed
61+
if (repCode == HttpURLConnection.HTTP_MOVED_PERM ||
62+
repCode == HttpURLConnection.HTTP_MOVED_TEMP ||
63+
repCode == HttpURLConnection.HTTP_SEE_OTHER ||
64+
repCode == 307 || // TEMPORARY_REDIRECT
65+
repCode == 308) { // PERMANENT_REDIRECT
66+
67+
String redirectUrl = ucon.getHeaderField("Location");
68+
if (redirectUrl != null && !redirectUrl.isEmpty()) {
69+
GLog.debug("Following redirect to: " + redirectUrl);
70+
ucon.disconnect();
71+
return downloadFile(redirectUrl, downloadTo, redirectCount + 1);
72+
}
6773
}
6874

69-
m_listener.DownloadComplete(m_url, result);
75+
if (repCode == HttpURLConnection.HTTP_OK) {
76+
m_listener.DownloadProgress(m_url, 0);
77+
int bytesTotal = ucon.getContentLength();
78+
79+
GLog.debug("bytes in file: " + bytesTotal);
80+
81+
try (InputStream is = ucon.getInputStream();
82+
FileOutputStream fos = new FileOutputStream(file)) {
83+
byte[] buffer = new byte[1024 * 128];
84+
int count;
85+
int bytesDownloaded = 0;
86+
87+
while ((count = is.read(buffer)) != -1) {
88+
fos.write(buffer, 0, count);
89+
bytesDownloaded += count;
90+
m_listener.DownloadProgress(m_url, bytesDownloaded);
91+
Thread.yield();
92+
}
93+
}
94+
95+
ucon.disconnect();
96+
return true;
97+
} else {
98+
GLog.debug("Download failed with HTTP response code: " + repCode);
99+
ucon.disconnect();
100+
return false;
101+
}
70102
}
71103
}

tools/mods/mods2.json

Lines changed: 0 additions & 146 deletions
This file was deleted.

0 commit comments

Comments
 (0)