http_request.cpp 21 KB

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