diff --git a/CHANGELOG.md b/CHANGELOG.md index c1245b4..6eea07c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/client/javascript/http.js b/client/javascript/http.js index 110e650..56ae508 100644 --- a/client/javascript/http.js +++ b/client/javascript/http.js @@ -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; diff --git a/client/javascript/util.js b/client/javascript/util.js index c71db82..ec47961 100644 --- a/client/javascript/util.js +++ b/client/javascript/util.js @@ -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); } diff --git a/client/package.json b/client/package.json index 8940462..b695331 100644 --- a/client/package.json +++ b/client/package.json @@ -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": { diff --git a/client/tests/run_tests.py b/client/tests/run_tests.py index 2cff216..7ad511e 100644 --- a/client/tests/run_tests.py +++ b/client/tests/run_tests.py @@ -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() \ No newline at end of file diff --git a/client/tests/scripts/test_post.js b/client/tests/scripts/test_post.js new file mode 100644 index 0000000..27172d0 --- /dev/null +++ b/client/tests/scripts/test_post.js @@ -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"); +} \ No newline at end of file