BestHTTP_WebRequest.jslib 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. var Lib_BEST_HTTP_WebGL_HTTP_Bridge =
  2. {
  3. /*LogLevels: {
  4. All: 0,
  5. Information: 1,
  6. Warning: 2,
  7. Error: 3,
  8. Exception: 4,
  9. None: 5
  10. }*/
  11. $wr: {
  12. requestInstances: {},
  13. nextRequestId: 1,
  14. loglevel: 2
  15. },
  16. XHR_Create: function(method, url, user, passwd)
  17. {
  18. var _url = /*encodeURI*/(Pointer_stringify(url))
  19. .replace(/\+/g, '%2B')
  20. .replace(/%252[fF]/ig, '%2F');
  21. var _method = Pointer_stringify(method);
  22. if (wr.loglevel <= 1) /*information*/
  23. console.log(wr.nextRequestId + ' XHR_Create ' + _method + ' ' + _url);
  24. var http = new XMLHttpRequest();
  25. if (user && passwd)
  26. {
  27. var u = Pointer_stringify(user);
  28. var p = Pointer_stringify(passwd);
  29. http.withCredentials = true;
  30. http.open(_method, _url, /*async:*/ true , u, p);
  31. }
  32. else
  33. http.open(_method, _url, /*async:*/ true);
  34. http.responseType = 'arraybuffer';
  35. wr.requestInstances[wr.nextRequestId] = http;
  36. return wr.nextRequestId++;
  37. },
  38. XHR_SetTimeout: function (request, timeout)
  39. {
  40. if (wr.loglevel <= 1) /*information*/
  41. console.log(request + ' XHR_SetTimeout ' + timeout);
  42. wr.requestInstances[request].timeout = timeout;
  43. },
  44. XHR_SetRequestHeader: function (request, header, value)
  45. {
  46. var _header = Pointer_stringify(header);
  47. var _value = Pointer_stringify(value);
  48. if (wr.loglevel <= 1) /*information*/
  49. console.log(request + ' XHR_SetRequestHeader ' + _header + ' ' + _value);
  50. wr.requestInstances[request].setRequestHeader(_header, _value);
  51. },
  52. XHR_SetResponseHandler: function (request, onresponse, onerror, ontimeout, onaborted)
  53. {
  54. if (wr.loglevel <= 1) /*information*/
  55. console.log(request + ' XHR_SetResponseHandler');
  56. var http = wr.requestInstances[request];
  57. // LOAD
  58. http.onload = function http_onload(e) {
  59. if (wr.loglevel <= 1) /*information*/
  60. console.log(request + ' - onload ' + http.status + ' ' + http.statusText);
  61. if (onresponse)
  62. {
  63. var response = 0;
  64. if (!!http.response)
  65. response = http.response;
  66. var byteArray = new Uint8Array(response);
  67. var buffer = _malloc(byteArray.length);
  68. HEAPU8.set(byteArray, buffer);
  69. Runtime.dynCall('viiiii', onresponse, [request, http.status, buffer, byteArray.length, 0]);
  70. _free(buffer);
  71. }
  72. };
  73. if (onerror)
  74. {
  75. http.onerror = function http_onerror(e) {
  76. function HandleError(err)
  77. {
  78. var length = lengthBytesUTF8(err) + 1;
  79. var buffer = _malloc(length);
  80. stringToUTF8Array(err, HEAPU8, buffer, length);
  81. Runtime.dynCall('vii', onerror, [request, buffer]);
  82. _free(buffer);
  83. }
  84. if (e.error)
  85. HandleError(e.error);
  86. else
  87. HandleError("Unknown Error! Maybe a CORS porblem?");
  88. };
  89. }
  90. if (ontimeout)
  91. http.ontimeout = function http_onerror(e) {
  92. Runtime.dynCall('vi', ontimeout, [request]);
  93. };
  94. if (onaborted)
  95. http.onabort = function http_onerror(e) {
  96. Runtime.dynCall('vi', onaborted, [request]);
  97. };
  98. },
  99. XHR_SetProgressHandler: function (request, onprogress, onuploadprogress)
  100. {
  101. if (wr.loglevel <= 1) /*information*/
  102. console.log(request + ' XHR_SetProgressHandler');
  103. var http = wr.requestInstances[request];
  104. if (http)
  105. {
  106. if (onprogress)
  107. http.onprogress = function http_onprogress(e) {
  108. if (wr.loglevel <= 1) /*information*/
  109. console.log(request + ' XHR_SetProgressHandler - onProgress ' + e.loaded + ' ' + e.total);
  110. if (e.lengthComputable)
  111. Runtime.dynCall('viii', onprogress, [request, e.loaded, e.total]);
  112. };
  113. if (onuploadprogress)
  114. http.upload.addEventListener("progress", function http_onprogress(e) {
  115. if (wr.loglevel <= 1) /*information*/
  116. console.log(request + ' XHR_SetProgressHandler - onUploadProgress ' + e.loaded + ' ' + e.total);
  117. if (e.lengthComputable)
  118. Runtime.dynCall('viii', onuploadprogress, [request, e.loaded, e.total]);
  119. }, true);
  120. }
  121. },
  122. XHR_Send: function (request, ptr, length)
  123. {
  124. if (wr.loglevel <= 1) /*information*/
  125. console.log(request + ' XHR_Send ' + ptr + ' ' + length);
  126. var http = wr.requestInstances[request];
  127. try {
  128. if (length > 0)
  129. http.send(HEAPU8.subarray(ptr, ptr+length));
  130. else
  131. http.send();
  132. }
  133. catch(e) {
  134. if (wr.loglevel <= 4) /*exception*/
  135. console.error(request + ' ' + e.name + ": " + e.message);
  136. }
  137. },
  138. XHR_GetResponseHeaders: function(request, callback)
  139. {
  140. if (wr.loglevel <= 1) /*information*/
  141. console.log(request + ' XHR_GetResponseHeaders');
  142. var headers = wr.requestInstances[request].getAllResponseHeaders().trim() + "\r\n";
  143. if (wr.loglevel <= 1) /*information*/
  144. console.log(' "' + headers + '"');
  145. var byteArray = new Uint8Array(headers.length);
  146. for(var i=0,j=headers.length;i<j;++i){
  147. byteArray[i]=headers.charCodeAt(i);
  148. }
  149. var buffer = _malloc(byteArray.length);
  150. HEAPU8.set(byteArray, buffer);
  151. Runtime.dynCall('viii', callback, [request, buffer, byteArray.length]);
  152. _free(buffer);
  153. },
  154. XHR_GetStatusLine: function(request, callback)
  155. {
  156. if (wr.loglevel <= 1) /*information*/
  157. console.log(request + ' XHR_GetStatusLine');
  158. var status = "HTTP/1.1 " + wr.requestInstances[request].status + " " + wr.requestInstances[request].statusText;
  159. if (wr.loglevel <= 1) /*information*/
  160. console.log(status);
  161. var byteArray = new Uint8Array(status.length);
  162. for(var i=0,j=status.length;i<j;++i){
  163. byteArray[i]=status.charCodeAt(i);
  164. }
  165. var buffer = _malloc(byteArray.length);
  166. HEAPU8.set(byteArray, buffer);
  167. Runtime.dynCall('viii', callback, [request, buffer, byteArray.length]);
  168. _free(buffer);
  169. },
  170. XHR_Abort: function (request)
  171. {
  172. if (wr.loglevel <= 1) /*information*/
  173. console.log(request + ' XHR_Abort');
  174. wr.requestInstances[request].abort();
  175. },
  176. XHR_Release: function (request)
  177. {
  178. if (wr.loglevel <= 1) /*information*/
  179. console.log(request + ' XHR_Release');
  180. delete wr.requestInstances[request];
  181. },
  182. XHR_SetLoglevel: function (level)
  183. {
  184. wr.loglevel = level;
  185. }
  186. };
  187. autoAddDeps(Lib_BEST_HTTP_WebGL_HTTP_Bridge, '$wr');
  188. mergeInto(LibraryManager.library, Lib_BEST_HTTP_WebGL_HTTP_Bridge);