Remove Curl, Switch to NetApi in Package Kit

Change-Id: I92213ab60dc987175c323d1d9ed11ac8b3517f2f
Reviewed-on: https://review.haiku-os.org/475
Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
CodeforEvolution
2018-09-29 17:23:52 +00:00
committed by waddlesplash
parent 66f8dcb1f1
commit 106ae4ec35
9 changed files with 155 additions and 145 deletions
+129 -87
View File
@@ -9,22 +9,26 @@
*/
#include <package/FetchFileJob.h>
#include "FetchFileJob.h"
#include <stdio.h>
#ifndef HAIKU_BOOTSTRAP_BUILD
#include <curl/curl.h>
#endif
#include <sys/wait.h>
#include <Path.h>
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
# include <UrlRequest.h>
# include <UrlProtocolRoster.h>
#endif
namespace BPackageKit {
namespace BPrivate {
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
FetchFileJob::FetchFileJob(const BContext& context, const BString& title,
const BString& fileURL, const BEntry& targetEntry)
:
@@ -84,108 +88,74 @@ FetchFileJob::Execute()
if (result != B_OK)
return result;
#ifndef HAIKU_BOOTSTRAP_BUILD
CURL* handle = curl_easy_init();
BUrlRequest* request = BUrlProtocolRoster::MakeRequest(fFileURL.String(),
this);
thread_id thread = request->Run();
wait_for_thread(thread, NULL);
if (handle == NULL)
return B_NO_MEMORY;
#if LIBCURL_VERSION_MAJOR > 7 \
|| (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 32)
result = curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0);
result = curl_easy_setopt(handle, CURLOPT_XFERINFOFUNCTION,
&_TransferCallback);
if (result != CURLE_OK)
return B_BAD_VALUE;
#endif
result = curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, this);
if (result != CURLE_OK)
if (fSuccess == true)
return B_OK;
else
return B_ERROR;
result = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION,
&_WriteCallback);
if (result != CURLE_OK)
return B_ERROR;
//TODO: More detailed error codes?
#if 0
const BHttpResult& outResult = dynamic_cast<const BHttpResult&>
(request->Result());
result = curl_easy_setopt(handle, CURLOPT_WRITEDATA, this);
if (result != CURLE_OK)
return B_ERROR;
result = curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
if (result != CURLE_OK)
return B_ERROR;
result = curl_easy_setopt(handle, CURLOPT_URL, fFileURL.String());
if (result != CURLE_OK)
return B_ERROR;
result = curl_easy_perform(handle);
curl_easy_cleanup(handle);
switch (result) {
case CURLE_OK:
switch (outResult.StatusCode()) {
case B_HTTP_STATUS_OK:
return B_OK;
case CURLE_UNSUPPORTED_PROTOCOL:
return EPROTONOSUPPORT;
case CURLE_FAILED_INIT:
return B_NO_INIT;
case CURLE_URL_MALFORMAT:
return B_BAD_VALUE;
case CURLE_NOT_BUILT_IN:
return B_NOT_SUPPORTED;
case CURLE_COULDNT_RESOLVE_PROXY:
case CURLE_COULDNT_RESOLVE_HOST:
return B_NAME_NOT_FOUND;
case CURLE_COULDNT_CONNECT:
return ECONNREFUSED;
case CURLE_FTP_WEIRD_SERVER_REPLY:
return B_BAD_DATA;
case CURLE_REMOTE_ACCESS_DENIED:
return B_NOT_ALLOWED;
case CURLE_PARTIAL_FILE:
case B_HTTP_STATUS_PARTIAL_CONTENT:
return B_PARTIAL_READ;
case CURLE_OUT_OF_MEMORY:
return B_NO_MEMORY;
case CURLE_OPERATION_TIMEDOUT:
case B_HTTP_STATUS_REQUEST_TIMEOUT:
case B_HTTP_STATUS_GATEWAY_TIMEOUT:
return B_TIMED_OUT;
case CURLE_SSL_CONNECT_ERROR:
return B_BAD_REPLY;
case B_HTTP_STATUS_NOT_IMPLEMENTED:
case B_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE:
return B_NOT_SUPPORTED;
case B_HTTP_STATUS_UNAUTHORIZED:
return B_PERMISSION_DENIED;
case B_HTTP_STATUS_FORBIDDEN:
case B_HTTP_STATUS_METHOD_NOT_ALLOWED:
case B_HTTP_STATUS_NOT_ACCEPTABLE:
return B_NOT_ALLOWED;
case B_HTTP_STATUS_NOT_FOUND:
return B_NAME_NOT_FOUND;
case B_HTTP_STATUS_BAD_GATEWAY:
return B_BAD_DATA;
default:
// TODO: map more curl error codes to ours for more
// precise error reporting
return B_ERROR;
}
#endif /* !HAIKU_BOOTSTRAP_BUILD */
return B_OK;
#endif
}
int
FetchFileJob::_TransferCallback(void* _job, off_t downloadTotal,
off_t downloaded, off_t uploadTotal, off_t uploaded)
void
FetchFileJob::DataReceived(BUrlRequest*, const char* data, off_t position,
ssize_t size)
{
FetchFileJob* job = reinterpret_cast<FetchFileJob*>(_job);
if (downloadTotal != 0) {
job->fBytes = downloaded;
job->fTotalBytes = downloadTotal;
job->fDownloadProgress = (float)downloaded / downloadTotal;
job->NotifyStateListeners();
fTargetFile.WriteAt(position, data, size);
}
void
FetchFileJob::DownloadProgress(BUrlRequest*, ssize_t bytesReceived,
ssize_t bytesTotal)
{
if (bytesTotal != 0) {
fBytes = bytesReceived;
fTotalBytes = bytesTotal;
fDownloadProgress = (float)bytesReceived/bytesTotal;
NotifyStateListeners();
}
return 0;
}
size_t
FetchFileJob::_WriteCallback(void *buffer, size_t size, size_t nmemb,
void *userp)
void
FetchFileJob::RequestCompleted(BUrlRequest*, bool success)
{
FetchFileJob* job = reinterpret_cast<FetchFileJob*>(userp);
ssize_t dataWritten = job->fTargetFile.Write(buffer, size * nmemb);
return size_t(dataWritten);
fSuccess = success;
}
@@ -197,6 +167,78 @@ FetchFileJob::Cleanup(status_t jobResult)
}
#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_ERASE_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