http_request.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*************************************************************************/
  2. /* http_request.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "core/io/compression.h"
  32. #include "core/string/ustring.h"
  33. void HTTPRequest::_redirect_request(const String &p_new_url) {
  34. }
  35. Error HTTPRequest::_request() {
  36. return client->connect_to_host(url, port, use_ssl, validate_ssl);
  37. }
  38. Error HTTPRequest::_parse_url(const String &p_url) {
  39. url = p_url;
  40. use_ssl = false;
  41. request_string = "";
  42. port = 80;
  43. request_sent = false;
  44. got_response = false;
  45. body_len = -1;
  46. body.resize(0);
  47. downloaded.set(0);
  48. redirections = 0;
  49. String url_lower = url.to_lower();
  50. if (url_lower.begins_with("http://")) {
  51. url = url.substr(7, url.length() - 7);
  52. } else if (url_lower.begins_with("https://")) {
  53. url = url.substr(8, url.length() - 8);
  54. use_ssl = true;
  55. port = 443;
  56. } else {
  57. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Malformed URL: " + url + ".");
  58. }
  59. ERR_FAIL_COND_V_MSG(url.length() < 1, ERR_INVALID_PARAMETER, "URL too short: " + url + ".");
  60. int slash_pos = url.find("/");
  61. if (slash_pos != -1) {
  62. request_string = url.substr(slash_pos, url.length());
  63. url = url.substr(0, slash_pos);
  64. } else {
  65. request_string = "/";
  66. }
  67. int colon_pos = url.find(":");
  68. if (colon_pos != -1) {
  69. port = url.substr(colon_pos + 1, url.length()).to_int();
  70. url = url.substr(0, colon_pos);
  71. ERR_FAIL_COND_V(port < 1 || port > 65535, ERR_INVALID_PARAMETER);
  72. }
  73. return OK;
  74. }
  75. bool HTTPRequest::has_header(const PackedStringArray &p_headers, const String &p_header_name) {
  76. bool exists = false;
  77. String lower_case_header_name = p_header_name.to_lower();
  78. for (int i = 0; i < p_headers.size() && !exists; i++) {
  79. String sanitized = p_headers[i].strip_edges().to_lower();
  80. if (sanitized.begins_with(lower_case_header_name)) {
  81. exists = true;
  82. }
  83. }
  84. return exists;
  85. }
  86. String HTTPRequest::get_header_value(const PackedStringArray &p_headers, const String &p_header_name) {
  87. String value = "";
  88. String lowwer_case_header_name = p_header_name.to_lower();
  89. for (int i = 0; i < p_headers.size(); i++) {
  90. if (p_headers[i].find(":", 0) >= 0) {
  91. Vector<String> parts = p_headers[i].split(":", false, 1);
  92. if (parts[0].strip_edges().to_lower() == lowwer_case_header_name) {
  93. value = parts[1].strip_edges();
  94. break;
  95. }
  96. }
  97. }
  98. return value;
  99. }
  100. Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_headers, bool p_ssl_validate_domain, HTTPClient::Method p_method, const String &p_request_data) {
  101. // Copy the string into a raw buffer
  102. Vector<uint8_t> raw_data;
  103. CharString charstr = p_request_data.utf8();
  104. size_t len = charstr.length();
  105. raw_data.resize(len);
  106. uint8_t *w = raw_data.ptrw();
  107. copymem(w, charstr.ptr(), len);
  108. return request_raw(p_url, p_custom_headers, p_ssl_validate_domain, p_method, raw_data);
  109. }
  110. Error HTTPRequest::request_raw(const String &p_url, const Vector<String> &p_custom_headers, bool p_ssl_validate_domain, HTTPClient::Method p_method, const Vector<uint8_t> &p_request_data_raw) {
  111. ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
  112. ERR_FAIL_COND_V_MSG(requesting, ERR_BUSY, "HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
  113. if (timeout > 0) {
  114. timer->stop();
  115. timer->start(timeout);
  116. }
  117. method = p_method;
  118. Error err = _parse_url(p_url);
  119. if (err) {
  120. return err;
  121. }
  122. validate_ssl = p_ssl_validate_domain;
  123. headers = p_custom_headers;
  124. if (accept_gzip) {
  125. // If the user has specified a different Accept-Encoding, don't overwrite it
  126. if (!has_header(headers, "Accept-Encoding")) {
  127. headers.push_back("Accept-Encoding: gzip, deflate");
  128. }
  129. }
  130. request_data = p_request_data_raw;
  131. requesting = true;
  132. if (use_threads.is_set()) {
  133. thread_done.clear();
  134. thread_request_quit.clear();
  135. client->set_blocking_mode(true);
  136. thread.start(_thread_func, this);
  137. } else {
  138. client->set_blocking_mode(false);
  139. err = _request();
  140. if (err != OK) {
  141. call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
  142. return ERR_CANT_CONNECT;
  143. }
  144. set_process_internal(true);
  145. }
  146. return OK;
  147. }
  148. void HTTPRequest::_thread_func(void *p_userdata) {
  149. HTTPRequest *hr = (HTTPRequest *)p_userdata;
  150. Error err = hr->_request();
  151. if (err != OK) {
  152. hr->call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
  153. } else {
  154. while (!hr->thread_request_quit.is_set()) {
  155. bool exit = hr->_update_connection();
  156. if (exit) {
  157. break;
  158. }
  159. OS::get_singleton()->delay_usec(1);
  160. }
  161. }
  162. hr->thread_done.set();
  163. }
  164. void HTTPRequest::cancel_request() {
  165. timer->stop();
  166. if (!requesting) {
  167. return;
  168. }
  169. if (!use_threads.is_set()) {
  170. set_process_internal(false);
  171. } else {
  172. thread_request_quit.set();
  173. thread.wait_to_finish();
  174. }
  175. if (file) {
  176. memdelete(file);
  177. file = nullptr;
  178. }
  179. client->close();
  180. body.resize(0);
  181. got_response = false;
  182. response_code = -1;
  183. request_sent = false;
  184. requesting = false;
  185. }
  186. bool HTTPRequest::_handle_response(bool *ret_value) {
  187. if (!client->has_response()) {
  188. call_deferred("_request_done", RESULT_NO_RESPONSE, 0, PackedStringArray(), PackedByteArray());
  189. *ret_value = true;
  190. return true;
  191. }
  192. got_response = true;
  193. response_code = client->get_response_code();
  194. List<String> rheaders;
  195. client->get_response_headers(&rheaders);
  196. response_headers.resize(0);
  197. downloaded.set(0);
  198. for (List<String>::Element *E = rheaders.front(); E; E = E->next()) {
  199. response_headers.push_back(E->get());
  200. }
  201. if (response_code == 301 || response_code == 302) {
  202. // Handle redirect
  203. if (max_redirects >= 0 && redirections >= max_redirects) {
  204. call_deferred("_request_done", RESULT_REDIRECT_LIMIT_REACHED, response_code, response_headers, PackedByteArray());
  205. *ret_value = true;
  206. return true;
  207. }
  208. String new_request;
  209. for (List<String>::Element *E = rheaders.front(); E; E = E->next()) {
  210. if (E->get().findn("Location: ") != -1) {
  211. new_request = E->get().substr(9, E->get().length()).strip_edges();
  212. }
  213. }
  214. if (new_request != "") {
  215. // Process redirect
  216. client->close();
  217. int new_redirs = redirections + 1; // Because _request() will clear it
  218. Error err;
  219. if (new_request.begins_with("http")) {
  220. // New url, request all again
  221. _parse_url(new_request);
  222. } else {
  223. request_string = new_request;
  224. }
  225. err = _request();
  226. if (err == OK) {
  227. request_sent = false;
  228. got_response = false;
  229. body_len = -1;
  230. body.resize(0);
  231. downloaded.set(0);
  232. redirections = new_redirs;
  233. *ret_value = false;
  234. return true;
  235. }
  236. }
  237. }
  238. return false;
  239. }
  240. bool HTTPRequest::_update_connection() {
  241. switch (client->get_status()) {
  242. case HTTPClient::STATUS_DISCONNECTED: {
  243. call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
  244. return true; // End it, since it's doing something
  245. } break;
  246. case HTTPClient::STATUS_RESOLVING: {
  247. client->poll();
  248. // Must wait
  249. return false;
  250. } break;
  251. case HTTPClient::STATUS_CANT_RESOLVE: {
  252. call_deferred("_request_done", RESULT_CANT_RESOLVE, 0, PackedStringArray(), PackedByteArray());
  253. return true;
  254. } break;
  255. case HTTPClient::STATUS_CONNECTING: {
  256. client->poll();
  257. // Must wait
  258. return false;
  259. } break; // Connecting to IP
  260. case HTTPClient::STATUS_CANT_CONNECT: {
  261. call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
  262. return true;
  263. } break;
  264. case HTTPClient::STATUS_CONNECTED: {
  265. if (request_sent) {
  266. if (!got_response) {
  267. // No body
  268. bool ret_value;
  269. if (_handle_response(&ret_value)) {
  270. return ret_value;
  271. }
  272. call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, PackedByteArray());
  273. return true;
  274. }
  275. if (body_len < 0) {
  276. // Chunked transfer is done
  277. call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
  278. return true;
  279. }
  280. call_deferred("_request_done", RESULT_CHUNKED_BODY_SIZE_MISMATCH, response_code, response_headers, PackedByteArray());
  281. return true;
  282. // Request might have been done
  283. } else {
  284. // Did not request yet, do request
  285. Error err = client->request_raw(method, request_string, headers, request_data);
  286. if (err != OK) {
  287. call_deferred("_request_done", RESULT_CONNECTION_ERROR, 0, PackedStringArray(), PackedByteArray());
  288. return true;
  289. }
  290. request_sent = true;
  291. return false;
  292. }
  293. } break; // Connected: break requests only accepted here
  294. case HTTPClient::STATUS_REQUESTING: {
  295. // Must wait, still requesting
  296. client->poll();
  297. return false;
  298. } break; // Request in progress
  299. case HTTPClient::STATUS_BODY: {
  300. if (!got_response) {
  301. bool ret_value;
  302. if (_handle_response(&ret_value)) {
  303. return ret_value;
  304. }
  305. if (!client->is_response_chunked() && client->get_response_body_length() == 0) {
  306. call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, PackedByteArray());
  307. return true;
  308. }
  309. // No body len (-1) if chunked or no content-length header was provided.
  310. // Change your webserver configuration if you want body len.
  311. body_len = client->get_response_body_length();
  312. if (body_size_limit >= 0 && body_len > body_size_limit) {
  313. call_deferred("_request_done", RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PackedByteArray());
  314. return true;
  315. }
  316. if (download_to_file != String()) {
  317. file = FileAccess::open(download_to_file, FileAccess::WRITE);
  318. if (!file) {
  319. call_deferred("_request_done", RESULT_DOWNLOAD_FILE_CANT_OPEN, response_code, response_headers, PackedByteArray());
  320. return true;
  321. }
  322. }
  323. }
  324. client->poll();
  325. if (client->get_status() != HTTPClient::STATUS_BODY) {
  326. return false;
  327. }
  328. PackedByteArray chunk = client->read_response_body_chunk();
  329. downloaded.add(chunk.size());
  330. if (file) {
  331. const uint8_t *r = chunk.ptr();
  332. file->store_buffer(r, chunk.size());
  333. if (file->get_error() != OK) {
  334. call_deferred("_request_done", RESULT_DOWNLOAD_FILE_WRITE_ERROR, response_code, response_headers, PackedByteArray());
  335. return true;
  336. }
  337. } else {
  338. body.append_array(chunk);
  339. }
  340. if (body_size_limit >= 0 && downloaded.get() > body_size_limit) {
  341. call_deferred("_request_done", RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PackedByteArray());
  342. return true;
  343. }
  344. if (body_len >= 0) {
  345. if (downloaded.get() == body_len) {
  346. call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
  347. return true;
  348. }
  349. } else if (client->get_status() == HTTPClient::STATUS_DISCONNECTED) {
  350. // We read till EOF, with no errors. Request is done.
  351. call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
  352. return true;
  353. }
  354. return false;
  355. } break; // Request resulted in body: break which must be read
  356. case HTTPClient::STATUS_CONNECTION_ERROR: {
  357. call_deferred("_request_done", RESULT_CONNECTION_ERROR, 0, PackedStringArray(), PackedByteArray());
  358. return true;
  359. } break;
  360. case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR: {
  361. call_deferred("_request_done", RESULT_SSL_HANDSHAKE_ERROR, 0, PackedStringArray(), PackedByteArray());
  362. return true;
  363. } break;
  364. }
  365. ERR_FAIL_V(false);
  366. }
  367. void HTTPRequest::_request_done(int p_status, int p_code, const PackedStringArray &p_headers, const PackedByteArray &p_data) {
  368. cancel_request();
  369. // Determine if the request body is compressed
  370. bool is_compressed;
  371. String content_encoding = get_header_value(p_headers, "Content-Encoding").to_lower();
  372. Compression::Mode mode;
  373. if (content_encoding == "gzip") {
  374. mode = Compression::Mode::MODE_GZIP;
  375. is_compressed = true;
  376. } else if (content_encoding == "deflate") {
  377. mode = Compression::Mode::MODE_DEFLATE;
  378. is_compressed = true;
  379. } else {
  380. is_compressed = false;
  381. }
  382. const PackedByteArray *data = NULL;
  383. if (accept_gzip && is_compressed && p_data.size() > 0) {
  384. // Decompress request body
  385. PackedByteArray *decompressed = memnew(PackedByteArray);
  386. int result = Compression::decompress_dynamic(decompressed, body_size_limit, p_data.ptr(), p_data.size(), mode);
  387. if (result == OK) {
  388. data = decompressed;
  389. } else if (result == -5) {
  390. WARN_PRINT("Decompressed size of HTTP response body exceeded body_size_limit");
  391. p_status = RESULT_BODY_SIZE_LIMIT_EXCEEDED;
  392. // Just return the raw data if we failed to decompress it
  393. data = &p_data;
  394. } else {
  395. WARN_PRINT("Failed to decompress HTTP response body");
  396. p_status = RESULT_BODY_DECOMPRESS_FAILED;
  397. // Just return the raw data if we failed to decompress it
  398. data = &p_data;
  399. }
  400. } else {
  401. data = &p_data;
  402. }
  403. emit_signal("request_completed", p_status, p_code, p_headers, *data);
  404. }
  405. void HTTPRequest::_notification(int p_what) {
  406. if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
  407. if (use_threads.is_set()) {
  408. return;
  409. }
  410. bool done = _update_connection();
  411. if (done) {
  412. set_process_internal(false);
  413. // cancel_request(); called from _request done now
  414. }
  415. }
  416. if (p_what == NOTIFICATION_EXIT_TREE) {
  417. if (requesting) {
  418. cancel_request();
  419. }
  420. }
  421. }
  422. void HTTPRequest::set_use_threads(bool p_use) {
  423. ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
  424. use_threads.set_to(p_use);
  425. }
  426. bool HTTPRequest::is_using_threads() const {
  427. return use_threads.is_set();
  428. }
  429. void HTTPRequest::set_accept_gzip(bool p_gzip) {
  430. accept_gzip = p_gzip;
  431. }
  432. bool HTTPRequest::is_accepting_gzip() const {
  433. return accept_gzip;
  434. }
  435. void HTTPRequest::set_body_size_limit(int p_bytes) {
  436. ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
  437. body_size_limit = p_bytes;
  438. }
  439. int HTTPRequest::get_body_size_limit() const {
  440. return body_size_limit;
  441. }
  442. void HTTPRequest::set_download_file(const String &p_file) {
  443. ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
  444. download_to_file = p_file;
  445. }
  446. String HTTPRequest::get_download_file() const {
  447. return download_to_file;
  448. }
  449. void HTTPRequest::set_download_chunk_size(int p_chunk_size) {
  450. ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
  451. client->set_read_chunk_size(p_chunk_size);
  452. }
  453. int HTTPRequest::get_download_chunk_size() const {
  454. return client->get_read_chunk_size();
  455. }
  456. HTTPClient::Status HTTPRequest::get_http_client_status() const {
  457. return client->get_status();
  458. }
  459. void HTTPRequest::set_max_redirects(int p_max) {
  460. max_redirects = p_max;
  461. }
  462. int HTTPRequest::get_max_redirects() const {
  463. return max_redirects;
  464. }
  465. int HTTPRequest::get_downloaded_bytes() const {
  466. return downloaded.get();
  467. }
  468. int HTTPRequest::get_body_size() const {
  469. return body_len;
  470. }
  471. void HTTPRequest::set_timeout(int p_timeout) {
  472. ERR_FAIL_COND(p_timeout < 0);
  473. timeout = p_timeout;
  474. }
  475. int HTTPRequest::get_timeout() {
  476. return timeout;
  477. }
  478. void HTTPRequest::_timeout() {
  479. cancel_request();
  480. call_deferred("_request_done", RESULT_TIMEOUT, 0, PackedStringArray(), PackedByteArray());
  481. }
  482. void HTTPRequest::_bind_methods() {
  483. ClassDB::bind_method(D_METHOD("request", "url", "custom_headers", "ssl_validate_domain", "method", "request_data"), &HTTPRequest::request, DEFVAL(PackedStringArray()), DEFVAL(true), DEFVAL(HTTPClient::METHOD_GET), DEFVAL(String()));
  484. ClassDB::bind_method(D_METHOD("request_raw", "url", "custom_headers", "ssl_validate_domain", "method", "request_data_raw"), &HTTPRequest::request_raw, DEFVAL(PackedStringArray()), DEFVAL(true), DEFVAL(HTTPClient::METHOD_GET), DEFVAL(PackedByteArray()));
  485. ClassDB::bind_method(D_METHOD("cancel_request"), &HTTPRequest::cancel_request);
  486. ClassDB::bind_method(D_METHOD("get_http_client_status"), &HTTPRequest::get_http_client_status);
  487. ClassDB::bind_method(D_METHOD("set_use_threads", "enable"), &HTTPRequest::set_use_threads);
  488. ClassDB::bind_method(D_METHOD("is_using_threads"), &HTTPRequest::is_using_threads);
  489. ClassDB::bind_method(D_METHOD("set_accept_gzip", "enable"), &HTTPRequest::set_accept_gzip);
  490. ClassDB::bind_method(D_METHOD("is_accepting_gzip"), &HTTPRequest::is_accepting_gzip);
  491. ClassDB::bind_method(D_METHOD("set_body_size_limit", "bytes"), &HTTPRequest::set_body_size_limit);
  492. ClassDB::bind_method(D_METHOD("get_body_size_limit"), &HTTPRequest::get_body_size_limit);
  493. ClassDB::bind_method(D_METHOD("set_max_redirects", "amount"), &HTTPRequest::set_max_redirects);
  494. ClassDB::bind_method(D_METHOD("get_max_redirects"), &HTTPRequest::get_max_redirects);
  495. ClassDB::bind_method(D_METHOD("set_download_file", "path"), &HTTPRequest::set_download_file);
  496. ClassDB::bind_method(D_METHOD("get_download_file"), &HTTPRequest::get_download_file);
  497. ClassDB::bind_method(D_METHOD("get_downloaded_bytes"), &HTTPRequest::get_downloaded_bytes);
  498. ClassDB::bind_method(D_METHOD("get_body_size"), &HTTPRequest::get_body_size);
  499. ClassDB::bind_method(D_METHOD("_redirect_request"), &HTTPRequest::_redirect_request);
  500. ClassDB::bind_method(D_METHOD("_request_done"), &HTTPRequest::_request_done);
  501. ClassDB::bind_method(D_METHOD("set_timeout", "timeout"), &HTTPRequest::set_timeout);
  502. ClassDB::bind_method(D_METHOD("get_timeout"), &HTTPRequest::get_timeout);
  503. ClassDB::bind_method(D_METHOD("set_download_chunk_size"), &HTTPRequest::set_download_chunk_size);
  504. ClassDB::bind_method(D_METHOD("get_download_chunk_size"), &HTTPRequest::get_download_chunk_size);
  505. ADD_PROPERTY(PropertyInfo(Variant::STRING, "download_file", PROPERTY_HINT_FILE), "set_download_file", "get_download_file");
  506. ADD_PROPERTY(PropertyInfo(Variant::INT, "download_chunk_size", PROPERTY_HINT_RANGE, "256,16777216"), "set_download_chunk_size", "get_download_chunk_size");
  507. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_threads"), "set_use_threads", "is_using_threads");
  508. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "accept_gzip"), "set_accept_gzip", "is_accepting_gzip");
  509. ADD_PROPERTY(PropertyInfo(Variant::INT, "body_size_limit", PROPERTY_HINT_RANGE, "-1,2000000000"), "set_body_size_limit", "get_body_size_limit");
  510. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_redirects", PROPERTY_HINT_RANGE, "-1,64"), "set_max_redirects", "get_max_redirects");
  511. ADD_PROPERTY(PropertyInfo(Variant::INT, "timeout", PROPERTY_HINT_RANGE, "0,86400"), "set_timeout", "get_timeout");
  512. ADD_SIGNAL(MethodInfo("request_completed", PropertyInfo(Variant::INT, "result"), PropertyInfo(Variant::INT, "response_code"), PropertyInfo(Variant::PACKED_STRING_ARRAY, "headers"), PropertyInfo(Variant::PACKED_BYTE_ARRAY, "body")));
  513. BIND_ENUM_CONSTANT(RESULT_SUCCESS);
  514. //BIND_ENUM_CONSTANT( RESULT_NO_BODY );
  515. BIND_ENUM_CONSTANT(RESULT_CHUNKED_BODY_SIZE_MISMATCH);
  516. BIND_ENUM_CONSTANT(RESULT_CANT_CONNECT);
  517. BIND_ENUM_CONSTANT(RESULT_CANT_RESOLVE);
  518. BIND_ENUM_CONSTANT(RESULT_CONNECTION_ERROR);
  519. BIND_ENUM_CONSTANT(RESULT_SSL_HANDSHAKE_ERROR);
  520. BIND_ENUM_CONSTANT(RESULT_NO_RESPONSE);
  521. BIND_ENUM_CONSTANT(RESULT_BODY_SIZE_LIMIT_EXCEEDED);
  522. BIND_ENUM_CONSTANT(RESULT_BODY_DECOMPRESS_FAILED);
  523. BIND_ENUM_CONSTANT(RESULT_REQUEST_FAILED);
  524. BIND_ENUM_CONSTANT(RESULT_DOWNLOAD_FILE_CANT_OPEN);
  525. BIND_ENUM_CONSTANT(RESULT_DOWNLOAD_FILE_WRITE_ERROR);
  526. BIND_ENUM_CONSTANT(RESULT_REDIRECT_LIMIT_REACHED);
  527. BIND_ENUM_CONSTANT(RESULT_TIMEOUT);
  528. }
  529. HTTPRequest::HTTPRequest() {
  530. client.instance();
  531. timer = memnew(Timer);
  532. timer->set_one_shot(true);
  533. timer->connect("timeout", callable_mp(this, &HTTPRequest::_timeout));
  534. add_child(timer);
  535. }
  536. HTTPRequest::~HTTPRequest() {
  537. if (file) {
  538. memdelete(file);
  539. }
  540. }