| 703 | } |
| 704 | |
| 705 | Error gdre::wpost_sync(const String &p_url, const Vector<uint8_t> &p_data, const Vector<String> &extra_headers, bool gzip, float *p_progress, bool *p_cancelled) { |
| 706 | #define WPOST_CANCELLED_CHECK() \ |
| 707 | if (p_cancelled && *p_cancelled) { \ |
| 708 | return ERR_SKIP; \ |
| 709 | } |
| 710 | WPOST_CANCELLED_CHECK(); |
| 711 | Ref<HTTPClient> client = HTTPClient::create(); |
| 712 | client->set_blocking_mode(true); |
| 713 | Error err; |
| 714 | String request_url = p_url; |
| 715 | int64_t data_size = p_data.size(); |
| 716 | Vector<uint8_t> data = p_data; |
| 717 | Vector<String> headers = extra_headers; |
| 718 | if (gzip) { |
| 719 | auto ret = Compression::compress(data.ptrw(), p_data.ptr(), p_data.size(), Compression::MODE_GZIP); |
| 720 | if (ret < 0) { |
| 721 | return ERR_BUG; |
| 722 | } |
| 723 | data.resize(ret); |
| 724 | if (!headers.has("Content-Encoding: gzip")) { |
| 725 | headers.push_back("Content-Encoding: gzip"); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | auto connect_to_host_and_request = [&](const String &url) { |
| 730 | WPOST_CANCELLED_CHECK(); |
| 731 | bool is_https = url.begins_with("https://"); |
| 732 | String host = url.get_slice("://", 1).get_slice("/", 0); |
| 733 | String thingy = (is_https ? "https://" : "http://") + host; |
| 734 | Error connect_err = client->connect_to_host(thingy, is_https ? 443 : 80); |
| 735 | ERR_FAIL_COND_V_MSG(connect_err, connect_err, "Failed to connect to host " + url); |
| 736 | while (client->get_status() == HTTPClient::STATUS_RESOLVING || client->get_status() == HTTPClient::STATUS_CONNECTING) { |
| 737 | WPOST_CANCELLED_CHECK(); |
| 738 | err = client->poll(); |
| 739 | if (err) { |
| 740 | return err; |
| 741 | } |
| 742 | } |
| 743 | if (client->get_status() != HTTPClient::STATUS_CONNECTED) { |
| 744 | return ERR_CANT_CONNECT; |
| 745 | } |
| 746 | WPOST_CANCELLED_CHECK(); |
| 747 | // Send PUT request with body data |
| 748 | const uint8_t *body_ptr = data_size > 0 ? p_data.ptr() : nullptr; |
| 749 | Error request_err = client->request(HTTPClient::METHOD_POST, url, extra_headers, body_ptr, data_size); |
| 750 | ERR_FAIL_COND_V_MSG(request_err, request_err, "Failed to send PUT request to " + url); |
| 751 | return OK; |
| 752 | }; |
| 753 | |
| 754 | bool done = false; |
| 755 | bool got_response = false; |
| 756 | List<String> response_headers; |
| 757 | int redirections = 0; |
| 758 | int response_code = 0; |
| 759 | |
| 760 | auto _handle_response = [&]() -> Error { |
| 761 | WPOST_CANCELLED_CHECK(); |
| 762 | if (!client->has_response()) { |