42 =
"Could not create download thread!";
58 const std::string &
url,
60 const bool ignoreError,
70 mUpdateFunction(updateFunction),
75 mError(static_cast<char*>(calloc(CURL_ERROR_SIZE + 1, 1))),
76 mIgnoreError(ignoreError),
89 if (!serverName.empty())
91 if (
mUrl.find(
'?') == std::string::npos)
92 mUrl.append(
"?host=");
94 mUrl.append(
"&host=");
95 mUrl.append(serverName);
129 fseek(file, 0, SEEK_END);
130 const long fileSize = ftell(file);
138 char *
const buffer =
new char[
CAST_SIZE(fileSize)];
139 const uInt
read =
static_cast<uInt
>(fread(buffer, 1, fileSize, file));
140 unsigned long adler = adler32(0L,
nullptr, 0);
141 adler = adler32(
static_cast<uInt
>(adler),
142 reinterpret_cast<Bytef*
>(buffer),
read);
150 unsigned long adler = adler32(0L,
nullptr, 0);
151 return adler32(
static_cast<uInt
>(adler),
152 reinterpret_cast<const Bytef*
>(buffer),
size);
173 mAdler =
static_cast<unsigned long>(adler32);
249 bool complete =
false;
256 std::string outFilename;
278 while (attempts < 3 &&
289 d->
mCurl = curl_easy_init();
291 if (d->
mCurl !=
nullptr &&
295 FILE *file =
nullptr;
300 curl_easy_setopt(d->
mCurl, CURLOPT_URL, d->
mUrl.c_str());
302 curl_easy_setopt(d->
mCurl, CURLOPT_WRITEFUNCTION,
309 curl_easy_setopt(d->
mCurl, CURLOPT_FOLLOWLOCATION, 1);
310 curl_easy_setopt(d->
mCurl, CURLOPT_HTTPHEADER,
314 curl_easy_setopt(d->
mCurl, CURLOPT_FAILONERROR, 1);
315 curl_easy_setopt(d->
mCurl, CURLOPT_WRITEFUNCTION,
317 curl_easy_setopt(d->
mCurl, CURLOPT_WRITEDATA, d->
mPtr);
321 file = fopen(outFilename.c_str(),
"w+b");
324 curl_easy_setopt(d->
mCurl, CURLOPT_WRITEDATA,
328 curl_easy_setopt(d->
mCurl,
332 curl_easy_setopt(d->
mCurl, CURLOPT_ERRORBUFFER, d->
mError);
333 curl_easy_setopt(d->
mCurl, CURLOPT_URL, d->
mUrl.c_str());
334 curl_easy_setopt(d->
mCurl, CURLOPT_NOPROGRESS, 0);
335 curl_easy_setopt(d->
mCurl, CURLOPT_PROGRESSFUNCTION,
337 curl_easy_setopt(d->
mCurl, CURLOPT_PROGRESSDATA, ptr);
338 #if LIBCURL_VERSION_NUM >= 0x070a00
339 curl_easy_setopt(d->
mCurl, CURLOPT_NOSIGNAL, 1);
341 curl_easy_setopt(d->
mCurl, CURLOPT_CONNECTTIMEOUT, 30);
342 curl_easy_setopt(d->
mCurl, CURLOPT_TIMEOUT, 1800);
349 if ((res = curl_easy_perform(d->
mCurl)) != 0 &&
354 PRAGMA45(GCC diagnostic ignored
"-Wswitch-enum")
357 case CURLE_ABORTED_BY_CALLBACK:
360 case CURLE_COULDNT_CONNECT:
393 curl_easy_cleanup(d->
mCurl);
413 const unsigned long adler =
fadler32(file);
459 file = fopen(d->
mFileName.c_str(),
"rb");
476 if (d->
mCurl !=
nullptr)
478 curl_easy_cleanup(d->
mCurl);
497 else if (!complete || attempts >= 3)
517 curl_easy_setopt(curl, CURLOPT_PROXY,
525 curl_easy_setopt(curl, CURLOPT_PROXY,
"");
528 #if LIBCURL_VERSION_NUM >= 0x070300
529 curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL,
534 #if LIBCURL_VERSION_NUM >= 0x071304
535 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP_1_0);
537 #if LIBCURL_VERSION_NUM >= 0x070300
538 curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL,
543 #if LIBCURL_VERSION_NUM >= 0x070a00
544 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
548 #if LIBCURL_VERSION_NUM >= 0x071200
549 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
550 #elif LIBCURL_VERSION_NUM >= 0x071000
551 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
556 #if LIBCURL_VERSION_NUM >= 0x071200
557 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
562 #if LIBCURL_VERSION_NUM >= 0x071200
563 curl_easy_setopt(curl, CURLOPT_PROXYTYPE,
564 CURLPROXY_SOCKS5_HOSTNAME);
571 #if LIBCURL_VERSION_NUM >= 0x070a08
577 #if LIBCURL_VERSION_NUM >= 0x071304
578 curl_easy_setopt(curl, CURLOPT_PROTOCOLS,
579 CURLPROTO_HTTP | CURLPROTO_HTTPS);
580 curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS,
581 CURLPROTO_HTTP | CURLPROTO_HTTPS);
583 #if LIBCURL_VERSION_NUM >= 0x071500
584 curl_easy_setopt(curl, CURLOPT_WILDCARDMATCH, 0);
586 #if LIBCURL_VERSION_NUM >= 0x070f01
587 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3);
589 #if LIBCURL_VERSION_NUM >= 0x070a08
590 curl_easy_setopt(curl, CURLOPT_MAXFILESIZE, 536870912);
593 #if LIBCURL_VERSION_NUM >= 0x073100
594 curl_easy_setopt(curl, CURLOPT_TCP_FASTOPEN, 1L);
597 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
598 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60L);
601 #if LIBCURL_VERSION_NUM >= 0x071507
604 curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING,
"");
616 #if LIBCURL_VERSION_NUM >= 0x072D00
617 curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL,
"http");
623 curl_httppost *lastPtr =
nullptr;
626 file.open(
fileName.c_str(), std::ios::in);
630 char *line =
new char[10001];
631 std::ostringstream str;
632 while (file.getline(line, 10000))
637 curl_formadd(form, &lastPtr,
638 CURLFORM_COPYNAME,
"f:1",
639 CURLFORM_COPYCONTENTS, str.str().c_str(),
648 const size_t totalMem =
size * nmemb;
649 char *buf =
new char[totalMem + 1];
650 memcpy(buf, ptr, totalMem);
std::string getStringValue(const std::string &key) const
int getIntValue(const std::string &key) const
void log(const char *const log_text,...)
void log_r(const char *const log_text,...)
void log1(const char *const log_text)
static std::string mUploadResponse
void setFile(const std::string &filename, const int64_t adler32)
static unsigned long fadler32(FILE *const file)
void addHeader(const std::string &header)
static void addCommonFlags(CURL *const curl)
static void secureCurl(CURL *const curl)
static unsigned long adlerBuffer(const char *const buffer, int size)
static void addProxy(CURL *const curl)
std::queue< std::string > mUrlQueue
void setWriteFunction(WriteFunction write)
WriteFunction mWriteFunction
static void prepareForm(curl_httppost **form, const std::string &fileName)
struct Net::Download::@15 mOptions
const char * getError() const
curl_httppost * mFormPost
Download(void *const ptr, const std::string &url, const DownloadUpdate updateFunction, const bool ignoreError, const bool isUpload, const bool isXml)
static void addHeaders(CURL *const curl)
static int downloadThread(void *ptr)
DownloadUpdate mUpdateFunction
static size_t writeFunction(void *ptr, size_t size, size_t nmemb, void *stream)
static int downloadProgress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
static bool validateXml(const std::string &fileName)
char * DOWNLOAD_ERROR_MESSAGE_THREAD
volatile bool isTerminate
int(* DownloadUpdate)(void *ptr, const DownloadStatusT status, size_t total, const size_t remaining)
size_t(* WriteFunction)(void *ptr, size_t size, size_t nmemb, void *stream)
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wredundant-decls") PRAGMA45(GCC diagnostic pop) class TestMain
bool url(InputEvent &event)
int renameFile(const std::string &pFrom, const std::string &pTo)
SDL_Thread * createThread(int(*fn)(void *), const char *const name, void *const data)
void WaitThread(SDL_Thread *const thread)
bool remove(const std::string &filename)
int64_t write(File *const file, const void *const buffer, const uint32_t objSize, const uint32_t objCount)
int64_t read(File *const file, void *const buffer, const uint32_t objSize, const uint32_t objCount)
ItemOptionDb::OptionInfos mOptions
#define FUNC_BLOCK(name, id)