http_request.cpp 21 KB

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