http_request.cpp 23 KB

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