Skip to content

Commit 5ffd6ce

Browse files
committed
Improve downloader
- print http respone code when error - make checksum case-insensitive
1 parent 042c9b7 commit 5ffd6ce

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

core/network/Downloader-curl.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ class DownloadTaskCURL : public IDownloadTask
250250
return status;
251251
}
252252

253-
void setErrorProc(int code, int codeInternal, const char* desc)
253+
void setErrorDesc(int code, int codeInternal, std::string&& desc)
254254
{
255255
std::lock_guard<std::recursive_mutex> lock(_mutex);
256256
_errCode = code;
257257
_errCodeInternal = codeInternal;
258-
_errDescription = desc;
258+
_errDescription = std::move(desc);
259259
}
260260

261261
size_t writeDataProc(unsigned char* buffer, size_t size, size_t count)
@@ -617,8 +617,14 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this<DownloaderCURL:
617617
auto coTask = static_cast<DownloadTaskCURL*>(task->_coTask.get());
618618
if (CURLE_OK != errCode)
619619
{
620-
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, errCode,
621-
curl_easy_strerror(errCode));
620+
std::string errorMsg = curl_easy_strerror(errCode);
621+
if (errCode == CURLE_HTTP_RETURNED_ERROR) {
622+
long responeCode = 0;
623+
curl_easy_getinfo(curlHandle, CURLINFO_RESPONSE_CODE, &responeCode);
624+
fmt::format_to(std::back_inserter(errorMsg), FMT_COMPILE(": {}"), responeCode);
625+
}
626+
627+
coTask->setErrorDesc(DownloadTask::ERROR_IMPL_INTERNAL, errCode, std::move(errorMsg));
622628
break;
623629
}
624630

@@ -689,7 +695,7 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this<DownloaderCURL:
689695

690696
if (nullptr == curlHandle)
691697
{
692-
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, 0, "Alloc curl handle failed.");
698+
coTask->setErrorDesc(DownloadTask::ERROR_IMPL_INTERNAL, 0, "Alloc curl handle failed.");
693699
_owner->_onDownloadFinished(*task);
694700
continue;
695701
}
@@ -701,7 +707,7 @@ class DownloaderCURL::Impl : public std::enable_shared_from_this<DownloaderCURL:
701707
mcode = curl_multi_add_handle(curlmHandle, curlHandle);
702708
if (CURLM_OK != mcode)
703709
{
704-
coTask->setErrorProc(DownloadTask::ERROR_IMPL_INTERNAL, mcode, curl_multi_strerror(mcode));
710+
coTask->setErrorDesc(DownloadTask::ERROR_IMPL_INTERNAL, mcode, curl_multi_strerror(mcode));
705711
_owner->_onDownloadFinished(*task);
706712
continue;
707713
}
@@ -903,7 +909,7 @@ void DownloaderCURL::_onDownloadFinished(DownloadTask& task, int checkState)
903909
break;
904910
}
905911

906-
if (coTask._fileName.empty() || DownloadTask::ERROR_NO_ERROR != coTask._errCode)
912+
if (coTask._fileName.empty() || coTask._errCode != DownloadTask::ERROR_NO_ERROR)
907913
{
908914
if (coTask._errCodeInternal == CURLE_RANGE_ERROR)
909915
{

core/network/Downloader.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@
2727
#include "network/Downloader.h"
2828

2929
#if EMSCRIPTEN
30-
#include "network/Downloader-wasm.h"
31-
#define DownloaderImpl DownloaderEmscripten
30+
# include "network/Downloader-wasm.h"
31+
# define DownloaderImpl DownloaderEmscripten
3232
#else
33-
#include "network/Downloader-curl.h"
34-
#define DownloaderImpl DownloaderCURL
33+
# include "network/Downloader-curl.h"
34+
# define DownloaderImpl DownloaderCURL
3535
#endif
3636

37+
#include <ctype.h>
38+
#include <algorithm>
39+
3740
NS_AX_BEGIN
3841

3942
namespace network
@@ -63,7 +66,9 @@ DownloadTask::DownloadTask(std::string_view srcUrl,
6366
this->checksum = checksum;
6467
this->identifier = identifier;
6568
this->background = background;
66-
this->cacertPath = cacertPath;
69+
this->cacertPath = cacertPath;
70+
if (!this->checksum.empty())
71+
std::transform(this->checksum.begin(), this->checksum.end(), this->checksum.begin(), ::tolower);
6772
}
6873

6974
DownloadTask::~DownloadTask()

tests/cpp-tests/Source/NetworkTest/DownloaderTest/DownloaderTest.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ USING_NS_AX;
3737
static const char* sURLList[] = {
3838
"https://www.cocos2d-x.org/attachments/802/cocos2dx_landscape.png", "https://cocos2d-x.org/images/logo.png",
3939
"https://www.cocos2d-x.org/attachments/1503/no_exist.txt", // try to download no exist file
40-
"https://ash-speed.hetzner.com/1GB.bin"
40+
"https://github.com/axmolengine/axmol/releases/download/v2.1.3/axmol-2.1.3.zip"
4141
};
4242
const static int sListSize = (sizeof(sURLList) / sizeof(sURLList[0]));
4343
static const char* sNameList[sListSize] = {
@@ -208,8 +208,7 @@ struct DownloaderTest : public TestCase
208208
bar->setVisible(true);
209209
bar->setEnabled(true);
210210
auto path = FileUtils::getInstance()->getWritablePath() + "CppTests/DownloaderTest/" + sNameList[3];
211-
auto task = this->downloader->createDownloadFileTask(sURLList[3], path, sNameList[3], "5fa2035a209e73f5727a72aafd332916", false);
212-
task->progressInfo.totalBytesExpected = 89945032;
211+
auto task = this->downloader->createDownloadFileTask(sURLList[3], path, sNameList[3], "1CF78E3F23A2B1A6806D8719A5771D34", false);
213212
});
214213
bottomRightView->setName(sNameList[3]);
215214
bottomRightView->setAnchorPoint(Vec2(0, 1));

0 commit comments

Comments
 (0)