From fa1e07fa7a774d0504c21b37ee051e90e1816747 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Wed, 14 Jul 2021 20:20:15 +0200 Subject: [PATCH] Package kit: fix error handling in case of HTTP errors HTTP errors can have some content sent with them (a message to display to the user, usually). The package kit would not ignore this and append the content to the end of the downloaded file. This could result in a file larger than the expected size, and in that case, it would keep growing infinitely by adding more error messages to it. Also add some more specific error messages for some HTTP codes, in particular, "invalid range" which is likely to happen if something goes wrong with range requests. Now this case will be detected and the download will stop. Change-Id: I18927f361235e9f72a5701c1bd7977abda9e21ad Reviewed-on: https://review.haiku-os.org/c/haiku/+/4210 Tested-by: Commit checker robot Reviewed-by: Adrien Destugues --- src/kits/package/FetchFileJob.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/kits/package/FetchFileJob.cpp b/src/kits/package/FetchFileJob.cpp index 389bc851b0..b5aa44c171 100644 --- a/src/kits/package/FetchFileJob.cpp +++ b/src/kits/package/FetchFileJob.cpp @@ -118,6 +118,13 @@ FetchFileJob::Execute() thread_id thread = request->Run(); wait_for_thread(thread, NULL); + + if (fError != B_IO_ERROR && fError != B_DEV_TIMEOUT && fError != B_OK) { + // Something went wrong with the download and it's not just a + // timeout. Remove whatever we wrote to the file, since the content + // returned by the server was probably not part of the file. + fTargetFile.SetSize(currentPosition); + } } while (fError == B_IO_ERROR || fError == B_DEV_TIMEOUT); if (fError == B_OK) { @@ -176,9 +183,14 @@ FetchFileJob::RequestCompleted(BUrlRequest* request, bool success) fError = B_DEV_TIMEOUT; break; case B_HTTP_STATUS_NOT_IMPLEMENTED: - case B_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE: fError = B_NOT_SUPPORTED; break; + case B_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE: + fError = B_UNKNOWN_MIME_TYPE; + break; + case B_HTTP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE: + fError = B_RESULT_NOT_REPRESENTABLE; // alias for ERANGE + break; case B_HTTP_STATUS_UNAUTHORIZED: fError = B_PERMISSION_DENIED; break;