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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
#include "platform/chunks_download_strategy.hpp"
#include "platform/http_request.hpp"
#include "platform/http_thread_callback.hpp"
#include "platform/platform.hpp"
#include "defines.hpp"
#ifdef DEBUG
#include "base/thread.hpp"
#endif
#include "coding/internal/file_data.hpp"
#include "coding/file_writer.hpp"
#include "base/logging.hpp"
#include "base/string_utils.hpp"
#include "std/list.hpp"
#include "std/unique_ptr.hpp"
#include "3party/Alohalytics/src/alohalytics.h"
class HttpThread;
namespace downloader
{
namespace non_http_error_code
{
string DebugPrint(long errorCode)
{
switch (errorCode)
{
case kIOException:
return "IO exception";
case kWriteException:
return "Write exception";
case kInconsistentFileSize:
return "Inconsistent file size";
case kNonHttpResponse:
return "Non-http response";
case kInvalidURL:
return "Invalid URL";
case kCancelled:
return "Cancelled";
default:
return to_string(errorCode);
}
}
} // namespace non_http_error_code
/// @return 0 if creation failed
HttpThread * CreateNativeHttpThread(string const & url,
IHttpThreadCallback & callback,
int64_t begRange = 0,
int64_t endRange = -1,
int64_t expectedSize = -1,
string const & postBody = string());
void DeleteNativeHttpThread(HttpThread * thread);
//////////////////////////////////////////////////////////////////////////////////////////
/// Stores server response into the memory
class MemoryHttpRequest : public HttpRequest, public IHttpThreadCallback
{
HttpThread * m_thread;
string m_requestUrl;
string m_downloadedData;
MemWriter<string> m_writer;
virtual bool OnWrite(int64_t, void const * buffer, size_t size)
{
m_writer.Write(buffer, size);
m_progress.first += size;
if (m_onProgress)
m_onProgress(*this);
return true;
}
virtual void OnFinish(long httpOrErrorCode, int64_t, int64_t)
{
if (httpOrErrorCode == 200)
m_status = Status::Completed;
else
{
auto const message = non_http_error_code::DebugPrint(httpOrErrorCode);
LOG(LWARNING, ("HttpRequest error:", message));
alohalytics::LogEvent(
"$httpRequestError",
{{"url", m_requestUrl}, {"code", message}, {"servers", "1"}});
if (httpOrErrorCode == 404)
m_status = Status::FileNotFound;
else
m_status = Status::Failed;
}
m_onFinish(*this);
}
public:
MemoryHttpRequest(string const & url, Callback const & onFinish, Callback const & onProgress)
: HttpRequest(onFinish, onProgress), m_requestUrl(url), m_writer(m_downloadedData)
{
m_thread = CreateNativeHttpThread(url, *this);
ASSERT ( m_thread, () );
}
MemoryHttpRequest(string const & url, string const & postData,
Callback onFinish, Callback onProgress)
: HttpRequest(onFinish, onProgress), m_writer(m_downloadedData)
{
m_thread = CreateNativeHttpThread(url, *this, 0, -1, -1, postData);
ASSERT ( m_thread, () );
}
virtual ~MemoryHttpRequest()
{
DeleteNativeHttpThread(m_thread);
}
virtual string const & GetData() const
{
return m_downloadedData;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////
class FileHttpRequest : public HttpRequest, public IHttpThreadCallback
{
ChunksDownloadStrategy m_strategy;
typedef pair<HttpThread *, int64_t> ThreadHandleT;
typedef list<ThreadHandleT> ThreadsContainerT;
ThreadsContainerT m_threads;
string m_filePath;
unique_ptr<FileWriter> m_writer;
size_t m_goodChunksCount;
bool m_doCleanProgressFiles;
ChunksDownloadStrategy::ResultT StartThreads()
{
string url;
pair<int64_t, int64_t> range;
ChunksDownloadStrategy::ResultT result;
while ((result = m_strategy.NextChunk(url, range)) == ChunksDownloadStrategy::ENextChunk)
{
HttpThread * p = CreateNativeHttpThread(url, *this, range.first, range.second, m_progress.second);
ASSERT ( p, () );
m_threads.push_back(make_pair(p, range.first));
}
return result;
}
class ThreadByPos
{
int64_t m_pos;
public:
explicit ThreadByPos(int64_t pos) : m_pos(pos) {}
inline bool operator() (ThreadHandleT const & p) const
{
return (p.second == m_pos);
}
};
void RemoveHttpThreadByKey(int64_t begRange)
{
ThreadsContainerT::iterator it = find_if(m_threads.begin(), m_threads.end(),
ThreadByPos(begRange));
if (it != m_threads.end())
{
HttpThread * p = it->first;
m_threads.erase(it);
DeleteNativeHttpThread(p);
}
else
LOG(LERROR, ("Tried to remove invalid thread for position", begRange));
}
virtual bool OnWrite(int64_t offset, void const * buffer, size_t size)
{
#ifdef DEBUG
static threads::ThreadID const id = threads::GetCurrentThreadID();
ASSERT_EQUAL(id, threads::GetCurrentThreadID(), ("OnWrite called from different threads"));
#endif
try
{
m_writer->Seek(offset);
m_writer->Write(buffer, size);
return true;
}
catch (Writer::Exception const & e)
{
LOG(LWARNING, ("Can't write buffer for size", size, e.Msg()));
return false;
}
}
void SaveResumeChunks()
{
try
{
// Flush writer before saving downloaded chunks.
m_writer->Flush();
m_strategy.SaveChunks(m_progress.second, m_filePath + RESUME_FILE_EXTENSION);
}
catch (Writer::Exception const & e)
{
LOG(LWARNING, ("Can't flush writer", e.Msg()));
}
}
/// Called for each chunk by one main (GUI) thread.
virtual void OnFinish(long httpOrErrorCode, int64_t begRange, int64_t endRange)
{
#ifdef DEBUG
static threads::ThreadID const id = threads::GetCurrentThreadID();
ASSERT_EQUAL(id, threads::GetCurrentThreadID(), ("OnFinish called from different threads"));
#endif
bool const isChunkOk = (httpOrErrorCode == 200);
string const urlError = m_strategy.ChunkFinished(isChunkOk, make_pair(begRange, endRange));
// remove completed chunk from the list, beg is the key
RemoveHttpThreadByKey(begRange);
// report progress
if (isChunkOk)
{
m_progress.first += (endRange - begRange) + 1;
if (m_onProgress)
m_onProgress(*this);
}
else
{
auto const message = non_http_error_code::DebugPrint(httpOrErrorCode);
LOG(LWARNING, (m_filePath, "HttpRequest error:", message));
alohalytics::LogEvent("$httpRequestError",
{{"url", urlError},
{"code", message},
{"servers", strings::to_string(m_strategy.ActiveServersCount())}});
}
ChunksDownloadStrategy::ResultT const result = StartThreads();
if (result == ChunksDownloadStrategy::EDownloadFailed)
m_status = httpOrErrorCode == 404 ? Status::FileNotFound : Status::Failed;
else if (result == ChunksDownloadStrategy::EDownloadSucceeded)
m_status = Status::Completed;
if (isChunkOk)
{
// save information for download resume
++m_goodChunksCount;
if (m_status != Status::Completed && m_goodChunksCount % 10 == 0)
SaveResumeChunks();
}
if (m_status == Status::InProgress)
return;
// 1. Save downloaded chunks if some error occured.
if (m_status == Status::Failed || m_status == Status::FileNotFound)
SaveResumeChunks();
// 2. Free file handle.
CloseWriter();
// 3. Clean up resume file with chunks range on success
if (m_status == Status::Completed)
{
Platform::RemoveFileIfExists(m_filePath + RESUME_FILE_EXTENSION);
// Rename finished file to it's original name.
Platform::RemoveFileIfExists(m_filePath);
CHECK(base::RenameFileX(m_filePath + DOWNLOADING_FILE_EXTENSION, m_filePath),
(m_filePath, strerror(errno)));
Platform::DisableBackupForFile(m_filePath);
}
// 4. Finish downloading.
m_onFinish(*this);
}
void CloseWriter()
{
try
{
m_writer.reset();
}
catch (Writer::Exception const & e)
{
LOG(LWARNING, ("Can't close file correctly", e.Msg()));
m_status = Status::Failed;
}
}
public:
FileHttpRequest(vector<string> const & urls, string const & filePath, int64_t fileSize,
Callback const & onFinish, Callback const & onProgress,
int64_t chunkSize, bool doCleanProgressFiles)
: HttpRequest(onFinish, onProgress), m_strategy(urls), m_filePath(filePath),
m_goodChunksCount(0), m_doCleanProgressFiles(doCleanProgressFiles)
{
ASSERT ( !urls.empty(), () );
// Load resume downloading information.
m_progress.first = m_strategy.LoadOrInitChunks(m_filePath + RESUME_FILE_EXTENSION,
fileSize, chunkSize);
m_progress.second = fileSize;
FileWriter::Op openMode = FileWriter::OP_WRITE_TRUNCATE;
if (m_progress.first != 0)
{
// Check that resume information is correct with existing file.
uint64_t size;
if (base::GetFileSize(filePath + DOWNLOADING_FILE_EXTENSION, size) &&
size <= static_cast<uint64_t>(fileSize))
openMode = FileWriter::OP_WRITE_EXISTING;
else
m_strategy.InitChunks(fileSize, chunkSize);
}
// Create file and reserve needed size.
unique_ptr<FileWriter> writer(new FileWriter(filePath + DOWNLOADING_FILE_EXTENSION, openMode));
// Reserving disk space is very slow on a device.
//writer->Reserve(fileSize);
// Assign here, because previous functions can throw an exception.
m_writer.swap(writer);
Platform::DisableBackupForFile(filePath + DOWNLOADING_FILE_EXTENSION);
StartThreads();
}
virtual ~FileHttpRequest()
{
// Do safe delete with removing from list in case if DeleteNativeHttpThread
// can produce final notifications to this->OnFinish().
while (!m_threads.empty())
{
HttpThread * p = m_threads.back().first;
m_threads.pop_back();
DeleteNativeHttpThread(p);
}
if (m_status == Status::InProgress)
{
// means that client canceled download process, so delete all temporary files
CloseWriter();
if (m_doCleanProgressFiles)
{
Platform::RemoveFileIfExists(m_filePath + DOWNLOADING_FILE_EXTENSION);
Platform::RemoveFileIfExists(m_filePath + RESUME_FILE_EXTENSION);
}
}
}
virtual string const & GetData() const
{
return m_filePath;
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////
HttpRequest::HttpRequest(Callback const & onFinish, Callback const & onProgress)
: m_status(Status::InProgress), m_progress(make_pair(0, -1)),
m_onFinish(onFinish), m_onProgress(onProgress)
{
}
HttpRequest::~HttpRequest()
{
}
HttpRequest * HttpRequest::Get(string const & url, Callback const & onFinish, Callback const & onProgress)
{
return new MemoryHttpRequest(url, onFinish, onProgress);
}
HttpRequest * HttpRequest::PostJson(string const & url, string const & postData,
Callback const & onFinish, Callback const & onProgress)
{
return new MemoryHttpRequest(url, postData, onFinish, onProgress);
}
HttpRequest * HttpRequest::GetFile(vector<string> const & urls,
string const & filePath, int64_t fileSize,
Callback const & onFinish, Callback const & onProgress,
int64_t chunkSize, bool doCleanOnCancel)
{
try
{
return new FileHttpRequest(urls, filePath, fileSize, onFinish, onProgress, chunkSize, doCleanOnCancel);
}
catch (FileWriter::Exception const & e)
{
// Can't create or open file for writing.
LOG(LWARNING, ("Can't create file", filePath, "with size", fileSize, e.Msg()));
}
return nullptr;
}
string DebugPrint(HttpRequest::Status status)
{
switch (status)
{
case HttpRequest::Status::InProgress:
return "In progress";
case HttpRequest::Status::Completed:
return "Completed";
case HttpRequest::Status::Failed:
return "Failed";
case HttpRequest::Status::FileNotFound:
return "File not found";
}
UNREACHABLE();
}
} // namespace downloader
|