From b4f0abd97da0dfcc5c26b71fc66d9957633e44c8 Mon Sep 17 00:00:00 2001 From: Gabriele M Date: Sat, 8 Jul 2017 17:20:47 +0200 Subject: [PATCH] Cancel partial downloads if the server doesn't fulfil them If the server doesn't fulfil a partial content request, bail out. While at it, fix a typo. --- .../download/OkHttpDownloadClient.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/org/lineageos/updater/download/OkHttpDownloadClient.java b/src/org/lineageos/updater/download/OkHttpDownloadClient.java index 0e57da23..795535b1 100644 --- a/src/org/lineageos/updater/download/OkHttpDownloadClient.java +++ b/src/org/lineageos/updater/download/OkHttpDownloadClient.java @@ -124,7 +124,7 @@ class OkHttpDownloadClient implements DownloadClient { .url(mUrl) .tag(DOWNLOAD_TAG) .build(); - downloadFileInternalCommon(request, callback, progressListener); + downloadFileInternalCommon(request, callback, progressListener, false); } private void downloadFileResumeInternal(final DownloadClient.DownloadCallback callback, @@ -135,16 +135,21 @@ class OkHttpDownloadClient implements DownloadClient { long offset = mDestination.length(); requestBuilder.addHeader("Range", "bytes=" + offset + "-"); final Request request = requestBuilder.build(); - downloadFileInternalCommon(request, callback, progressListener); + downloadFileInternalCommon(request, callback, progressListener, true); } - private boolean isSuccessful(int statusCode) { + private static boolean isSuccessCode(int statusCode) { return (statusCode / 100) == 2; } + private static boolean isPartialContentCode(int statusCode) { + return statusCode == 206; + } + private void downloadFileInternalCommon(final Request request, final DownloadClient.DownloadCallback callback, - final DownloadClient.ProgressListener progressListener) { + final DownloadClient.ProgressListener progressListener, + final boolean resume) { mClient.networkInterceptors().add(new Interceptor() { @Override @@ -171,17 +176,16 @@ class OkHttpDownloadClient implements DownloadClient { Log.d(TAG, "Downloading"); final ResponseBody body = response.body(); - final boolean resume = response.code() == 206; - if (resume) { + if (resume && isPartialContentCode(response.code())) { mResumeOffset = mDestination.length(); Log.d(TAG, "The server fulfilled the partial content request"); - } else if (!isSuccessful(response.code())) { + } else if (resume || !isSuccessCode(response.code())) { Log.e(TAG, "The server replied with code " + response.code()); callback.onFailure(mCancelled); try { body.close(); } catch (IOException e) { - Log.e(TAG, "Could not close reponse body", e); + Log.e(TAG, "Could not close response body", e); } return; }