1 |
|
|
/* |
2 |
|
|
* The ManaPlus Client |
3 |
|
|
* Copyright (C) 2009-2010 The Mana Developers |
4 |
|
|
* Copyright (C) 2011-2019 The ManaPlus Developers |
5 |
|
|
* Copyright (C) 2019-2021 Andrei Karas |
6 |
|
|
* |
7 |
|
|
* This file is part of The ManaPlus Client. |
8 |
|
|
* |
9 |
|
|
* This program is free software; you can redistribute it and/or modify |
10 |
|
|
* it under the terms of the GNU General Public License as published by |
11 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
12 |
|
|
* any later version. |
13 |
|
|
* |
14 |
|
|
* This program is distributed in the hope that it will be useful, |
15 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
|
|
* GNU General Public License for more details. |
18 |
|
|
* |
19 |
|
|
* You should have received a copy of the GNU General Public License |
20 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
#ifndef NET_DOWNLOAD_H |
24 |
|
|
#define NET_DOWNLOAD_H |
25 |
|
|
|
26 |
|
|
#include "enums/net/downloadstatus.h" |
27 |
|
|
|
28 |
|
|
#include <curl/curl.h> |
29 |
|
|
|
30 |
|
|
#include <string> |
31 |
|
|
#include <queue> |
32 |
|
|
|
33 |
|
|
#include "localconsts.h" |
34 |
|
|
|
35 |
|
|
typedef int (*DownloadUpdate)(void *ptr, |
36 |
|
|
const DownloadStatusT status, |
37 |
|
|
size_t total, |
38 |
|
|
const size_t remaining); |
39 |
|
|
|
40 |
|
|
// Matches what CURL expects |
41 |
|
|
typedef size_t (*WriteFunction)( void *ptr, size_t size, size_t nmemb, |
42 |
|
|
void *stream); |
43 |
|
|
|
44 |
|
|
struct SDL_Thread; |
45 |
|
|
|
46 |
|
|
namespace Net |
47 |
|
|
{ |
48 |
|
|
class Download final |
49 |
|
|
{ |
50 |
|
|
public: |
51 |
|
|
Download(void *const ptr, |
52 |
|
|
const std::string &url, |
53 |
|
|
const DownloadUpdate updateFunction, |
54 |
|
|
const bool ignoreError, |
55 |
|
|
const bool isUpload, |
56 |
|
|
const bool isXml); |
57 |
|
|
|
58 |
|
|
A_DELETE_COPY(Download) |
59 |
|
|
|
60 |
|
|
~Download(); |
61 |
|
|
|
62 |
|
|
void addHeader(const std::string &header); |
63 |
|
|
|
64 |
|
|
/** |
65 |
|
|
* Convience method for adding no-cache headers. |
66 |
|
|
*/ |
67 |
|
|
void noCache(); |
68 |
|
|
|
69 |
|
|
void setFile(const std::string &filename, |
70 |
|
|
const int64_t adler32); |
71 |
|
|
|
72 |
|
|
void setWriteFunction(WriteFunction write); |
73 |
|
|
|
74 |
|
|
/** |
75 |
|
|
* Starts the download thread. |
76 |
|
|
* @returns true if thread was created |
77 |
|
|
* false if the thread could not be made or download wasn't |
78 |
|
|
* properly setup |
79 |
|
|
*/ |
80 |
|
|
bool start(); |
81 |
|
|
|
82 |
|
|
/** |
83 |
|
|
* Cancels the download. Returns immediately, the cancelled status will |
84 |
|
|
* be noted in the next avialable update call. |
85 |
|
|
*/ |
86 |
|
|
void cancel(); |
87 |
|
|
|
88 |
|
|
void addMirror(const std::string &str) |
89 |
|
1 |
{ mUrlQueue.push(str); } |
90 |
|
|
|
91 |
|
|
const char *getError() const A_WARN_UNUSED; |
92 |
|
|
|
93 |
|
|
void setIgnoreError(const bool n) |
94 |
|
|
{ mIgnoreError = n; } |
95 |
|
|
|
96 |
|
|
static size_t writeFunction(void *ptr, size_t size, |
97 |
|
|
size_t nmemb, void *stream); |
98 |
|
|
|
99 |
|
|
static void prepareForm(curl_httppost **form, |
100 |
|
|
const std::string &fileName); |
101 |
|
|
|
102 |
|
|
static unsigned long fadler32(FILE *const file) A_WARN_UNUSED; |
103 |
|
|
|
104 |
|
|
static void addProxy(CURL *const curl); |
105 |
|
|
|
106 |
|
|
static void secureCurl(CURL *const curl); |
107 |
|
|
|
108 |
|
|
static void addHeaders(CURL *const curl); |
109 |
|
|
|
110 |
|
|
static void addCommonFlags(CURL *const curl); |
111 |
|
|
|
112 |
|
|
static unsigned long adlerBuffer(const char *const buffer, int size); |
113 |
|
|
|
114 |
|
|
static std::string getUploadResponse() |
115 |
|
|
{ return mUploadResponse; } |
116 |
|
|
|
117 |
|
|
private: |
118 |
|
|
static int downloadThread(void *ptr); |
119 |
|
|
static int downloadProgress(void *clientp, double dltotal, |
120 |
|
|
double dlnow, double ultotal, |
121 |
|
|
double ulnow); |
122 |
|
|
static std::string mUploadResponse; |
123 |
|
|
void *mPtr; |
124 |
|
|
std::string mUrl; |
125 |
|
|
struct |
126 |
|
|
{ |
127 |
|
|
unsigned cancel : 1; |
128 |
|
|
unsigned memoryWrite: 1; |
129 |
|
|
unsigned checkAdler: 1; |
130 |
|
|
} mOptions; |
131 |
|
|
std::string mFileName; |
132 |
|
|
std::queue<std::string> mUrlQueue; |
133 |
|
|
WriteFunction mWriteFunction; |
134 |
|
|
unsigned long mAdler; |
135 |
|
|
DownloadUpdate mUpdateFunction; |
136 |
|
|
SDL_Thread *mThread; |
137 |
|
|
CURL *mCurl; |
138 |
|
|
curl_slist *mHeaders; |
139 |
|
|
curl_httppost *mFormPost; |
140 |
|
|
char *mError; |
141 |
|
|
bool mIgnoreError; |
142 |
|
|
bool mUpload; |
143 |
|
|
bool mIsXml; |
144 |
|
|
}; |
145 |
|
|
|
146 |
|
|
} // namespace Net |
147 |
|
|
|
148 |
|
|
#endif // NET_DOWNLOAD_H |