diff options
Diffstat (limited to 'scene/main/http_request.cpp')
-rw-r--r-- | scene/main/http_request.cpp | 68 |
1 files changed, 19 insertions, 49 deletions
diff --git a/scene/main/http_request.cpp b/scene/main/http_request.cpp index fee2ada76d..82ee4dde50 100644 --- a/scene/main/http_request.cpp +++ b/scene/main/http_request.cpp @@ -34,12 +34,10 @@ void HTTPRequest::_redirect_request(const String &p_new_url) { } Error HTTPRequest::_request() { - return client->connect_to_host(url, port, use_ssl, validate_ssl); } Error HTTPRequest::_parse_url(const String &p_url) { - url = p_url; use_ssl = false; @@ -85,7 +83,6 @@ Error HTTPRequest::_parse_url(const String &p_url) { } 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) { - ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED); ERR_FAIL_COND_V_MSG(requesting, ERR_BUSY, "HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one."); @@ -97,8 +94,9 @@ Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_h method = p_method; Error err = _parse_url(p_url); - if (err) + if (err) { return err; + } validate_ssl = p_ssl_validate_domain; @@ -109,7 +107,6 @@ Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_h requesting = true; if (use_threads) { - thread_done = false; thread_request_quit = false; client->set_blocking_mode(true); @@ -129,7 +126,6 @@ Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_h } void HTTPRequest::_thread_func(void *p_userdata) { - HTTPRequest *hr = (HTTPRequest *)p_userdata; Error err = hr->_request(); @@ -138,10 +134,10 @@ void HTTPRequest::_thread_func(void *p_userdata) { hr->call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray()); } else { while (!hr->thread_request_quit) { - bool exit = hr->_update_connection(); - if (exit) + if (exit) { break; + } OS::get_singleton()->delay_usec(1); } } @@ -150,11 +146,11 @@ void HTTPRequest::_thread_func(void *p_userdata) { } void HTTPRequest::cancel_request() { - timer->stop(); - if (!requesting) + if (!requesting) { return; + } if (!use_threads) { set_process_internal(false); @@ -162,12 +158,12 @@ void HTTPRequest::cancel_request() { thread_request_quit = true; Thread::wait_to_finish(thread); memdelete(thread); - thread = NULL; + thread = nullptr; } if (file) { memdelete(file); - file = NULL; + file = nullptr; } client->close(); body.resize(0); @@ -178,7 +174,6 @@ void HTTPRequest::cancel_request() { } bool HTTPRequest::_handle_response(bool *ret_value) { - if (!client->has_response()) { call_deferred("_request_done", RESULT_NO_RESPONSE, 0, PackedStringArray(), PackedByteArray()); *ret_value = true; @@ -199,7 +194,6 @@ bool HTTPRequest::_handle_response(bool *ret_value) { // Handle redirect if (max_redirects >= 0 && redirections >= max_redirects) { - call_deferred("_request_done", RESULT_REDIRECT_LIMIT_REACHED, response_code, response_headers, PackedByteArray()); *ret_value = true; return true; @@ -243,7 +237,6 @@ bool HTTPRequest::_handle_response(bool *ret_value) { } bool HTTPRequest::_update_connection() { - switch (client->get_status()) { case HTTPClient::STATUS_DISCONNECTED: { call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray()); @@ -265,23 +258,20 @@ bool HTTPRequest::_update_connection() { return false; } break; // Connecting to IP case HTTPClient::STATUS_CANT_CONNECT: { - call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray()); return true; } break; case HTTPClient::STATUS_CONNECTED: { - if (request_sent) { - if (!got_response) { - // No body bool ret_value; - if (_handle_response(&ret_value)) + if (_handle_response(&ret_value)) { return ret_value; + } call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, PackedByteArray()); return true; @@ -315,16 +305,14 @@ bool HTTPRequest::_update_connection() { } break; // Request in progress case HTTPClient::STATUS_BODY: { - if (!got_response) { - bool ret_value; - if (_handle_response(&ret_value)) + if (_handle_response(&ret_value)) { return ret_value; + } if (!client->is_response_chunked() && client->get_response_body_length() == 0) { - call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, PackedByteArray()); return true; } @@ -341,7 +329,6 @@ bool HTTPRequest::_update_connection() { if (download_to_file != String()) { file = FileAccess::open(download_to_file, FileAccess::WRITE); if (!file) { - call_deferred("_request_done", RESULT_DOWNLOAD_FILE_CANT_OPEN, response_code, response_headers, PackedByteArray()); return true; } @@ -370,7 +357,6 @@ bool HTTPRequest::_update_connection() { } if (body_len >= 0) { - if (downloaded == body_len) { call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body); return true; @@ -397,20 +383,17 @@ bool HTTPRequest::_update_connection() { } void HTTPRequest::_request_done(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) { - cancel_request(); emit_signal("request_completed", p_status, p_code, headers, p_data); } void HTTPRequest::_notification(int p_what) { - if (p_what == NOTIFICATION_INTERNAL_PROCESS) { - - if (use_threads) + if (use_threads) { return; + } bool done = _update_connection(); if (done) { - set_process_internal(false); // cancel_request(); called from _request done now } @@ -424,42 +407,35 @@ void HTTPRequest::_notification(int p_what) { } void HTTPRequest::set_use_threads(bool p_use) { - ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED); use_threads = p_use; } bool HTTPRequest::is_using_threads() const { - return use_threads; } void HTTPRequest::set_body_size_limit(int p_bytes) { - ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED); body_size_limit = p_bytes; } int HTTPRequest::get_body_size_limit() const { - return body_size_limit; } void HTTPRequest::set_download_file(const String &p_file) { - ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED); download_to_file = p_file; } String HTTPRequest::get_download_file() const { - return download_to_file; } void HTTPRequest::set_download_chunk_size(int p_chunk_size) { - ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED); client->set_read_chunk_size(p_chunk_size); @@ -474,42 +450,36 @@ HTTPClient::Status HTTPRequest::get_http_client_status() const { } void HTTPRequest::set_max_redirects(int p_max) { - max_redirects = p_max; } int HTTPRequest::get_max_redirects() const { - return max_redirects; } int HTTPRequest::get_downloaded_bytes() const { - return downloaded; } + int HTTPRequest::get_body_size() const { return body_len; } void HTTPRequest::set_timeout(int p_timeout) { - ERR_FAIL_COND(p_timeout < 0); timeout = p_timeout; } int HTTPRequest::get_timeout() { - return timeout; } void HTTPRequest::_timeout() { - cancel_request(); call_deferred("_request_done", RESULT_TIMEOUT, 0, PackedStringArray(), PackedByteArray()); } void HTTPRequest::_bind_methods() { - 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())); ClassDB::bind_method(D_METHOD("cancel_request"), &HTTPRequest::cancel_request); @@ -565,8 +535,7 @@ void HTTPRequest::_bind_methods() { } HTTPRequest::HTTPRequest() { - - thread = NULL; + thread = nullptr; port = 80; redirections = 0; @@ -583,7 +552,7 @@ HTTPRequest::HTTPRequest() { thread_done = false; downloaded = 0; body_size_limit = -1; - file = NULL; + file = nullptr; timer = memnew(Timer); timer->set_one_shot(true); @@ -593,6 +562,7 @@ HTTPRequest::HTTPRequest() { } HTTPRequest::~HTTPRequest() { - if (file) + if (file) { memdelete(file); + } } |