BestHTTP_WebRequest.jslib 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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, withCredentials)
  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 - withCredentials: ' + withCredentials + ' method: ' + _method + ' url: ' + _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.withCredentials = withCredentials;
  34. http.open(_method, _url, /*async:*/ true);
  35. }
  36. http.responseType = 'arraybuffer';
  37. wr.requestInstances[wr.nextRequestId] = http;
  38. return wr.nextRequestId++;
  39. },
  40. XHR_SetTimeout: function (request, timeout)
  41. {
  42. if (wr.loglevel <= 1) /*information*/
  43. console.log(request + ' XHR_SetTimeout ' + timeout);
  44. wr.requestInstances[request].timeout = timeout;
  45. },
  46. XHR_SetRequestHeader: function (request, header, value)
  47. {
  48. var _header = Pointer_stringify(header);
  49. var _value = Pointer_stringify(value);
  50. if (wr.loglevel <= 1) /*information*/
  51. console.log(request + ' XHR_SetRequestHeader ' + _header + ' ' + _value);
  52. if (_header != 'Cookie')
  53. wr.requestInstances[request].setRequestHeader(_header, _value);
  54. else {
  55. var cookies = _value.split(';');
  56. for (var i = 0; i < cookies.length; i++) {
  57. document.cookie = cookies[i];
  58. }
  59. }
  60. },
  61. XHR_SetResponseHandler: function (request, onresponse, onerror, ontimeout, onaborted)
  62. {
  63. if (wr.loglevel <= 1) /*information*/
  64. console.log(request + ' XHR_SetResponseHandler');
  65. var http = wr.requestInstances[request];
  66. // LOAD
  67. http.onload = function http_onload(e) {
  68. if (wr.loglevel <= 1) /*information*/
  69. console.log(request + ' - onload ' + http.status + ' ' + http.statusText);
  70. if (onresponse)
  71. {
  72. var response = 0;
  73. if (!!http.response)
  74. response = http.response;
  75. var byteArray = new Uint8Array(response);
  76. var buffer = _malloc(byteArray.length);
  77. HEAPU8.set(byteArray, buffer);
  78. Runtime.dynCall('viiiii', onresponse, [request, http.status, buffer, byteArray.length, 0]);
  79. _free(buffer);
  80. }
  81. };
  82. if (onerror)
  83. {
  84. http.onerror = function http_onerror(e) {
  85. function HandleError(err)
  86. {
  87. var length = lengthBytesUTF8(err) + 1;
  88. var buffer = _malloc(length);
  89. stringToUTF8Array(err, HEAPU8, buffer, length);
  90. Runtime.dynCall('vii', onerror, [request, buffer]);
  91. _free(buffer);
  92. }
  93. if (e.error)
  94. HandleError(e.error);
  95. else
  96. HandleError("Unknown Error! Maybe a CORS porblem?");
  97. };
  98. }
  99. if (ontimeout)
  100. http.ontimeout = function http_onerror(e) {
  101. Runtime.dynCall('vi', ontimeout, [request]);
  102. };
  103. if (onaborted)
  104. http.onabort = function http_onerror(e) {
  105. Runtime.dynCall('vi', onaborted, [request]);
  106. };
  107. },
  108. XHR_SetProgressHandler: function (request, onprogress, onuploadprogress)
  109. {
  110. if (wr.loglevel <= 1) /*information*/
  111. console.log(request + ' XHR_SetProgressHandler');
  112. var http = wr.requestInstances[request];
  113. if (http)
  114. {
  115. if (onprogress)
  116. http.onprogress = function http_onprogress(e) {
  117. if (wr.loglevel <= 1) /*information*/
  118. console.log(request + ' XHR_SetProgressHandler - onProgress ' + e.loaded + ' ' + e.total);
  119. if (e.lengthComputable)
  120. Runtime.dynCall('viii', onprogress, [request, e.loaded, e.total]);
  121. };
  122. if (onuploadprogress)
  123. http.upload.addEventListener("progress", function http_onprogress(e) {
  124. if (wr.loglevel <= 1) /*information*/
  125. console.log(request + ' XHR_SetProgressHandler - onUploadProgress ' + e.loaded + ' ' + e.total);
  126. if (e.lengthComputable)
  127. Runtime.dynCall('viii', onuploadprogress, [request, e.loaded, e.total]);
  128. }, true);
  129. }
  130. },
  131. XHR_Send: function (request, ptr, length)
  132. {
  133. if (wr.loglevel <= 1) /*information*/
  134. console.log(request + ' XHR_Send ' + ptr + ' ' + length);
  135. var http = wr.requestInstances[request];
  136. try {
  137. if (length > 0)
  138. http.send(HEAPU8.subarray(ptr, ptr+length));
  139. else
  140. http.send();
  141. }
  142. catch(e) {
  143. if (wr.loglevel <= 4) /*exception*/
  144. console.error(request + ' ' + e.name + ": " + e.message);
  145. }
  146. },
  147. XHR_GetResponseHeaders: function(request, callback)
  148. {
  149. if (wr.loglevel <= 1) /*information*/
  150. console.log(request + ' XHR_GetResponseHeaders');
  151. var headers = ''
  152. var cookies = document.cookie.split(';');
  153. for(var i = 0; i < cookies.length; ++i)
  154. headers += "Set-Cookie:" + cookies[i] + "\r\n";
  155. var additionalHeaders = wr.requestInstances[request].getAllResponseHeaders().trim();
  156. if (additionalHeaders.length > 0) {
  157. headers += additionalHeaders;
  158. headers += "\r\n";
  159. }
  160. if (wr.loglevel <= 1) /*information*/
  161. console.log(' "' + headers + '"');
  162. var byteArray = new Uint8Array(headers.length);
  163. for(var i=0,j=headers.length;i<j;++i){
  164. byteArray[i]=headers.charCodeAt(i);
  165. }
  166. var buffer = _malloc(byteArray.length);
  167. HEAPU8.set(byteArray, buffer);
  168. Runtime.dynCall('viii', callback, [request, buffer, byteArray.length]);
  169. _free(buffer);
  170. },
  171. XHR_GetStatusLine: function(request, callback)
  172. {
  173. if (wr.loglevel <= 1) /*information*/
  174. console.log(request + ' XHR_GetStatusLine');
  175. var status = "HTTP/1.1 " + wr.requestInstances[request].status + " " + wr.requestInstances[request].statusText;
  176. if (wr.loglevel <= 1) /*information*/
  177. console.log(status);
  178. var byteArray = new Uint8Array(status.length);
  179. for(var i=0,j=status.length;i<j;++i){
  180. byteArray[i]=status.charCodeAt(i);
  181. }
  182. var buffer = _malloc(byteArray.length);
  183. HEAPU8.set(byteArray, buffer);
  184. Runtime.dynCall('viii', callback, [request, buffer, byteArray.length]);
  185. _free(buffer);
  186. },
  187. XHR_Abort: function (request)
  188. {
  189. if (wr.loglevel <= 1) /*information*/
  190. console.log(request + ' XHR_Abort');
  191. wr.requestInstances[request].abort();
  192. },
  193. XHR_Release: function (request)
  194. {
  195. if (wr.loglevel <= 1) /*information*/
  196. console.log(request + ' XHR_Release');
  197. delete wr.requestInstances[request];
  198. },
  199. XHR_SetLoglevel: function (level)
  200. {
  201. wr.loglevel = level;
  202. }
  203. };
  204. autoAddDeps(Lib_BEST_HTTP_WebGL_HTTP_Bridge, '$wr');
  205. mergeInto(LibraryManager.library, Lib_BEST_HTTP_WebGL_HTTP_Bridge);