diff options
author | Joel Croteau <jcroteau@gmail.com> | 2014-03-03 16:24:43 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-03-07 18:09:28 -0800 |
commit | 4d32fb5e5b361dca5d72ed7892f3315eb70edcc1 (patch) | |
tree | 822f150fbe71f8f68296ad7f21d59202ed9b913f /src | |
parent | e351933a7ae98cd190cb023284cec66dd7cfb7f1 (diff) |
Implement emscripten_async_wget2_data
This adds the new function emscripten_async_wget2_data to read from
an XMLHTTPRequest directly into memory while supporting advanced features.
Diffstat (limited to 'src')
-rw-r--r-- | src/library_browser.js | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/src/library_browser.js b/src/library_browser.js index a280d1b2..0808b9f0 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -723,8 +723,59 @@ mergeInto(LibraryManager.library, { // PROGRESS http.onprogress = function http_onprogress(e) { - var percentComplete = (e.position / e.totalSize)*100; - if (onprogress) Runtime.dynCall('vii', onprogress, [arg, percentComplete]); + if (e.lengthComputable || (e.lengthComputable === undefined && e.totalSize != 0)) { + var percentComplete = (e.position / e.totalSize)*100; + if (onprogress) Runtime.dynCall('vii', onprogress, [arg, percentComplete]); + } + }; + + // 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); + } else { + http.send(null); + } + }, + + emscripten_async_wget2_data: function(url, request, param, arg, free, onload, onerror, onprogress) { + var _url = Pointer_stringify(url); + var _request = Pointer_stringify(request); + var _param = Pointer_stringify(param); + + var http = new XMLHttpRequest(); + http.open(_request, _url, true); + http.responseType = 'arraybuffer'; + + // 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 (free) _free(buffer); + } else { + if (onerror) Runtime.dynCall('viii', onerror, [arg, http.status, http.statusText]); + } + }; + + // ERROR + http.onerror = function http_onerror(e) { + if (onerror) Runtime.dynCall('viii', onerror, [arg, http.status, http.statusText]); + }; + + // PROGRESS + http.onprogress = function http_onprogress(e) { + if (onprogress) Runtime.dynCall('viii', onprogress, [arg, e.loaded, e.lengthComputable || e.lengthComputable === undefined ? e.total : 0]); }; // Useful because the browser can limit the number of redirection |