diff options
Diffstat (limited to 'platform/javascript/http_client_javascript.cpp')
-rw-r--r-- | platform/javascript/http_client_javascript.cpp | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/platform/javascript/http_client_javascript.cpp b/platform/javascript/http_client_javascript.cpp index 1cd2719723..8d90e01ae1 100644 --- a/platform/javascript/http_client_javascript.cpp +++ b/platform/javascript/http_client_javascript.cpp @@ -81,12 +81,14 @@ Ref<StreamPeer> HTTPClient::get_connection() const { Error HTTPClient::prepare_request(Method p_method, const String &p_url, const Vector<String> &p_headers) { ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER); + ERR_EXPLAIN("HTTP methods TRACE and CONNECT are not supported for the HTML5 platform"); + ERR_FAIL_COND_V(p_method == METHOD_TRACE || p_method == METHOD_CONNECT, ERR_UNAVAILABLE); ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(host.empty(), ERR_UNCONFIGURED); ERR_FAIL_COND_V(port < 0, ERR_UNCONFIGURED); ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER); - String url = (use_tls ? "https://" : "http://") + host + ":" + itos(port) + "/" + p_url; + String url = (use_tls ? "https://" : "http://") + host + ":" + itos(port) + p_url; godot_xhr_reset(xhr_id); godot_xhr_open(xhr_id, _methods[p_method], url.utf8().get_data(), username.empty() ? NULL : username.utf8().get_data(), @@ -158,7 +160,7 @@ int HTTPClient::get_response_code() const { Error HTTPClient::get_response_headers(List<String> *r_response) { - if (!polled_response_header.size()) + if (polled_response_header.empty()) return ERR_INVALID_PARAMETER; Vector<String> header_lines = polled_response_header.split("\r\n", false); @@ -191,8 +193,6 @@ PoolByteArray HTTPClient::read_response_body_chunk() { if (response_read_offset == polled_response.size()) { status = STATUS_CONNECTED; polled_response.resize(0); - polled_response_code = 0; - polled_response_header = String(); godot_xhr_reset(xhr_id); } @@ -238,34 +238,47 @@ Error HTTPClient::poll() { return ERR_CONNECTION_ERROR; case STATUS_REQUESTING: - polled_response_code = godot_xhr_get_status(xhr_id); - int response_length = godot_xhr_get_response_length(xhr_id); - if (response_length == 0) { - godot_xhr_ready_state_t ready_state = godot_xhr_get_ready_state(xhr_id); - if (ready_state == XHR_READY_STATE_HEADERS_RECEIVED || ready_state == XHR_READY_STATE_LOADING) { - return OK; - } else { - status = STATUS_CONNECTION_ERROR; - return ERR_CONNECTION_ERROR; + +#ifdef DEBUG_ENABLED + if (!has_polled) { + has_polled = true; + } else { + // forcing synchronous requests is not possible on the web + if (last_polling_frame == Engine::get_singleton()->get_idle_frames()) { + WARN_PRINT("HTTPClient polled multiple times in one frame, " + "but request cannot progress more than once per " + "frame on the HTML5 platform."); } } + last_polling_frame = Engine::get_singleton()->get_idle_frames(); +#endif + + polled_response_code = godot_xhr_get_status(xhr_id); + if (godot_xhr_get_ready_state(xhr_id) != XHR_READY_STATE_DONE) { + return OK; + } else if (!polled_response_code) { + status = STATUS_CONNECTION_ERROR; + return ERR_CONNECTION_ERROR; + } status = STATUS_BODY; PoolByteArray bytes; int len = godot_xhr_get_response_headers_length(xhr_id); - bytes.resize(len); + bytes.resize(len + 1); + PoolByteArray::Write write = bytes.write(); godot_xhr_get_response_headers(xhr_id, reinterpret_cast<char *>(write.ptr()), len); + write[len] = 0; write = PoolByteArray::Write(); PoolByteArray::Read read = bytes.read(); polled_response_header = String::utf8(reinterpret_cast<const char *>(read.ptr())); read = PoolByteArray::Read(); - polled_response.resize(response_length); + polled_response.resize(godot_xhr_get_response_length(xhr_id)); write = polled_response.write(); - godot_xhr_get_response(xhr_id, write.ptr(), response_length); + godot_xhr_get_response(xhr_id, write.ptr(), polled_response.size()); write = PoolByteArray::Write(); break; } @@ -280,6 +293,10 @@ HTTPClient::HTTPClient() { port = -1; use_tls = false; polled_response_code = 0; +#ifdef DEBUG_ENABLED + has_polled = false; + last_polling_frame = 0; +#endif } HTTPClient::~HTTPClient() { |