axmol/core/network/Downloader-curl.cpp

1141 lines
39 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2021-2022 Bytedance Inc.
2019-11-23 20:27:39 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "network/Downloader-curl.h"
2019-11-23 20:27:39 +08:00
#include <cinttypes>
2019-11-23 20:27:39 +08:00
#include <set>
#include <curl/curl.h>
#include <thread>
#include "base/Utils.h"
#include "base/UTF8.h"
#include "base/Director.h"
#include "base/Scheduler.h"
#include "platform/FileUtils.h"
#include "network/Downloader.h"
#include "platform/FileStream.h"
2021-06-02 00:44:45 +08:00
#include "openssl/md5.h"
#include "yasio/xxsocket.hpp"
2019-11-23 20:27:39 +08:00
// **NOTE**
// In the file:
// member function with suffix "Proc" designed called in DownloaderCURL::_threadProc
// member function without suffix designed called in main thread
// !!! Don't change the `long` type to `int32_t` at this file, because
// some curl variadic API require explicit number types, please refer to:
// https://curl.se/libcurl/c/curl_easy_getinfo.html
// https://curl.se/libcurl/c/curl_easy_setopt.html
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#define AX_CURL_POLL_TIMEOUT_MS 50 // wait until DNS query done
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
enum
{
kCheckSumStateSucceed = 1,
kCheckSumStateFailed = 1 << 1,
};
NS_AX_BEGIN
2021-12-25 10:04:45 +08:00
namespace network
{
2019-11-23 20:27:39 +08:00
////////////////////////////////////////////////////////////////////////////////
// Implementation DownloadTaskCURL
2021-12-25 10:04:45 +08:00
class DownloadTaskCURL : public IDownloadTask
{
static int _sSerialId;
// if more than one task write to one file, cause file broken
// so use a set to check this situation
2021-07-04 16:40:34 +08:00
static std::set<std::string> _sStoragePathSet;
2019-11-23 20:27:39 +08:00
public:
int serialId;
2021-07-04 19:26:18 +08:00
DownloaderCURL& owner;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
DownloadTaskCURL(DownloaderCURL& o) : serialId(_sSerialId++), owner(o), _requestHeaders(nullptr)
{
_initInternal();
DLLOG("Construct DownloadTaskCURL %p", this);
}
2021-12-25 10:04:45 +08:00
virtual ~DownloadTaskCURL()
{
if (_errCode != DownloadTask::ERROR_TASK_DUPLICATED)
{
// if task destroyed unnormally, we should release WritenFileName stored in set.
// Normally, this action should done when task finished.
2021-12-25 10:04:45 +08:00
if (_tempFileName.length() && _sStoragePathSet.end() != _sStoragePathSet.find(_tempFileName))
{
DownloadTaskCURL::_sStoragePathSet.erase(_tempFileName);
}
}
2021-04-25 16:36:20 +08:00
_fs.reset();
_fsMd5.reset();
2019-11-23 20:27:39 +08:00
if (_requestHeaders)
curl_slist_free_all(_requestHeaders);
2021-04-25 23:05:06 +08:00
DLLOG("Destruct DownloadTaskCURL %p", this);
}
2020-01-04 22:25:01 +08:00
bool init(std::string_view filename, std::string_view tempSuffix)
2021-12-25 10:04:45 +08:00
{
if (0 == filename.length())
{
// data task
_buf.reserve(CURL_MAX_WRITE_SIZE);
return true;
}
// file task
_fileName = filename;
_tempFileName = filename;
_tempFileName.append(tempSuffix);
2021-12-25 10:04:45 +08:00
if (_sStoragePathSet.end() != _sStoragePathSet.find(_tempFileName))
{
// there is another task uses this storage path
_errCode = DownloadTask::ERROR_TASK_DUPLICATED;
_errCodeInternal = 0;
_errDescription = "More than one download file task write to same file:";
_errDescription.append(_tempFileName);
return false;
}
_sStoragePathSet.insert(_tempFileName);
// open temp file handle for write
bool ret = false;
2021-12-25 10:04:45 +08:00
do
{
2021-07-04 16:40:34 +08:00
std::string dir;
size_t found = _tempFileName.find_last_of("/\\");
2021-12-25 10:04:45 +08:00
if (found == std::string::npos)
{
_errCode = DownloadTask::ERROR_INVALID_PARAMS;
2021-04-26 00:22:07 +08:00
_errCodeInternal = 0;
_errDescription = "Can't find dirname in storagePath.";
break;
2021-04-26 00:22:07 +08:00
}
// ensure directory is exist
dir = _tempFileName.substr(0, found + 1);
2021-12-25 10:04:45 +08:00
if (!FileUtils::getInstance()->isDirectoryExistInternal(dir))
{
if (!FileUtils::getInstance()->createDirectory(dir))
{
_errCode = DownloadTask::ERROR_CREATE_DIR_FAILED;
_errCodeInternal = 0;
_errDescription = "Can't create dir:";
_errDescription.append(dir);
break;
}
}
// open file
2023-08-31 21:20:01 +08:00
_fs = FileUtils::getInstance()->openFileStream(_tempFileName, FileStream::Mode::APPEND);
2021-12-25 10:04:45 +08:00
if (!_fs)
{
_errCode = DownloadTask::ERROR_OPEN_FILE_FAILED;
_errCodeInternal = 0;
_errDescription = "Can't open file:";
_errDescription.append(_tempFileName);
break;
}
// init md5 state
_checksumFileName = _tempFileName + ".chksum";
2023-08-31 21:20:01 +08:00
_fsMd5 = FileUtils::getInstance()->openFileStream(_checksumFileName, FileStream::Mode::OVERLAPPED);
if(!_fsMd5) {
_errCode = DownloadTask::ERROR_OPEN_FILE_FAILED;
_errCodeInternal = 0;
_errDescription = "Can't open checksum file:";
_errDescription.append(_checksumFileName);
break;
}
_fsMd5->seek(0, SEEK_END);
2021-12-25 10:04:45 +08:00
if (_fsMd5->tell() != sizeof(_md5State))
{
2021-06-02 00:44:45 +08:00
MD5_Init(&_md5State);
2021-12-25 10:04:45 +08:00
}
else
{
_fsMd5->seek(0, SEEK_SET);
2021-06-02 00:44:45 +08:00
_fsMd5->read(&_md5State, sizeof(_md5State));
}
ret = true;
} while (0);
return ret;
}
2021-04-26 00:22:07 +08:00
2021-12-25 10:04:45 +08:00
void cancel() override
{
2021-07-04 16:40:34 +08:00
std::lock_guard<std::recursive_mutex> lock(_mutex);
2021-12-25 10:04:45 +08:00
if (!_cancelled)
{
_cancelled = true;
2021-12-25 10:04:45 +08:00
if (this->_sockfd != -1)
{
if(::shutdown(this->_sockfd, SD_BOTH) == -1) // may cause curl CURLE_SEND_ERROR(55) or CURLE_RECV_ERROR(56)
::closesocket(this->_sockfd);
this->_sockfd = -1;
}
}
}
2021-12-25 10:04:45 +08:00
curl_socket_t openSocket(curlsocktype propose, curl_sockaddr* addr)
{
2021-07-04 16:40:34 +08:00
std::lock_guard<std::recursive_mutex> lock(_mutex);
2020-01-04 22:25:01 +08:00
2021-12-25 10:04:45 +08:00
if (!_cancelled)
{
this->_sockfd = ::socket(addr->family, addr->socktype, addr->protocol);
return this->_sockfd;
}
return -1;
}
/*
retval: 0. don't check, 1. check succeed, 2. check failed
*/
int checkFileMd5(std::string_view requiredsum, std::string* outsum = nullptr)
2021-12-25 10:04:45 +08:00
{
int status = 0;
2021-12-25 10:04:45 +08:00
if (!requiredsum.empty())
{
std::string digest(16, '\0');
2021-12-25 10:04:45 +08:00
auto state = _md5State; // Excellent, make a copy, don't modify the origin state.
MD5_Final((uint8_t*)&digest.front(), &state);
auto checksum = utils::bin2hex(digest);
status = requiredsum == checksum ? kCheckSumStateSucceed : kCheckSumStateFailed;
if (outsum != nullptr)
*outsum = std::move(checksum);
2019-11-23 20:27:39 +08:00
}
return status;
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void initProc()
{
2021-07-04 16:40:34 +08:00
std::lock_guard<std::recursive_mutex> lock(_mutex);
_initInternal();
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void setErrorProc(int code, int codeInternal, const char* desc)
{
2021-07-04 16:40:34 +08:00
std::lock_guard<std::recursive_mutex> lock(_mutex);
_errCode = code;
_errCodeInternal = codeInternal;
_errDescription = desc;
}
2021-12-25 10:04:45 +08:00
size_t writeDataProc(unsigned char* buffer, size_t size, size_t count)
{
2021-07-04 16:40:34 +08:00
std::lock_guard<std::recursive_mutex> lock(_mutex);
size_t ret = 0;
auto bytes_transferred = size * count;
2021-12-25 10:04:45 +08:00
if (_fs)
{
ret = _fs->write(buffer, static_cast<unsigned int>(bytes_transferred)); // fwrite(buffer, size, count, _fp);
2021-12-25 10:04:45 +08:00
}
else
{
ret = bytes_transferred;
auto cap = _buf.capacity();
auto bufSize = _buf.size();
2021-12-25 10:04:45 +08:00
if (cap < bufSize + ret)
{
_buf.reserve(bufSize * 2);
}
_buf.insert(_buf.end(), buffer, buffer + ret);
}
2021-12-25 10:04:45 +08:00
if (ret > 0)
{
_bytesReceived += ret;
_totalBytesReceived += ret;
2021-04-25 16:36:20 +08:00
2021-12-25 10:04:45 +08:00
if (_fsMd5)
{
2021-07-04 16:40:34 +08:00
::MD5_Update(&_md5State, buffer, bytes_transferred);
_fsMd5->seek(0, SEEK_SET);
_fsMd5->write(&_md5State, sizeof(_md5State));
}
}
2019-11-23 20:27:39 +08:00
curl_easy_getinfo(_curl, CURLINFO_SPEED_DOWNLOAD, &_speed);
return ret;
}
2021-04-25 23:05:06 +08:00
private:
friend class DownloaderCURL;
// for lock object instance
std::recursive_mutex _mutex;
// header info
bool _acceptRanges;
bool _headerAchieved;
int64_t _totalBytesExpected;
double _speed;
CURL* _curl;
2021-12-25 10:04:45 +08:00
curl_socket_t _sockfd = -1; // store the sockfd to support cancel download manually
bool _cancelled = false;
2021-12-25 10:04:45 +08:00
std::string _header; // temp buffer for receive header string, only used in thread proc
curl_slist* _requestHeaders;
// progress
int64_t _bytesReceived;
int64_t _totalBytesReceived;
// error
int _errCode;
int _errCodeInternal;
2021-07-04 16:40:34 +08:00
std::string _errDescription;
// for saving data
2021-07-04 16:40:34 +08:00
std::string _fileName;
std::string _tempFileName;
std::string _checksumFileName;
2021-07-04 16:40:34 +08:00
std::vector<unsigned char> _buf;
2023-08-31 21:20:01 +08:00
std::unique_ptr<FileStream> _fs{};
// calculate md5 in downloading time support
2023-08-31 21:20:01 +08:00
std::unique_ptr<FileStream> _fsMd5{}; // store md5 state realtime
2021-06-02 00:44:45 +08:00
MD5state_st _md5State;
2021-12-25 10:04:45 +08:00
void _initInternal()
{
_acceptRanges = (false);
_headerAchieved = (false);
_bytesReceived = (0);
_totalBytesReceived = (0);
_totalBytesExpected = (0);
_speed = 0;
_curl = nullptr;
_errCode = (DownloadTask::ERROR_NO_ERROR);
_errCodeInternal = (CURLE_OK);
_header.resize(0);
2021-12-25 10:04:45 +08:00
_header.reserve(384); // pre alloc header string buffer
}
};
int DownloadTaskCURL::_sSerialId;
2021-07-04 21:21:29 +08:00
std::set<std::string> DownloadTaskCURL::_sStoragePathSet;
////////////////////////////////////////////////////////////////////////////////
// Implementation DownloaderCURL::Impl
// This class shared by DownloaderCURL and work thread.
2021-12-25 10:04:45 +08:00
class DownloaderCURL::Impl : public std::enable_shared_from_this<DownloaderCURL::Impl>
{
public:
DownloaderHints hints;
2021-04-25 23:05:06 +08:00
Impl()
// : _thread(nullptr)
{
DLLOG("Construct DownloaderCURL::Impl %p", this);
}
2021-12-25 10:04:45 +08:00
~Impl() { DLLOG("Destruct DownloaderCURL::Impl %p", this); }
2021-12-25 10:04:45 +08:00
void addTask(std::shared_ptr<DownloadTask> task, DownloadTaskCURL* coTask)
{
int status = coTask->checkFileMd5(task->checksum);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (status & kCheckSumStateSucceed || DownloadTask::ERROR_NO_ERROR != coTask->_errCode)
{
2021-07-04 19:26:18 +08:00
_owner->_onDownloadFinished(*task, status);
2021-12-25 10:04:45 +08:00
}
else
{
2021-07-04 21:21:29 +08:00
std::lock_guard<std::mutex> lock(_requestMutex);
_requestQueue.emplace_back(task);
}
}
2021-12-25 10:04:45 +08:00
void run()
{
2021-07-04 21:21:29 +08:00
std::lock_guard<std::mutex> lock(_threadMutex);
2021-04-26 00:22:07 +08:00
2021-12-25 10:04:45 +08:00
if (_tasksFinished)
{ // all tasks finished, make sure thread not joinable
if (_thread.joinable())
_thread.join();
_tasksFinished = false;
2021-04-25 16:36:20 +08:00
}
2019-11-23 20:27:39 +08:00
if (!_thread.joinable())
_thread = std::thread(&DownloaderCURL::Impl::_threadProc, this);
}
2021-12-25 10:04:45 +08:00
void stop()
{ // make sure all task exit properly
if (!_requestQueue.empty())
{
2021-07-04 16:40:34 +08:00
std::lock_guard<std::mutex> lock(_requestMutex);
_requestQueue.clear();
2021-04-25 23:05:06 +08:00
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (!_processSet.empty())
{
2021-07-04 16:40:34 +08:00
std::lock_guard<std::mutex> lock(_processMutex);
for (auto&& task : _processSet)
2021-07-04 19:26:18 +08:00
task->cancel();
_processSet.clear();
}
if (_thread.joinable())
_thread.join();
}
2021-12-25 10:04:45 +08:00
bool stopped() const
{
2021-07-04 21:21:29 +08:00
std::lock_guard<std::mutex> lock(_threadMutex);
2021-07-04 16:40:34 +08:00
return false == _thread.joinable() ? true : false;
}
2021-07-04 19:26:18 +08:00
// Gets non background task list
2021-12-25 10:04:45 +08:00
void getProcessTasks(std::vector<std::shared_ptr<DownloadTask>>& outList)
{
2021-07-04 16:40:34 +08:00
std::lock_guard<std::mutex> lock(_processMutex);
outList.reserve(_processSet.size());
for (auto&& task : _processSet)
2021-12-25 10:04:45 +08:00
{
2021-07-04 19:26:18 +08:00
if (!task->background)
outList.emplace_back(task);
2021-07-04 19:26:18 +08:00
}
}
2021-12-25 10:04:45 +08:00
void getFinishedTasks(std::vector<std::shared_ptr<DownloadTask>>& outList)
{
2021-07-04 16:40:34 +08:00
std::lock_guard<std::mutex> lock(_finishedMutex);
outList.reserve(_finishedQueue.size());
outList.insert(outList.end(), _finishedQueue.begin(), _finishedQueue.end());
_finishedQueue.clear();
}
private:
2021-12-25 10:04:45 +08:00
static size_t _outputHeaderCallbackProc(void* buffer, size_t size, size_t count, void* userdata)
{
int strLen = int(size * count);
DLLOG(" _outputHeaderCallbackProc: %.*s", strLen, buffer);
2021-12-25 10:04:45 +08:00
DownloadTaskCURL& coTask = *((DownloadTaskCURL*)(userdata));
coTask._header.append((const char*)buffer, strLen);
return strLen;
}
2021-04-25 16:36:20 +08:00
2021-12-25 10:04:45 +08:00
static size_t _outputDataCallbackProc(void* buffer, size_t size, size_t count, void* userdata)
{
// DLLOG(" _outputDataCallbackProc: size(%ld), count(%ld)", size, count);
2021-12-25 10:04:45 +08:00
DownloadTaskCURL* coTask = (DownloadTaskCURL*)userdata;
// If your callback function returns CURL_WRITEFUNC_PAUSE it will cause this transfer to become paused.
2021-12-25 10:04:45 +08:00
return coTask->writeDataProc((unsigned char*)buffer, size, count);
}
2021-12-25 10:04:45 +08:00
static int _progressCallbackProc(void* ptr,
double totalToDownload,
double nowDownloaded,
double totalToUpLoad,
double nowUpLoaded)
{
auto task = (DownloadTask*)ptr;
2021-07-04 19:26:18 +08:00
if (!task || !task->background)
return 0;
auto& coTask = task->_coTask;
2021-12-25 10:04:45 +08:00
if (coTask)
{
2021-07-04 19:26:18 +08:00
auto& downloaderImpl = static_cast<DownloadTaskCURL*>(coTask.get())->owner;
downloaderImpl._updateTaskProgressInfo(*task);
downloaderImpl.onTaskProgress(*task, downloaderImpl._transferDataToBuffer);
}
return 0;
}
2021-12-25 10:04:45 +08:00
static curl_socket_t _openSocketCallback(DownloadTaskCURL& pTask, curlsocktype propose, curl_sockaddr* addr)
{
return pTask.openSocket(propose, addr);
}
2021-04-25 23:05:06 +08:00
// this function designed call in work thread
// the curl handle destroyed in _threadProc
// handle inited for get header
2021-12-25 10:04:45 +08:00
CURLcode _initCurlHandleProc(CURL* handle, std::shared_ptr<DownloadTask>& task, bool forContent = false)
{
2021-07-04 19:26:18 +08:00
DownloadTaskCURL* coTask = static_cast<DownloadTaskCURL*>(task->_coTask.get());
/* Resolve host domain to ip */
2021-07-04 19:26:18 +08:00
std::string internalURL = task->requestURL;
// Curl_custom_setup(handle, internalURL, (void**)& coTask->_requestHeaders);
// set url
curl_easy_setopt(handle, CURLOPT_URL, internalURL.c_str());
// set write func
2021-12-25 10:04:45 +08:00
if (forContent)
{
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, _outputDataCallbackProc);
2021-12-25 10:04:45 +08:00
}
else
{
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, _outputHeaderCallbackProc);
}
curl_easy_setopt(handle, CURLOPT_WRITEDATA, coTask);
2021-12-25 10:04:45 +08:00
if (task->background)
{
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
2021-07-04 19:26:18 +08:00
curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, task.get());
2021-07-04 16:40:34 +08:00
curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, _progressCallbackProc);
2021-12-25 10:04:45 +08:00
}
else
{
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1L);
2021-07-04 16:40:34 +08:00
}
curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1L);
curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(handle, CURLOPT_OPENSOCKETFUNCTION, _openSocketCallback);
curl_easy_setopt(handle, CURLOPT_OPENSOCKETDATA, coTask);
2021-12-25 10:04:45 +08:00
if (forContent)
{
/** if server acceptRanges and local has part of file, we continue to download **/
2021-12-25 10:04:45 +08:00
if (coTask->_acceptRanges && coTask->_totalBytesReceived > 0)
{
char buf[128];
snprintf(buf, sizeof(buf), "%" PRId64 "-", coTask->_totalBytesReceived);
curl_easy_setopt(handle, CURLOPT_RANGE, buf);
2021-12-25 10:04:45 +08:00
curl_easy_setopt(handle, CURLOPT_RESUME_FROM_LARGE, (curl_off_t)coTask->_totalBytesReceived);
}
2021-12-25 10:04:45 +08:00
}
else
{
// get header options
curl_easy_setopt(handle, CURLOPT_HEADER, 1L);
curl_easy_setopt(handle, CURLOPT_NOBODY, 1L);
}
// if (!sProxy.empty())
// {
// curl_easy_setopt(curl, CURLOPT_PROXY, sProxy.c_str());
// }
2021-12-25 10:04:45 +08:00
if (hints.timeoutInSeconds)
{
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, hints.timeoutInSeconds);
}
2021-04-26 00:22:07 +08:00
curl_easy_setopt(handle, CURLOPT_LOW_SPEED_LIMIT, 1L);
curl_easy_setopt(handle, CURLOPT_LOW_SPEED_TIME, 10L);
if(task->cacertPath.empty())
{
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
}
else {
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(handle, CURLOPT_CAINFO, task->cacertPath.c_str());
}
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(handle, CURLOPT_MAXREDIRS, 5L);
coTask->_curl = handle;
return CURLE_OK;
}
// get header info, if success set handle to content download state
2021-12-25 10:04:45 +08:00
bool _getHeaderInfoProc(CURL* handle, DownloadTaskCURL* coTask)
{
2021-07-04 19:26:18 +08:00
CURLcode rc = CURLE_OK;
2021-12-25 10:04:45 +08:00
do
{
long httpResponseCode = 0;
rc = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &httpResponseCode);
2021-12-25 10:04:45 +08:00
if (CURLE_OK != rc)
{
break;
}
// Comment follow code to support ftp
/*if (200 != httpResponseCode)
{
char buf[256] = {0};
sprintf(buf
, "When request url(%s) header info, return unexcept http response code(%ld)"
, wrapper.first->requestURL.c_str()
, httpResponseCode);
coTask.setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, CURLE_OK, buf);
}*/
// curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &effectiveUrl);
// curl_easy_getinfo(handle, CURLINFO_CONTENT_TYPE, &contentType);
double contentLen = 0;
rc = curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentLen);
2021-12-25 10:04:45 +08:00
if (CURLE_OK != rc)
{
break;
}
2021-04-25 16:36:20 +08:00
// std::transform(coTask._header.begin(), coTask._header.end(), coTask._header.begin(), ::toupper);
2021-12-25 10:04:45 +08:00
bool acceptRanges = true; // (string::npos != coTask._header.find("ACCEPT-RANGES")) ? true : false;
// get current file size
int64_t fileSize = 0;
2021-12-25 10:04:45 +08:00
if (acceptRanges && coTask->_tempFileName.length())
{
2021-07-04 19:26:18 +08:00
fileSize = FileUtils::getInstance()->getFileSize(coTask->_tempFileName);
2021-04-26 00:22:07 +08:00
}
2021-04-25 23:05:06 +08:00
// set header info to coTask
2021-07-04 21:21:29 +08:00
std::lock_guard<std::recursive_mutex> lock(coTask->_mutex);
coTask->_totalBytesExpected = static_cast<int64_t>(contentLen);
2021-07-04 19:26:18 +08:00
coTask->_acceptRanges = acceptRanges;
2021-12-25 10:04:45 +08:00
if (acceptRanges && fileSize > 0)
{
2021-07-04 19:26:18 +08:00
coTask->_totalBytesReceived = fileSize;
}
2021-07-04 19:26:18 +08:00
coTask->_headerAchieved = true;
} while (0);
2021-12-25 10:04:45 +08:00
if (CURLE_OK != rc)
{
2021-07-04 19:26:18 +08:00
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, rc, curl_easy_strerror(rc));
2019-11-23 20:27:39 +08:00
}
2021-07-04 19:26:18 +08:00
return coTask->_headerAchieved;
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void _threadProc()
{
DLLOG("++++DownloaderCURL::Impl::_threadProc begin %p", this);
// the holder prevent DownloaderCURL::Impl class instance be destruct in main thread
auto holder = this->shared_from_this();
2021-07-04 21:21:29 +08:00
auto thisThreadId = std::this_thread::get_id();
uint32_t countOfMaxProcessingTasks = this->hints.countOfMaxProcessingTasks;
// init curl content
CURLM* curlmHandle = curl_multi_init();
2021-07-04 21:21:29 +08:00
std::unordered_map<CURL*, std::shared_ptr<DownloadTask>> coTaskMap;
int runningHandles = 0;
CURLMcode mcode = CURLM_OK;
2021-12-25 10:04:45 +08:00
int rc = 0; // select return code
2021-12-25 10:04:45 +08:00
do
{
// check the thread should exit or not
2019-11-23 20:27:39 +08:00
{
2021-07-04 21:21:29 +08:00
std::lock_guard<std::mutex> lock(_threadMutex);
// if the Impl stoped, this->_thread.reset will be called, thus _thread.get_id() not equal with
// thisThreadId
2021-12-25 10:04:45 +08:00
if (thisThreadId != this->_thread.get_id())
{
2019-11-23 20:27:39 +08:00
break;
}
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (runningHandles)
{
// get timeout setting from multi-handle
2021-09-02 14:20:16 +08:00
long timeoutMS = -1;
curl_multi_timeout(curlmHandle, &timeoutMS);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (timeoutMS < 0)
{
timeoutMS = 1000;
2019-11-23 20:27:39 +08:00
}
/* get file descriptors from the transfers */
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -1;
2019-11-23 20:27:39 +08:00
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
2019-11-23 20:27:39 +08:00
mcode = curl_multi_fdset(curlmHandle, &fdread, &fdwrite, &fdexcep, &maxfd);
2021-12-25 10:04:45 +08:00
if (CURLM_OK != mcode)
{
break;
2019-11-23 20:27:39 +08:00
}
// do wait action
2021-12-25 10:04:45 +08:00
if (maxfd == -1)
{
2022-07-16 10:43:05 +08:00
std::this_thread::sleep_for(std::chrono::milliseconds(AX_CURL_POLL_TIMEOUT_MS));
rc = 0;
2021-12-25 10:04:45 +08:00
}
else
{
struct timeval timeout;
2019-11-23 20:27:39 +08:00
timeout.tv_sec = timeoutMS / 1000;
timeout.tv_usec = (timeoutMS % 1000) * 1000;
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
}
2021-04-25 16:36:20 +08:00
2021-12-25 10:04:45 +08:00
if (rc < 0)
{
DLLOG(" _threadProc: select return unexpect code: %d", rc);
}
}
2021-04-25 16:36:20 +08:00
2021-12-25 10:04:45 +08:00
if (!coTaskMap.empty())
{
mcode = CURLM_CALL_MULTI_PERFORM;
2021-12-25 10:04:45 +08:00
while (CURLM_CALL_MULTI_PERFORM == mcode)
{
mcode = curl_multi_perform(curlmHandle, &runningHandles);
}
2021-12-25 10:04:45 +08:00
if (CURLM_OK != mcode)
{
break;
}
2021-04-25 23:05:06 +08:00
struct CURLMsg* m;
2021-12-25 10:04:45 +08:00
do
{
int msgq = 0;
m = curl_multi_info_read(curlmHandle, &msgq);
2021-12-25 10:04:45 +08:00
if (m && (m->msg == CURLMSG_DONE))
{
CURL* curlHandle = m->easy_handle;
CURLcode errCode = m->data.result;
2021-07-04 19:26:18 +08:00
auto task = coTaskMap[curlHandle];
// remove from multi-handle
curl_multi_remove_handle(curlmHandle, curlHandle);
bool reinited = false;
2021-12-25 10:04:45 +08:00
do
{
2021-07-04 19:26:18 +08:00
auto coTask = static_cast<DownloadTaskCURL*>(task->_coTask.get());
2021-12-25 10:04:45 +08:00
if (CURLE_OK != errCode)
{
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, errCode,
curl_easy_strerror(errCode));
break;
}
2021-04-25 23:05:06 +08:00
// if the task is content download task, cleanup the handle
2021-12-25 10:04:45 +08:00
if (coTask->_headerAchieved)
{
break;
}
// the task is get header task
// first, we get info from response
2021-12-25 10:04:45 +08:00
if (!_getHeaderInfoProc(curlHandle, coTask))
{
// the error info has been set in _getHeaderInfoProc
break;
}
// after get header info success
// wrapper.second->_totalBytesReceived inited by local file size
// if the local file size equal with the content size from header, the file has
// downloaded finish
2021-12-25 10:04:45 +08:00
if (coTask->_totalBytesReceived &&
coTask->_totalBytesReceived == coTask->_totalBytesExpected)
{
// the file has download complete
// break to move this task to finish queue
break;
}
// reinit curl handle for download content
curl_easy_reset(curlHandle);
2021-07-04 19:26:18 +08:00
auto error = _initCurlHandleProc(curlHandle, task, true);
2021-12-25 10:04:45 +08:00
if (error != CURLE_OK)
{
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, error,
curl_easy_strerror(error));
break;
2019-11-23 20:27:39 +08:00
}
mcode = curl_multi_add_handle(curlmHandle, curlHandle);
2021-12-25 10:04:45 +08:00
if (CURLM_OK != mcode)
{
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, mcode,
curl_multi_strerror(mcode));
break;
2019-11-23 20:27:39 +08:00
}
reinited = true;
} while (0);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (reinited)
{
continue;
2019-11-23 20:27:39 +08:00
}
curl_easy_cleanup(curlHandle);
DLLOG(" _threadProc task clean cur handle :%p with errCode:%d", curlHandle, errCode);
2021-04-26 00:22:07 +08:00
// remove from coTaskMap
coTaskMap.erase(curlHandle);
// remove from _processSet
2019-11-23 20:27:39 +08:00
{
2021-07-04 21:21:29 +08:00
std::lock_guard<std::mutex> lock(_processMutex);
2021-12-25 10:04:45 +08:00
if (_processSet.end() != _processSet.find(task))
{
2021-07-04 19:26:18 +08:00
_processSet.erase(task);
}
2019-11-23 20:27:39 +08:00
}
2021-07-04 19:26:18 +08:00
if (task->background)
_owner->_onDownloadFinished(*task);
2021-12-25 10:04:45 +08:00
else
{
2021-07-04 21:21:29 +08:00
std::lock_guard<std::mutex> lock(_finishedMutex);
_finishedQueue.emplace_back(task);
2021-07-04 16:40:34 +08:00
}
2019-11-23 20:27:39 +08:00
}
} while (m);
}
2019-11-23 20:27:39 +08:00
// process tasks in _requestList
auto size = coTaskMap.size();
2021-12-25 10:04:45 +08:00
while (0 == countOfMaxProcessingTasks || size < countOfMaxProcessingTasks)
{
// get task wrapper from request queue
2021-07-04 19:26:18 +08:00
std::shared_ptr<DownloadTask> task;
{
2021-07-04 21:21:29 +08:00
std::lock_guard<std::mutex> lock(_requestMutex);
2021-12-25 10:04:45 +08:00
if (!_requestQueue.empty())
{
2021-07-04 19:26:18 +08:00
task = _requestQueue.front();
_requestQueue.pop_front();
2019-11-23 20:27:39 +08:00
}
}
2019-11-23 20:27:39 +08:00
// if request queue is empty, the wrapper.first is nullptr
2021-12-25 10:04:45 +08:00
if (!task)
{
break;
}
2019-11-23 20:27:39 +08:00
2021-07-04 19:26:18 +08:00
auto coTask = static_cast<DownloadTaskCURL*>(task->_coTask.get());
coTask->initProc();
2019-11-23 20:27:39 +08:00
// create curl handle from task and add into curl multi handle
CURL* curlHandle = curl_easy_init();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (nullptr == curlHandle)
{
2021-07-04 19:26:18 +08:00
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, 0, "Alloc curl handle failed.");
_owner->_onDownloadFinished(*task);
continue;
}
2019-11-23 20:27:39 +08:00
// init curl handle for get header info
2021-07-04 19:26:18 +08:00
_initCurlHandleProc(curlHandle, task);
2019-11-23 20:27:39 +08:00
// add curl handle to process list
mcode = curl_multi_add_handle(curlmHandle, curlHandle);
2021-12-25 10:04:45 +08:00
if (CURLM_OK != mcode)
{
2021-07-04 19:26:18 +08:00
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, mcode, curl_multi_strerror(mcode));
_owner->_onDownloadFinished(*task);
continue;
2019-11-23 20:27:39 +08:00
}
DLLOG(" _threadProc task create curl handle:%p", curlHandle);
2021-07-04 19:26:18 +08:00
coTaskMap[curlHandle] = task;
2021-07-04 21:21:29 +08:00
std::lock_guard<std::mutex> lock(_processMutex);
2021-07-04 19:26:18 +08:00
_processSet.insert(task);
}
} while (!coTaskMap.empty());
_tasksFinished = true;
2021-04-26 00:22:07 +08:00
curl_multi_cleanup(curlmHandle);
DLLOG("----DownloaderCURL::Impl::_threadProc end");
}
2021-07-04 16:40:34 +08:00
std::thread _thread;
std::atomic_bool _tasksFinished{};
2021-07-04 19:26:18 +08:00
std::deque<std::shared_ptr<DownloadTask>> _requestQueue;
std::set<std::shared_ptr<DownloadTask>> _processSet;
std::deque<std::shared_ptr<DownloadTask>> _finishedQueue;
2019-11-23 20:27:39 +08:00
2021-07-04 16:40:34 +08:00
mutable std::mutex _threadMutex;
std::mutex _requestMutex;
std::mutex _processMutex;
std::mutex _finishedMutex;
public:
DownloaderCURL* _owner = nullptr;
};
2019-11-23 20:27:39 +08:00
////////////////////////////////////////////////////////////////////////////////
// Implementation DownloaderCURL
2021-12-25 10:04:45 +08:00
DownloaderCURL::DownloaderCURL(const DownloaderHints& hints) : _impl(std::make_shared<Impl>()), _currTask(nullptr)
{
DLLOG("Construct DownloaderCURL %p", this);
_impl->hints = hints;
_impl->_owner = this;
_transferDataToBuffer = [this](void* buf, int64_t len) -> int64_t {
DownloadTaskCURL& coTask = *_currTask;
int64_t dataLen = coTask._buf.size();
2021-12-25 10:04:45 +08:00
if (len < dataLen)
{
return 0;
}
2019-11-23 20:27:39 +08:00
memcpy(buf, coTask._buf.data(), dataLen);
coTask._buf.resize(0);
return dataLen;
};
}
2021-12-25 10:04:45 +08:00
DownloaderCURL::~DownloaderCURL()
{
if (_scheduler)
{
2021-07-04 16:40:34 +08:00
_scheduler->unschedule(_schedulerKey, this);
_scheduler->release();
}
_impl->stop();
DLLOG("Destruct DownloaderCURL %p", this);
}
2021-12-25 10:04:45 +08:00
void DownloaderCURL::startTask(std::shared_ptr<DownloadTask>& task)
{
DownloadTaskCURL* coTask = new DownloadTaskCURL(*this);
task->_coTask.reset(coTask); // coTask auto managed by task
if (coTask->init(task->storagePath, _impl->hints.tempFileNameSuffix))
{
DLLOG("DownloaderCURL: createTask: Id(%d)", coTask->serialId);
_impl->addTask(task, coTask);
_impl->run();
if (!task->background)
{
_lazyScheduleUpdate();
_scheduler->resumeTarget(this);
}
}
2021-12-25 10:04:45 +08:00
else
{
2022-08-08 18:02:17 +08:00
ax::log("DownloaderCURL createTask fail, error: %d, detail: %s", coTask->_errCode,
2021-12-25 10:04:45 +08:00
coTask->_errDescription.c_str());
task.reset();
2021-07-04 16:40:34 +08:00
}
}
2021-12-25 10:04:45 +08:00
void DownloaderCURL::_lazyScheduleUpdate()
{
if (!_scheduler)
{
2021-07-04 16:40:34 +08:00
_scheduler = Director::getInstance()->getScheduler();
_scheduler->retain();
char key[128];
snprintf(key, sizeof(key), "DownloaderCURL(%p)", this);
2021-07-04 16:40:34 +08:00
_schedulerKey = key;
2021-12-25 10:04:45 +08:00
_scheduler->schedule(std::bind(&DownloaderCURL::_onUpdate, this, std::placeholders::_1), this, 0.1f, true,
_schedulerKey);
2021-07-04 16:40:34 +08:00
}
}
2021-12-25 10:04:45 +08:00
void DownloaderCURL::_onUpdate(float)
{
2021-07-04 19:26:18 +08:00
std::vector<std::shared_ptr<DownloadTask>> tasks;
2021-07-04 16:40:34 +08:00
// update processing tasks
_impl->getProcessTasks(tasks);
for (auto&& task : tasks)
2021-12-25 10:04:45 +08:00
{
2021-07-04 19:26:18 +08:00
DownloadTaskCURL& coTask = static_cast<DownloadTaskCURL&>(*task->_coTask);
2021-07-04 16:40:34 +08:00
std::lock_guard<std::recursive_mutex> lock(coTask._mutex);
2021-12-25 10:04:45 +08:00
if (coTask._bytesReceived)
{
2021-07-04 16:40:34 +08:00
_currTask = &coTask;
2021-07-04 19:26:18 +08:00
_updateTaskProgressInfo(*task);
onTaskProgress(*task, _transferDataToBuffer);
2021-07-04 16:40:34 +08:00
_currTask = nullptr;
coTask._bytesReceived = 0;
}
}
tasks.clear();
// update finished tasks
_impl->getFinishedTasks(tasks);
2021-12-25 10:04:45 +08:00
if (_impl->stopped())
{
2021-07-04 16:40:34 +08:00
if (_scheduler)
_scheduler->pauseTarget(this);
}
for (auto&& task : tasks)
2021-12-25 10:04:45 +08:00
{
2021-07-04 19:26:18 +08:00
_onDownloadFinished(*task);
2021-07-04 16:40:34 +08:00
}
}
2021-12-25 10:04:45 +08:00
void DownloaderCURL::_updateTaskProgressInfo(DownloadTask& task, int64_t totalExpected)
{
2021-07-04 21:21:29 +08:00
auto& coTask = static_cast<DownloadTaskCURL&>(*task._coTask);
2021-07-04 16:40:34 +08:00
task.progressInfo.bytesReceived = coTask._bytesReceived;
task.progressInfo.totalBytesReceived = coTask._totalBytesReceived;
task.progressInfo.totalBytesExpected = totalExpected < 0 ? coTask._totalBytesExpected : totalExpected;
task.progressInfo.speedInBytes = coTask._speed;
}
2021-12-25 10:04:45 +08:00
void DownloaderCURL::_onDownloadFinished(DownloadTask& task, int checkState)
{
2021-07-04 19:26:18 +08:00
auto& coTask = static_cast<DownloadTaskCURL&>(*task._coTask);
// if there is bytesReceived, call progress update first
2021-12-25 10:04:45 +08:00
if (coTask._bytesReceived)
{
2021-07-04 16:40:34 +08:00
_currTask = &coTask;
2021-07-04 19:26:18 +08:00
_updateTaskProgressInfo(task);
onTaskProgress(task, _transferDataToBuffer);
coTask._bytesReceived = 0;
_currTask = nullptr;
2019-11-23 20:27:39 +08:00
}
// if file task, close file handle and rename file if needed
2021-12-25 10:04:45 +08:00
if (coTask._fs)
{
do
{
auto pFileUtils = FileUtils::getInstance();
coTask._fs.reset();
coTask._fsMd5.reset();
2021-12-25 10:04:45 +08:00
if (checkState & kCheckSumStateSucceed) // No need download
{
2023-08-31 21:20:01 +08:00
auto fsOrigin = pFileUtils->openFileStream(coTask._fileName, FileStream::Mode::READ);
2021-12-25 10:04:45 +08:00
if (fsOrigin)
{
fsOrigin->seek(0, SEEK_END);
2021-07-04 19:26:18 +08:00
_updateTaskProgressInfo(task, fsOrigin->tell());
2021-07-04 16:40:34 +08:00
coTask._errCode = DownloadTask::ERROR_NO_ERROR;
coTask._errCodeInternal = 0;
coTask._errDescription = "";
pFileUtils->removeFile(coTask._tempFileName);
onTaskProgress(task, _transferDataToBuffer);
fsOrigin = nullptr;
2021-12-25 10:04:45 +08:00
}
else
{
coTask._errCode = DownloadTask::ERROR_ORIGIN_FILE_MISSING;
coTask._errCodeInternal = 0;
coTask._errDescription = "Check file md5 succeed, but the origin file is missing!";
pFileUtils->removeFile(coTask._checksumFileName);
pFileUtils->removeFile(coTask._tempFileName);
}
2021-04-26 00:22:07 +08:00
break;
}
2021-12-25 10:04:45 +08:00
if (coTask._fileName.empty() || DownloadTask::ERROR_NO_ERROR != coTask._errCode)
{
if (coTask._errCodeInternal == CURLE_RANGE_ERROR)
{
// If CURLE_RANGE_ERROR, means the server not support resume from download.
pFileUtils->removeFile(coTask._checksumFileName);
pFileUtils->removeFile(coTask._tempFileName);
}
break;
}
2021-04-26 00:22:07 +08:00
// if file already exist, remove it
2021-12-25 10:04:45 +08:00
if (pFileUtils->isFileExistInternal(coTask._fileName))
{
if (!pFileUtils->removeFile(coTask._fileName))
{
coTask._errCode = DownloadTask::ERROR_REMOVE_FILE_FAILED;
coTask._errCodeInternal = errno;
coTask._errDescription = "Can't remove old file: ";
coTask._errDescription.append(coTask._fileName);
break;
}
}
2021-04-26 00:22:07 +08:00
// Try check sum with md5 digest
std::string realMd5;
2021-12-25 10:04:45 +08:00
if (coTask.checkFileMd5(task.checksum, &realMd5) & kCheckSumStateFailed)
{
coTask._errCode = DownloadTask::ERROR_CHECK_SUM_FAILED;
coTask._errCodeInternal = 0;
2021-12-25 10:04:45 +08:00
coTask._errDescription =
StringUtils::format("Check file: %s md5 failed, required:%s, real:%s", coTask._fileName.c_str(),
task.checksum.c_str(), realMd5.c_str());
pFileUtils->removeFile(coTask._checksumFileName);
pFileUtils->removeFile(coTask._tempFileName);
break;
2021-04-26 00:22:07 +08:00
}
// Rename file work fine.
2021-12-25 10:04:45 +08:00
if (pFileUtils->renameFile(coTask._tempFileName, coTask._fileName))
{
// success, remove storage from set
DownloadTaskCURL::_sStoragePathSet.erase(coTask._tempFileName);
break;
}
// failed
coTask._errCode = DownloadTask::ERROR_RENAME_FILE_FAILED;
coTask._errCodeInternal = 0;
coTask._errDescription = "Can't renamefile from: ";
coTask._errDescription.append(coTask._tempFileName);
coTask._errDescription.append(" to: ");
coTask._errDescription.append(coTask._fileName);
} while (0);
2021-04-26 00:22:07 +08:00
}
2021-04-25 23:05:06 +08:00
// needn't lock coTask here, because tasks has removed form _impl
onTaskFinish(task, coTask._errCode, coTask._errCodeInternal, coTask._errDescription, coTask._buf);
DLLOG(" DownloaderCURL: finish Task: Id(%d)", coTask.serialId);
}
2021-12-25 10:04:45 +08:00
} // namespace network
NS_AX_END // namespace ax