http_client_javascript.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. String url = (use_tls ? "https://" : "http://") + host + ":" + itos(port) + p_url;
  77. Vector<CharString> keeper;
  78. Vector<const char *> c_strings;
  79. for (int i = 0; i < p_headers.size(); i++) {
  80. keeper.push_back(p_headers[i].utf8());
  81. c_strings.push_back(keeper[i].get_data());
  82. }
  83. if (js_id) {
  84. godot_js_fetch_free(js_id);
  85. }
  86. js_id = godot_js_fetch_create(_methods[p_method], url.utf8().get_data(), c_strings.ptrw(), c_strings.size(), p_body, p_body_len);
  87. status = STATUS_REQUESTING;
  88. return OK;
  89. }
  90. void HTTPClientJavaScript::close() {
  91. host = "";
  92. port = -1;
  93. use_tls = false;
  94. status = STATUS_DISCONNECTED;
  95. polled_response_code = 0;
  96. response_headers.resize(0);
  97. response_buffer.resize(0);
  98. if (js_id) {
  99. godot_js_fetch_free(js_id);
  100. js_id = 0;
  101. }
  102. }
  103. HTTPClientJavaScript::Status HTTPClientJavaScript::get_status() const {
  104. return status;
  105. }
  106. bool HTTPClientJavaScript::has_response() const {
  107. return response_headers.size() > 0;
  108. }
  109. bool HTTPClientJavaScript::is_response_chunked() const {
  110. return godot_js_fetch_is_chunked(js_id);
  111. }
  112. int HTTPClientJavaScript::get_response_code() const {
  113. return polled_response_code;
  114. }
  115. Error HTTPClientJavaScript::get_response_headers(List<String> *r_response) {
  116. if (!response_headers.size()) {
  117. return ERR_INVALID_PARAMETER;
  118. }
  119. for (int i = 0; i < response_headers.size(); i++) {
  120. r_response->push_back(response_headers[i]);
  121. }
  122. response_headers.clear();
  123. return OK;
  124. }
  125. int64_t HTTPClientJavaScript::get_response_body_length() const {
  126. return godot_js_fetch_body_length_get(js_id);
  127. }
  128. PackedByteArray HTTPClientJavaScript::read_response_body_chunk() {
  129. ERR_FAIL_COND_V(status != STATUS_BODY, PackedByteArray());
  130. if (response_buffer.size() != read_limit) {
  131. response_buffer.resize(read_limit);
  132. }
  133. int read = godot_js_fetch_read_chunk(js_id, response_buffer.ptrw(), read_limit);
  134. // Check if the stream is over.
  135. godot_js_fetch_state_t state = godot_js_fetch_state_get(js_id);
  136. if (state == GODOT_JS_FETCH_STATE_DONE) {
  137. status = STATUS_DISCONNECTED;
  138. } else if (state != GODOT_JS_FETCH_STATE_BODY) {
  139. status = STATUS_CONNECTION_ERROR;
  140. }
  141. PackedByteArray chunk;
  142. if (!read) {
  143. return chunk;
  144. }
  145. chunk.resize(read);
  146. memcpy(chunk.ptrw(), response_buffer.ptr(), read);
  147. return chunk;
  148. }
  149. void HTTPClientJavaScript::set_blocking_mode(bool p_enable) {
  150. ERR_FAIL_COND_MSG(p_enable, "HTTPClientJavaScript blocking mode is not supported for the HTML5 platform.");
  151. }
  152. bool HTTPClientJavaScript::is_blocking_mode_enabled() const {
  153. return false;
  154. }
  155. void HTTPClientJavaScript::set_read_chunk_size(int p_size) {
  156. read_limit = p_size;
  157. }
  158. int HTTPClientJavaScript::get_read_chunk_size() const {
  159. return read_limit;
  160. }
  161. Error HTTPClientJavaScript::poll() {
  162. switch (status) {
  163. case STATUS_DISCONNECTED:
  164. return ERR_UNCONFIGURED;
  165. case STATUS_RESOLVING:
  166. status = STATUS_CONNECTING;
  167. return OK;
  168. case STATUS_CONNECTING:
  169. status = STATUS_CONNECTED;
  170. return OK;
  171. case STATUS_CONNECTED:
  172. return OK;
  173. case STATUS_BODY: {
  174. godot_js_fetch_state_t state = godot_js_fetch_state_get(js_id);
  175. if (state == GODOT_JS_FETCH_STATE_DONE) {
  176. status = STATUS_DISCONNECTED;
  177. } else if (state != GODOT_JS_FETCH_STATE_BODY) {
  178. status = STATUS_CONNECTION_ERROR;
  179. return ERR_CONNECTION_ERROR;
  180. }
  181. return OK;
  182. }
  183. case STATUS_CONNECTION_ERROR:
  184. return ERR_CONNECTION_ERROR;
  185. case STATUS_REQUESTING: {
  186. #ifdef DEBUG_ENABLED
  187. // forcing synchronous requests is not possible on the web
  188. if (last_polling_frame == Engine::get_singleton()->get_process_frames()) {
  189. WARN_PRINT("HTTPClientJavaScript polled multiple times in one frame, "
  190. "but request cannot progress more than once per "
  191. "frame on the HTML5 platform.");
  192. }
  193. last_polling_frame = Engine::get_singleton()->get_process_frames();
  194. #endif
  195. polled_response_code = godot_js_fetch_http_status_get(js_id);
  196. godot_js_fetch_state_t js_state = godot_js_fetch_state_get(js_id);
  197. if (js_state == GODOT_JS_FETCH_STATE_REQUESTING) {
  198. return OK;
  199. } else if (js_state == GODOT_JS_FETCH_STATE_ERROR) {
  200. // Fetch is in error state.
  201. status = STATUS_CONNECTION_ERROR;
  202. return ERR_CONNECTION_ERROR;
  203. }
  204. if (godot_js_fetch_read_headers(js_id, &_parse_headers, this)) {
  205. // Failed to parse headers.
  206. status = STATUS_CONNECTION_ERROR;
  207. return ERR_CONNECTION_ERROR;
  208. }
  209. status = STATUS_BODY;
  210. break;
  211. }
  212. default:
  213. ERR_FAIL_V(ERR_BUG);
  214. }
  215. return OK;
  216. }
  217. HTTPClient *HTTPClientJavaScript::_create_func() {
  218. return memnew(HTTPClientJavaScript);
  219. }
  220. HTTPClient *(*HTTPClient::_create)() = HTTPClientJavaScript::_create_func;
  221. HTTPClientJavaScript::HTTPClientJavaScript() {
  222. }
  223. HTTPClientJavaScript::~HTTPClientJavaScript() {
  224. close();
  225. }