http_request.js 5.3 KB

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