http_request.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*************************************************************************/
  2. /* http_request.js */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. var GodotHTTPRequest = {
  31. $GodotHTTPRequest: {
  32. requests: [],
  33. getUnusedRequestId: function() {
  34. var idMax = GodotHTTPRequest.requests.length;
  35. for (var potentialId = 0; potentialId < idMax; ++potentialId) {
  36. if (GodotHTTPRequest.requests[potentialId] instanceof XMLHttpRequest) {
  37. continue;
  38. }
  39. return potentialId;
  40. }
  41. GodotHTTPRequest.requests.push(null)
  42. return idMax;
  43. },
  44. setupRequest: function(xhr) {
  45. xhr.responseType = 'arraybuffer';
  46. },
  47. },
  48. godot_xhr_new: function() {
  49. var newId = GodotHTTPRequest.getUnusedRequestId();
  50. GodotHTTPRequest.requests[newId] = new XMLHttpRequest;
  51. GodotHTTPRequest.setupRequest(GodotHTTPRequest.requests[newId]);
  52. return newId;
  53. },
  54. godot_xhr_reset: function(xhrId) {
  55. GodotHTTPRequest.requests[xhrId] = new XMLHttpRequest;
  56. GodotHTTPRequest.setupRequest(GodotHTTPRequest.requests[xhrId]);
  57. },
  58. godot_xhr_free: function(xhrId) {
  59. GodotHTTPRequest.requests[xhrId].abort();
  60. GodotHTTPRequest.requests[xhrId] = null;
  61. },
  62. godot_xhr_open: function(xhrId, method, url, user, password) {
  63. user = user > 0 ? UTF8ToString(user) : null;
  64. password = password > 0 ? UTF8ToString(password) : null;
  65. GodotHTTPRequest.requests[xhrId].open(UTF8ToString(method), UTF8ToString(url), true, user, password);
  66. },
  67. godot_xhr_set_request_header: function(xhrId, header, value) {
  68. GodotHTTPRequest.requests[xhrId].setRequestHeader(UTF8ToString(header), UTF8ToString(value));
  69. },
  70. godot_xhr_send_null: function(xhrId) {
  71. GodotHTTPRequest.requests[xhrId].send();
  72. },
  73. godot_xhr_send_string: function(xhrId, strPtr) {
  74. if (!strPtr) {
  75. err("Failed to send string per XHR: null pointer");
  76. return;
  77. }
  78. GodotHTTPRequest.requests[xhrId].send(UTF8ToString(strPtr));
  79. },
  80. godot_xhr_send_data: function(xhrId, ptr, len) {
  81. if (!ptr) {
  82. err("Failed to send data per XHR: null pointer");
  83. return;
  84. }
  85. if (len < 0) {
  86. err("Failed to send data per XHR: buffer length less than 0");
  87. return;
  88. }
  89. GodotHTTPRequest.requests[xhrId].send(HEAPU8.subarray(ptr, ptr + len));
  90. },
  91. godot_xhr_abort: function(xhrId) {
  92. GodotHTTPRequest.requests[xhrId].abort();
  93. },
  94. godot_xhr_get_status: function(xhrId) {
  95. return GodotHTTPRequest.requests[xhrId].status;
  96. },
  97. godot_xhr_get_ready_state: function(xhrId) {
  98. return GodotHTTPRequest.requests[xhrId].readyState;
  99. },
  100. godot_xhr_get_response_headers_length: function(xhrId) {
  101. var headers = GodotHTTPRequest.requests[xhrId].getAllResponseHeaders();
  102. return headers === null ? 0 : lengthBytesUTF8(headers);
  103. },
  104. godot_xhr_get_response_headers: function(xhrId, dst, len) {
  105. var str = GodotHTTPRequest.requests[xhrId].getAllResponseHeaders();
  106. if (str === null)
  107. return;
  108. var buf = new Uint8Array(len + 1);
  109. stringToUTF8Array(str, buf, 0, buf.length);
  110. buf = buf.subarray(0, -1);
  111. HEAPU8.set(buf, dst);
  112. },
  113. godot_xhr_get_response_length: function(xhrId) {
  114. var body = GodotHTTPRequest.requests[xhrId].response;
  115. return body === null ? 0 : body.byteLength;
  116. },
  117. godot_xhr_get_response: function(xhrId, dst, len) {
  118. var buf = GodotHTTPRequest.requests[xhrId].response;
  119. if (buf === null)
  120. return;
  121. buf = new Uint8Array(buf).subarray(0, len);
  122. HEAPU8.set(buf, dst);
  123. },
  124. };
  125. autoAddDeps(GodotHTTPRequest, "$GodotHTTPRequest");
  126. mergeInto(LibraryManager.library, GodotHTTPRequest);