aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar dave caruso <me@paperdave.net> 2023-07-02 23:24:15 -0400
committerGravatar GitHub <noreply@github.com> 2023-07-02 20:24:15 -0700
commitc21fadf9bcc939dcf7d949cda86c974481b5f609 (patch)
tree8506f5b8b96ae6ce3d6721929f5a44b560c187c4
parent0db31c2b435ab7cd4d09810c2a2b22969c7366c5 (diff)
downloadbun-c21fadf9bcc939dcf7d949cda86c974481b5f609.tar.gz
bun-c21fadf9bcc939dcf7d949cda86c974481b5f609.tar.zst
bun-c21fadf9bcc939dcf7d949cda86c974481b5f609.zip
set content-length 0 in some cases (#3503)
-rw-r--r--src/http_client_async.zig3
-rw-r--r--test/js/web/fetch/fetch.test.ts19
2 files changed, 20 insertions, 2 deletions
diff --git a/src/http_client_async.zig b/src/http_client_async.zig
index a1962a8b9..2cb534ed2 100644
--- a/src/http_client_async.zig
+++ b/src/http_client_async.zig
@@ -1629,7 +1629,6 @@ pub fn buildRequest(this: *HTTPClient, body_len: usize) picohttp.Request {
var override_accept_encoding = false;
var override_accept_header = false;
var override_host_header = false;
-
var override_user_agent = false;
for (header_names, 0..) |head, i| {
@@ -1710,7 +1709,7 @@ pub fn buildRequest(this: *HTTPClient, body_len: usize) picohttp.Request {
header_count += 1;
}
- if (body_len > 0) {
+ if (body_len > 0 or this.method.hasRequestBody()) {
request_headers_buf[header_count] = .{
.name = content_length_header_name,
.value = std.fmt.bufPrint(&this.request_content_len_buf, "{d}", .{body_len}) catch "0",
diff --git a/test/js/web/fetch/fetch.test.ts b/test/js/web/fetch/fetch.test.ts
index 4d529b231..6b87cc613 100644
--- a/test/js/web/fetch/fetch.test.ts
+++ b/test/js/web/fetch/fetch.test.ts
@@ -387,6 +387,25 @@ describe("fetch", () => {
}).toThrow("fetch() request with GET/HEAD/OPTIONS method cannot have body.");
}),
);
+
+ it("content length is inferred", async () => {
+ startServer({
+ fetch(req) {
+ return new Response(req.headers.get("content-length"));
+ },
+ hostname: "localhost",
+ });
+
+ // POST with body
+ const url = `http://${server.hostname}:${server.port}`;
+ const response = await fetch(url, { method: "POST", body: "buntastic" });
+ expect(response.status).toBe(200);
+ expect(await response.text()).toBe("9");
+
+ const response2 = await fetch(url, { method: "POST", body: "" });
+ expect(response2.status).toBe(200);
+ expect(await response2.text()).toBe("0");
+ });
});
it("simultaneous HTTPS fetch", async () => {