Previously, BUrlRequest returns data received via a callback that can't return any value. This approach have several issues: - It's not possible to signify failures to the request. - Users have to implement custom listeners just to handle the common case of outputting to a buffer/file/etc. - The received data has to be serialized into BMessage when BUrlProtocolDispatchingListener is employed. This can cause a noticible slowdown in real-world scenarios as evident by #10748. With this change, BUrlRequest will output directly into a BDataIO, which exposes a richer API for request handlers to work with (for example a BitTorrent client can request a BPositionIO for non-linear data delivery), as well as simplifying common cases for users. The adaptation only requires one additional API: BHttpRequest::SetStopOnError(). This API simply instructs the HTTP request handler to cancel the request if an HTTP error is occurred. Change-Id: I4160884d77bff0e7678e0a623e2587987704443a Reviewed-on: https://review.haiku-os.org/c/haiku/+/3084 Reviewed-by: Adrien Destugues <[email protected]>
266 lines
4.5 KiB
C++
266 lines
4.5 KiB
C++
/*
|
|
* Copyright 2011-2021, Haiku, Inc. All Rights Reserved.
|
|
* Distributed under the terms of the MIT License.
|
|
*
|
|
* Authors:
|
|
* Axel Dörfler <[email protected]>
|
|
* Rene Gollent <[email protected]>
|
|
* Oliver Tappe <[email protected]>
|
|
* Stephan Aßmus <[email protected]>
|
|
*/
|
|
|
|
|
|
#include "FetchFileJob.h"
|
|
|
|
#include <stdio.h>
|
|
#include <sys/wait.h>
|
|
|
|
#include <Path.h>
|
|
|
|
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
|
|
# include <HttpRequest.h>
|
|
# include <UrlRequest.h>
|
|
# include <UrlProtocolRoster.h>
|
|
using namespace BPrivate::Network;
|
|
#endif
|
|
|
|
#include "FetchUtils.h"
|
|
|
|
|
|
namespace BPackageKit {
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
|
|
|
|
FetchFileJob::FetchFileJob(const BContext& context, const BString& title,
|
|
const BString& fileURL, const BEntry& targetEntry)
|
|
:
|
|
inherited(context, title),
|
|
fFileURL(fileURL),
|
|
fTargetEntry(targetEntry),
|
|
fTargetFile(&targetEntry, B_CREATE_FILE | B_WRITE_ONLY),
|
|
fError(B_ERROR),
|
|
fDownloadProgress(0.0)
|
|
{
|
|
}
|
|
|
|
|
|
FetchFileJob::~FetchFileJob()
|
|
{
|
|
}
|
|
|
|
|
|
float
|
|
FetchFileJob::DownloadProgress() const
|
|
{
|
|
return fDownloadProgress;
|
|
}
|
|
|
|
|
|
const char*
|
|
FetchFileJob::DownloadURL() const
|
|
{
|
|
return fFileURL.String();
|
|
}
|
|
|
|
|
|
const char*
|
|
FetchFileJob::DownloadFileName() const
|
|
{
|
|
return fTargetEntry.Name();
|
|
}
|
|
|
|
|
|
off_t
|
|
FetchFileJob::DownloadBytes() const
|
|
{
|
|
return fBytes;
|
|
}
|
|
|
|
|
|
off_t
|
|
FetchFileJob::DownloadTotalBytes() const
|
|
{
|
|
return fTotalBytes;
|
|
}
|
|
|
|
|
|
status_t
|
|
FetchFileJob::Execute()
|
|
{
|
|
status_t result = fTargetFile.InitCheck();
|
|
if (result != B_OK)
|
|
return result;
|
|
|
|
BUrlRequest* request = BUrlProtocolRoster::MakeRequest(fFileURL.String(),
|
|
&fTargetFile, this);
|
|
if (request == NULL)
|
|
return B_BAD_VALUE;
|
|
|
|
if (fError == B_OK) {
|
|
result = FetchUtils::MarkDownloadComplete(fTargetFile);
|
|
if (result != B_OK) {
|
|
fprintf(stderr, "failed to mark download '%s' as complete: %s\n",
|
|
DownloadFileName(), strerror(result));
|
|
}
|
|
}
|
|
|
|
return fError;
|
|
}
|
|
|
|
|
|
void
|
|
FetchFileJob::DownloadProgress(BUrlRequest*, off_t bytesReceived,
|
|
off_t bytesTotal)
|
|
{
|
|
if (bytesTotal != 0) {
|
|
fBytes = bytesReceived;
|
|
fTotalBytes = bytesTotal;
|
|
fDownloadProgress = (float)bytesReceived/bytesTotal;
|
|
NotifyStateListeners();
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
FetchFileJob::RequestCompleted(BUrlRequest* request, bool success)
|
|
{
|
|
fError = request->Status();
|
|
|
|
if (success) {
|
|
const BHttpResult* httpResult = dynamic_cast<const BHttpResult*>
|
|
(&request->Result());
|
|
if (httpResult != NULL) {
|
|
uint16 code = httpResult->StatusCode();
|
|
uint16 codeClass = BHttpRequest::StatusCodeClass(code);
|
|
|
|
switch (codeClass) {
|
|
case B_HTTP_STATUS_CLASS_CLIENT_ERROR:
|
|
case B_HTTP_STATUS_CLASS_SERVER_ERROR:
|
|
fError = B_IO_ERROR;
|
|
break;
|
|
default:
|
|
fError = B_OK;
|
|
break;
|
|
}
|
|
switch (code) {
|
|
case B_HTTP_STATUS_OK:
|
|
case B_HTTP_STATUS_PARTIAL_CONTENT:
|
|
fError = B_OK;
|
|
break;
|
|
case B_HTTP_STATUS_REQUEST_TIMEOUT:
|
|
case B_HTTP_STATUS_GATEWAY_TIMEOUT:
|
|
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_UNAUTHORIZED:
|
|
fError = B_PERMISSION_DENIED;
|
|
break;
|
|
case B_HTTP_STATUS_FORBIDDEN:
|
|
case B_HTTP_STATUS_METHOD_NOT_ALLOWED:
|
|
case B_HTTP_STATUS_NOT_ACCEPTABLE:
|
|
fError = B_NOT_ALLOWED;
|
|
break;
|
|
case B_HTTP_STATUS_NOT_FOUND:
|
|
fError = B_NAME_NOT_FOUND;
|
|
break;
|
|
case B_HTTP_STATUS_BAD_GATEWAY:
|
|
fError = B_BAD_DATA;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void
|
|
FetchFileJob::Cleanup(status_t jobResult)
|
|
{
|
|
if (jobResult != B_OK)
|
|
fTargetEntry.Remove();
|
|
}
|
|
|
|
|
|
#else // HAIKU_TARGET_PLATFORM_HAIKU
|
|
|
|
|
|
FetchFileJob::FetchFileJob(const BContext& context, const BString& title,
|
|
const BString& fileURL, const BEntry& targetEntry)
|
|
:
|
|
inherited(context, title),
|
|
fFileURL(fileURL),
|
|
fTargetEntry(targetEntry),
|
|
fTargetFile(&targetEntry, B_CREATE_FILE | B_WRITE_ONLY),
|
|
fDownloadProgress(0.0)
|
|
{
|
|
}
|
|
|
|
|
|
FetchFileJob::~FetchFileJob()
|
|
{
|
|
}
|
|
|
|
|
|
float
|
|
FetchFileJob::DownloadProgress() const
|
|
{
|
|
return fDownloadProgress;
|
|
}
|
|
|
|
|
|
const char*
|
|
FetchFileJob::DownloadURL() const
|
|
{
|
|
return fFileURL.String();
|
|
}
|
|
|
|
|
|
const char*
|
|
FetchFileJob::DownloadFileName() const
|
|
{
|
|
return fTargetEntry.Name();
|
|
}
|
|
|
|
|
|
off_t
|
|
FetchFileJob::DownloadBytes() const
|
|
{
|
|
return fBytes;
|
|
}
|
|
|
|
|
|
off_t
|
|
FetchFileJob::DownloadTotalBytes() const
|
|
{
|
|
return fTotalBytes;
|
|
}
|
|
|
|
|
|
status_t
|
|
FetchFileJob::Execute()
|
|
{
|
|
return B_UNSUPPORTED;
|
|
}
|
|
|
|
|
|
void
|
|
FetchFileJob::Cleanup(status_t jobResult)
|
|
{
|
|
if (jobResult != B_OK)
|
|
fTargetEntry.Remove();
|
|
}
|
|
|
|
|
|
#endif // HAIKU_TARGET_PLATFORM_HAIKU
|
|
|
|
} // namespace BPrivate
|
|
|
|
} // namespace BPackageKit
|