http_request.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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/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.empty()) {
  60. request_string = "/";
  61. }
  62. return OK;
  63. }
  64. bool HTTPRequest::has_header(const Vector<String> &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 PoolStringArray &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. PoolVector<uint8_t> raw_data;
  92. CharString charstr = p_request_data.utf8();
  93. size_t len = charstr.length();
  94. raw_data.resize(len);
  95. memcpy(raw_data.write().ptr(), charstr.ptr(), len);
  96. return request_raw(p_url, p_custom_headers, p_ssl_validate_domain, p_method, raw_data);
  97. }
  98. Error HTTPRequest::request_raw(const String &p_url, const Vector<String> &p_custom_headers, bool p_ssl_validate_domain, HTTPClient::Method p_method, const PoolVector<uint8_t> &p_request_data_raw) {
  99. ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
  100. ERR_FAIL_COND_V_MSG(requesting, ERR_BUSY, "HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
  101. if (timeout > 0) {
  102. timer->stop();
  103. timer->start(timeout);
  104. }
  105. method = p_method;
  106. Error err = _parse_url(p_url);
  107. if (err) {
  108. return err;
  109. }
  110. validate_ssl = p_ssl_validate_domain;
  111. headers = p_custom_headers;
  112. if (accept_gzip) {
  113. // If the user has specified a different Accept-Encoding, don't overwrite it
  114. if (!has_header(headers, "Accept-Encoding")) {
  115. headers.push_back("Accept-Encoding: gzip, deflate");
  116. }
  117. }
  118. request_data = p_request_data_raw;
  119. requesting = true;
  120. if (use_threads.is_set()) {
  121. thread_done.clear();
  122. thread_request_quit.clear();
  123. client->set_blocking_mode(true);
  124. thread.start(_thread_func, this);
  125. } else {
  126. client->set_blocking_mode(false);
  127. err = _request();
  128. if (err != OK) {
  129. call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
  130. return ERR_CANT_CONNECT;
  131. }
  132. set_process_internal(true);
  133. }
  134. return OK;
  135. }
  136. void HTTPRequest::_thread_func(void *p_userdata) {
  137. HTTPRequest *hr = (HTTPRequest *)p_userdata;
  138. Error err = hr->_request();
  139. if (err != OK) {
  140. hr->call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
  141. } else {
  142. while (!hr->thread_request_quit.is_set()) {
  143. bool exit = hr->_update_connection();
  144. if (exit) {
  145. break;
  146. }
  147. OS::get_singleton()->delay_usec(1);
  148. }
  149. }
  150. hr->thread_done.set();
  151. }
  152. void HTTPRequest::cancel_request() {
  153. timer->stop();
  154. if (!requesting) {
  155. return;
  156. }
  157. if (!use_threads.is_set()) {
  158. set_process_internal(false);
  159. } else {
  160. thread_request_quit.set();
  161. thread.wait_to_finish();
  162. }
  163. if (file) {
  164. memdelete(file);
  165. file = nullptr;
  166. }
  167. client->close();
  168. body.resize(0);
  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. call_deferred("_request_done", RESULT_NO_RESPONSE, 0, PoolStringArray(), PoolByteArray());
  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.resize(0);
  185. downloaded.set(0);
  186. for (List<String>::Element *E = rheaders.front(); E; E = E->next()) {
  187. response_headers.push_back(E->get());
  188. }
  189. if (response_code == 301 || response_code == 302) {
  190. // Handle redirect
  191. if (max_redirects >= 0 && redirections >= max_redirects) {
  192. call_deferred("_request_done", RESULT_REDIRECT_LIMIT_REACHED, response_code, response_headers, PoolByteArray());
  193. *ret_value = true;
  194. return true;
  195. }
  196. String new_request;
  197. for (List<String>::Element *E = rheaders.front(); E; E = E->next()) {
  198. if (E->get().findn("Location: ") != -1) {
  199. new_request = E->get().substr(9, E->get().length()).strip_edges();
  200. }
  201. }
  202. if (new_request != "") {
  203. // Process redirect
  204. client->close();
  205. int new_redirs = redirections + 1; // Because _request() will clear it
  206. Error err;
  207. if (new_request.begins_with("http")) {
  208. // New url, request all again
  209. _parse_url(new_request);
  210. } else {
  211. request_string = new_request;
  212. }
  213. err = _request();
  214. if (err == OK) {
  215. request_sent = false;
  216. got_response = false;
  217. body_len = -1;
  218. body.resize(0);
  219. downloaded.set(0);
  220. redirections = new_redirs;
  221. *ret_value = false;
  222. return true;
  223. }
  224. }
  225. }
  226. return false;
  227. }
  228. bool HTTPRequest::_update_connection() {
  229. switch (client->get_status()) {
  230. case HTTPClient::STATUS_DISCONNECTED: {
  231. call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
  232. return true; // End it, since it's doing something
  233. } break;
  234. case HTTPClient::STATUS_RESOLVING: {
  235. client->poll();
  236. // Must wait
  237. return false;
  238. } break;
  239. case HTTPClient::STATUS_CANT_RESOLVE: {
  240. call_deferred("_request_done", RESULT_CANT_RESOLVE, 0, PoolStringArray(), PoolByteArray());
  241. return true;
  242. } break;
  243. case HTTPClient::STATUS_CONNECTING: {
  244. client->poll();
  245. // Must wait
  246. return false;
  247. } break; // Connecting to IP
  248. case HTTPClient::STATUS_CANT_CONNECT: {
  249. call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
  250. return true;
  251. } break;
  252. case HTTPClient::STATUS_CONNECTED: {
  253. if (request_sent) {
  254. if (!got_response) {
  255. // No body
  256. bool ret_value;
  257. if (_handle_response(&ret_value)) {
  258. return ret_value;
  259. }
  260. call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, PoolByteArray());
  261. return true;
  262. }
  263. if (body_len < 0) {
  264. // Chunked transfer is done
  265. call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
  266. return true;
  267. }
  268. call_deferred("_request_done", RESULT_CHUNKED_BODY_SIZE_MISMATCH, response_code, response_headers, PoolByteArray());
  269. return true;
  270. // Request migh have been done
  271. } else {
  272. // Did not request yet, do request
  273. Error err = client->request_raw(method, request_string, headers, request_data);
  274. if (err != OK) {
  275. call_deferred("_request_done", RESULT_CONNECTION_ERROR, 0, PoolStringArray(), PoolByteArray());
  276. return true;
  277. }
  278. request_sent = true;
  279. return false;
  280. }
  281. } break; // Connected: break requests only accepted here
  282. case HTTPClient::STATUS_REQUESTING: {
  283. // Must wait, still requesting
  284. client->poll();
  285. return false;
  286. } break; // Request in progress
  287. case HTTPClient::STATUS_BODY: {
  288. if (!got_response) {
  289. bool ret_value;
  290. if (_handle_response(&ret_value)) {
  291. return ret_value;
  292. }
  293. if (!client->is_response_chunked() && client->get_response_body_length() == 0) {
  294. call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, PoolByteArray());
  295. return true;
  296. }
  297. // No body len (-1) if chunked or no content-length header was provided.
  298. // Change your webserver configuration if you want body len.
  299. body_len = client->get_response_body_length();
  300. if (body_size_limit >= 0 && body_len > body_size_limit) {
  301. call_deferred("_request_done", RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PoolByteArray());
  302. return true;
  303. }
  304. if (download_to_file != String()) {
  305. file = FileAccess::open(download_to_file, FileAccess::WRITE);
  306. if (!file) {
  307. call_deferred("_request_done", RESULT_DOWNLOAD_FILE_CANT_OPEN, response_code, response_headers, PoolByteArray());
  308. return true;
  309. }
  310. }
  311. }
  312. client->poll();
  313. if (client->get_status() != HTTPClient::STATUS_BODY) {
  314. return false;
  315. }
  316. PoolByteArray chunk = client->read_response_body_chunk();
  317. downloaded.add(chunk.size());
  318. if (file) {
  319. PoolByteArray::Read r = chunk.read();
  320. file->store_buffer(r.ptr(), chunk.size());
  321. if (file->get_error() != OK) {
  322. call_deferred("_request_done", RESULT_DOWNLOAD_FILE_WRITE_ERROR, response_code, response_headers, PoolByteArray());
  323. return true;
  324. }
  325. } else {
  326. body.append_array(chunk);
  327. }
  328. if (body_size_limit >= 0 && downloaded.get() > body_size_limit) {
  329. call_deferred("_request_done", RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PoolByteArray());
  330. return true;
  331. }
  332. if (body_len >= 0) {
  333. if (downloaded.get() == body_len) {
  334. call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
  335. return true;
  336. }
  337. } else if (client->get_status() == HTTPClient::STATUS_DISCONNECTED) {
  338. // We read till EOF, with no errors. Request is done.
  339. call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
  340. return true;
  341. }
  342. return false;
  343. } break; // Request resulted in body: break which must be read
  344. case HTTPClient::STATUS_CONNECTION_ERROR: {
  345. call_deferred("_request_done", RESULT_CONNECTION_ERROR, 0, PoolStringArray(), PoolByteArray());
  346. return true;
  347. } break;
  348. case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR: {
  349. call_deferred("_request_done", RESULT_SSL_HANDSHAKE_ERROR, 0, PoolStringArray(), PoolByteArray());
  350. return true;
  351. } break;
  352. }
  353. ERR_FAIL_V(false);
  354. }
  355. void HTTPRequest::_request_done(int p_status, int p_code, const PoolStringArray &p_headers, const PoolByteArray &p_data) {
  356. cancel_request();
  357. // Determine if the request body is compressed
  358. bool is_compressed;
  359. String content_encoding = get_header_value(p_headers, "Content-Encoding").to_lower();
  360. Compression::Mode mode;
  361. if (content_encoding == "gzip") {
  362. mode = Compression::Mode::MODE_GZIP;
  363. is_compressed = true;
  364. } else if (content_encoding == "deflate") {
  365. mode = Compression::Mode::MODE_DEFLATE;
  366. is_compressed = true;
  367. } else {
  368. is_compressed = false;
  369. }
  370. const PoolByteArray *data = NULL;
  371. if (accept_gzip && is_compressed && p_data.size() > 0) {
  372. // Decompress request body
  373. PoolByteArray *decompressed = memnew(PoolByteArray);
  374. int result = Compression::decompress_dynamic(decompressed, body_size_limit, p_data.read().ptr(), p_data.size(), mode);
  375. if (result == OK) {
  376. data = decompressed;
  377. } else if (result == -5) {
  378. WARN_PRINT("Decompressed size of HTTP response body exceeded body_size_limit");
  379. p_status = RESULT_BODY_SIZE_LIMIT_EXCEEDED;
  380. // Just return the raw data if we failed to decompress it
  381. data = &p_data;
  382. } else {
  383. WARN_PRINT("Failed to decompress HTTP response body");
  384. p_status = RESULT_BODY_DECOMPRESS_FAILED;
  385. // Just return the raw data if we failed to decompress it
  386. data = &p_data;
  387. }
  388. } else {
  389. data = &p_data;
  390. }
  391. emit_signal("request_completed", p_status, p_code, p_headers, *data);
  392. }
  393. void HTTPRequest::_notification(int p_what) {
  394. if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
  395. if (use_threads.is_set()) {
  396. return;
  397. }
  398. bool done = _update_connection();
  399. if (done) {
  400. set_process_internal(false);
  401. // cancel_request(); called from _request done now
  402. }
  403. }
  404. if (p_what == NOTIFICATION_EXIT_TREE) {
  405. if (requesting) {
  406. cancel_request();
  407. }
  408. }
  409. }
  410. void HTTPRequest::set_accept_gzip(bool p_gzip) {
  411. accept_gzip = p_gzip;
  412. }
  413. bool HTTPRequest::is_accepting_gzip() const {
  414. return accept_gzip;
  415. }
  416. void HTTPRequest::set_use_threads(bool p_use) {
  417. ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
  418. use_threads.set_to(p_use);
  419. }
  420. bool HTTPRequest::is_using_threads() const {
  421. return use_threads.is_set();
  422. }
  423. void HTTPRequest::set_body_size_limit(int p_bytes) {
  424. ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
  425. body_size_limit = p_bytes;
  426. }
  427. int HTTPRequest::get_body_size_limit() const {
  428. return body_size_limit;
  429. }
  430. void HTTPRequest::set_download_file(const String &p_file) {
  431. ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
  432. download_to_file = p_file;
  433. }
  434. String HTTPRequest::get_download_file() const {
  435. return download_to_file;
  436. }
  437. void HTTPRequest::set_download_chunk_size(int p_chunk_size) {
  438. ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
  439. client->set_read_chunk_size(p_chunk_size);
  440. }
  441. int HTTPRequest::get_download_chunk_size() const {
  442. return client->get_read_chunk_size();
  443. }
  444. HTTPClient::Status HTTPRequest::get_http_client_status() const {
  445. return client->get_status();
  446. }
  447. void HTTPRequest::set_max_redirects(int p_max) {
  448. max_redirects = p_max;
  449. }
  450. int HTTPRequest::get_max_redirects() const {
  451. return max_redirects;
  452. }
  453. int HTTPRequest::get_downloaded_bytes() const {
  454. return downloaded.get();
  455. }
  456. int HTTPRequest::get_body_size() const {
  457. return body_len;
  458. }
  459. void HTTPRequest::set_timeout(int p_timeout) {
  460. ERR_FAIL_COND(p_timeout < 0);
  461. timeout = p_timeout;
  462. }
  463. int HTTPRequest::get_timeout() {
  464. return timeout;
  465. }
  466. void HTTPRequest::_timeout() {
  467. cancel_request();
  468. call_deferred("_request_done", RESULT_TIMEOUT, 0, PoolStringArray(), PoolByteArray());
  469. }
  470. void HTTPRequest::_bind_methods() {
  471. ClassDB::bind_method(D_METHOD("request", "url", "custom_headers", "ssl_validate_domain", "method", "request_data"), &HTTPRequest::request, DEFVAL(PoolStringArray()), DEFVAL(true), DEFVAL(HTTPClient::METHOD_GET), DEFVAL(String()));
  472. ClassDB::bind_method(D_METHOD("request_raw", "url", "custom_headers", "ssl_validate_domain", "method", "request_data_raw"), &HTTPRequest::request_raw, DEFVAL(PoolStringArray()), DEFVAL(true), DEFVAL(HTTPClient::METHOD_GET), DEFVAL(PoolVector<uint8_t>()));
  473. ClassDB::bind_method(D_METHOD("cancel_request"), &HTTPRequest::cancel_request);
  474. ClassDB::bind_method(D_METHOD("get_http_client_status"), &HTTPRequest::get_http_client_status);
  475. ClassDB::bind_method(D_METHOD("set_use_threads", "enable"), &HTTPRequest::set_use_threads);
  476. ClassDB::bind_method(D_METHOD("is_using_threads"), &HTTPRequest::is_using_threads);
  477. ClassDB::bind_method(D_METHOD("set_accept_gzip", "enable"), &HTTPRequest::set_accept_gzip);
  478. ClassDB::bind_method(D_METHOD("is_accepting_gzip"), &HTTPRequest::is_accepting_gzip);
  479. ClassDB::bind_method(D_METHOD("set_body_size_limit", "bytes"), &HTTPRequest::set_body_size_limit);
  480. ClassDB::bind_method(D_METHOD("get_body_size_limit"), &HTTPRequest::get_body_size_limit);
  481. ClassDB::bind_method(D_METHOD("set_max_redirects", "amount"), &HTTPRequest::set_max_redirects);
  482. ClassDB::bind_method(D_METHOD("get_max_redirects"), &HTTPRequest::get_max_redirects);
  483. ClassDB::bind_method(D_METHOD("set_download_file", "path"), &HTTPRequest::set_download_file);
  484. ClassDB::bind_method(D_METHOD("get_download_file"), &HTTPRequest::get_download_file);
  485. ClassDB::bind_method(D_METHOD("get_downloaded_bytes"), &HTTPRequest::get_downloaded_bytes);
  486. ClassDB::bind_method(D_METHOD("get_body_size"), &HTTPRequest::get_body_size);
  487. ClassDB::bind_method(D_METHOD("_redirect_request"), &HTTPRequest::_redirect_request);
  488. ClassDB::bind_method(D_METHOD("_request_done"), &HTTPRequest::_request_done);
  489. ClassDB::bind_method(D_METHOD("set_timeout", "timeout"), &HTTPRequest::set_timeout);
  490. ClassDB::bind_method(D_METHOD("get_timeout"), &HTTPRequest::get_timeout);
  491. ClassDB::bind_method(D_METHOD("set_download_chunk_size"), &HTTPRequest::set_download_chunk_size);
  492. ClassDB::bind_method(D_METHOD("get_download_chunk_size"), &HTTPRequest::get_download_chunk_size);
  493. ClassDB::bind_method(D_METHOD("_timeout"), &HTTPRequest::_timeout);
  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::POOL_STRING_ARRAY, "headers"), PropertyInfo(Variant::POOL_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_DECOMPRESS_FAILED);
  511. BIND_ENUM_CONSTANT(RESULT_BODY_SIZE_LIMIT_EXCEEDED);
  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. port = 80;
  520. redirections = 0;
  521. max_redirects = 8;
  522. body_len = -1;
  523. got_response = false;
  524. validate_ssl = false;
  525. use_ssl = false;
  526. accept_gzip = true;
  527. response_code = 0;
  528. request_sent = false;
  529. requesting = false;
  530. client.instance();
  531. body_size_limit = -1;
  532. file = nullptr;
  533. timer = memnew(Timer);
  534. timer->set_one_shot(true);
  535. timer->connect("timeout", this, "_timeout");
  536. add_child(timer);
  537. timeout = 0;
  538. }
  539. HTTPRequest::~HTTPRequest() {
  540. if (file) {
  541. memdelete(file);
  542. }
  543. }