ManaPlus
Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes
Net::Download Class Reference

#include <download.h>

Public Member Functions

 Download (void *const ptr, const std::string &url, const DownloadUpdate updateFunction, const bool ignoreError, const bool isUpload, const bool isXml)
 
 ~Download ()
 
void addHeader (const std::string &header)
 
void noCache ()
 
void setFile (const std::string &filename, const int64_t adler32)
 
void setWriteFunction (WriteFunction write)
 
bool start ()
 
void cancel ()
 
void addMirror (const std::string &str)
 
const char * getError () const
 
void setIgnoreError (const bool n)
 

Static Public Member Functions

static size_t writeFunction (void *ptr, size_t size, size_t nmemb, void *stream)
 
static void prepareForm (curl_httppost **form, const std::string &fileName)
 
static unsigned long fadler32 (FILE *const file)
 
static void addProxy (CURL *const curl)
 
static void secureCurl (CURL *const curl)
 
static void addHeaders (CURL *const curl)
 
static void addCommonFlags (CURL *const curl)
 
static unsigned long adlerBuffer (const char *const buffer, int size)
 
static std::string getUploadResponse ()
 

Static Private Member Functions

static int downloadThread (void *ptr)
 
static int downloadProgress (void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
 

Private Attributes

void * mPtr
 
std::string mUrl
 
struct {
   unsigned   cancel: 1
 
   unsigned   memoryWrite: 1
 
   unsigned   checkAdler: 1
 
mOptions
 
std::string mFileName
 
std::queue< std::string > mUrlQueue
 
WriteFunction mWriteFunction
 
unsigned long mAdler
 
DownloadUpdate mUpdateFunction
 
SDL_Thread * mThread
 
CURL * mCurl
 
curl_slist * mHeaders
 
curl_httppost * mFormPost
 
char * mError
 
bool mIgnoreError
 
bool mUpload
 
bool mIsXml
 

Static Private Attributes

static std::string mUploadResponse
 

Detailed Description

Definition at line 48 of file download.h.

Constructor & Destructor Documentation

◆ Download()

Net::Download::Download ( void *const  ptr,
const std::string &  url,
const DownloadUpdate  updateFunction,
const bool  ignoreError,
const bool  isUpload,
const bool  isXml 
)

Definition at line 57 of file download.cpp.

62  :
63  mPtr(ptr),
64  mUrl(url),
65  mOptions(),
66  mFileName(),
67  mUrlQueue(),
68  mWriteFunction(nullptr),
69  mAdler(0),
70  mUpdateFunction(updateFunction),
71  mThread(nullptr),
72  mCurl(nullptr),
73  mHeaders(nullptr),
74  mFormPost(nullptr),
75  mError(static_cast<char*>(calloc(CURL_ERROR_SIZE + 1, 1))),
76  mIgnoreError(ignoreError),
77  mUpload(isUpload),
78  mIsXml(isXml)
79 {
80  if (mError != nullptr)
81  mError[0] = 0;
82 
83  mOptions.cancel = 0;
84  mOptions.memoryWrite = 0;
85  mOptions.checkAdler = 1U;
86  if (!mUpload)
87  {
88  const std::string serverName = settings.serverName;
89  if (!serverName.empty())
90  {
91  if (mUrl.find('?') == std::string::npos)
92  mUrl.append("?host=");
93  else
94  mUrl.append("&host=");
95  mUrl.append(serverName);
96  }
97  }
98  mUrlQueue.push(url);
99 }
bool mUpload
Definition: download.h:142
char * mError
Definition: download.h:140
std::queue< std::string > mUrlQueue
Definition: download.h:132
WriteFunction mWriteFunction
Definition: download.h:133
unsigned long mAdler
Definition: download.h:134
void * mPtr
Definition: download.h:123
struct Net::Download::@15 mOptions
curl_httppost * mFormPost
Definition: download.h:139
curl_slist * mHeaders
Definition: download.h:138
SDL_Thread * mThread
Definition: download.h:136
DownloadUpdate mUpdateFunction
Definition: download.h:135
std::string mFileName
Definition: download.h:131
CURL * mCurl
Definition: download.h:137
std::string mUrl
Definition: download.h:124
bool mIgnoreError
Definition: download.h:141
std::string serverName
Definition: settings.h:114
bool url(InputEvent &event)
Definition: commands.cpp:64
Settings settings
Definition: settings.cpp:32

References mError, mOptions, mUpload, mUrl, mUrlQueue, Settings::serverName, settings, and Actions::url().

◆ ~Download()

Net::Download::~Download ( )

Definition at line 101 of file download.cpp.

102 {
103  if (mFormPost != nullptr)
104  {
105  curl_formfree(mFormPost);
106  mFormPost = nullptr;
107  }
108 
109  if (mHeaders != nullptr)
110  {
111  curl_slist_free_all(mHeaders);
112  mHeaders = nullptr;
113  }
114 
116  mThread = nullptr;
117  free(mError);
118 }
void WaitThread(SDL_Thread *const thread)
Definition: sdlhelper.cpp:195

References mError, mFormPost, mHeaders, mThread, and SDL::WaitThread().

Member Function Documentation

◆ addCommonFlags()

void Net::Download::addCommonFlags ( CURL *const  curl)
static

Definition at line 613 of file download.cpp.

614 {
615  curl_easy_setopt(curl, CURLOPT_STDERR, logger->getFile());
616 #if LIBCURL_VERSION_NUM >= 0x072D00
617  curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http");
618 #endif // LIBCURL_VERSION_NUM >= 0x072D00
619 }
FILE * getFile() const
Definition: logger.cpp:552
Logger * logger
Definition: logger.cpp:89

References Logger::getFile(), and logger.

Referenced by WhoIsOnline::downloadThread(), and downloadThread().

◆ addHeader()

void Net::Download::addHeader ( const std::string &  header)

Definition at line 155 of file download.cpp.

156 {
157  mHeaders = curl_slist_append(mHeaders, header.c_str());
158 }

References mHeaders.

Referenced by noCache().

◆ addHeaders()

void Net::Download::addHeaders ( CURL *const  curl)
static

Definition at line 608 of file download.cpp.

609 {
610 }

Referenced by WhoIsOnline::downloadThread(), and downloadThread().

◆ addMirror()

void Net::Download::addMirror ( const std::string &  str)
inline

Definition at line 88 of file download.h.

89  { mUrlQueue.push(str); }

References mUrlQueue.

Referenced by UpdaterWindow::download(), and ServerDialog::downloadServerList().

◆ addProxy()

void Net::Download::addProxy ( CURL *const  curl)
static

Definition at line 509 of file download.cpp.

510 {
511  const int mode = config.getIntValue("downloadProxyType");
512  if (mode == 0)
513  return;
514 
515  if (mode > 1)
516  {
517  curl_easy_setopt(curl, CURLOPT_PROXY,
518  config.getStringValue("downloadProxy").c_str());
519  }
520 
521  switch (mode)
522  {
523  case 1: // direct connection
524  default:
525  curl_easy_setopt(curl, CURLOPT_PROXY, "");
526  break;
527  case 2: // HTTP
528 #if LIBCURL_VERSION_NUM >= 0x070300
529  curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL,
530  config.getIntValue("downloadProxyTunnel"));
531 #endif // LIBCURL_VERSION_NUM >= 0x070300
532  break;
533  case 3: // HTTP 1.0
534 #if LIBCURL_VERSION_NUM >= 0x071304
535  curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP_1_0);
536 #endif // LIBCURL_VERSION_NUM >= 0x071304
537 #if LIBCURL_VERSION_NUM >= 0x070300
538  curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL,
539  config.getIntValue("downloadProxyTunnel"));
540 #endif // LIBCURL_VERSION_NUM >= 0x070300
541  break;
542  case 4: // SOCKS4
543 #if LIBCURL_VERSION_NUM >= 0x070a00
544  curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
545 #endif // LIBCURL_VERSION_NUM >= 0x070a00
546  break;
547  case 5: // SOCKS4A
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);
552 #endif // LIBCURL_VERSION_NUM >= 0x071200
553 
554  break;
555  case 6: // SOCKS5
556 #if LIBCURL_VERSION_NUM >= 0x071200
557  curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
558 #endif // LIBCURL_VERSION_NUM >= 0x071200
559 
560  break;
561  case 7: // SOCKS5 hostname
562 #if LIBCURL_VERSION_NUM >= 0x071200
563  curl_easy_setopt(curl, CURLOPT_PROXYTYPE,
564  CURLPROXY_SOCKS5_HOSTNAME);
565 #endif // LIBCURL_VERSION_NUM >= 0x071200
566 
567  break;
568  }
569 }
std::string getStringValue(const std::string &key) const
int getIntValue(const std::string &key) const
Configuration config

