mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-11 13:30:01 -04:00
fix handling of request bodies
This commit is contained in:
parent
3a50060e07
commit
5e9f26f818
6 changed files with 32 additions and 8 deletions
|
@ -1,5 +1,8 @@
|
|||
# Libcurl.js Changelog:
|
||||
|
||||
## v0.6.4 (3/20/24):
|
||||
- Fix handling of request bodies
|
||||
|
||||
## v0.6.3 (3/20/24):
|
||||
- Fix an error during websocket cleanup
|
||||
|
||||
|
|
|
@ -41,6 +41,7 @@ class HTTPSession extends CurlSession {
|
|||
request_async(url, params, body) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let http_handle;
|
||||
let body_ptr = null;
|
||||
|
||||
let headers_callback = (stream) => {
|
||||
let response_json = c_func_str(_http_get_info, [http_handle]);
|
||||
|
@ -53,6 +54,7 @@ class HTTPSession extends CurlSession {
|
|||
resolve(response);
|
||||
}
|
||||
let finish_callback = (error) => {
|
||||
_free(body_ptr);
|
||||
if (error > 0) {
|
||||
error_msg(`Request "${url}" failed with error code ${error}: ${get_error_str(error)}`);
|
||||
reject(`Request failed with error code ${error}: ${get_error_str(error)}`);
|
||||
|
@ -65,12 +67,13 @@ class HTTPSession extends CurlSession {
|
|||
}
|
||||
this.remove_request(http_handle);
|
||||
}
|
||||
|
||||
|
||||
body_ptr = body ? allocate_array(body) : null;
|
||||
let body_length = body ? body.length : 0;
|
||||
let params_json = JSON.stringify(params);
|
||||
|
||||
http_handle = this.stream_response(url, headers_callback, finish_callback, params.signal);
|
||||
c_func(_http_set_options, [http_handle, params_json, body, body_length]);
|
||||
c_func(_http_set_options, [http_handle, params_json, body_ptr, body_length]);
|
||||
if (this.cookie_filename && params.credentials !== "omit") {
|
||||
c_func(_http_set_cookie_jar, [http_handle, this.cookie_filename]);
|
||||
}
|
||||
|
@ -139,8 +142,8 @@ class HTTPSession extends CurlSession {
|
|||
if (!params.headers["User-Agent"]) {
|
||||
params.headers["User-Agent"] = navigator.userAgent;
|
||||
}
|
||||
if (body) {
|
||||
params.headers["Content-Type"] = request_obj.headers.get("Content-Type");
|
||||
if (body && !params.headers["Content-Type"]) {
|
||||
params.headers["Content-Type"] = request_obj.headers.get("Content-Type") || "";
|
||||
}
|
||||
|
||||
return body;
|
||||
|
|
|
@ -77,10 +77,15 @@ function data_to_array(data) {
|
|||
function c_func(target, args=[]) {
|
||||
let str_pointers = [];
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (typeof args[i] !== "string") {
|
||||
continue;
|
||||
let ptr = null;
|
||||
if (typeof args[i] === "string") {
|
||||
ptr = allocate_str(args[i]);
|
||||
}
|
||||
let ptr = allocate_str(args[i]);
|
||||
if (args[i] instanceof Uint8Array) {
|
||||
ptr = allocate_array(args[i]);
|
||||
}
|
||||
|
||||
if (!ptr) continue;
|
||||
args[i] = ptr;
|
||||
str_pointers.push(ptr);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "libcurl.js",
|
||||
"version": "0.6.3",
|
||||
"version": "0.6.4",
|
||||
"description": "An experimental port of libcurl to WebAssembly for use in the browser.",
|
||||
"main": "libcurl.mjs",
|
||||
"exports": {
|
||||
|
|
|
@ -55,5 +55,8 @@ class JSTest(unittest.TestCase):
|
|||
def test_http_session(self):
|
||||
self.run_test("test_http_session.js")
|
||||
|
||||
def test_post(self):
|
||||
self.run_test("test_post.js")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
10
client/tests/scripts/test_post.js
Normal file
10
client/tests/scripts/test_post.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
async function test() {
|
||||
let payload = "hello".repeat(100);
|
||||
let r = await libcurl.fetch("https://httpbin.org/anything", {
|
||||
method: "POST",
|
||||
body: new Blob([payload], {type: "text/plain"})
|
||||
});
|
||||
assert(r.status === 200, "wrong status");
|
||||
let json = await r.json();
|
||||
assert(json.data === payload, "server reports wrong payload");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue