-
Notifications
You must be signed in to change notification settings - Fork 5
/
client_download_http.cpp
381 lines (355 loc) · 13.7 KB
/
client_download_http.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
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
// client_download_http.cpp
// hsync_http: http(s) download demo by http(s)
// Created by housisong on 2020-01-29.
/*
The MIT License (MIT)
Copyright (c) 2020-2023 HouSisong
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "client_download_http.h"
#include <assert.h>
#include "hsync_import_patch.h" // HSYNC_VERSION_STRING
#include "../HDiffPatch/file_for_patch.h"
#include "../HDiffPatch/libHDiffPatch/HDiff/private_diff/mem_buf.h"
#if (_IS_USED_MULTITHREAD)
# include "../HDiffPatch/libParallel/parallel_import.h" //this_thread_yield
# include "../HDiffPatch/libParallel/parallel_channel.h"
#endif
using namespace hdiff_private;
#include "minihttp/minihttp.h" // https://github.com/sisong/minihttp
using namespace minihttp;
#if defined(_MSC_VER)
# ifdef MINIHTTP_USE_MBEDTLS
# pragma comment( lib, "Advapi32.lib" )
# endif
# if defined(_WIN32_WCE)
# pragma comment( lib, "ws2.lib" )
# else
# pragma comment( lib, "ws2_32.lib" )
# endif
#endif /* _MSC_VER */
namespace{
static bool doInitNetwork(){
struct TInitNetwork {
TInitNetwork():initOk(false){ initOk=InitNetwork(); }
~TInitNetwork(){ if (initOk) StopNetwork(); }
bool initOk;
};
static TInitNetwork nt;
return nt.initOk;
}
static const size_t kBestBufSize = hpatch_kFileIOBestMaxSize>>4;
static const size_t kBestRangesCacheSize=hpatch_kFileIOBestMaxSize;
static const int kTimeout_s=10;
static const hpatch_StreamPos_t kEmptyEndPos=kNullRangePos;
static const char* kHttpUserAgent="hsynz/" HSYNC_VERSION_STRING;
struct THttpDownload:public HttpSocket{
explicit THttpDownload(const hpatch_TStreamOutput* out_stream=0,hpatch_StreamPos_t curOutPos=0)
:requestSumSize(0),requestCount(0),is_write_error(false),
_out_stream(out_stream),_cur_pos(curOutPos){
if (!doInitNetwork()){ throw std::runtime_error("InitNetwork error!"); }
this->SetBufsizeIn(kBestBufSize);
this->SetNonBlocking(false);
this->SetFollowRedirect(true);
this->SetAlwaysHandle(false);
this->SetKeepAlive(kTimeout_s);
this->SetUserAgent(kHttpUserAgent);
}
inline bool isDownloadSuccess(){
return IsSuccess() && (requestCount==0) && (!is_write_error)
&&((requestSumSize==0)||(requestSumSize==kEmptyEndPos)); }
inline bool isNeedUpdate(){
return (isOpen() || HasPendingTask())&&(!is_write_error)&&(requestCount>0);
}
bool doDownload(const std::string& file_url){
const hpatch_StreamPos_t requestSumSize_back=requestSumSize;
const std::vector<TRange>& ranges=this->Ranges();
if (ranges.empty())
requestSumSize=kEmptyEndPos;
for (size_t i=0;(i<ranges.size())&&(requestSumSize!=kEmptyEndPos);++i){
if (ranges[i].second!=kEmptyEndPos)
requestSumSize+=(hpatch_StreamPos_t)(ranges[i].second+1-ranges[i].first);
else
requestSumSize=kEmptyEndPos;
}
++requestCount;
if (Download(file_url)){
return true;
}else{
--requestCount;
requestSumSize=requestSumSize_back;
return false;
}
}
bool waitDownloaded(const std::string& file_url){
if (!doDownload(file_url))
return false;
while(isNeedUpdate()){
if (!update()){
#if (_IS_USED_MULTITHREAD)
this_thread_yield();
#else
//sleep?
#endif
}
}
return isDownloadSuccess();
}
protected:
volatile hpatch_StreamPos_t requestSumSize;
volatile size_t requestCount;
volatile bool is_write_error;
const hpatch_TStreamOutput* _out_stream;
hpatch_StreamPos_t _cur_pos;
virtual void _OnRequestDone(){
HttpSocket::_OnRequestDone();
if (requestCount<1)
is_write_error=true;
--requestCount;
}
inline void _OnRecvSize(unsigned size){
if (requestSumSize!=kEmptyEndPos){
if (requestSumSize<size)
is_write_error=true;
requestSumSize-=size;
}
}
virtual void _OnRecv(void *incoming, unsigned size){
if(!size || !IsSuccess() || is_write_error)
return;
_OnRecvSize(size);
const unsigned char* data=(const unsigned char*)incoming;
if (_out_stream->write(_out_stream,_cur_pos,data,data+size)){
_cur_pos+=size;
}else{
is_write_error=true;
this->close();
}
}
};
struct THttpRangeDownload:public THttpDownload{
explicit THttpRangeDownload(const char* file_url,size_t stepRangeNumber)
:_hd(*this),_file_url(file_url),_readPos(0),_writePos(0),nsi(0),
curBlockIndex(~0),curPosInNewSyncData(hpatch_kNullStreamPos){
kStepRangeNumber=(stepRangeNumber>1)?stepRangeNumber:1;
}
virtual ~THttpRangeDownload(){ _closeAll(); }
static hpatch_BOOL readSyncDataBegin(IReadSyncDataListener* listener,const TNeedSyncInfos* needSyncInfo,
uint32_t blockIndex,hpatch_StreamPos_t posInNewSyncData,hpatch_StreamPos_t posInNeedSyncData){
THttpRangeDownload* self=(THttpRangeDownload*)listener->readSyncDataImport;
self->nsi=needSyncInfo;
try{
size_t cacheSize=kBestRangesCacheSize;
self->_cache.realloc(cacheSize);
self->_writePos=0;
self->_readPos=0;
//if (!self->_sendDownloads_all(blockIndex,posInNewSyncData))
// return hpatch_FALSE;
if (!self->_sendDownloads_init(blockIndex,posInNewSyncData)) //step by step send
return hpatch_FALSE;
}catch(...){
return hpatch_FALSE;
}
return hpatch_TRUE;
}
static void readSyncDataEnd(IReadSyncDataListener* listener){
THttpRangeDownload* self=(THttpRangeDownload*)listener->readSyncDataImport;
self->_closeAll();
}
static hpatch_BOOL readSyncData(IReadSyncDataListener* listener,uint32_t blockIndex,
hpatch_StreamPos_t posInNewSyncData,hpatch_StreamPos_t posInNeedSyncData,
unsigned char* out_syncDataBuf,uint32_t syncDataSize){
THttpRangeDownload* self=(THttpRangeDownload*)listener->readSyncDataImport;
try{
return self->readSyncData(blockIndex,posInNewSyncData,posInNeedSyncData,out_syncDataBuf,syncDataSize);
}catch(...){
return hpatch_FALSE;
}
}
protected:
THttpDownload& _hd;
const std::string _file_url;
TAutoMem _cache;
size_t _readPos;
size_t _writePos;
size_t kStepRangeNumber;
const TNeedSyncInfos* nsi;
void makeRanges(std::vector<TRange>& out_ranges,uint32_t& blockIndex,
hpatch_StreamPos_t& posInNewSyncData){
out_ranges.resize(kStepRangeNumber);
size_t gotRangeCount=TNeedSyncInfos_getNextRanges(nsi,(hpatch_StreamPos_t*)out_ranges.data(),
kStepRangeNumber,&blockIndex,&posInNewSyncData);
out_ranges.resize(gotRangeCount);
}
inline void _closeAll(){
_hd.close();
}
inline hpatch_BOOL readSyncData(uint32_t blockIndex,hpatch_StreamPos_t posInNewSyncData,
hpatch_StreamPos_t posInNeedSyncData,
unsigned char* out_syncDataBuf,uint32_t syncDataSize){
while (syncDataSize>0){
size_t savedSize=_savedSize();
if (savedSize>0){
size_t readSize=(savedSize<=syncDataSize)?savedSize:syncDataSize;
_readFromCache(out_syncDataBuf,readSize);
out_syncDataBuf+=readSize;
syncDataSize-=(uint32_t)readSize;
continue;
}
while(isNeedUpdate()&&(_emptySize()>GetBufSize())){
if (!update()){
#if (_IS_USED_MULTITHREAD)
this_thread_yield();
#else
//sleep?
#endif
}
}
savedSize=_savedSize();
if (savedSize==0){
if (!IsSuccess()||is_write_error)
return hpatch_FALSE;
if ((!isNeedUpdate())&&(requestCount==0))
return hpatch_FALSE;
}
}
return hpatch_TRUE;
}
inline size_t _savedSize()const{
return ((_writePos>=_readPos)?_writePos:_writePos+_cache.size())-_readPos;
}
inline size_t _emptySize()const{
return _cache.size()-_savedSize();
}
uint32_t curBlockIndex;
hpatch_StreamPos_t curPosInNewSyncData;
bool _sendDownloads_init(uint32_t blockIndex,hpatch_StreamPos_t posInNewSyncData){
assert(nsi!=0);
printf("\nhttp download from block index %d/%d\n",blockIndex,nsi->blockCount);
curBlockIndex=blockIndex;
curPosInNewSyncData=posInNewSyncData;
return _sendDownloads_step();
}
bool _sendDownloads_step(){
try{
while (curBlockIndex<nsi->blockCount){
makeRanges(_hd.Ranges(),curBlockIndex,curPosInNewSyncData);
if (!_hd.Ranges().empty())
return _hd.doDownload(_file_url);
}
}catch(...){
return false;
}
return true;
}
virtual void _OnRequestDone(){
THttpDownload::_OnRequestDone();
if(!IsSuccess() || is_write_error)
return;
if (curBlockIndex<nsi->blockCount) {
if (!_sendDownloads_step())
is_write_error=true;
}
}
bool _sendDownloads_all(uint32_t blockIndex,hpatch_StreamPos_t posInNewSyncData){
if (!_sendDownloads_init(blockIndex,posInNewSyncData))
return false;
while (curBlockIndex<nsi->blockCount){
if (!_sendDownloads_step())
return false;
}
return true;
}
inline void _readFromCache(unsigned char* out_syncDataBuf,size_t readSize){
const size_t cacheSize=_cache.size();
while (readSize>0){
size_t rlen=cacheSize-_readPos;
rlen=(rlen<=readSize)?rlen:readSize;
memcpy(out_syncDataBuf,_cache.data()+_readPos,rlen);
out_syncDataBuf+=rlen;
readSize-=rlen;
_readPos+=rlen;
_readPos=(_readPos<cacheSize)?_readPos:(_readPos-cacheSize);
}
}
virtual void _OnRecv(void *incoming, unsigned size){
if(!size || !IsSuccess() || is_write_error)
return;
_OnRecvSize(size);
size_t emptySize=_emptySize();
if (emptySize<=size)
is_write_error=true;
if (is_write_error)
this->close();
const unsigned char* data=(const unsigned char*)incoming;
const size_t cacheSize=_cache.size();
while (size>0){
size_t rlen=cacheSize-_writePos;
rlen=(rlen<=size)?rlen:size;
memcpy(_cache.data()+_writePos,data,rlen);
data+=rlen;
size-=(unsigned)rlen;
_writePos+=rlen;
_writePos=(_writePos<cacheSize)?_writePos:(_writePos-cacheSize);
}
}
};
} //end namespace
hpatch_BOOL download_range_by_http_open(IReadSyncDataListener* out_httpListener,
const char* file_url,size_t kStepRangeNumber){
THttpRangeDownload* self=0;
try {
self=new THttpRangeDownload(file_url,kStepRangeNumber);
} catch (...) {
if (self) delete self;
return hpatch_FALSE;
}
out_httpListener->readSyncDataImport=self;
out_httpListener->readSyncDataBegin=THttpRangeDownload::readSyncDataBegin;
out_httpListener->readSyncData=THttpRangeDownload::readSyncData;
out_httpListener->readSyncDataEnd=THttpRangeDownload::readSyncDataEnd;
return hpatch_TRUE;
}
hpatch_BOOL download_range_by_http_close(IReadSyncDataListener* httpListener){
if (httpListener==0) return hpatch_TRUE;
THttpRangeDownload* self=(THttpRangeDownload*)httpListener->readSyncDataImport;
if (self==0) return hpatch_TRUE;
httpListener->readSyncDataImport=0;
try {
delete self;
return hpatch_TRUE;
} catch (...) {
return hpatch_FALSE;
}
}
hpatch_BOOL download_file_by_http(const char* file_url,const hpatch_TStreamOutput* out_stream,
hpatch_StreamPos_t continueDownloadPos){
THttpDownload hd(out_stream,continueDownloadPos);
if (continueDownloadPos>0)
hd.Ranges().push_back(TRange(continueDownloadPos,kEmptyEndPos));
if (!hd.waitDownloaded(file_url))
return hpatch_FALSE;
hpatch_StreamPos_t endPos;
if (continueDownloadPos>0)
endPos=hd.GetRangsBytesLen();
else
endPos=continueDownloadPos+hd.GetContentLen();
return hpatch_TRUE;
}