References config, Configuration::getIntValue(), and Configuration::getStringValue().

Referenced by WhoIsOnline::downloadThread(), and downloadThread().

◆ adlerBuffer()

unsigned long Net::Download::adlerBuffer ( const char *const  buffer,
int  size 
)
static

Definition at line 147 of file download.cpp.

148 {
149  FUNC_BLOCK("Download::adlerBuffer", 1)
150  unsigned long adler = adler32(0L, nullptr, 0);
151  return adler32(static_cast<uInt>(adler),
152  reinterpret_cast<const Bytef*>(buffer), size);
153 }
int size()
Definition: emotedb.cpp:306
#define FUNC_BLOCK(name, id)
Definition: perfomance.h:81

References FUNC_BLOCK, and EmoteDB::size().

Referenced by UpdaterWindow::getFileHash().

◆ cancel()

void Net::Download::cancel ( )

Cancels the download. Returns immediately, the cancelled status will be noted in the next avialable update call.

◆ downloadProgress()

int Net::Download::downloadProgress ( void *  clientp,
double  dltotal,
double  dlnow,
double  ultotal,
double  ulnow 
)
staticprivate

Definition at line 223 of file download.cpp.

225 {
226  Download *const d = reinterpret_cast<Download *>(clientp);
227 
228  if (d == nullptr)
229  return -5;
230 
231  if (d->mUpload)
232  return 0;
233 
234  if (d->mOptions.cancel != 0U)
235  {
236  return d->mUpdateFunction(d->mPtr, DownloadStatus::Cancelled,
237  CAST_SIZE(dltotal),
238  CAST_SIZE(dlnow));
239  }
240 
241  return d->mUpdateFunction(d->mPtr, DownloadStatus::Idle,
242  CAST_SIZE(dltotal),
243  CAST_SIZE(dlnow));
244 }
#define CAST_SIZE
Definition: cast.h:34
Download(void *const ptr, const std::string &url, const DownloadUpdate updateFunction, const bool ignoreError, const bool isUpload, const bool isXml)
Definition: download.cpp:57

