Spaces:
Sleeping
Sleeping
File size: 2,717 Bytes
5cee033 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
//========================================================================
//
// CurlCachedFile.cc
//
// This file is licensed under the GPLv2 or later
//
// Copyright 2009 Stefan Thomas <[email protected]>
// Copyright 2010, 2011 Hib Eris <[email protected]>
// Copyright 2010, 2019, 2021, 2022 Albert Astals Cid <[email protected]>
//
//========================================================================
#include <config.h>
#include "CurlCachedFile.h"
#include "goo/GooString.h"
//------------------------------------------------------------------------
CurlCachedFileLoader::CurlCachedFileLoader(const std::string &urlA) : url(urlA)
{
cachedFile = nullptr;
curl = nullptr;
}
CurlCachedFileLoader::~CurlCachedFileLoader()
{
curl_easy_cleanup(curl);
}
static size_t noop_cb(char *ptr, size_t size, size_t nmemb, void *ptr2)
{
return size * nmemb;
}
size_t CurlCachedFileLoader::init(CachedFile *cachedFileA)
{
curl_off_t contentLength = -1;
long code = 0;
size_t size;
cachedFile = cachedFileA;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &noop_cb);
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if (code) {
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &contentLength);
size = contentLength;
} else {
error(errInternal, -1, "Failed to get size of '{0:s}'.", url.c_str());
size = -1;
}
curl_easy_reset(curl);
return size;
}
static size_t load_cb(const char *ptr, size_t size, size_t nmemb, void *data)
{
CachedFileWriter *writer = (CachedFileWriter *)data;
return (writer->write)(ptr, size * nmemb);
}
int CurlCachedFileLoader::load(const std::vector<ByteRange> &ranges, CachedFileWriter *writer)
{
CURLcode r = CURLE_OK;
unsigned long long fromByte, toByte;
for (const ByteRange &bRange : ranges) {
fromByte = bRange.offset;
toByte = fromByte + bRange.length - 1;
const std::unique_ptr<GooString> range = GooString::format("{0:ulld}-{1:ulld}", fromByte, toByte);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, load_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, writer);
curl_easy_setopt(curl, CURLOPT_RANGE, range->c_str());
r = curl_easy_perform(curl);
curl_easy_reset(curl);
if (r != CURLE_OK) {
break;
}
}
return r;
}
//------------------------------------------------------------------------
|