library_godot_http_request.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*************************************************************************/
  2. /* http_request.js */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. const GodotHTTPRequest = {
  31. $GodotHTTPRequest__deps: ['$GodotRuntime'],
  32. $GodotHTTPRequest: {
  33. requests: [],
  34. getUnusedRequestId: function () {
  35. const idMax = GodotHTTPRequest.requests.length;
  36. for (let potentialId = 0; potentialId < idMax; ++potentialId) {
  37. if (GodotHTTPRequest.requests[potentialId] instanceof XMLHttpRequest) {
  38. continue;
  39. }
  40. return potentialId;
  41. }
  42. GodotHTTPRequest.requests.push(null);
  43. return idMax;
  44. },
  45. setupRequest: function (xhr) {
  46. xhr.responseType = 'arraybuffer';
  47. },
  48. },
  49. godot_xhr_new__sig: 'i',
  50. godot_xhr_new: function () {
  51. const newId = GodotHTTPRequest.getUnusedRequestId();
  52. GodotHTTPRequest.requests[newId] = new XMLHttpRequest();
  53. GodotHTTPRequest.setupRequest(GodotHTTPRequest.requests[newId]);
  54. return newId;
  55. },
  56. godot_xhr_reset__sig: 'vi',
  57. godot_xhr_reset: function (xhrId) {
  58. GodotHTTPRequest.requests[xhrId] = new XMLHttpRequest();
  59. GodotHTTPRequest.setupRequest(GodotHTTPRequest.requests[xhrId]);
  60. },
  61. godot_xhr_free__sig: 'vi',
  62. godot_xhr_free: function (xhrId) {
  63. GodotHTTPRequest.requests[xhrId].abort();
  64. GodotHTTPRequest.requests[xhrId] = null;
  65. },
  66. godot_xhr_open__sig: 'viiiii',
  67. godot_xhr_open: function (xhrId, method, url, p_user, p_password) {
  68. const user = p_user > 0 ? GodotRuntime.parseString(p_user) : null;
  69. const password = p_password > 0 ? GodotRuntime.parseString(p_password) : null;
  70. GodotHTTPRequest.requests[xhrId].open(GodotRuntime.parseString(method), GodotRuntime.parseString(url), true, user, password);
  71. },
  72. godot_xhr_set_request_header__sig: 'viii',
  73. godot_xhr_set_request_header: function (xhrId, header, value) {
  74. GodotHTTPRequest.requests[xhrId].setRequestHeader(GodotRuntime.parseString(header), GodotRuntime.parseString(value));
  75. },
  76. godot_xhr_send_null__sig: 'vi',
  77. godot_xhr_send_null: function (xhrId) {
  78. GodotHTTPRequest.requests[xhrId].send();
  79. },
  80. godot_xhr_send_string__sig: 'vii',
  81. godot_xhr_send_string: function (xhrId, strPtr) {
  82. if (!strPtr) {
  83. GodotRuntime.error('Failed to send string per XHR: null pointer');
  84. return;
  85. }
  86. GodotHTTPRequest.requests[xhrId].send(GodotRuntime.parseString(strPtr));
  87. },
  88. godot_xhr_send_data__sig: 'viii',
  89. godot_xhr_send_data: function (xhrId, ptr, len) {
  90. if (!ptr) {
  91. GodotRuntime.error('Failed to send data per XHR: null pointer');
  92. return;
  93. }
  94. if (len < 0) {
  95. GodotRuntime.error('Failed to send data per XHR: buffer length less than 0');
  96. return;
  97. }
  98. GodotHTTPRequest.requests[xhrId].send(HEAPU8.subarray(ptr, ptr + len));
  99. },
  100. godot_xhr_abort__sig: 'vi',
  101. godot_xhr_abort: function (xhrId) {
  102. GodotHTTPRequest.requests[xhrId].abort();
  103. },
  104. godot_xhr_get_status__sig: 'ii',
  105. godot_xhr_get_status: function (xhrId) {
  106. return GodotHTTPRequest.requests[xhrId].status;
  107. },
  108. godot_xhr_get_ready_state__sig: 'ii',
  109. godot_xhr_get_ready_state: function (xhrId) {
  110. return GodotHTTPRequest.requests[xhrId].readyState;
  111. },
  112. godot_xhr_get_response_headers_length__sig: 'ii',
  113. godot_xhr_get_response_headers_length: function (xhrId) {
  114. const headers = GodotHTTPRequest.requests[xhrId].getAllResponseHeaders();
  115. return headers === null ? 0 : GodotRuntime.strlen(headers);
  116. },
  117. godot_xhr_get_response_headers__sig: 'viii',
  118. godot_xhr_get_response_headers: function (xhrId, dst, len) {
  119. const str = GodotHTTPRequest.requests[xhrId].getAllResponseHeaders();
  120. if (str === null) {
  121. return;
  122. }
  123. GodotRuntime.stringToHeap(str, dst, len);
  124. },
  125. godot_xhr_get_response_length__sig: 'ii',
  126. godot_xhr_get_response_length: function (xhrId) {
  127. const body = GodotHTTPRequest.requests[xhrId].response;
  128. return body === null ? 0 : body.byteLength;
  129. },
  130. godot_xhr_get_response__sig: 'viii',
  131. godot_xhr_get_response: function (xhrId, dst, len) {
  132. let buf = GodotHTTPRequest.requests[xhrId].response;
  133. if (buf === null) {
  134. return;
  135. }
  136. buf = new Uint8Array(buf).subarray(0, len);
  137. HEAPU8.set(buf, dst);
  138. },
  139. };
  140. autoAddDeps(GodotHTTPRequest, '$GodotHTTPRequest');
  141. mergeInto(LibraryManager.library, GodotHTTPRequest);