http_client_javascript.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*************************************************************************/
  2. /* http_client_javascript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #include "core/io/http_client.h"
  31. #include "http_request.h"
  32. Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl, bool p_verify_host) {
  33. close();
  34. if (p_ssl && !p_verify_host) {
  35. WARN_PRINT("Disabling HTTPClient's host verification is not supported for the HTML5 platform, host will be verified");
  36. }
  37. port = p_port;
  38. use_tls = p_ssl;
  39. host = p_host;
  40. String host_lower = host.to_lower();
  41. if (host_lower.begins_with("http://")) {
  42. host = host.substr(7, host.length() - 7);
  43. } else if (host_lower.begins_with("https://")) {
  44. use_tls = true;
  45. host = host.substr(8, host.length() - 8);
  46. }
  47. ERR_FAIL_COND_V(host.length() < HOST_MIN_LEN, ERR_INVALID_PARAMETER);
  48. if (port < 0) {
  49. if (use_tls) {
  50. port = PORT_HTTPS;
  51. } else {
  52. port = PORT_HTTP;
  53. }
  54. }
  55. status = host.is_valid_ip_address() ? STATUS_CONNECTING : STATUS_RESOLVING;
  56. return OK;
  57. }
  58. void HTTPClient::set_connection(const Ref<StreamPeer> &p_connection) {
  59. ERR_EXPLAIN("Accessing an HTTPClient's StreamPeer is not supported for the HTML5 platform");
  60. ERR_FAIL();
  61. }
  62. Ref<StreamPeer> HTTPClient::get_connection() const {
  63. ERR_EXPLAIN("Accessing an HTTPClient's StreamPeer is not supported for the HTML5 platform");
  64. ERR_FAIL_V(REF());
  65. }
  66. Error HTTPClient::prepare_request(Method p_method, const String &p_url, const Vector<String> &p_headers) {
  67. ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
  68. ERR_EXPLAIN("HTTP methods TRACE and CONNECT are not supported for the HTML5 platform");
  69. ERR_FAIL_COND_V(p_method == METHOD_TRACE || p_method == METHOD_CONNECT, ERR_UNAVAILABLE);
  70. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
  71. ERR_FAIL_COND_V(host.empty(), ERR_UNCONFIGURED);
  72. ERR_FAIL_COND_V(port < 0, ERR_UNCONFIGURED);
  73. ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER);
  74. String url = (use_tls ? "https://" : "http://") + host + ":" + itos(port) + p_url;
  75. godot_xhr_reset(xhr_id);
  76. godot_xhr_open(xhr_id, _methods[p_method], url.utf8().get_data(),
  77. username.empty() ? NULL : username.utf8().get_data(),
  78. password.empty() ? NULL : password.utf8().get_data());
  79. for (int i = 0; i < p_headers.size(); i++) {
  80. int header_separator = p_headers[i].find(": ");
  81. ERR_FAIL_COND_V(header_separator < 0, ERR_INVALID_PARAMETER);
  82. godot_xhr_set_request_header(xhr_id,
  83. p_headers[i].left(header_separator).utf8().get_data(),
  84. p_headers[i].right(header_separator + 2).utf8().get_data());
  85. }
  86. response_read_offset = 0;
  87. status = STATUS_REQUESTING;
  88. return OK;
  89. }
  90. Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const PoolVector<uint8_t> &p_body) {
  91. Error err = prepare_request(p_method, p_url, p_headers);
  92. if (err != OK)
  93. return err;
  94. PoolByteArray::Read read = p_body.read();
  95. godot_xhr_send_data(xhr_id, read.ptr(), p_body.size());
  96. return OK;
  97. }
  98. Error HTTPClient::request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body) {
  99. Error err = prepare_request(p_method, p_url, p_headers);
  100. if (err != OK)
  101. return err;
  102. godot_xhr_send_string(xhr_id, p_body.utf8().get_data());
  103. return OK;
  104. }
  105. void HTTPClient::close() {
  106. host = "";
  107. port = -1;
  108. use_tls = false;
  109. status = STATUS_DISCONNECTED;
  110. polled_response.resize(0);
  111. polled_response_code = 0;
  112. polled_response_header = String();
  113. godot_xhr_reset(xhr_id);
  114. }
  115. HTTPClient::Status HTTPClient::get_status() const {
  116. return status;
  117. }
  118. bool HTTPClient::has_response() const {
  119. return !polled_response_header.empty();
  120. }
  121. bool HTTPClient::is_response_chunked() const {
  122. // TODO evaluate using moz-chunked-arraybuffer, fetch & ReadableStream
  123. return false;
  124. }
  125. int HTTPClient::get_response_code() const {
  126. return polled_response_code;
  127. }
  128. Error HTTPClient::get_response_headers(List<String> *r_response) {
  129. if (polled_response_header.empty())
  130. return ERR_INVALID_PARAMETER;
  131. Vector<String> header_lines = polled_response_header.split("\r\n", false);
  132. for (int i = 0; i < header_lines.size(); ++i) {
  133. r_response->push_back(header_lines[i]);
  134. }
  135. polled_response_header = String();
  136. return OK;
  137. }
  138. int HTTPClient::get_response_body_length() const {
  139. return polled_response.size();
  140. }
  141. PoolByteArray HTTPClient::read_response_body_chunk() {
  142. ERR_FAIL_COND_V(status != STATUS_BODY, PoolByteArray());
  143. int to_read = MIN(read_limit, polled_response.size() - response_read_offset);
  144. PoolByteArray chunk;
  145. chunk.resize(to_read);
  146. PoolByteArray::Write write = chunk.write();
  147. PoolByteArray::Read read = polled_response.read();
  148. memcpy(write.ptr(), read.ptr() + response_read_offset, to_read);
  149. write = PoolByteArray::Write();
  150. read = PoolByteArray::Read();
  151. response_read_offset += to_read;
  152. if (response_read_offset == polled_response.size()) {
  153. status = STATUS_CONNECTED;
  154. polled_response.resize(0);
  155. godot_xhr_reset(xhr_id);
  156. }
  157. return chunk;
  158. }
  159. void HTTPClient::set_blocking_mode(bool p_enable) {
  160. ERR_EXPLAIN("HTTPClient blocking mode is not supported for the HTML5 platform");
  161. ERR_FAIL_COND(p_enable);
  162. }
  163. bool HTTPClient::is_blocking_mode_enabled() const {
  164. return false;
  165. }
  166. void HTTPClient::set_read_chunk_size(int p_size) {
  167. read_limit = p_size;
  168. }
  169. Error HTTPClient::poll() {
  170. switch (status) {
  171. case STATUS_DISCONNECTED:
  172. return ERR_UNCONFIGURED;
  173. case STATUS_RESOLVING:
  174. status = STATUS_CONNECTING;
  175. return OK;
  176. case STATUS_CONNECTING:
  177. status = STATUS_CONNECTED;
  178. return OK;
  179. case STATUS_CONNECTED:
  180. case STATUS_BODY:
  181. return OK;
  182. case STATUS_CONNECTION_ERROR:
  183. return ERR_CONNECTION_ERROR;
  184. case STATUS_REQUESTING: {
  185. #ifdef DEBUG_ENABLED
  186. if (!has_polled) {
  187. has_polled = true;
  188. } else {
  189. // forcing synchronous requests is not possible on the web
  190. if (last_polling_frame == Engine::get_singleton()->get_idle_frames()) {
  191. WARN_PRINT("HTTPClient polled multiple times in one frame, "
  192. "but request cannot progress more than once per "
  193. "frame on the HTML5 platform.");
  194. }
  195. }
  196. last_polling_frame = Engine::get_singleton()->get_idle_frames();
  197. #endif
  198. polled_response_code = godot_xhr_get_status(xhr_id);
  199. if (godot_xhr_get_ready_state(xhr_id) != XHR_READY_STATE_DONE) {
  200. return OK;
  201. } else if (!polled_response_code) {
  202. status = STATUS_CONNECTION_ERROR;
  203. return ERR_CONNECTION_ERROR;
  204. }
  205. status = STATUS_BODY;
  206. PoolByteArray bytes;
  207. int len = godot_xhr_get_response_headers_length(xhr_id);
  208. bytes.resize(len + 1);
  209. PoolByteArray::Write write = bytes.write();
  210. godot_xhr_get_response_headers(xhr_id, reinterpret_cast<char *>(write.ptr()), len);
  211. write[len] = 0;
  212. write = PoolByteArray::Write();
  213. PoolByteArray::Read read = bytes.read();
  214. polled_response_header = String::utf8(reinterpret_cast<const char *>(read.ptr()));
  215. read = PoolByteArray::Read();
  216. polled_response.resize(godot_xhr_get_response_length(xhr_id));
  217. write = polled_response.write();
  218. godot_xhr_get_response(xhr_id, write.ptr(), polled_response.size());
  219. write = PoolByteArray::Write();
  220. break;
  221. }
  222. default:
  223. ERR_FAIL_V(ERR_BUG);
  224. }
  225. return OK;
  226. }
  227. HTTPClient::HTTPClient() {
  228. xhr_id = godot_xhr_new();
  229. read_limit = 4096;
  230. status = STATUS_DISCONNECTED;
  231. port = -1;
  232. use_tls = false;
  233. polled_response_code = 0;
  234. #ifdef DEBUG_ENABLED
  235. has_polled = false;
  236. last_polling_frame = 0;
  237. #endif
  238. }
  239. HTTPClient::~HTTPClient() {
  240. godot_xhr_free(xhr_id);
  241. }