2
0

library_godot_http_request.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. const GodotHTTPRequest = {
  31. $GodotHTTPRequest__deps: ['$GodotRuntime'],
  32. $GodotHTTPRequest: {
  33. requests: [],
  34. getUnusedRequestId: function() {
  35. var idMax = GodotHTTPRequest.requests.length;
  36. for (var 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: function() {
  50. var newId = GodotHTTPRequest.getUnusedRequestId();
  51. GodotHTTPRequest.requests[newId] = new XMLHttpRequest;
  52. GodotHTTPRequest.setupRequest(GodotHTTPRequest.requests[newId]);
  53. return newId;
  54. },
  55. godot_xhr_reset: function(xhrId) {
  56. GodotHTTPRequest.requests[xhrId] = new XMLHttpRequest;
  57. GodotHTTPRequest.setupRequest(GodotHTTPRequest.requests[xhrId]);
  58. },
  59. godot_xhr_free: function(xhrId) {
  60. GodotHTTPRequest.requests[xhrId].abort();
  61. GodotHTTPRequest.requests[xhrId] = null;
  62. },
  63. godot_xhr_open: function(xhrId, method, url, p_user, p_password) {
  64. const user = p_user > 0 ? GodotRuntime.parseString(p_user) : null;
  65. const password = p_password > 0 ? GodotRuntime.parseString(p_password) : null;
  66. GodotHTTPRequest.requests[xhrId].open(GodotRuntime.parseString(method), GodotRuntime.parseString(url), true, user, password);
  67. },
  68. godot_xhr_set_request_header: function(xhrId, header, value) {
  69. GodotHTTPRequest.requests[xhrId].setRequestHeader(GodotRuntime.parseString(header), GodotRuntime.parseString(value));
  70. },
  71. godot_xhr_send_null: function(xhrId) {
  72. GodotHTTPRequest.requests[xhrId].send();
  73. },
  74. godot_xhr_send_string: function(xhrId, strPtr) {
  75. if (!strPtr) {
  76. GodotRuntime.error("Failed to send string per XHR: null pointer");
  77. return;
  78. }
  79. GodotHTTPRequest.requests[xhrId].send(GodotRuntime.parseString(strPtr));
  80. },
  81. godot_xhr_send_data: function(xhrId, ptr, len) {
  82. if (!ptr) {
  83. GodotRuntime.error("Failed to send data per XHR: null pointer");
  84. return;
  85. }
  86. if (len < 0) {
  87. GodotRuntime.error("Failed to send data per XHR: buffer length less than 0");
  88. return;
  89. }
  90. GodotHTTPRequest.requests[xhrId].send(HEAPU8.subarray(ptr, ptr + len));
  91. },
  92. godot_xhr_abort: function(xhrId) {
  93. GodotHTTPRequest.requests[xhrId].abort();
  94. },
  95. godot_xhr_get_status: function(xhrId) {
  96. return GodotHTTPRequest.requests[xhrId].status;
  97. },
  98. godot_xhr_get_ready_state: function(xhrId) {
  99. return GodotHTTPRequest.requests[xhrId].readyState;
  100. },
  101. godot_xhr_get_response_headers_length: function(xhrId) {
  102. var headers = GodotHTTPRequest.requests[xhrId].getAllResponseHeaders();
  103. return headers === null ? 0 : GodotRuntime.strlen(headers);
  104. },
  105. godot_xhr_get_response_headers: function(xhrId, dst, len) {
  106. var str = GodotHTTPRequest.requests[xhrId].getAllResponseHeaders();
  107. if (str === null)
  108. return;
  109. GodotRuntime.stringToHeap(str, dst, len);
  110. },
  111. godot_xhr_get_response_length: function(xhrId) {
  112. var body = GodotHTTPRequest.requests[xhrId].response;
  113. return body === null ? 0 : body.byteLength;
  114. },
  115. godot_xhr_get_response: function(xhrId, dst, len) {
  116. var buf = GodotHTTPRequest.requests[xhrId].response;
  117. if (buf === null)
  118. return;
  119. buf = new Uint8Array(buf).subarray(0, len);
  120. HEAPU8.set(buf, dst);
  121. },
  122. };
  123. autoAddDeps(GodotHTTPRequest, "$GodotHTTPRequest");
  124. mergeInto(LibraryManager.library, GodotHTTPRequest);