[HTML5] Fix HTTPClient request_raw.

Now send data according to the spec, properly handle null data.
Simplify JS code since we are at it.
This commit is contained in:
Fabio Alessandrelli 2021-02-11 09:32:17 +01:00
parent 9fb27eba8d
commit 4086a123b9
3 changed files with 19 additions and 30 deletions

View file

@ -108,8 +108,12 @@ Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector
Error err = prepare_request(p_method, p_url, p_headers); Error err = prepare_request(p_method, p_url, p_headers);
if (err != OK) if (err != OK)
return err; return err;
PoolByteArray::Read read = p_body.read(); if (p_body.empty()) {
godot_xhr_send_data(xhr_id, read.ptr(), p_body.size()); 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 OK; return OK;
} }
@ -118,7 +122,12 @@ Error HTTPClient::request(Method p_method, const String &p_url, const Vector<Str
Error err = prepare_request(p_method, p_url, p_headers); Error err = prepare_request(p_method, p_url, p_headers);
if (err != OK) if (err != OK)
return err; return err;
godot_xhr_send_string(xhr_id, p_body.utf8().get_data()); 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 OK; return OK;
} }

View file

@ -53,9 +53,7 @@ extern int godot_xhr_open(int p_xhr_id, const char *p_method, const char *p_url,
extern void godot_xhr_set_request_header(int p_xhr_id, const char *p_header, const char *p_value); extern void godot_xhr_set_request_header(int p_xhr_id, const char *p_header, const char *p_value);
extern void godot_xhr_send_null(int p_xhr_id); extern void godot_xhr_send(int p_xhr_id, const void *p_data, int p_len);
extern void godot_xhr_send_string(int p_xhr_id, const char *p_data);
extern void godot_xhr_send_data(int p_xhr_id, const void *p_data, int p_len);
extern void godot_xhr_abort(int p_xhr_id); extern void godot_xhr_abort(int p_xhr_id);
/* this is an HTTPClient::ResponseCode, not ::Status */ /* this is an HTTPClient::ResponseCode, not ::Status */

View file

@ -82,31 +82,13 @@ const GodotHTTPRequest = {
GodotHTTPRequest.requests[xhrId].setRequestHeader(GodotRuntime.parseString(header), GodotRuntime.parseString(value)); GodotHTTPRequest.requests[xhrId].setRequestHeader(GodotRuntime.parseString(header), GodotRuntime.parseString(value));
}, },
godot_xhr_send_null__sig: 'vi', godot_xhr_send__sig: 'viii',
godot_xhr_send_null: function (xhrId) { godot_xhr_send: function (xhrId, p_ptr, p_len) {
GodotHTTPRequest.requests[xhrId].send(); let data = null;
}, if (p_ptr && p_len) {
data = GodotRuntime.heapCopy(HEAP8, p_ptr, p_len);
godot_xhr_send_string__sig: 'vii',
godot_xhr_send_string: function (xhrId, strPtr) {
if (!strPtr) {
GodotRuntime.error('Failed to send string per XHR: null pointer');
return;
} }
GodotHTTPRequest.requests[xhrId].send(GodotRuntime.parseString(strPtr)); GodotHTTPRequest.requests[xhrId].send(data);
},
godot_xhr_send_data__sig: 'viii',
godot_xhr_send_data: function (xhrId, ptr, len) {
if (!ptr) {
GodotRuntime.error('Failed to send data per XHR: null pointer');
return;
}
if (len < 0) {
GodotRuntime.error('Failed to send data per XHR: buffer length less than 0');
return;
}
GodotHTTPRequest.requests[xhrId].send(HEAPU8.subarray(ptr, ptr + len));
}, },
godot_xhr_abort__sig: 'vi', godot_xhr_abort__sig: 'vi',