http_request.cpp 21 KB

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