diff Platforms/Wasm/WasmWebService.js @ 257:9afafb192180 am-2

using PAM
author am@osimis.io
date Tue, 10 Jul 2018 12:39:01 +0200
parents f73d722d98c8
children 3897f9f28cfa
line wrap: on
line diff
--- a/Platforms/Wasm/WasmWebService.js	Tue Jul 03 13:19:56 2018 +0200
+++ b/Platforms/Wasm/WasmWebService.js	Tue Jul 10 12:39:01 2018 +0200
@@ -1,22 +1,28 @@
 mergeInto(LibraryManager.library, {
-  WasmWebService_ScheduleGetRequest: function(callback, url, payload) {
+  WasmWebService_ScheduleGetRequest: function(callback, url, headersInJsonString, payload) {
     // Directly use XMLHttpRequest (no jQuery) to retrieve the raw binary data
     // http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/
     var xhr = new XMLHttpRequest();
-    var tmp = UTF8ToString(url);
-    xhr.open('GET', tmp, true);
+    var url_ = UTF8ToString(url);
+    var headersInJsonString_ = UTF8ToString(headersInJsonString);
+
+    xhr.open('GET', url_, true);
     xhr.responseType = 'arraybuffer';
-
+    var headers = JSON.parse(headersInJsonString_);
+    for (var key in headers) {
+      xhr.setRequestHeader(key, headers[key]);
+    }
+    // console.log(xhr); 
     xhr.onreadystatechange = function() {
       if (this.readyState == XMLHttpRequest.DONE) {
         if (xhr.status === 200) {
           // TODO - Is "new Uint8Array()" necessary? This copies the
           // answer to the WebAssembly stack, hence necessitating
           // increasing the TOTAL_STACK parameter of Emscripten
-          WasmWebService_NotifySuccess(callback, tmp, new Uint8Array(this.response),
+          WasmWebService_NotifySuccess(callback, url_, new Uint8Array(this.response),
                                        this.response.byteLength, payload);
         } else {
-          WasmWebService_NotifyError(callback, tmp, payload);
+          WasmWebService_NotifyError(callback, url_, payload);
         }
       }
     }
@@ -24,20 +30,26 @@
     xhr.send();
   },
 
-  WasmWebService_SchedulePostRequest: function(callback, url, body, bodySize, payload) {
+  WasmWebService_SchedulePostRequest: function(callback, url, headersInJsonString, body, bodySize, payload) {
     var xhr = new XMLHttpRequest();
-    var tmp = UTF8ToString(url);
-    xhr.open('POST', tmp, true);
+    var url_ = UTF8ToString(url);
+    var headersInJsonString_ = UTF8ToString(headersInJsonString);
+    xhr.open('POST', url_, true);
     xhr.responseType = 'arraybuffer';
     xhr.setRequestHeader('Content-type', 'application/octet-stream');
+
+    var headers = JSON.parse(headersInJsonString_);
+    for (var key in headers) {
+      xhr.setRequestHeader(key, headers[key]);
+    }
     
     xhr.onreadystatechange = function() {
       if (this.readyState == XMLHttpRequest.DONE) {
         if (xhr.status === 200) {
-          WasmWebService_NotifySuccess(callback, tmp, new Uint8Array(this.response),
+          WasmWebService_NotifySuccess(callback, url_, new Uint8Array(this.response),
                                        this.response.byteLength, payload);
         } else {
-          WasmWebService_NotifyError(callback, tmp, payload);
+          WasmWebService_NotifyError(callback, url_, payload);
         }
       }
     }