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 <[email protected]>
Reviewed-by: Adrien Destugues <[email protected]>
This commit is contained in:
Adrien Destugues
2021-07-16 18:33:35 +00:00
committed by Adrien Destugues
parent 2641b03c91
commit fa1e07fa7a
+13 -1
View File
@@ -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;