|
@@ -29,10 +29,41 @@
|
|
|
/*************************************************************************/
|
|
|
|
|
|
#include "core/io/http_client.h"
|
|
|
-#include "http_request.h"
|
|
|
|
|
|
-Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl, bool p_verify_host) {
|
|
|
+#ifdef __cplusplus
|
|
|
+extern "C" {
|
|
|
+#endif
|
|
|
+
|
|
|
+#include "stddef.h"
|
|
|
+
|
|
|
+typedef enum {
|
|
|
+ GODOT_JS_FETCH_STATE_REQUESTING = 0,
|
|
|
+ GODOT_JS_FETCH_STATE_BODY = 1,
|
|
|
+ GODOT_JS_FETCH_STATE_DONE = 2,
|
|
|
+ GODOT_JS_FETCH_STATE_ERROR = -1,
|
|
|
+} godot_js_fetch_state_t;
|
|
|
+
|
|
|
+extern int godot_js_fetch_create(const char *p_method, const char *p_url, const char **p_headers, int p_headers_len, const uint8_t *p_body, int p_body_len);
|
|
|
+extern int godot_js_fetch_read_headers(int p_id, void (*parse_callback)(int p_size, const char **p_headers, void *p_ref), void *p_ref);
|
|
|
+extern int godot_js_fetch_read_chunk(int p_id, uint8_t *p_buf, int p_buf_size);
|
|
|
+extern void godot_js_fetch_free(int p_id);
|
|
|
+extern godot_js_fetch_state_t godot_js_fetch_state_get(int p_id);
|
|
|
+extern int godot_js_fetch_body_length_get(int p_id);
|
|
|
+extern int godot_js_fetch_http_status_get(int p_id);
|
|
|
+extern int godot_js_fetch_is_chunked(int p_id);
|
|
|
+
|
|
|
+#ifdef __cplusplus
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
+void HTTPClient::_parse_headers(int p_len, const char **p_headers, void *p_ref) {
|
|
|
+ HTTPClient *client = static_cast<HTTPClient *>(p_ref);
|
|
|
+ for (int i = 0; i < p_len; i++) {
|
|
|
+ client->response_headers.push_back(String::utf8(p_headers[i]));
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
+Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl, bool p_verify_host) {
|
|
|
close();
|
|
|
if (p_ssl && !p_verify_host) {
|
|
|
WARN_PRINT("Disabling HTTPClient's host verification is not supported for the HTML5 platform, host will be verified");
|
|
@@ -67,17 +98,14 @@ Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl,
|
|
|
}
|
|
|
|
|
|
void HTTPClient::set_connection(const Ref<StreamPeer> &p_connection) {
|
|
|
-
|
|
|
ERR_FAIL_MSG("Accessing an HTTPClient's StreamPeer is not supported for the HTML5 platform.");
|
|
|
}
|
|
|
|
|
|
Ref<StreamPeer> HTTPClient::get_connection() const {
|
|
|
-
|
|
|
ERR_FAIL_V_MSG(REF(), "Accessing an HTTPClient's StreamPeer is not supported for the HTML5 platform.");
|
|
|
}
|
|
|
|
|
|
-Error HTTPClient::prepare_request(Method p_method, const String &p_url, const Vector<String> &p_headers) {
|
|
|
-
|
|
|
+Error HTTPClient::make_request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_len) {
|
|
|
ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
|
|
|
ERR_FAIL_COND_V_MSG(p_method == METHOD_TRACE || p_method == METHOD_CONNECT, ERR_UNAVAILABLE, "HTTP methods TRACE and CONNECT are not supported for the HTML5 platform.");
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
|
|
@@ -86,137 +114,116 @@ Error HTTPClient::prepare_request(Method p_method, const String &p_url, const Ve
|
|
|
ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER);
|
|
|
|
|
|
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(),
|
|
|
- password.empty() ? NULL : password.utf8().get_data());
|
|
|
-
|
|
|
+ Vector<CharString> keeper;
|
|
|
+ Vector<const char *> c_strings;
|
|
|
for (int i = 0; i < p_headers.size(); i++) {
|
|
|
- int header_separator = p_headers[i].find(": ");
|
|
|
- ERR_FAIL_COND_V(header_separator < 0, ERR_INVALID_PARAMETER);
|
|
|
- godot_xhr_set_request_header(xhr_id,
|
|
|
- p_headers[i].left(header_separator).utf8().get_data(),
|
|
|
- p_headers[i].right(header_separator + 2).utf8().get_data());
|
|
|
+ keeper.push_back(p_headers[i].utf8());
|
|
|
+ c_strings.push_back(keeper[i].get_data());
|
|
|
+ }
|
|
|
+ if (js_id) {
|
|
|
+ godot_js_fetch_free(js_id);
|
|
|
}
|
|
|
- response_read_offset = 0;
|
|
|
+ js_id = godot_js_fetch_create(_methods[p_method], url.utf8().get_data(), c_strings.ptrw(), c_strings.size(), p_body, p_body_len);
|
|
|
status = STATUS_REQUESTING;
|
|
|
return OK;
|
|
|
}
|
|
|
|
|
|
Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const PoolVector<uint8_t> &p_body) {
|
|
|
-
|
|
|
- Error err = prepare_request(p_method, p_url, p_headers);
|
|
|
- if (err != OK)
|
|
|
- return err;
|
|
|
if (p_body.empty()) {
|
|
|
- godot_xhr_send(xhr_id, nullptr, 0);
|
|
|
- } else {
|
|
|
- PoolByteArray::Read read = p_body.read();
|
|
|
- godot_xhr_send(xhr_id, read.ptr(), p_body.size());
|
|
|
+ return make_request(p_method, p_url, p_headers, nullptr, 0);
|
|
|
}
|
|
|
- return OK;
|
|
|
+ PoolByteArray::Read read = p_body.read();
|
|
|
+ return make_request(p_method, p_url, p_headers, read.ptr(), p_body.size());
|
|
|
}
|
|
|
|
|
|
Error HTTPClient::request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body) {
|
|
|
-
|
|
|
- Error err = prepare_request(p_method, p_url, p_headers);
|
|
|
- if (err != OK)
|
|
|
- return err;
|
|
|
if (p_body.empty()) {
|
|
|
- godot_xhr_send(xhr_id, nullptr, 0);
|
|
|
- } else {
|
|
|
- const CharString cs = p_body.utf8();
|
|
|
- godot_xhr_send(xhr_id, cs.get_data(), cs.length());
|
|
|
+ return make_request(p_method, p_url, p_headers, nullptr, 0);
|
|
|
}
|
|
|
- return OK;
|
|
|
+ const CharString cs = p_body.utf8();
|
|
|
+ return make_request(p_method, p_url, p_headers, (const uint8_t *)cs.get_data(), cs.size() - 1);
|
|
|
}
|
|
|
|
|
|
void HTTPClient::close() {
|
|
|
-
|
|
|
host = "";
|
|
|
port = -1;
|
|
|
use_tls = false;
|
|
|
status = STATUS_DISCONNECTED;
|
|
|
- polled_response.resize(0);
|
|
|
polled_response_code = 0;
|
|
|
- polled_response_header = String();
|
|
|
- godot_xhr_reset(xhr_id);
|
|
|
+ response_headers.resize(0);
|
|
|
+ response_buffer.resize(0);
|
|
|
+ if (js_id) {
|
|
|
+ godot_js_fetch_free(js_id);
|
|
|
+ js_id = 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
HTTPClient::Status HTTPClient::get_status() const {
|
|
|
-
|
|
|
return status;
|
|
|
}
|
|
|
|
|
|
bool HTTPClient::has_response() const {
|
|
|
-
|
|
|
- return !polled_response_header.empty();
|
|
|
+ return response_headers.size() > 0;
|
|
|
}
|
|
|
|
|
|
bool HTTPClient::is_response_chunked() const {
|
|
|
-
|
|
|
- // TODO evaluate using moz-chunked-arraybuffer, fetch & ReadableStream
|
|
|
- return false;
|
|
|
+ return godot_js_fetch_is_chunked(js_id);
|
|
|
}
|
|
|
|
|
|
int HTTPClient::get_response_code() const {
|
|
|
-
|
|
|
return polled_response_code;
|
|
|
}
|
|
|
|
|
|
Error HTTPClient::get_response_headers(List<String> *r_response) {
|
|
|
-
|
|
|
- if (polled_response_header.empty())
|
|
|
+ if (!response_headers.size()) {
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
-
|
|
|
- Vector<String> header_lines = polled_response_header.split("\r\n", false);
|
|
|
- for (int i = 0; i < header_lines.size(); ++i) {
|
|
|
- r_response->push_back(header_lines[i]);
|
|
|
}
|
|
|
- polled_response_header = String();
|
|
|
+ for (int i = 0; i < response_headers.size(); i++) {
|
|
|
+ r_response->push_back(response_headers[i]);
|
|
|
+ }
|
|
|
+ response_headers.clear();
|
|
|
return OK;
|
|
|
}
|
|
|
|
|
|
int HTTPClient::get_response_body_length() const {
|
|
|
-
|
|
|
- return polled_response.size();
|
|
|
+ return godot_js_fetch_body_length_get(js_id);
|
|
|
}
|
|
|
|
|
|
PoolByteArray HTTPClient::read_response_body_chunk() {
|
|
|
-
|
|
|
ERR_FAIL_COND_V(status != STATUS_BODY, PoolByteArray());
|
|
|
|
|
|
- int to_read = MIN(read_limit, polled_response.size() - response_read_offset);
|
|
|
- PoolByteArray chunk;
|
|
|
- chunk.resize(to_read);
|
|
|
- PoolByteArray::Write write = chunk.write();
|
|
|
- PoolByteArray::Read read = polled_response.read();
|
|
|
- memcpy(write.ptr(), read.ptr() + response_read_offset, to_read);
|
|
|
- write = PoolByteArray::Write();
|
|
|
- read = PoolByteArray::Read();
|
|
|
- response_read_offset += to_read;
|
|
|
-
|
|
|
- if (response_read_offset == polled_response.size()) {
|
|
|
- status = STATUS_CONNECTED;
|
|
|
- polled_response.resize(0);
|
|
|
- godot_xhr_reset(xhr_id);
|
|
|
+ if (response_buffer.size() != read_limit) {
|
|
|
+ response_buffer.resize(read_limit);
|
|
|
+ }
|
|
|
+ int read = godot_js_fetch_read_chunk(js_id, response_buffer.ptrw(), read_limit);
|
|
|
+
|
|
|
+ // Check if the stream is over.
|
|
|
+ godot_js_fetch_state_t state = godot_js_fetch_state_get(js_id);
|
|
|
+ if (state == GODOT_JS_FETCH_STATE_DONE) {
|
|
|
+ status = STATUS_DISCONNECTED;
|
|
|
+ } else if (state != GODOT_JS_FETCH_STATE_BODY) {
|
|
|
+ status = STATUS_CONNECTION_ERROR;
|
|
|
}
|
|
|
|
|
|
+ PoolByteArray chunk;
|
|
|
+ if (!read) {
|
|
|
+ return chunk;
|
|
|
+ }
|
|
|
+ chunk.resize(read);
|
|
|
+ PoolByteArray::Write w = chunk.write();
|
|
|
+ copymem(&w[0], response_buffer.ptr(), read);
|
|
|
return chunk;
|
|
|
}
|
|
|
|
|
|
void HTTPClient::set_blocking_mode(bool p_enable) {
|
|
|
-
|
|
|
ERR_FAIL_COND_MSG(p_enable, "HTTPClient blocking mode is not supported for the HTML5 platform.");
|
|
|
}
|
|
|
|
|
|
bool HTTPClient::is_blocking_mode_enabled() const {
|
|
|
-
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
void HTTPClient::set_read_chunk_size(int p_size) {
|
|
|
-
|
|
|
read_limit = p_size;
|
|
|
}
|
|
|
|
|
@@ -225,9 +232,7 @@ int HTTPClient::get_read_chunk_size() const {
|
|
|
}
|
|
|
|
|
|
Error HTTPClient::poll() {
|
|
|
-
|
|
|
switch (status) {
|
|
|
-
|
|
|
case STATUS_DISCONNECTED:
|
|
|
return ERR_UNCONFIGURED;
|
|
|
|
|
@@ -240,55 +245,49 @@ Error HTTPClient::poll() {
|
|
|
return OK;
|
|
|
|
|
|
case STATUS_CONNECTED:
|
|
|
- case STATUS_BODY:
|
|
|
return OK;
|
|
|
|
|
|
+ case STATUS_BODY: {
|
|
|
+ godot_js_fetch_state_t state = godot_js_fetch_state_get(js_id);
|
|
|
+ if (state == GODOT_JS_FETCH_STATE_DONE) {
|
|
|
+ status = STATUS_DISCONNECTED;
|
|
|
+ } else if (state != GODOT_JS_FETCH_STATE_BODY) {
|
|
|
+ status = STATUS_CONNECTION_ERROR;
|
|
|
+ return ERR_CONNECTION_ERROR;
|
|
|
+ }
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
+
|
|
|
case STATUS_CONNECTION_ERROR:
|
|
|
return ERR_CONNECTION_ERROR;
|
|
|
|
|
|
case STATUS_REQUESTING: {
|
|
|
|
|
|
#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.");
|
|
|
- }
|
|
|
+ // 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) {
|
|
|
+ polled_response_code = godot_js_fetch_http_status_get(js_id);
|
|
|
+ godot_js_fetch_state_t js_state = godot_js_fetch_state_get(js_id);
|
|
|
+ if (js_state == GODOT_JS_FETCH_STATE_REQUESTING) {
|
|
|
return OK;
|
|
|
- } else if (!polled_response_code) {
|
|
|
+ } else if (js_state == GODOT_JS_FETCH_STATE_ERROR) {
|
|
|
+ // Fetch is in error state.
|
|
|
+ status = STATUS_CONNECTION_ERROR;
|
|
|
+ return ERR_CONNECTION_ERROR;
|
|
|
+ }
|
|
|
+ if (godot_js_fetch_read_headers(js_id, &_parse_headers, this)) {
|
|
|
+ // Failed to parse headers.
|
|
|
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 + 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(godot_xhr_get_response_length(xhr_id));
|
|
|
- write = polled_response.write();
|
|
|
- godot_xhr_get_response(xhr_id, write.ptr(), polled_response.size());
|
|
|
- write = PoolByteArray::Write();
|
|
|
break;
|
|
|
}
|
|
|
|
|
@@ -299,20 +298,8 @@ Error HTTPClient::poll() {
|
|
|
}
|
|
|
|
|
|
HTTPClient::HTTPClient() {
|
|
|
-
|
|
|
- xhr_id = godot_xhr_new();
|
|
|
- read_limit = 4096;
|
|
|
- status = STATUS_DISCONNECTED;
|
|
|
- port = -1;
|
|
|
- use_tls = false;
|
|
|
- polled_response_code = 0;
|
|
|
-#ifdef DEBUG_ENABLED
|
|
|
- has_polled = false;
|
|
|
- last_polling_frame = 0;
|
|
|
-#endif
|
|
|
}
|
|
|
|
|
|
HTTPClient::~HTTPClient() {
|
|
|
-
|
|
|
- godot_xhr_free(xhr_id);
|
|
|
+ close();
|
|
|
}
|