/
FileTransporter_curl.cpp
285 lines (250 loc) · 9.64 KB
/
FileTransporter_curl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* (c) Copyright Ascensio System SIA 2010-2023
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#include "FileTransporter_private.h"
#include <iostream>
#include <unistd.h>
#include "../../DesktopEditor/common/Directory.h"
#ifndef USE_EXTERNAL_TRANSPORT
#include <fcntl.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>
#else
#include "transport_external.h"
#endif
namespace NSNetwork
{
namespace NSFileTransport
{
class CFileTransporterBaseCURL : public CFileTransporterBase
{
public :
CFileTransporterBaseCURL(const std::wstring &sDownloadFileUrl, bool bDelete = true)
: CFileTransporterBase(sDownloadFileUrl, bDelete)
{
}
CFileTransporterBaseCURL(const std::wstring &sUploadUrl, const unsigned char* cData, const int nSize)
: CFileTransporterBase(sUploadUrl, cData, nSize)
{
}
CFileTransporterBaseCURL(const std::wstring &sUploadUrl, const std::wstring &sUploadFilePath)
: CFileTransporterBase(sUploadUrl, sUploadFilePath)
{
}
virtual ~CFileTransporterBaseCURL()
{
if (m_bDelete && !m_sDownloadFilePath.empty())
{
std::string sFilePath = U_TO_UTF8(m_sDownloadFilePath);
unlink(sFilePath.c_str());
}
}
#ifndef USE_EXTERNAL_TRANSPORT
static size_t write_data(void *ptr, size_t size, size_t nmemb, int fd) {
size_t written = write(fd, ptr, size * nmemb);
return written;
}
/*int progress_func(void* ptr, double TotalToDownload, double NowDownloaded, double TotalToUpload, double NowUploaded)
{
// It's here you will write the code for the progress message or bar
int percent = static_cast<int>((100.0 * NowDownloaded) / TotalToDownload);
if(CFileTransporterBase::m_func_onProgress)
CFileTransporterBase::m_func_onProgress(percent);
return 0;
}
*/
static size_t write_data_to_string(char *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
virtual int DownloadFile() override
{
CURL *curl;
int fp;
CURLcode res;
std::string sUrl = U_TO_UTF8(m_sDownloadFileUrl);
std::string sOut;
const char *url = sUrl.c_str();
curl = curl_easy_init();
if (curl)
{
fp = createUniqueTempFile(sOut);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
//curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
// Install the callback function
//curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_func);
#if defined(__linux__)
//в linux нет встроенных в систему корневых сертификатов, поэтому отключаем проверку
//http://curl.haxx.se/docs/sslcerts.html
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
/* tell libcurl to follow redirection(default false) */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* some servers don't like requests that are made without a user-agent field, so we provide one */
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
close(fp);
}
m_bComplete = (CURLE_OK == res);
if (m_bComplete)
{
if (m_sDownloadFilePath.empty())
m_sDownloadFilePath = NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)sOut.c_str(), sOut.length());
else
NSFile::CFileBinary::Move(UTF8_TO_U(sOut), m_sDownloadFilePath);
}
//int nRes = execl("/usr/bin/wget", stringWstingToUtf8String (m_sFileUrl).c_str(), "-P", stringWstingToUtf8String (m_sDownloadFilePath).c_str(), (char *)NULL);
//m_bComplete = nRes >= 0;
return m_bComplete ? 0 : 1;
}
virtual int UploadData() override
{
CURL *curl;
CURLcode res;
std::string sUrl = U_TO_UTF8(m_sUploadUrl);
const char *url = sUrl.c_str();
struct curl_slist *headerlist = NULL;
std::string readBuffer;
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
headerlist = curl_slist_append(headerlist, "Content-Type: application/octet-stream");
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_HEADER, true);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, url);
/* Now specify the POST data */
/* size of the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, m_nSize);
/* binary data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, m_cData);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data_to_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
#if defined(__linux__)
//в linux нет встроенных в систему корневых сертификатов, поэтому отключаем проверку
//http://curl.haxx.se/docs/sslcerts.html
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if (res == CURLE_OK)
{
if (http_code == 200 || http_code == 1223)
{
size_t startLenghtPos = readBuffer.find("Content-Length:") + sizeof("Content-Length:");
size_t endLenghtPos = readBuffer.substr(startLenghtPos, readBuffer.length()).find("\r");
std::string dataSize = readBuffer.substr(startLenghtPos, endLenghtPos);
readBuffer = readBuffer.substr(readBuffer.length() - std::stoi(dataSize));
NSFile::CUtf8Converter::GetUnicodeStringFromUTF8((BYTE*)readBuffer.c_str(), (LONG)readBuffer.length(), m_sResponse);
}
else
{
res = CURLE_HTTP_RETURNED_ERROR;
}
}
/* always cleanup */
curl_easy_cleanup(curl);
}
m_bComplete = (CURLE_OK == res);
return m_bComplete ? 0 : 1;
}
virtual int UploadFile() override
{
//stub
return -1;
}
protected:
int createUniqueTempFile (std::string &filename)
{
std::string sTempPath = NSFile::CUtf8Converter::GetUtf8StringFromUnicode(NSDirectory::GetTempPath());
sTempPath += "/fileXXXXXX";
int fd = mkstemp(const_cast <char *> (sTempPath.c_str()));
if (-1 != fd)
filename = sTempPath;
return fd;
}
#else
virtual int DownloadFile() override
{
if (m_sDownloadFilePath.empty())
{
m_sDownloadFilePath = NSFile::CFileBinary::CreateTempFileWithUniqueName(NSDirectory::GetTempPath(), L"DW");
if (NSFile::CFileBinary::Exists(m_sDownloadFilePath))
NSFile::CFileBinary::Remove(m_sDownloadFilePath);
}
return download_external(m_sDownloadFileUrl, m_sDownloadFilePath, m_func_onProgress, m_check_aborted);
}
virtual int UploadData() override
{
if (!m_sUploadUrl.empty() && m_cData != NULL && m_nSize != 0)
{
return uploaddata_external(m_sUploadUrl, m_cData, m_nSize);
}
return -1;
}
virtual int UploadFile() override
{
if (!m_sUploadUrl.empty() && !m_sUploadFilePath.empty())
{
return uploadfile_external(m_sUploadUrl, m_sUploadFilePath);
}
return -1;
}
#endif
};
CFileTransporter_private::CFileTransporter_private(const std::wstring &sDownloadFileUrl, bool bDelete)
: m_pInternal(new CFileTransporterBaseCURL(sDownloadFileUrl, bDelete))
{
m_pInternal->m_check_aborted = std::bind(&CBaseThread::isAborted, this);
}
CFileTransporter_private::CFileTransporter_private(const std::wstring &sUploadUrl, const unsigned char* cData, const int nSize)
: m_pInternal(new CFileTransporterBaseCURL(sUploadUrl, cData, nSize))
{
m_pInternal->m_check_aborted = std::bind(&CBaseThread::isAborted, this);
}
CFileTransporter_private::CFileTransporter_private(const std::wstring &sUploadUrl, const std::wstring &sUploadFilePath)
: m_pInternal(new CFileTransporterBaseCURL(sUploadUrl, sUploadFilePath))
{
m_pInternal->m_check_aborted = std::bind(&CBaseThread::isAborted, this);
}
}
}