http_client_javascript.cpp 10.0 KB

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