References cancel, DownloadStatus::Cancelled, CAST_SIZE, DownloadStatus::Idle, mOptions, mPtr, mUpdateFunction, and mUpload.

Referenced by downloadThread().

◆ downloadThread()

int Net::Download::downloadThread ( void *  ptr)
staticprivate

Definition at line 246 of file download.cpp.

247 {
248  int attempts = 0;
249  bool complete = false;
250  Download *const d = reinterpret_cast<Download*>(ptr);
251  CURLcode res;
252 
253  if (d == nullptr)
254  return 0;
255 
256  std::string outFilename;
257  if (d->mUpload)
258  {
259  outFilename = d->mFileName;
260  prepareForm(&d->mFormPost, outFilename);
261  }
262  else
263  {
264  if (d->mOptions.memoryWrite == 0U)
265  outFilename = d->mFileName + ".part";
266  else
267  outFilename.clear();
268  }
269 
270  while (!d->mUrlQueue.empty())
271  {
272  attempts = 0;
273  complete = false;
274  d->mUrl = d->mUrlQueue.front();
275  d->mUrlQueue.pop();
276 
277  logger->log_r("selected url: %s", d->mUrl.c_str());
278  while (attempts < 3 &&
279  !complete &&
280  (d->mOptions.cancel == 0U) &&
281  isTerminate == false)
282  {
283  d->mUpdateFunction(d->mPtr, DownloadStatus::Starting, 0, 0);
284 
285  if ((d->mOptions.cancel != 0U) || isTerminate == true)
286  {
287  return 0;
288  }
289  d->mCurl = curl_easy_init();
290 
291  if (d->mCurl != nullptr &&
292  d->mOptions.cancel == 0U &&
293  isTerminate == false)
294  {
295  FILE *file = nullptr;
296 
297  if (d->mUpload)
298  {
299  logger->log_r("Uploading: %s", d->mUrl.c_str());
300  curl_easy_setopt(d->mCurl, CURLOPT_URL, d->mUrl.c_str());
301  curl_easy_setopt(d->mCurl, CURLOPT_HTTPPOST, d->mFormPost);
302  curl_easy_setopt(d->mCurl, CURLOPT_WRITEFUNCTION,
304  mUploadResponse.clear();
305  }
306  else
307  {
308  logger->log_r("Downloading: %s", d->mUrl.c_str());
309  curl_easy_setopt(d->mCurl, CURLOPT_FOLLOWLOCATION, 1);
310  curl_easy_setopt(d->mCurl, CURLOPT_HTTPHEADER,
311  d->mHeaders);
312  if (d->mOptions.memoryWrite != 0U)
313  {
314  curl_easy_setopt(d->mCurl, CURLOPT_FAILONERROR, 1);
315  curl_easy_setopt(d->mCurl, CURLOPT_WRITEFUNCTION,
316  d->mWriteFunction);
317  curl_easy_setopt(d->mCurl, CURLOPT_WRITEDATA, d->mPtr);
318  }
319  else
320  {
321  file = fopen(outFilename.c_str(), "w+b");
322  if (file != nullptr)
323  {
324  curl_easy_setopt(d->mCurl, CURLOPT_WRITEDATA,
325  file);
326  }
327  }
328  curl_easy_setopt(d->mCurl,
329  CURLOPT_USERAGENT,
330  settings.userAgent.c_str());
331 
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);
340 #endif // LIBCURL_VERSION_NUM >= 0x070a00
341  curl_easy_setopt(d->mCurl, CURLOPT_CONNECTTIMEOUT, 30);
342  curl_easy_setopt(d->mCurl, CURLOPT_TIMEOUT, 1800);
343  addHeaders(d->mCurl);
344  addProxy(d->mCurl);
345  secureCurl(d->mCurl);
346  }
347  addCommonFlags(d->mCurl);
348 
349  if ((res = curl_easy_perform(d->mCurl)) != 0 &&
350  (d->mOptions.cancel == 0U) &&
351  isTerminate == false)
352  {
353  PRAGMA45(GCC diagnostic push)
354  PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
355  switch (res)
356  {
357  case CURLE_ABORTED_BY_CALLBACK:
358  d->mOptions.cancel = 1U;
359  break;
360  case CURLE_COULDNT_CONNECT:
361  default:
362  break;
363  }
364  PRAGMA45(GCC diagnostic pop)
365 
366  if (res != 0U)
367  {
368  if (d->mError != nullptr)
369  {
370  logger->log_r("curl error %d: %s host: %s",
371  res, d->mError, d->mUrl.c_str());
372  }
373  attempts++;
374  continue;
375  }
376 
377  if ((d->mOptions.cancel != 0U) || isTerminate == true)
378  break;
379 
380 // d->mUpdateFunction(d->mPtr, DownloadStatus::Error, 0, 0);
381 
382  if (file != nullptr)
383  {
384  fclose(file);
385  file = nullptr;
386  }
387  if (!d->mUpload && (d->mOptions.memoryWrite == 0U))
388  ::remove(outFilename.c_str());
389  attempts++;
390  continue;
391  }
392 
393  curl_easy_cleanup(d->mCurl);
394  d->mCurl = nullptr;
395 
396  if (d->mUpload)
397  {
398  if (file != nullptr)
399  {
400  fclose(file);
401  file = nullptr;
402  }
403  // need check first if we read data from server
404  complete = true;
405  }
406  else
407  {
408  if (d->mOptions.memoryWrite == 0U)
409  {
410  // Don't check resources.xml checksum
411  if (d->mOptions.checkAdler != 0U)
412  {
413  const unsigned long adler = fadler32(file);
414 
415  if (d->mAdler != adler)
416  {
417  if (file != nullptr)
418  {
419  fclose(file);
420  file = nullptr;
421  }
422 
423  // Remove the corrupted file
424  ::remove(d->mFileName.c_str());
425  logger->log_r("Checksum for file %s failed:"
426  " (%lx/%lx)",
427  d->mFileName.c_str(),
428  adler, d->mAdler);
429  attempts++;
430  continue;
431  }
432  }
433 
434  if (file != nullptr)
435  {
436  fclose(file);
437  file = nullptr;
438  }
439 
440  // Any existing file with this name is deleted first,
441  // otherwise the rename will fail on Windows.
442  if ((d->mOptions.cancel == 0U) && isTerminate == false)
443  {
444  if (d->mIsXml)
445  {
446  if (!XML::Document::validateXml(outFilename))
447  {
448  logger->log_r("Xml file validation error");
449  attempts++;
450  continue;
451  }
452  }
453 
454  ::remove(d->mFileName.c_str());
455  Files::renameFile(outFilename, d->mFileName);
456 
457  // Check if we can open it and no errors were
458  // encountered during renaming
459  file = fopen(d->mFileName.c_str(), "rb");
460  if (file != nullptr)
461  {
462  fclose(file);
463  file = nullptr;
464  complete = true;
465  }
466  }
467  }
468  else
469  {
470  // It's stored in memory, we're done
471  complete = true;
472  }
473  }
474  }
475 
476  if (d->mCurl != nullptr)
477  {
478  curl_easy_cleanup(d->mCurl);
479  d->mCurl = nullptr;
480  }
481 
482  if ((d->mOptions.cancel != 0U) || isTerminate == true)
483  {
484  return 0;
485  }
486  attempts++;
487  }
488 
489  if ((complete && attempts < 3) || (d->mOptions.cancel != 0U))
490  break;
491  }
492 
493  if ((d->mOptions.cancel != 0U) || isTerminate == true)
494  {
495  // Nothing to do...
496  }
497  else if (!complete || attempts >= 3)
498  {
499  d->mUpdateFunction(d->mPtr, DownloadStatus::Error, 0, 0);
500  }
501  else
502  {
503  d->mUpdateFunction(d->mPtr, DownloadStatus::Complete, 0, 0);
504  }
505 
506  return 0;
507 }
void log_r(const char *const log_text,...)
Definition: logger.cpp:365
static std::string mUploadResponse
Definition: download.h:122
static unsigned long fadler32(FILE *const file)
Definition: download.cpp:123
static void addCommonFlags(CURL *const curl)
Definition: download.cpp:613
static void secureCurl(CURL *const curl)
Definition: download.cpp:574
static void addProxy(CURL *const curl)
Definition: download.cpp:509
static void prepareForm(curl_httppost **form, const std::string &fileName)
Definition: download.cpp:621
static void addHeaders(CURL *const curl)
Definition: download.cpp:608
static size_t writeFunction(void *ptr, size_t size, size_t nmemb, void *stream)
Definition: download.cpp:643
static int downloadProgress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
Definition: download.cpp:223
std::string userAgent
Definition: settings.h:128
static bool validateXml(const std::string &fileName)
Definition: libxml.cpp:320
volatile bool isTerminate
Definition: client.cpp:209
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wredundant-decls") PRAGMA45(GCC diagnostic pop) class TestMain
int renameFile(const std::string &pFrom, const std::string &pTo)
Definition: files.cpp:134
bool remove(const std::string &filename)
Definition: fs.cpp:780

