From bc1e082b561daee25c0b67b255ae77d2b0a83e3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sun, 10 Jan 2021 17:59:14 +0100 Subject: [PATCH] BHttpRequest: Treat incomplete downloads as error When BSocket::Read() returns 0, it was treated as a finished transfer. This is OK when we don't know the content length, but when we do, there is no reason not to bubble up the error. Return B_IO_ERROR in this case. Change-Id: I68801dbbb85bcfd2e7aa68fd6a9ded6304e82b19 Reviewed-on: https://review.haiku-os.org/c/haiku/+/3621 Reviewed-by: Adrien Destugues Reviewed-by: waddlesplash --- src/kits/network/libnetapi/HttpRequest.cpp | 14 +++++++++++++- src/kits/package/FetchFileJob.cpp | 2 -- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/kits/network/libnetapi/HttpRequest.cpp b/src/kits/network/libnetapi/HttpRequest.cpp index 21325f40ff..6eae914945 100644 --- a/src/kits/network/libnetapi/HttpRequest.cpp +++ b/src/kits/network/libnetapi/HttpRequest.cpp @@ -611,8 +611,20 @@ BHttpRequest::_MakeRequest() if (bytesRead < 0) { readError = bytesRead; break; - } else if (bytesRead == 0) + } else if (bytesRead == 0) { + // Check if we got the expected number of bytes. + // Exceptions: + // - If the content-length is not known (bytesTotal is 0), for + // example in the case of a chunked transfer, we can't know + // - If the request method is "HEAD" which explicitly asks the + // server to not send any data (only the headers) + if (bytesTotal > 0 && bytesReceived != bytesTotal + && fRequestMethod != B_HTTP_HEAD) { + readError = B_IO_ERROR; + break; + } receiveEnd = true; + } fInputBuffer.AppendData(chunk, bytesRead); } else diff --git a/src/kits/package/FetchFileJob.cpp b/src/kits/package/FetchFileJob.cpp index 50de8052ef..92ab42828f 100644 --- a/src/kits/package/FetchFileJob.cpp +++ b/src/kits/package/FetchFileJob.cpp @@ -101,7 +101,6 @@ FetchFileJob::Execute() } do { - printf("downloading %s\n", DownloadURL()); BUrlRequest* request = BUrlProtocolRoster::MakeRequest(DownloadURL(), this); if (request == NULL) @@ -112,7 +111,6 @@ FetchFileJob::Execute() BHttpRequest* http= dynamic_cast(request); if (http != NULL && fTargetFile.GetSize(¤tPosition) == B_OK && currentPosition > 0) { - printf("requesting range start %" B_PRIdOFF "\n", currentPosition); http->SetRangeStart(currentPosition); }