http_client_web.cpp 8.7 KB

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