References addCommonFlags(), addHeaders(), addProxy(), cancel, checkAdler, DownloadStatus::Complete, downloadProgress(), DownloadStatus::Error, fadler32(), isTerminate, Logger::log_r(), logger, mAdler, mCurl, memoryWrite, mError, mFileName, mFormPost, mHeaders, mIsXml, mOptions, mPtr, mUpdateFunction, mUpload, mUploadResponse, mUrl, mUrlQueue, mWriteFunction, PRAGMA45(), prepareForm(), VirtFs::remove(), Files::renameFile(), secureCurl(), settings, DownloadStatus::Starting, Settings::userAgent, XML::Document::validateXml(), and writeFunction().

Referenced by start().

◆ fadler32()

unsigned long Net::Download::fadler32 ( FILE *const  file)
static

Calculates the Alder-32 checksum for the given file.

Definition at line 123 of file download.cpp.

124 {
125  if (file == nullptr)
126  return 0;
127 
128  // Obtain file size
129  fseek(file, 0, SEEK_END);
130  const long fileSize = ftell(file);
131  if (fileSize < 0)
132  { // file size error
133  return 0;
134  }
135  rewind(file);
136 
137  // Calculate Adler-32 checksum
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);
143  delete [] buffer;
144  return adler;
145 }
int64_t read(File *const file, void *const buffer, const uint32_t objSize, const uint32_t objCount)
Definition: fs.cpp:815

