Close #2911: 下载失败时打印重定向链 (#2912)

* Support #2911.

* update

* update

* update

* update

* update

---------

Co-authored-by: Glavo <zjx001202@gmail.com>
This commit is contained in:
Burning_TNT 2024-03-14 13:50:25 +08:00 committed by GitHub
parent 90cfe3d0b7
commit c0838468b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 6 deletions

View File

@ -94,6 +94,7 @@ public abstract class FetchTask<T> extends Task<T> {
break download;
}
List<String> redirects = null;
try {
beforeDownload(url);
@ -103,7 +104,9 @@ public abstract class FetchTask<T> extends Task<T> {
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<T> extends Task<T> {
} 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);
}
}
}

View File

@ -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 <a href="https://github.com/curl/curl/issues/473">Issue with libcurl</a>
* @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<String> 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");
}