http_client_javascript.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "http_client_javascript.h"
  31. void HTTPClientJavaScript::_parse_headers(int p_len, const char **p_headers, void *p_ref) {
  32. HTTPClientJavaScript *client = static_cast<HTTPClientJavaScript *>(p_ref);
  33. for (int i = 0; i < p_len; i++) {
  34. client->response_headers.push_back(String::utf8(p_headers[i]));
  35. }
  36. }
  37. Error HTTPClientJavaScript::connect_to_host(const String &p_host, int p_port, bool p_ssl, bool p_verify_host) {
  38. close();
  39. if (p_ssl && !p_verify_host) {
  40. WARN_PRINT("Disabling HTTPClientJavaScript's host verification is not supported for the HTML5 platform, host will be verified");
  41. }
  42. port = p_port;
  43. use_tls = p_ssl;
  44. host = p_host;
  45. String host_lower = host.to_lower();
  46. if (host_lower.begins_with("http://")) {
  47. host = host.substr(7, host.length() - 7);
  48. } else if (host_lower.begins_with("https://")) {
  49. use_tls = true;
  50. host = host.substr(8, host.length() - 8);
  51. }
  52. ERR_FAIL_COND_V(host.length() < HOST_MIN_LEN, ERR_INVALID_PARAMETER);
  53. if (port < 0) {
  54. if (use_tls) {
  55. port = PORT_HTTPS;
  56. } else {
  57. port = PORT_HTTP;
  58. }
  59. }
  60. status = host.is_valid_ip_address() ? STATUS_CONNECTING : STATUS_RESOLVING;
  61. return OK;
  62. }
  63. void HTTPClientJavaScript::set_connection(const Ref<StreamPeer> &p_connection) {
  64. ERR_FAIL_MSG("Accessing an HTTPClientJavaScript's StreamPeer is not supported for the HTML5 platform.");
  65. }
  66. Ref<StreamPeer> HTTPClientJavaScript::get_connection() const {
  67. ERR_FAIL_V_MSG(REF(), "Accessing an HTTPClientJavaScript's StreamPeer is not supported for the HTML5 platform.");
  68. }
  69. Error HTTPClientJavaScript::request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_len) {
  70. ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
  71. ERR_FAIL_COND_V_MSG(p_method == METHOD_TRACE || p_method == METHOD_CONNECT, ERR_UNAVAILABLE, "HTTP methods TRACE and CONNECT are not supported for the HTML5 platform.");
  72. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
  73. ERR_FAIL_COND_V(host.is_empty(), ERR_UNCONFIGURED);
  74. ERR_FAIL_COND_V(port < 0, ERR_UNCONFIGURED);
  75. ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER);
  76. Error err = verify_headers(p_headers);
  77. if (err) {
  78. return err;
  79. }
  80. String url = (use_tls ? "https://" : "http://") + host + ":" + itos(port) + p_url;
  81. Vector<CharString> keeper;
  82. Vector<const char *> c_strings;
  83. for (int i = 0; i < p_headers.size(); i++) {
  84. keeper.push_back(p_headers[i].utf8());
  85. c_strings.push_back(keeper[i].get_data());
  86. }
  87. if (js_id) {
  88. godot_js_fetch_free(js_id);
  89. }
  90. js_id = godot_js_fetch_create(_methods[p_method], url.utf8().get_data(), c_strings.ptrw(), c_strings.size(), p_body, p_body_len);
  91. status = STATUS_REQUESTING;
  92. return OK;
  93. }
  94. void HTTPClientJavaScript::close() {
  95. host = "";
  96. port = -1;
  97. use_tls = false;
  98. status = STATUS_DISCONNECTED;
  99. polled_response_code = 0;
  100. response_headers.resize(0);
  101. response_buffer.resize(0);
  102. if (js_id) {
  103. godot_js_fetch_free(js_id);
  104. js_id = 0;
  105. }
  106. }
  107. HTTPClientJavaScript::Status HTTPClientJavaScript::get_status() const {
  108. return status;
  109. }
  110. bool HTTPClientJavaScript::has_response() const {
  111. return response_headers.size() > 0;
  112. }
  113. bool HTTPClientJavaScript::is_response_chunked() const {
  114. return godot_js_fetch_is_chunked(js_id);
  115. }
  116. int HTTPClientJavaScript::get_response_code() const {
  117. return polled_response_code;
  118. }
  119. Error HTTPClientJavaScript::get_response_headers(List<String> *r_response) {
  120. if (!response_headers.size()) {
  121. return ERR_INVALID_PARAMETER;
  122. }
  123. for (int i = 0; i < response_headers.size(); i++) {
  124. r_response->push_back(response_headers[i]);
  125. }
  126. response_headers.clear();
  127. return OK;
  128. }
  129. int64_t HTTPClientJavaScript::get_response_body_length() const {
  130. return godot_js_fetch_body_length_get(js_id);
  131. }
  132. PackedByteArray HTTPClientJavaScript::read_response_body_chunk() {
  133. ERR_FAIL_COND_V(status != STATUS_BODY, PackedByteArray());
  134. if (response_buffer.size() != read_limit) {
  135. response_buffer.resize(read_limit);
  136. }
  137. int read = godot_js_fetch_read_chunk(js_id, response_buffer.ptrw(), read_limit);
  138. // Check if the stream is over.
  139. godot_js_fetch_state_t state = godot_js_fetch_state_get(js_id);
  140. if (state == GODOT_JS_FETCH_STATE_DONE) {
  141. status = STATUS_DISCONNECTED;
  142. } else if (state != GODOT_JS_FETCH_STATE_BODY) {
  143. status = STATUS_CONNECTION_ERROR;
  144. }
  145. PackedByteArray chunk;
  146. if (!read) {
  147. return chunk;
  148. }
  149. chunk.resize(read);
  150. memcpy(chunk.ptrw(), response_buffer.ptr(), read);
  151. return chunk;
  152. }
  153. void HTTPClientJavaScript::set_blocking_mode(bool p_enable) {
  154. ERR_FAIL_COND_MSG(p_enable, "HTTPClientJavaScript blocking mode is not supported for the HTML5 platform.");
  155. }
  156. bool HTTPClientJavaScript::is_blocking_mode_enabled() const {
  157. return false;
  158. }
  159. void HTTPClientJavaScript::set_read_chunk_size(int p_size) {
  160. read_limit = p_size;
  161. }
  162. int HTTPClientJavaScript::get_read_chunk_size() const {
  163. return read_limit;
  164. }
  165. Error HTTPClientJavaScript::poll() {
  166. switch (status) {
  167. case STATUS_DISCONNECTED:
  168. return ERR_UNCONFIGURED;
  169. case STATUS_RESOLVING:
  170. status = STATUS_CONNECTING;
  171. return OK;
  172. case STATUS_CONNECTING:
  173. status = STATUS_CONNECTED;
  174. return OK;
  175. case STATUS_CONNECTED:
  176. return OK;
  177. case STATUS_BODY: {
  178. godot_js_fetch_state_t state = godot_js_fetch_state_get(js_id);
  179. if (state == GODOT_JS_FETCH_STATE_DONE) {
  180. status = STATUS_DISCONNECTED;
  181. } else if (state != GODOT_JS_FETCH_STATE_BODY) {
  182. status = STATUS_CONNECTION_ERROR;
  183. return ERR_CONNECTION_ERROR;
  184. }
  185. return OK;
  186. }
  187. case STATUS_CONNECTION_ERROR:
  188. return ERR_CONNECTION_ERROR;
  189. case STATUS_REQUESTING: {
  190. #ifdef DEBUG_ENABLED
  191. // forcing synchronous requests is not possible on the web
  192. if (last_polling_frame == Engine::get_singleton()->get_process_frames()) {
  193. WARN_PRINT("HTTPClientJavaScript polled multiple times in one frame, "
  194. "but request cannot progress more than once per "
  195. "frame on the HTML5 platform.");
  196. }
  197. last_polling_frame = Engine::get_singleton()->get_process_frames();
  198. #endif
  199. polled_response_code = godot_js_fetch_http_status_get(js_id);
  200. godot_js_fetch_state_t js_state = godot_js_fetch_state_get(js_id);
  201. if (js_state == GODOT_JS_FETCH_STATE_REQUESTING) {
  202. return OK;
  203. } else if (js_state == GODOT_JS_FETCH_STATE_ERROR) {
  204. // Fetch is in error state.
  205. status = STATUS_CONNECTION_ERROR;
  206. return ERR_CONNECTION_ERROR;
  207. }
  208. if (godot_js_fetch_read_headers(js_id, &_parse_headers, this)) {
  209. // Failed to parse headers.
  210. status = STATUS_CONNECTION_ERROR;
  211. return ERR_CONNECTION_ERROR;
  212. }
  213. status = STATUS_BODY;
  214. break;
  215. }
  216. default:
  217. ERR_FAIL_V(ERR_BUG);
  218. }
  219. return OK;
  220. }
  221. HTTPClient *HTTPClientJavaScript::_create_func() {
  222. return memnew(HTTPClientJavaScript);
  223. }
  224. HTTPClient *(*HTTPClient::_create)() = HTTPClientJavaScript::_create_func;
  225. HTTPClientJavaScript::HTTPClientJavaScript() {
  226. }
  227. HTTPClientJavaScript::~HTTPClientJavaScript() {
  228. close();
  229. }