References CAST_SIZE, and VirtFs::read().

Referenced by downloadThread(), and UpdaterWindow::validateFile().

◆ getError()

char * Net::Download::getError ( ) const

Definition at line 218 of file download.cpp.

219 {
220  return mError;
221 }

References mError.

Referenced by ServerDialog::downloadUpdate(), and UpdaterWindow::logic().

◆ getUploadResponse()

static std::string Net::Download::getUploadResponse ( )
inlinestatic

Definition at line 114 of file download.h.

115  { return mUploadResponse; }

References mUploadResponse.

Referenced by Actions::uploadUpdate().

◆ noCache()

void Net::Download::noCache ( )

Convience method for adding no-cache headers.

Definition at line 160 of file download.cpp.

161 {
162  addHeader("pragma: no-cache");
163  addHeader("Cache-Control: no-cache");
164 }
void addHeader(const std::string &header)
Definition: download.cpp:155

References addHeader().

Referenced by UpdaterWindow::download().

◆ prepareForm()

void Net::Download::prepareForm ( curl_httppost **  form,
const std::string &  fileName 
)
static

Definition at line 621 of file download.cpp.

622 {
623  curl_httppost *lastPtr = nullptr;
624 
625  std::ifstream file;
626  file.open(fileName.c_str(), std::ios::in);
627  if (!file.is_open())
628  return;
629 
630  char *line = new char[10001];
631  std::ostringstream str;
632  while (file.getline(line, 10000))
633  str << line << "\n";
634 
635  delete [] line;
636 
637  curl_formadd(form, &lastPtr,
638  CURLFORM_COPYNAME, "f:1",
639  CURLFORM_COPYCONTENTS, str.str().c_str(),
640  CURLFORM_END);
641 }
std::string fileName
Definition: testmain.cpp:39

