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