http_request.cpp 21 KB

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