References fileName.

Referenced by downloadThread().

◆ secureCurl()

void Net::Download::secureCurl ( CURL *const  curl)
static

Definition at line 574 of file download.cpp.

576 {
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);
582 #endif // LIBCURL_VERSION_NUM >= 0x071304
583 #if LIBCURL_VERSION_NUM >= 0x071500
584  curl_easy_setopt(curl, CURLOPT_WILDCARDMATCH, 0);
585 #endif // LIBCURL_VERSION_NUM >= 0x071500
586 #if LIBCURL_VERSION_NUM >= 0x070f01
587  curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3);
588 #endif // LIBCURL_VERSION_NUM >= 0x070f01
589 #if LIBCURL_VERSION_NUM >= 0x070a08
590  curl_easy_setopt(curl, CURLOPT_MAXFILESIZE, 536870912);
591 #endif // LIBCURL_VERSION_NUM >= 0x070a08
592 
593 #if LIBCURL_VERSION_NUM >= 0x073100
594  curl_easy_setopt(curl, CURLOPT_TCP_FASTOPEN, 1L);
595 #endif // LIBCURL_VERSION_NUM >= 0x073100
596 
597  curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
598  curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60L);
599 }

Referenced by WhoIsOnline::downloadThread(), and downloadThread().

◆ setFile()

void Net::Download::setFile ( const std::string &  filename,
const int64_t  adler32 
)

Definition at line 166 of file download.cpp.

167 {
168  mOptions.memoryWrite = 0;
169  mFileName = filename;
170 
171  if (adler32 > -1)
172  {
173  mAdler = static_cast<unsigned long>(adler32);
174  mOptions.checkAdler = 1U;
175  }
176  else
177  {
178  mOptions.checkAdler = 0;
179  }
180 }

References mAdler, mFileName, and mOptions.

Referenced by UpdaterWindow::download(), ServerDialog::downloadServerList(), and Actions::uploadFile().

◆ setIgnoreError()

void Net::Download::setIgnoreError ( const bool  n)
inline

Definition at line 93 of file download.h.

94  { mIgnoreError = n; }

References mIgnoreError.

◆ setWriteFunction()

void Net::Download::setWriteFunction ( WriteFunction  write)

Definition at line 182 of file download.cpp.

183 {
184  mOptions.memoryWrite = 1U;
186 }
int64_t write(File *const file, const void *const buffer, const uint32_t objSize, const uint32_t objCount)
Definition: fs.cpp:826

References mOptions, mWriteFunction, and VirtFs::write().

Referenced by UpdaterWindow::download().

◆ start()

bool Net::Download::start ( )

Starts the download thread.

Returns
true if thread was created false if the thread could not be made or download wasn't properly setup

Definition at line 188 of file download.cpp.

