http_client_javascript.cpp 8.6 KB

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