Skip to content

Commit

Permalink
Allow disabling keep-alive (oven-sh#14569)
Browse files Browse the repository at this point in the history
Co-authored-by: Ciro Spaciari <[email protected]>
  • Loading branch information
Jarred-Sumner and cirospaciari authored Oct 14, 2024
1 parent d2fe1ce commit 5fc5335
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2665,9 +2665,13 @@ pub fn buildRequest(this: *HTTPClient, body_len: usize) picohttp.Request {
// Skip host and connection header
// we manage those
switch (hash) {
hashHeaderConst("Connection"),
hashHeaderConst("Content-Length"),
=> continue,
hashHeaderConst("Connection") => {
if (!this.flags.disable_keepalive) {
continue;
}
},
hashHeaderConst("if-modified-since") => {
if (this.flags.force_last_modified and this.if_modified_since.len == 0) {
this.if_modified_since = this.headerStr(header_values[i]);
Expand Down Expand Up @@ -2709,8 +2713,10 @@ pub fn buildRequest(this: *HTTPClient, body_len: usize) picohttp.Request {
header_count += 1;
}

request_headers_buf[header_count] = connection_header;
header_count += 1;
if (!this.flags.disable_keepalive) {
request_headers_buf[header_count] = connection_header;
header_count += 1;
}

if (!override_user_agent) {
request_headers_buf[header_count] = user_agent_header;
Expand Down
36 changes: 36 additions & 0 deletions test/js/web/fetch/fetch-keepalive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { test, expect } from "bun:test";

test("keepalive", async () => {
using server = Bun.serve({
port: 0,
async fetch(req) {
return new Response(JSON.stringify(req.headers.toJSON()));
},
});
{
const res = await fetch(`http://localhost:${server.port}`, {
keepalive: false,
});
const headers = await res.json();
expect(headers.connection).toBeUndefined();
}

{
const res = await fetch(`http://localhost:${server.port}`, {
keepalive: true,
});
const headers = await res.json();
expect(headers.connection).toBe("keep-alive");
}

{
const res = await fetch(`http://localhost:${server.port}`, {
keepalive: false,
headers: {
"Connection": "HELLO!",
},
});
const headers = await res.json();
expect(headers.connection).toBe("HELLO!");
}
});

0 comments on commit 5fc5335

Please sign in to comment.