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.
This commit is contained in:
Gabriele M
2017-07-08 17:20:47 +02:00
parent 81229329f1
commit b4f0abd97d

View File

@@ -124,7 +124,7 @@ class OkHttpDownloadClient implements DownloadClient {
.url(mUrl) .url(mUrl)
.tag(DOWNLOAD_TAG) .tag(DOWNLOAD_TAG)
.build(); .build();
downloadFileInternalCommon(request, callback, progressListener); downloadFileInternalCommon(request, callback, progressListener, false);
} }
private void downloadFileResumeInternal(final DownloadClient.DownloadCallback callback, private void downloadFileResumeInternal(final DownloadClient.DownloadCallback callback,
@@ -135,16 +135,21 @@ class OkHttpDownloadClient implements DownloadClient {
long offset = mDestination.length(); long offset = mDestination.length();
requestBuilder.addHeader("Range", "bytes=" + offset + "-"); requestBuilder.addHeader("Range", "bytes=" + offset + "-");
final Request request = requestBuilder.build(); 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; return (statusCode / 100) == 2;
} }
private static boolean isPartialContentCode(int statusCode) {
return statusCode == 206;
}
private void downloadFileInternalCommon(final Request request, private void downloadFileInternalCommon(final Request request,
final DownloadClient.DownloadCallback callback, final DownloadClient.DownloadCallback callback,
final DownloadClient.ProgressListener progressListener) { final DownloadClient.ProgressListener progressListener,
final boolean resume) {
mClient.networkInterceptors().add(new Interceptor() { mClient.networkInterceptors().add(new Interceptor() {
@Override @Override
@@ -171,17 +176,16 @@ class OkHttpDownloadClient implements DownloadClient {
Log.d(TAG, "Downloading"); Log.d(TAG, "Downloading");
final ResponseBody body = response.body(); final ResponseBody body = response.body();
final boolean resume = response.code() == 206; if (resume && isPartialContentCode(response.code())) {
if (resume) {
mResumeOffset = mDestination.length(); mResumeOffset = mDestination.length();
Log.d(TAG, "The server fulfilled the partial content request"); 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()); Log.e(TAG, "The server replied with code " + response.code());
callback.onFailure(mCancelled); callback.onFailure(mCancelled);
try { try {
body.close(); body.close();
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Could not close reponse body", e); Log.e(TAG, "Could not close response body", e);
} }
return; return;
} }