forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_emscripten_async_wget2.cpp
372 lines (307 loc) · 7.99 KB
/
test_emscripten_async_wget2.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
#include <emscripten/emscripten.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string>
class http {
public:
enum Status {
ST_PENDING = 0,
ST_FAILED,
ST_OK,
ST_ABORTED,
};
enum RequestType {
REQUEST_GET = 0,
REQUEST_POST ,
};
enum AssyncMode {
ASSYNC_THREAD
};
// Callback
static void onLoaded(unsigned handle, void* parent, const char * file) {
http* req = reinterpret_cast<http*>(parent);
req->onLoaded(file);
}
static void onError(unsigned handle, void* parent, int statuserror) {
http* req = reinterpret_cast<http*>(parent);
req->onError(statuserror);
}
static void onProgress(unsigned handle, void* parent, int progress) {
http* req = reinterpret_cast<http*>(parent);
req->onProgress(progress);
}
// Constructeur
http(const char* hostname, int requestType, const char* targetFilename = "")
: _hostname(hostname), _targetFileName(targetFilename), _request((RequestType)requestType),
_status(ST_PENDING), _assync(ASSYNC_THREAD), _uid(uid++) {}
/**
* Effectue la requete
*/
void runRequest(const char* page, int assync);
/**
* Abort the request
*/
void abortRequest() {
emscripten_async_wget2_abort(_handle);
_status = ST_ABORTED;
}
/**
* Accede a la reponse
*/
const char* getContent() {
return _content.c_str();
}
/**
* Accede a l'erreur
*/
const char* getError() {
return _error.c_str();
}
/**
* Accede au status
*/
int getStatus() {
return _status;
}
/**
* Accede a la progression
*/
int getProgress() {
return _progressValue;
}
/**
* Get Id of http Class
*/
int getId() {
return _uid;
}
/**
*
*/
void addValue(const char* key, const char* value);
/**
* Callback
*/
void onProgress(int progress) {
_progressValue = progress;
}
void onLoaded(const char* file);
void onError(int error);
// Static parameter
static int uid;
static std::string cross_domain ;
private:
// Id of request
int _uid;
// nom de l'hote
std::string _hostname;
// nom de la page
std::string _page;
// target filename
std::string _targetFileName;
// param
std::string _param;
// resultat
std::string _content;
// probleme
std::string _error;
// request type
RequestType _request;
// status
int _status;
// progress value
int _progressValue = -1;
// mode assyncrone courant
AssyncMode _assync;
// request handle
unsigned _handle;
};
//this is safe and convenient but not exactly efficient
inline std::string format(const char* fmt, ...){
int size = 512;
char* buffer = 0;
buffer = new char[size];
va_list vl;
va_start(vl,fmt);
int nsize = vsnprintf(buffer, size, fmt, vl);
if (size <= nsize) {//fail delete buffer and try again
delete buffer; buffer = 0;
buffer = new char[nsize+1];//+1 for /0
nsize = vsnprintf(buffer, size, fmt, vl);
}
std::string ret(buffer);
va_end(vl);
delete buffer;
return ret;
}
int http::uid = 0;
/*
- Useful for download an url on other domain
<?php
header("Access-Control-Allow-Origin: *");
// verifie si on a les bons parametres
if( isset($_GET['url']) ) {
$fileName = $_GET['url'];
if($f = fopen($fileName,'rb') ){
$fSize = 0;
while(!feof($f)){
++$fSize;
$data = fread($f,1);
}
fclose($f);
if( $fSize < 1 ) {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
echo 'For empty file ' . $fileName;
die();
} else {
header("POST ".$fileName." HTTP/1.1\r\n");
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="' . basename($fileName) . "\";");
header('Content-Type: application/octet-stream');
header('Content-Length: '.$fSize);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
readfile($fileName);
exit;
}
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
echo 'For filename ' . $fileName;
}
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
}
?>
*/
// http://..../download.php?url=
std::string http::cross_domain = "";
/**
* Effectue la requete
*/
void http::runRequest(const char* page, int assync) {
_page = page;
_status = ST_PENDING;
_assync = (AssyncMode)assync;
_progressValue = 0;
std::string url = cross_domain;
url += _hostname + _page;
if (_hostname.size() > 0 && _page.size() > 0) {
printf("URL : %s\n",url.c_str());
printf("REQUEST : %s\n", _request == REQUEST_GET ? "GET" : "POST");
printf("PARAMS : %s\n",_param.c_str());
if (_targetFileName.size() == 0 ) {
_targetFileName = format("prepare%d",_uid);
}
_handle = emscripten_async_wget2(url.c_str(), _targetFileName.c_str(), _request == REQUEST_GET ? "GET" : "POST", _param.c_str(), this, http::onLoaded, http::onError, http::onProgress);
} else {
_error = format("malformed url : %s\n",url.c_str());
_content = "";
_status = ST_FAILED;
_progressValue = -1;
}
}
/**
* Post
*/
void http::addValue(const char* key, const char* value) {
if (_param.size() > 0) {
_param += "&";
_param += key;
_param += "=";
_param += value;
} else {
_param += key;
_param += "=";
_param += value;
}
}
void http::onLoaded(const char* file) {
if (strstr(file,"prepare")) {
FILE* f = fopen(file,"rb");
if (f) {
fseek (f, 0, SEEK_END);
int size=ftell (f);
fseek (f, 0, SEEK_SET);
char* data = new char[size];
fread(data,size,1,f);
_content = data;
delete[] data;
fclose(f);
} else {
_content = file;
}
} else {
_content = file;
}
_progressValue = 100;
_status = ST_OK;
}
void http::onError(int error) {
printf("onError status: %d\n",error);
_error = "";
_content = "";
_status = ST_FAILED;
_progressValue = -1;
}
/// TEST
int num_request = 0;
float time_elapsed = 0.0f;
void wait_https() {
if (num_request == 0) {
printf("End of all download ... %fs\n",(emscripten_get_now() - time_elapsed) / 1000.f);
emscripten_cancel_main_loop();
exit(0);
}
}
void wait_http(void* request) {
http* req = reinterpret_cast<http*>(request);
if (req != 0) {
if (req->getStatus() == http::ST_PENDING) {
if (req->getProgress()>0) {
printf("Progress Request %d : %d\n", req->getId(), req->getProgress());
}
emscripten_async_call(wait_http, request, 500);
} else {
if (req->getStatus() == http::ST_OK) {
printf("Success Request %d : %s\n", req->getId(),req->getContent());
} else if (req->getStatus() == http::ST_ABORTED) {
printf("Aborted Request %d\n", req->getId());
} else {
printf("Error Request %d : %s\n", req->getId(), req->getError());
}
num_request --;
}
} else {
num_request --;
}
}
int main() {
time_elapsed = emscripten_get_now();
http* http1 = new http("https://github.com", http::REQUEST_GET, "emscripten_main.zip");
http1->runRequest("/emscripten-core/emscripten/archive/main.zip", http::ASSYNC_THREAD);
http* http2 = new http("https://github.com",http::REQUEST_GET, "wolfviking_master.zip");
http2->runRequest("/wolfviking0/image.js/archive/master.zip", http::ASSYNC_THREAD);
http2->abortRequest();
http* http3 = new http("https://raw.github.com", http::REQUEST_GET);
http3->runRequest("/emscripten-core/emscripten/main/LICENSE", http::ASSYNC_THREAD);
num_request++;
emscripten_async_call(wait_http, http1, 500);
num_request++;
emscripten_async_call(wait_http, http2, 500);
num_request++;
emscripten_async_call(wait_http, http3, 500);
/*
Http* http4 = new Http("http://www.---.com",Http::REQUEST_POST);
http4->addValue("app","123");
http4->runRequest("/test.php",Http::ASSYNC_THREAD);
num_request ++;
emscripten_async_call(wait_http,http4,500);
*/
emscripten_set_main_loop(wait_https, 0, 0);
return 0;
}