execute status code check on HEAD

This commit is contained in:
Daniel Gultsch 2021-05-16 16:17:06 +02:00
parent 0f3181555a
commit b025265f91

View file

@ -318,6 +318,7 @@ public class HttpDownloadConnection implements Transferable {
mostRecentCall = client.newCall(request);
try {
final Response response = mostRecentCall.execute();
throwOnInvalidCode(response);
final String contentLength = response.header("Content-Length");
final String contentType = response.header("Content-Type");
final AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(mUrl.encodedPath());
@ -332,7 +333,11 @@ public class HttpDownloadConnection implements Transferable {
if (Strings.isNullOrEmpty(contentLength)) {
throw new IOException("no content-length found in HEAD response");
}
return Long.parseLong(contentLength, 10);
final long size = Long.parseLong(contentLength, 10);
if (size < 0) {
throw new IOException("Server reported negative file size");
}
return size;
} catch (IOException e) {
Log.d(Config.LOGTAG, "io exception during HEAD " + e.getMessage());
throw e;
@ -395,8 +400,7 @@ public class HttpDownloadConnection implements Transferable {
final Request request = requestBuilder.build();
mostRecentCall = client.newCall(request);
final Response response = mostRecentCall.execute();
final int code = response.code();
if (code >= 200 && code <= 299) {
throwOnInvalidCode(response);
final String contentRange = response.header("Content-Range");
final boolean serverResumed = tryResume && contentRange != null && contentRange.startsWith("bytes " + resumeSize + "-");
final InputStream inputStream = response.body().byteStream();
@ -431,9 +435,6 @@ public class HttpDownloadConnection implements Transferable {
updateProgress(Math.round(((double) transmitted / expected) * 100));
}
outputStream.flush();
} else {
throw new IOException(String.format(Locale.ENGLISH, "HTTP Status code was %d", code));
}
}
private void updateImageBounds() {
@ -451,4 +452,11 @@ public class HttpDownloadConnection implements Transferable {
}
}
private static void throwOnInvalidCode(final Response response) throws IOException {
final int code = response.code();
if (code < 200 || code >= 300) {
throw new IOException(String.format(Locale.ENGLISH, "HTTP Status code was %d", code));
}
}
}