Debian Codegraph @

scene/main/http_request.cpp

raw symbols SCIP
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
31 #include "http_request.h"
32
33 #include "core/io/file_access.h"
34 #include "scene/main/timer.h"
35
36 Error HTTPRequest::_request() {
37 return client->connect_to_host(url, port, use_tls ? tls_options : nullptr);
38 }
39
40 Error HTTPRequest::_parse_url(const String &p_url) {
41 use_tls = false;
42 request_string = "";
43 port = 80;
44 request_sent = false;
45 got_response = false;
46 body_len = -1;
47 body.clear();
48 downloaded.set(0);
49 final_body_size.set(0);
50 redirections = 0;
51
52 String scheme;
53 String fragment;
54 Error err = p_url.parse_url(scheme, url, port, request_string, fragment);
55 ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Error parsing URL: '%s'.", p_url));
56
57 if (scheme == "https://") {
58 use_tls = true;
59 } else if (scheme != "http://") {
60 ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, vformat("Invalid URL scheme: '%s'.", scheme));
61 }
62
63 if (port == 0) {
64 port = use_tls ? 443 : 80;
65 }
66 if (request_string.is_empty()) {
67 request_string = "/";
68 }
69 return OK;
70 }
71
72 bool HTTPRequest::has_header(const PackedStringArray &p_headers, const String &p_header_name) {
73 bool exists = false;
74
75 String lower_case_header_name = p_header_name.to_lower();
76 for (int i = 0; i < p_headers.size() && !exists; i++) {
77 String sanitized = p_headers[i].strip_edges().to_lower();
78 if (sanitized.begins_with(lower_case_header_name)) {
79 exists = true;
80 }
81 }
82
83 return exists;
84 }
85
86 String HTTPRequest::get_header_value(const PackedStringArray &p_headers, const String &p_header_name) {
87 String value = "";
88
89 String lowwer_case_header_name = p_header_name.to_lower();
90 for (int i = 0; i < p_headers.size(); i++) {
91 if (p_headers[i].find_char(':') > 0) {
92 Vector<String> parts = p_headers[i].split(":", false, 1);
93 if (parts.size() > 1 && parts[0].strip_edges().to_lower() == lowwer_case_header_name) {
94 value = parts[1].strip_edges();
95 break;
96 }
97 }
98 }
99
100 return value;
101 }
102
103 Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_headers, HTTPClient::Method p_method, const String &p_request_data) {
104 // Copy the string into a raw buffer.
105 Vector<uint8_t> raw_data;
106
107 CharString charstr = p_request_data.utf8();
108 size_t len = charstr.length();
109 if (len > 0) {
110 raw_data.resize(len);
111 uint8_t *w = raw_data.ptrw();
112 memcpy(w, charstr.ptr(), len);
113 }
114
115 return request_raw(p_url, p_custom_headers, p_method, raw_data);
116 }
117
118 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) {
119 ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
120 ERR_FAIL_COND_V_MSG(requesting, ERR_BUSY, "HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
121
122 if (timeout > 0) {
123 timer->stop();
124 timer->start(timeout);
125 }
126
127 method = p_method;
128
129 Error err = _parse_url(p_url);
130 if (err) {
131 return err;
132 }
133
134 headers = p_custom_headers;
135
136 if (accept_gzip) {
137 // If the user has specified an Accept-Encoding header, don't overwrite it.
138 if (!has_header(headers, "Accept-Encoding")) {
139 headers.push_back("Accept-Encoding: gzip, deflate");
140 }
141 }
142
143 request_data = p_request_data_raw;
144
145 requesting = true;
146
147 if (use_threads.is_set()) {
148 thread_done.clear();
149 thread_request_quit.clear();
150 client->set_blocking_mode(true);
151 thread.start(_thread_func, this);
152 } else {
153 client->set_blocking_mode(false);
154 err = _request();
155 if (err != OK) {
156 _defer_done(RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
157 return ERR_CANT_CONNECT;
158 }
159
160 set_process_internal(true);
161 }
162
163 return OK;
164 }
165
166 void HTTPRequest::_thread_func(void *p_userdata) {
167 HTTPRequest *hr = static_cast<HTTPRequest *>(p_userdata);
168
169 Error err = hr->_request();
170
171 if (err != OK) {
172 hr->_defer_done(RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
173 } else {
174 while (!hr->thread_request_quit.is_set()) {
175 bool exit = hr->_update_connection();
176 if (exit) {
177 break;
178 }
179 OS::get_singleton()->delay_usec(1);
180 }
181 }
182
183 hr->thread_done.set();
184 }
185
186 void HTTPRequest::cancel_request() {
187 timer->stop();
188
189 if (!requesting) {
190 return;
191 }
192
193 if (!use_threads.is_set()) {
194 set_process_internal(false);
195 } else {
196 thread_request_quit.set();
197 if (thread.is_started()) {
198 thread.wait_to_finish();
199 }
200 }
201
202 file.unref();
203 decompressor.unref();
204 client->close();
205 body.clear();
206 got_response = false;
207 response_code = -1;
208 request_sent = false;
209 requesting = false;
210 }
211
212 bool HTTPRequest::_is_content_header(const String &p_header) const {
213 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:") ||
214 p_header.begins_with("transfer-encoding:") || p_header.begins_with("connection:") || p_header.begins_with("authorization:"));
215 }
216
217 bool HTTPRequest::_is_method_safe() const {
218 return (method == HTTPClient::METHOD_GET || method == HTTPClient::METHOD_HEAD || method == HTTPClient::METHOD_OPTIONS || method == HTTPClient::METHOD_TRACE);
219 }
220
221 Error HTTPRequest::_get_redirect_headers(Vector<String> *r_headers) {
222 for (const String &E : headers) {
223 const String h = E.to_lower();
224 // We strip content headers when changing a redirect to GET.
225 if (!_is_content_header(h)) {
226 r_headers->push_back(E);
227 }
228 }
229 return OK;
230 }
231
232 bool HTTPRequest::_handle_response(bool *ret_value) {
233 if (!client->has_response()) {
234 _defer_done(RESULT_NO_RESPONSE, 0, PackedStringArray(), PackedByteArray());
235 *ret_value = true;
236 return true;
237 }
238
239 got_response = true;
240 response_code = client->get_response_code();
241 List<String> rheaders;
242 client->get_response_headers(&rheaders);
243 response_headers.clear();
244 downloaded.set(0);
245 final_body_size.set(0);
246 decompressor.unref();
247
248 for (const String &E : rheaders) {
249 response_headers.push_back(E);
250 }
251
252 if (response_code == 301 || response_code == 302) {
253 // Handle redirect.
254
255 if (max_redirects >= 0 && redirections >= max_redirects) {
256 _defer_done(RESULT_REDIRECT_LIMIT_REACHED, response_code, response_headers, PackedByteArray());
257 *ret_value = true;
258 return true;
259 }
260
261 String new_request;
262
263 for (const String &E : rheaders) {
264 if (E.to_lower().begins_with("location: ")) {
265 new_request = E.substr(9).strip_edges();
266 }
267 }
268
269 if (!new_request.is_empty()) {
270 // Process redirect.
271 client->close();
272 int new_redirs = redirections + 1; // Because _request() will clear it.
273 Error err;
274 if (new_request.begins_with("http")) {
275 // New url, new request.
276 _parse_url(new_request);
277 } else {
278 request_string = new_request;
279 }
280
281 err = _request();
282 if (err == OK) {
283 request_sent = false;
284 got_response = false;
285 body_len = -1;
286 body.clear();
287 downloaded.set(0);
288 final_body_size.set(0);
289 redirections = new_redirs;
290 *ret_value = false;
291 if (!_is_method_safe()) {
292 // 301, 302, and 303 are changed to GET for unsafe methods.
293 // See: https://www.rfc-editor.org/rfc/rfc9110#section-15.4-3.1
294 method = HTTPClient::METHOD_GET;
295 // Content headers should be dropped if changing method.
296 // See: https://www.rfc-editor.org/rfc/rfc9110#section-15.4-6.2.1
297 Vector<String> req_headers;
298 _get_redirect_headers(&req_headers);
299 headers = req_headers;
300 }
301 return true;
302 }
303 }
304 }
305
306 // Check if we need to start streaming decompression.
307 String content_encoding;
308 if (accept_gzip) {
309 content_encoding = get_header_value(response_headers, "Content-Encoding").to_lower();
310 }
311 if (content_encoding == "gzip") {
312 decompressor.instantiate();
313 decompressor->start_decompression(false, get_download_chunk_size());
314 } else if (content_encoding == "deflate") {
315 decompressor.instantiate();
316 decompressor->start_decompression(true, get_download_chunk_size());
317 }
318
319 return false;
320 }
321
322 bool HTTPRequest::_update_connection() {
323 switch (client->get_status()) {
324 case HTTPClient::STATUS_DISCONNECTED: {
325 _defer_done(RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
326 return true; // End it, since it's disconnected.
327 } break;
328 case HTTPClient::STATUS_RESOLVING: {
329 client->poll();
330 // Must wait.
331 return false;
332 } break;
333 case HTTPClient::STATUS_CANT_RESOLVE: {
334 _defer_done(RESULT_CANT_RESOLVE, 0, PackedStringArray(), PackedByteArray());
335 return true;
336
337 } break;
338 case HTTPClient::STATUS_CONNECTING: {
339 client->poll();
340 // Must wait.
341 return false;
342 } break; // Connecting to IP.
343 case HTTPClient::STATUS_CANT_CONNECT: {
344 _defer_done(RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
345 return true;
346
347 } break;
348 case HTTPClient::STATUS_CONNECTED: {
349 if (request_sent) {
350 if (!got_response) {
351 // No body.
352
353 bool ret_value;
354
355 if (_handle_response(&ret_value)) {
356 return ret_value;
357 }
358
359 _defer_done(RESULT_SUCCESS, response_code, response_headers, PackedByteArray());
360 return true;
361 }
362 if (body_len < 0) {
363 // Chunked transfer is done.
364 _defer_done(RESULT_SUCCESS, response_code, response_headers, body);
365 return true;
366 }
367
368 _defer_done(RESULT_CHUNKED_BODY_SIZE_MISMATCH, response_code, response_headers, PackedByteArray());
369 return true;
370 // Request might have been done.
371 } else {
372 // Did not request yet, do request.
373
374 int size = request_data.size();
375 Error err = client->request(method, request_string, headers, size > 0 ? request_data.ptr() : nullptr, size);
376 if (err != OK) {
377 _defer_done(RESULT_CONNECTION_ERROR, 0, PackedStringArray(), PackedByteArray());
378 return true;
379 }
380
381 request_sent = true;
382 return false;
383 }
384 } break; // Connected: break requests only accepted here.
385 case HTTPClient::STATUS_REQUESTING: {
386 // Must wait, still requesting.
387 client->poll();
388 return false;
389
390 } break; // Request in progress.
391 case HTTPClient::STATUS_BODY: {
392 if (!got_response) {
393 bool ret_value;
394
395 if (_handle_response(&ret_value)) {
396 return ret_value;
397 }
398
399 if (!client->is_response_chunked() && client->get_response_body_length() == 0) {
400 _defer_done(RESULT_SUCCESS, response_code, response_headers, PackedByteArray());
401 return true;
402 }
403
404 // No body len (-1) if chunked or no content-length header was provided.
405 // Change your webserver configuration if you want body len.
406 body_len = client->get_response_body_length();
407
408 if (body_size_limit >= 0 && body_len > body_size_limit) {
409 _defer_done(RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PackedByteArray());
410 return true;
411 }
412
413 if (!download_to_file.is_empty()) {
414 file = FileAccess::open(download_to_file, FileAccess::WRITE);
415 if (file.is_null()) {
416 _defer_done(RESULT_DOWNLOAD_FILE_CANT_OPEN, response_code, response_headers, PackedByteArray());
417 return true;
418 }
419 }
420 }
421
422 client->poll();
423 if (client->get_status() != HTTPClient::STATUS_BODY) {
424 return false;
425 }
426
427 PackedByteArray chunk;
428 if (decompressor.is_null()) {
429 // Chunk can be read directly.
430 chunk = client->read_response_body_chunk();
431 downloaded.add(chunk.size());
432 } else {
433 // Chunk is the result of decompression.
434 PackedByteArray compressed = client->read_response_body_chunk();
435 downloaded.add(compressed.size());
436
437 int pos = 0;
438 int left = compressed.size();
439 while (left) {
440 int w = 0;
441 Error err = decompressor->put_partial_data(compressed.ptr() + pos, left, w);
442 if (err == OK) {
443 PackedByteArray dc;
444 dc.resize(decompressor->get_available_bytes());
445 err = decompressor->get_data(dc.ptrw(), dc.size());
446 chunk.append_array(dc);
447 }
448 if (err != OK) {
449 _defer_done(RESULT_BODY_DECOMPRESS_FAILED, response_code, response_headers, PackedByteArray());
450 return true;
451 }
452 // We need this check here because a "zip bomb" could result in a chunk of few kilos decompressing into gigabytes of data.
453 if (body_size_limit >= 0 && final_body_size.get() + chunk.size() > body_size_limit) {
454 _defer_done(RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PackedByteArray());
455 return true;
456 }
457 pos += w;
458 left -= w;
459 }
460 }
461 final_body_size.add(chunk.size());
462
463 if (body_size_limit >= 0 && final_body_size.get() > body_size_limit) {
464 _defer_done(RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PackedByteArray());
465 return true;
466 }
467
468 if (chunk.size()) {
469 if (file.is_valid()) {
470 const uint8_t *r = chunk.ptr();
471 file->store_buffer(r, chunk.size());
472 if (file->get_error() != OK) {
473 _defer_done(RESULT_DOWNLOAD_FILE_WRITE_ERROR, response_code, response_headers, PackedByteArray());
474 return true;
475 }
476 } else {
477 body.append_array(chunk);
478 }
479 }
480
481 if (body_len >= 0) {
482 if (downloaded.get() == body_len) {
483 _defer_done(RESULT_SUCCESS, response_code, response_headers, body);
484 return true;
485 }
486 } else if (client->get_status() == HTTPClient::STATUS_DISCONNECTED) {
487 // We read till EOF, with no errors. Request is done.
488 _defer_done(RESULT_SUCCESS, response_code, response_headers, body);
489 return true;
490 }
491
492 return false;
493
494 } break; // Request resulted in body: break which must be read.
495 case HTTPClient::STATUS_CONNECTION_ERROR: {
496 _defer_done(RESULT_CONNECTION_ERROR, 0, PackedStringArray(), PackedByteArray());
497 return true;
498 } break;
499 case HTTPClient::STATUS_TLS_HANDSHAKE_ERROR: {
500 _defer_done(RESULT_TLS_HANDSHAKE_ERROR, 0, PackedStringArray(), PackedByteArray());
501 return true;
502 } break;
503 }
504
505 ERR_FAIL_V(false);
506 }
507
508 void HTTPRequest::_defer_done(int p_status, int p_code, const PackedStringArray &p_headers, const PackedByteArray &p_data) {
509 callable_mp(this, &HTTPRequest::_request_done).call_deferred(p_status, p_code, p_headers, p_data);
510 }
511
512 void HTTPRequest::_request_done(int p_status, int p_code, const PackedStringArray &p_headers, const PackedByteArray &p_data) {
513 cancel_request();
514
515 emit_signal(SNAME("request_completed"), p_status, p_code, p_headers, p_data);
516 }
517
518 void HTTPRequest::_notification(int p_what) {
519 switch (p_what) {
520 case NOTIFICATION_INTERNAL_PROCESS: {
521 if (use_threads.is_set()) {
522 return;
523 }
524 bool done = _update_connection();
525 if (done) {
526 set_process_internal(false);
527 }
528 } break;
529
530 case NOTIFICATION_EXIT_TREE: {
531 if (requesting) {
532 cancel_request();
533 }
534 } break;
535 }
536 }
537
538 void HTTPRequest::set_use_threads(bool p_use) {
539 ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
540 #ifdef THREADS_ENABLED
541 use_threads.set_to(p_use);
542 #endif
543 }
544
545 bool HTTPRequest::is_using_threads() const {
546 return use_threads.is_set();
547 }
548
549 void HTTPRequest::set_accept_gzip(bool p_gzip) {
550 accept_gzip = p_gzip;
551 }
552
553 bool HTTPRequest::is_accepting_gzip() const {
554 return accept_gzip;
555 }
556
557 void HTTPRequest::set_body_size_limit(int p_bytes) {
558 ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
559
560 body_size_limit = p_bytes;
561 }
562
563 int HTTPRequest::get_body_size_limit() const {
564 return body_size_limit;
565 }
566
567 void HTTPRequest::set_download_file(const String &p_file) {
568 ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
569
570 download_to_file = p_file;
571 }
572
573 String HTTPRequest::get_download_file() const {
574 return download_to_file;
575 }
576
577 void HTTPRequest::set_download_chunk_size(int p_chunk_size) {
578 ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
579
580 client->set_read_chunk_size(p_chunk_size);
581 }
582
583 int HTTPRequest::get_download_chunk_size() const {
584 return client->get_read_chunk_size();
585 }
586
587 HTTPClient::Status HTTPRequest::get_http_client_status() const {
588 return client->get_status();
589 }
590
591 void HTTPRequest::set_max_redirects(int p_max) {
592 max_redirects = p_max;
593 }
594
595 int HTTPRequest::get_max_redirects() const {
596 return max_redirects;
597 }
598
599 int HTTPRequest::get_downloaded_bytes() const {
600 return downloaded.get();
601 }
602
603 int HTTPRequest::get_body_size() const {
604 return body_len;
605 }
606
607 void HTTPRequest::set_http_proxy(const String &p_host, int p_port) {
608 client->set_http_proxy(p_host, p_port);
609 }
610
611 void HTTPRequest::set_https_proxy(const String &p_host, int p_port) {
612 client->set_https_proxy(p_host, p_port);
613 }
614
615 void HTTPRequest::set_timeout(double p_timeout) {
616 ERR_FAIL_COND(p_timeout < 0);
617 timeout = p_timeout;
618 }
619
620 double HTTPRequest::get_timeout() {
621 return timeout;
622 }
623
624 void HTTPRequest::_timeout() {
625 cancel_request();
626 _defer_done(RESULT_TIMEOUT, 0, PackedStringArray(), PackedByteArray());
627 }
628
629 void HTTPRequest::set_tls_options(const Ref<TLSOptions> &p_options) {
630 ERR_FAIL_COND(p_options.is_null() || p_options->is_server());
631 tls_options = p_options;
632 }
633
634 void HTTPRequest::_bind_methods() {
635 ClassDB::bind_method(D_METHOD("request", "url", "custom_headers", "method", "request_data"), &HTTPRequest::request, DEFVAL(PackedStringArray()), DEFVAL(HTTPClient::METHOD_GET), DEFVAL(String()));
636 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()));
637 ClassDB::bind_method(D_METHOD("cancel_request"), &HTTPRequest::cancel_request);
638 ClassDB::bind_method(D_METHOD("set_tls_options", "client_options"), &HTTPRequest::set_tls_options);
639
640 ClassDB::bind_method(D_METHOD("get_http_client_status"), &HTTPRequest::get_http_client_status);
641
642 ClassDB::bind_method(D_METHOD("set_use_threads", "enable"), &HTTPRequest::set_use_threads);
643 ClassDB::bind_method(D_METHOD("is_using_threads"), &HTTPRequest::is_using_threads);
644
645 ClassDB::bind_method(D_METHOD("set_accept_gzip", "enable"), &HTTPRequest::set_accept_gzip);
646 ClassDB::bind_method(D_METHOD("is_accepting_gzip"), &HTTPRequest::is_accepting_gzip);
647
648 ClassDB::bind_method(D_METHOD("set_body_size_limit", "bytes"), &HTTPRequest::set_body_size_limit);
649 ClassDB::bind_method(D_METHOD("get_body_size_limit"), &HTTPRequest::get_body_size_limit);
650
651 ClassDB::bind_method(D_METHOD("set_max_redirects", "amount"), &HTTPRequest::set_max_redirects);
652 ClassDB::bind_method(D_METHOD("get_max_redirects"), &HTTPRequest::get_max_redirects);
653
654 ClassDB::bind_method(D_METHOD("set_download_file", "path"), &HTTPRequest::set_download_file);
655 ClassDB::bind_method(D_METHOD("get_download_file"), &HTTPRequest::get_download_file);
656
657 ClassDB::bind_method(D_METHOD("get_downloaded_bytes"), &HTTPRequest::get_downloaded_bytes);
658 ClassDB::bind_method(D_METHOD("get_body_size"), &HTTPRequest::get_body_size);
659
660 ClassDB::bind_method(D_METHOD("set_timeout", "timeout"), &HTTPRequest::set_timeout);
661 ClassDB::bind_method(D_METHOD("get_timeout"), &HTTPRequest::get_timeout);
662
663 ClassDB::bind_method(D_METHOD("set_download_chunk_size", "chunk_size"), &HTTPRequest::set_download_chunk_size);
664 ClassDB::bind_method(D_METHOD("get_download_chunk_size"), &HTTPRequest::get_download_chunk_size);
665
666 ClassDB::bind_method(D_METHOD("set_http_proxy", "host", "port"), &HTTPRequest::set_http_proxy);
667 ClassDB::bind_method(D_METHOD("set_https_proxy", "host", "port"), &HTTPRequest::set_https_proxy);
668
669 ADD_PROPERTY(PropertyInfo(Variant::STRING, "download_file", PROPERTY_HINT_FILE_PATH), "set_download_file", "get_download_file");
670 ADD_PROPERTY(PropertyInfo(Variant::INT, "download_chunk_size", PROPERTY_HINT_RANGE, "256,16777216,suffix:B"), "set_download_chunk_size", "get_download_chunk_size");
671 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_threads"), "set_use_threads", "is_using_threads");
672 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "accept_gzip"), "set_accept_gzip", "is_accepting_gzip");
673 ADD_PROPERTY(PropertyInfo(Variant::INT, "body_size_limit", PROPERTY_HINT_RANGE, "-1,2000000000,suffix:B"), "set_body_size_limit", "get_body_size_limit");
674 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_redirects", PROPERTY_HINT_RANGE, "-1,64"), "set_max_redirects", "get_max_redirects");
675 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "timeout", PROPERTY_HINT_RANGE, "0,3600,0.1,or_greater,suffix:s"), "set_timeout", "get_timeout");
676
677 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")));
678
679 BIND_ENUM_CONSTANT(RESULT_SUCCESS);
680 BIND_ENUM_CONSTANT(RESULT_CHUNKED_BODY_SIZE_MISMATCH);
681 BIND_ENUM_CONSTANT(RESULT_CANT_CONNECT);
682 BIND_ENUM_CONSTANT(RESULT_CANT_RESOLVE);
683 BIND_ENUM_CONSTANT(RESULT_CONNECTION_ERROR);
684 BIND_ENUM_CONSTANT(RESULT_TLS_HANDSHAKE_ERROR);
685 BIND_ENUM_CONSTANT(RESULT_NO_RESPONSE);
686 BIND_ENUM_CONSTANT(RESULT_BODY_SIZE_LIMIT_EXCEEDED);
687 BIND_ENUM_CONSTANT(RESULT_BODY_DECOMPRESS_FAILED);
688 BIND_ENUM_CONSTANT(RESULT_REQUEST_FAILED);
689 BIND_ENUM_CONSTANT(RESULT_DOWNLOAD_FILE_CANT_OPEN);
690 BIND_ENUM_CONSTANT(RESULT_DOWNLOAD_FILE_WRITE_ERROR);
691 BIND_ENUM_CONSTANT(RESULT_REDIRECT_LIMIT_REACHED);
692 BIND_ENUM_CONSTANT(RESULT_TIMEOUT);
693 }
694
695 HTTPRequest::HTTPRequest() {
696 client = Ref<HTTPClient>(HTTPClient::create());
697 tls_options = TLSOptions::client();
698 timer = memnew(Timer);
699 timer->set_one_shot(true);
700 timer->set_ignore_time_scale(true);
701 timer->connect("timeout", callable_mp(this, &HTTPRequest::_timeout));
702 add_child(timer);
703 }