diff options
-rw-r--r-- | AUTHORS | 2 | ||||
-rw-r--r-- | src/library_browser.js | 51 | ||||
-rw-r--r-- | system/include/emscripten/emscripten.h | 13 | ||||
-rwxr-xr-x | tests/runner.py | 4 |
4 files changed, 43 insertions, 27 deletions
@@ -42,4 +42,4 @@ a license to everyone to use it as detailed in LICENSE.) * Manuel Wellmann <manuel.wellmann@autodesk.com> (copyright owned by Autodesk, Inc.) * Xuejie Xiao <xxuejie@gmail.com> * Dominic Wong <dom@slowbunyip.org> - +* Anthony Liot <wolfviking0@yahoo.com> diff --git a/src/library_browser.js b/src/library_browser.js index c18edf4a..0525dd49 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -394,45 +394,44 @@ mergeInto(LibraryManager.library, { var http = new XMLHttpRequest(); http.open(_request, _url, true); - http.responseType = 'arraybuffer'; + http.responseType = 'arraybuffer'; // LOAD http.onload = function(e) { - if (http.status == 200) { - FS.createDataFile( _file.substr(0, index), _file.substr(index +1), new Uint8Array(http.response), true, true); - if (onload) FUNCTION_TABLE[onload](arg,file); - } else { - if (onerror) FUNCTION_TABLE[onerror](arg,http.status); - } + if (http.status == 200) { + FS.createDataFile( _file.substr(0, index), _file.substr(index + 1), new Uint8Array(http.response), true, true); + if (onload) FUNCTION_TABLE[onload](arg, file); + } else { + if (onerror) FUNCTION_TABLE[onerror](arg, http.status); + } }; // ERROR http.onerror = function(e) { - if (onerror) FUNCTION_TABLE[onerror](arg,http.status); - }; + if (onerror) FUNCTION_TABLE[onerror](arg, http.status); + }; - // PROGRESS - http.onprogress = function(e) { - var percentComplete = (e.position / e.totalSize)*100; - if (onprogress) FUNCTION_TABLE[onprogress](arg,percentComplete); - }; + // PROGRESS + http.onprogress = function(e) { + var percentComplete = (e.position / e.totalSize)*100; + if (onprogress) FUNCTION_TABLE[onprogress](arg, percentComplete); + }; - try { - if (http.channel instanceof Ci.nsIHttpChannel) - http.channel.redirectionLimit = 0; - } catch (ex) { /* whatever */ } + // Useful because the browser can limit the number of redirection + try { + if (http.channel instanceof Ci.nsIHttpChannel) + http.channel.redirectionLimit = 0; + } catch (ex) { /* whatever */ } if (_request == "POST") { - //Send the proper header information along with the request - http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); - http.setRequestHeader("Content-length", _param.length); - http.setRequestHeader("Connection", "close"); - - http.send(_param); + //Send the proper header information along with the request + http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + http.setRequestHeader("Content-length", _param.length); + http.setRequestHeader("Connection", "close"); + http.send(_param); } else { - http.send(null); + http.send(null); } - }, emscripten_async_prepare: function(file, onload, onerror) { diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h index fb63384b..30165d8b 100644 --- a/system/include/emscripten/emscripten.h +++ b/system/include/emscripten/emscripten.h @@ -186,6 +186,19 @@ float emscripten_random(); */ void emscripten_async_wget(const char* url, const char* file, void (*onload)(const char*), void (*onerror)(const char*)); +/* + * Load file from url in asynchronous way. + * The requestype 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 + * and file if is a success, the progress value during progress + * and http status code if is an error. + */ void emscripten_async_wget2(const char* url, const char* file, const char* requesttype, const char* param, void *arg, void (*onload)(void*, const char*), void (*onerror)(void*, int), void (*onprogress)(void*, int)); /* diff --git a/tests/runner.py b/tests/runner.py index 775225c6..f04413cf 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -10005,6 +10005,10 @@ elif 'browser' in str(sys.argv): Popen(['python', EMCC, path_from_root('tests', 'worker_api_2_worker.cpp'), '-o', 'worker.js', '-s', 'BUILD_AS_WORKER=1', '-O2', '--minify', '0', '-s', 'EXPORTED_FUNCTIONS=["_one", "_two", "_three", "_four"]']).communicate() self.btest('worker_api_2_main.cpp', args=['-O2', '--minify', '0'], expected='11') + def test_emscripten_async_wget2(self): + Popen(['python', EMCC, path_from_root('tests', 'http.cpp'), '-o', 'http.html', '-I' + path_from_root('tests')]).communicate() + self.btest('http.cpp', expected='0', args=['-I' + path_from_root('tests')]) + pids_to_clean = [] def clean_pids(self): import signal, errno |