189 {
190  if (mUpload)
191  logger->log("Starting upload: %s", mUrl.c_str());
192  else
193  logger->log("Starting download: %s", mUrl.c_str());
194 
195  mThread = SDL::createThread(&downloadThread, "download", this);
196  if (mThread == nullptr)
197  {
199  if (mError != nullptr)
202  if (!mIgnoreError)
203  return false;
204  }
205 
206  return true;
207 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
void log1(const char *const log_text)
Definition: logger.cpp:238
static int downloadThread(void *ptr)
Definition: download.cpp:246
char * DOWNLOAD_ERROR_MESSAGE_THREAD
Definition: download.cpp:42
SDL_Thread * createThread(int(*fn)(void *), const char *const name, void *const data)
Definition: sdlhelper.cpp:118

References SDL::createThread(), DOWNLOAD_ERROR_MESSAGE_THREAD, downloadThread(), Logger::log(), Logger::log1(), logger, mError, mIgnoreError, mPtr, mThread, mUpdateFunction, mUpload, mUrl, and DownloadStatus::ThreadError.

Referenced by UpdaterWindow::download(), ServerDialog::downloadServerList(), and Actions::uploadFile().

◆ writeFunction()

size_t Net::Download::writeFunction ( void *  ptr,
size_t  size,
size_t  nmemb,
void *  stream 
)
static

Definition at line 643 of file download.cpp.

647 {
648  const size_t totalMem = size * nmemb;
649  char *buf = new char[totalMem + 1];
650  memcpy(buf, ptr, totalMem);
651  buf[totalMem] = 0;
652  mUploadResponse.append(buf);
653  delete [] buf;
654  return totalMem;
655 }

References mUploadResponse, and EmoteDB::size().

Referenced by downloadThread().

Field Documentation

◆ cancel

void Net::Download::cancel

◆ checkAdler

unsigned Net::Download::checkAdler

Definition at line 129 of file download.h.

Referenced by downloadThread().

◆ mAdler

unsigned long Net::Download::mAdler
private

Definition at line 134 of file download.h.

Referenced by downloadThread(), and setFile().

◆ mCurl

CURL* Net::Download::mCurl
private

Definition at line 137 of file download.h.

Referenced by downloadThread().

◆ memoryWrite

unsigned Net::Download::memoryWrite

Definition at line 128 of file download.h.

Referenced by downloadThread().

◆ mError

char* Net::Download::mError
private

Definition at line 140 of file download.h.

Referenced by Download(), downloadThread(), getError(), start(), and ~Download().

◆ mFileName

std::string Net::Download::mFileName
private

Definition at line 131 of file download.h.

Referenced by downloadThread(), and setFile().

◆ mFormPost

curl_httppost* Net::Download::mFormPost
private

Definition at line 139 of file download.h.

Referenced by downloadThread(), and ~Download().

◆ mHeaders

curl_slist* Net::Download::mHeaders
private

Definition at line 138 of file download.h.

Referenced by addHeader(), downloadThread(), and ~Download().

◆ mIgnoreError

bool Net::Download::mIgnoreError
private

Definition at line 141 of file download.h.

Referenced by setIgnoreError(), and start().

◆ mIsXml

bool Net::Download::mIsXml
private

Definition at line 143 of file download.h.

Referenced by downloadThread().

◆ 

struct { ... } Net::Download::mOptions

◆ mPtr

void* Net::Download::mPtr
private

Definition at line 123 of file download.h.

Referenced by downloadProgress(), downloadThread(), and start().

◆ mThread

SDL_Thread* Net::Download::mThread
private

Definition at line 136 of file download.h.

Referenced by start(), and ~Download().

◆ mUpdateFunction

DownloadUpdate Net::Download::mUpdateFunction
private

Definition at line 135 of file download.h.

Referenced by downloadProgress(), downloadThread(), and start().

◆ mUpload

bool Net::Download::mUpload
private

Definition at line 142 of file download.h.

Referenced by Download(), downloadProgress(), downloadThread(), and start().

◆ mUploadResponse

std::string Net::Download::mUploadResponse
staticprivate

Definition at line 122 of file download.h.

Referenced by downloadThread(), getUploadResponse(), and writeFunction().

◆ mUrl

std::string Net::Download::mUrl
private

Definition at line 124 of file download.h.

Referenced by Download(), downloadThread(), and start().

◆ mUrlQueue

std::queue<std::string> Net::Download::mUrlQueue
private

Definition at line 132 of file download.h.

Referenced by addMirror(), Download(), and downloadThread().

◆ mWriteFunction

WriteFunction Net::Download::mWriteFunction
private

Definition at line 133 of file download.h.

Referenced by downloadThread(), and setWriteFunction().


The documentation for this class was generated from the following files: