Conflicts:
	AUTHORS
This commit is contained in:
Alon Zakai 2014-08-05 15:59:18 -07:00
Родитель 56e56ccaaa 9cc8b331c8
Коммит 07e0c4147d
5 изменённых файлов: 124 добавлений и 41 удалений

Просмотреть файл

@ -153,3 +153,5 @@ a license to everyone to use it as detailed in LICENSE.)
* Sylvain Chevalier <sylvain.chevalier@gmail.com>
* Nathan Ross <nross.se@gmail.com>
* Zachary Pomerantz <zmp@umich.edu>
* Boris Tsarev <boristsarev@gmail.com>

Просмотреть файл

@ -673,6 +673,15 @@ mergeInto(LibraryManager.library, {
}
}
}
},
wgetRequests: {},
nextWgetRequestHandle: 0,
getNextWgetRequestHandle: function() {
var handle = Browser.nextWgetRequestHandle;
Browser.nextWgetRequestHandle++;
return handle;
}
},
@ -742,33 +751,43 @@ mergeInto(LibraryManager.library, {
http.open(_request, _url, true);
http.responseType = 'arraybuffer';
var handle = Browser.getNextWgetRequestHandle();
// LOAD
http.onload = function http_onload(e) {
if (http.status == 200) {
FS.createDataFile( _file.substr(0, index), _file.substr(index + 1), new Uint8Array(http.response), true, true);
if (onload) {
var stack = Runtime.stackSave();
Runtime.dynCall('vii', onload, [arg, allocate(intArrayFromString(_file), 'i8', ALLOC_STACK)]);
Runtime.dynCall('viii', onload, [handle, arg, allocate(intArrayFromString(_file), 'i8', ALLOC_STACK)]);
Runtime.stackRestore(stack);
}
} else {
if (onerror) Runtime.dynCall('vii', onerror, [arg, http.status]);
if (onerror) Runtime.dynCall('viii', onerror, [handle, arg, http.status]);
}
delete Browser.wgetRequests[handle];
};
// ERROR
http.onerror = function http_onerror(e) {
if (onerror) Runtime.dynCall('vii', onerror, [arg, http.status]);
if (onerror) Runtime.dynCall('viii', onerror, [handle, arg, http.status]);
delete Browser.wgetRequests[handle];
};
// PROGRESS
http.onprogress = function http_onprogress(e) {
if (e.lengthComputable || (e.lengthComputable === undefined && e.total != 0)) {
var percentComplete = (e.loaded / e.total)*100;
if (onprogress) Runtime.dynCall('vii', onprogress, [arg, percentComplete]);
if (onprogress) Runtime.dynCall('viii', onprogress, [handle, arg, percentComplete]);
}
};
// ABORT
http.onabort = function http_onabort(e) {
delete Browser.wgetRequests[handle];
};
// Useful because the browser can limit the number of redirection
try {
if (http.channel instanceof Ci.nsIHttpChannel)
@ -784,6 +803,10 @@ mergeInto(LibraryManager.library, {
} else {
http.send(null);
}
Browser.wgetRequests[handle] = http;
return handle;
},
emscripten_async_wget2_data: function(url, request, param, arg, free, onload, onerror, onprogress) {
@ -795,27 +818,38 @@ mergeInto(LibraryManager.library, {
http.open(_request, _url, true);
http.responseType = 'arraybuffer';
var handle = Browser.getNextWgetRequestHandle();
// LOAD
http.onload = function http_onload(e) {
if (http.status == 200 || _url.substr(0,4).toLowerCase() != "http") {
var byteArray = new Uint8Array(http.response);
var buffer = _malloc(byteArray.length);
HEAPU8.set(byteArray, buffer);
if (onload) Runtime.dynCall('viii', onload, [arg, buffer, byteArray.length]);
if (onload) Runtime.dynCall('viiii', onload, [handle, arg, buffer, byteArray.length]);
if (free) _free(buffer);
} else {
if (onerror) Runtime.dynCall('viii', onerror, [arg, http.status, http.statusText]);
if (onerror) Runtime.dynCall('viiii', onerror, [handle, arg, http.status, http.statusText]);
}
delete Browser.wgetRequests[handle];
};
// ERROR
http.onerror = function http_onerror(e) {
if (onerror) Runtime.dynCall('viii', onerror, [arg, http.status, http.statusText]);
if (onerror) {
Runtime.dynCall('viiii', onerror, [handle, arg, http.status, http.statusText]);
}
delete Browser.wgetRequests[handle];
};
// PROGRESS
http.onprogress = function http_onprogress(e) {
if (onprogress) Runtime.dynCall('viii', onprogress, [arg, e.loaded, e.lengthComputable || e.lengthComputable === undefined ? e.total : 0]);
if (onprogress) Runtime.dynCall('viiii', onprogress, [handle, arg, e.loaded, e.lengthComputable || e.lengthComputable === undefined ? e.total : 0]);
};
// ABORT
http.onabort = function http_onabort(e) {
delete Browser.wgetRequests[handle];
};
// Useful because the browser can limit the number of redirection
@ -833,6 +867,17 @@ mergeInto(LibraryManager.library, {
} else {
http.send(null);
}
Browser.wgetRequests[handle] = http;
return handle;
},
emscripten_async_wget2_abort: function(handle) {
var http = Browser.wgetRequests[handle];
if (http) {
http.abort();
}
},
emscripten_async_prepare: function(file, onload, onerror) {

Просмотреть файл

@ -436,47 +436,67 @@ void emscripten_async_wget_data(const char* url, void *arg, em_async_wget_onload
* More feature-complete version of emscripten_async_wget. Note:
* this version is experimental.
*
* The requesttype is 'GET' or 'POST',
* If is post request, param is the post parameter
* like key=value&key2=value2.
* The param 'arg' is a pointer will be pass to the callback
* When file is ready then 'onload' callback will called.
* During the download 'onprogress' callback will called.
* If any error occurred 'onerror' will called.
* The callbacks are called with an object pointer give in parameter
* @param url Requested url
* @param file File to load
* @param requesttype 'GET' or 'POST'
* @param param If is post request, param is the post parameter
* like key=value&key2=value2.
* @param arg It is a pointer will be pass to the callback
* @param onload When file is ready then 'onload' callback will called.
* @param onprogress During the download 'onprogress' callback will called.
* @param onerror If any error occurred 'onerror' will called.
*
* The callbacks are called with a request handle,
* an object pointer give in parameter
* and file if is a success, the progress value during progress
* and http status code if is an error.
*
* @return Handle to request. It can be used to abort request
*/
typedef void (*em_async_wget2_onload_func)(void*, const char*);
typedef void (*em_async_wget2_onstatus_func)(void*, int);
typedef void (*em_async_wget2_onload_func)(unsigned, void*, const char*);
typedef void (*em_async_wget2_onstatus_func)(unsigned, void*, int);
void emscripten_async_wget2(const char* url, const char* file, const char* requesttype, const char* param, void *arg, em_async_wget2_onload_func onload, em_async_wget2_onstatus_func onerror, em_async_wget2_onstatus_func onprogress);
int emscripten_async_wget2(const char* url, const char* file, const char* requesttype, const char* param, void *arg, em_async_wget2_onload_func onload, em_async_wget2_onstatus_func onerror, em_async_wget2_onstatus_func onprogress);
/*
* More feature-complete version of emscripten_async_wget_data. Note:
* this version is experimental.
*
* The requesttype is 'GET' or 'POST',
* If is post request, param is the post parameter
* like key=value&key2=value2.
* The param 'arg' is a pointer will be pass to the callback
* The free param tells the runtime whether to free the returned buffer
after onload is complete. If false freeing the buffer is the receiver's
responsibility.
* The callbacks are called with an object pointer give in parameter.
* When file is ready then 'onload' callback will called with a pointer to
* @param url Requested url
* @param requesttype 'GET' or 'POST'
* @param param If is post request, param is the post parameter
* like key=value&key2=value2.
* @param arg It is a pointer will be pass to the callback
* @param free Tells the runtime whether to free the returned buffer
after onload is complete. If false freeing the buffer is the receiver's
responsibility.
* @param onload When data is ready then 'onload' callback will called.
* @param onprogress During the download 'onprogress' callback will called.
* @param onerror If any error occurred 'onerror' will called.
*
* The callbacks are called with a request handle and an object pointer give in parameter
* When data is ready then 'onload' callback will called with a pointer to
the buffer in memory and the size in bytes.
* During the download 'onprogress' callback will called. The first argument is
the number of bytes loaded. The second argument is the total size in bytes,
or zero if the size is unavailable.
* If any error occurred 'onerror' will called with the HTTP status code
and a string with the status description.
*
* @return Handle to request. It can be used to abort request
*/
typedef void (*em_async_wget2_data_onload_func)(void*, void *, unsigned*);
typedef void (*em_async_wget2_data_onerror_func)(void*, int, const char*);
typedef void (*em_async_wget2_data_onprogress_func)(void*, int, int);
typedef void (*em_async_wget2_data_onload_func)(unsigned, void*, void*, unsigned);
typedef void (*em_async_wget2_data_onerror_func)(unsigned, void*, int, const char*);
typedef void (*em_async_wget2_data_onprogress_func)(unsigned, void*, int, int);
void emscripten_async_wget2_data(const char* url, const char* requesttype, const char* param, void *arg, int free, em_async_wget2_data_onload_func onload, em_async_wget2_data_onerror_func onerror, em_async_wget2_data_onprogress_func onprogress);
int emscripten_async_wget2_data(const char* url, const char* requesttype, const char* param, void *arg, int free, em_async_wget2_data_onload_func onload, em_async_wget2_data_onerror_func onerror, em_async_wget2_data_onprogress_func onprogress);
/*
* Abort async request
*
* @param handle Request handle
*/
void emscripten_async_wget2_abort(int handle);
/*
* Prepare a file in asynchronous way. This does just the

Просмотреть файл

@ -64,17 +64,17 @@ std::string http::cross_domain = "";
// HTTP CLASS
//----------------------------------------------------------------------------------------
void http::onLoaded(void* parent, const char * file) {
void http::onLoaded(unsigned handle, void* parent, const char * file) {
http* req = reinterpret_cast<http*>(parent);
req->onLoaded(file);
}
void http::onError(void* parent, int statuserror) {
void http::onError(unsigned handle, void* parent, int statuserror) {
http* req = reinterpret_cast<http*>(parent);
req->onError(statuserror);
}
void http::onProgress(void* parent, int progress) {
void http::onProgress(unsigned handle, void* parent, int progress) {
http* req = reinterpret_cast<http*>(parent);
req->onProgress(progress);
}
@ -116,7 +116,7 @@ void http::runRequest(const char* page, int assync) {
_targetFileName = format("prepare%d",_uid);
}
emscripten_async_wget2(url.c_str(), _targetFileName.c_str(), (_request==REQUEST_GET) ? "GET":"POST", _param.c_str(), this, http::onLoaded, http::onError, http::onProgress);
_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());
@ -126,6 +126,13 @@ void http::runRequest(const char* page, int assync) {
}
}
/**
* Abort the request
*/
void http::abortRequest() {
emscripten_async_wget2_abort(_handle);
}
/**
* Accede a la reponse
*/
@ -258,11 +265,12 @@ void wait_http(void* request) {
int main() {
time_elapsed = emscripten_get_now();
http* http1 = new http("https://github.com",http::REQUEST_GET,"emscripten_master.zip");
http1->runRequest("/kripken/emscripten/archive/master.zip",http::ASSYNC_THREAD);
http* http1 = new http("http://localhost/~boristsarev",http::REQUEST_GET,"Demo1.js");
http1->runRequest("/download.php?url=Demo1.js",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("/kripken/emscripten/master/LICENSE",http::ASSYNC_THREAD);

Просмотреть файл

@ -37,9 +37,9 @@ class http {
static void RegisterAsExtension(bool regis);
// Callback
static void onLoaded(void* parent, const char * file);
static void onError(void* parent, int statuserror);
static void onProgress(void* parent, int progress);
static void onLoaded(unsigned handle, void* parent, const char * file);
static void onError(unsigned handle, void* parent, int statuserror);
static void onProgress(unsigned handle, void* parent, int progress);
// Constructeur
http(const char* hostname, int requestType, const char* targetFileName = "");
@ -52,6 +52,11 @@ class http {
*/
void runRequest(const char* page, int assync);
/**
* Abort the request
*/
void abortRequest();
/**
* Accede a la reponse
*/
@ -128,6 +133,9 @@ class http {
// mode assyncrone courant
AssyncMode _assync;
// request handle
unsigned _handle;
};
//this is safe and convenient but not exactly efficient