library_godot_http_request.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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__sig: 'viii',
  77. godot_xhr_send: function (xhrId, p_ptr, p_len) {
  78. let data = null;
  79. if (p_ptr && p_len) {
  80. data = GodotRuntime.heapSlice(HEAP8, p_ptr, p_len);
  81. }
  82. GodotHTTPRequest.requests[xhrId].send(data);
  83. },
  84. godot_xhr_abort__sig: 'vi',
  85. godot_xhr_abort: function (xhrId) {
  86. GodotHTTPRequest.requests[xhrId].abort();
  87. },
  88. godot_xhr_get_status__sig: 'ii',
  89. godot_xhr_get_status: function (xhrId) {
  90. return GodotHTTPRequest.requests[xhrId].status;
  91. },
  92. godot_xhr_get_ready_state__sig: 'ii',
  93. godot_xhr_get_ready_state: function (xhrId) {
  94. return GodotHTTPRequest.requests[xhrId].readyState;
  95. },
  96. godot_xhr_get_response_headers_length__sig: 'ii',
  97. godot_xhr_get_response_headers_length: function (xhrId) {
  98. const headers = GodotHTTPRequest.requests[xhrId].getAllResponseHeaders();
  99. return headers === null ? 0 : GodotRuntime.strlen(headers);
  100. },
  101. godot_xhr_get_response_headers__sig: 'viii',
  102. godot_xhr_get_response_headers: function (xhrId, dst, len) {
  103. const str = GodotHTTPRequest.requests[xhrId].getAllResponseHeaders();
  104. if (str === null) {
  105. return;
  106. }
  107. GodotRuntime.stringToHeap(str, dst, len);
  108. },
  109. godot_xhr_get_response_length__sig: 'ii',
  110. godot_xhr_get_response_length: function (xhrId) {
  111. const body = GodotHTTPRequest.requests[xhrId].response;
  112. return body === null ? 0 : body.byteLength;
  113. },
  114. godot_xhr_get_response__sig: 'viii',
  115. godot_xhr_get_response: function (xhrId, dst, len) {
  116. let buf = GodotHTTPRequest.requests[xhrId].response;
  117. if (buf === null) {
  118. return;
  119. }
  120. buf = new Uint8Array(buf).subarray(0, len);
  121. HEAPU8.set(buf, dst);
  122. },
  123. };
  124. autoAddDeps(GodotHTTPRequest, '$GodotHTTPRequest');
  125. mergeInto(LibraryManager.library, GodotHTTPRequest);