Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(HTTP Server): can't set custom statusText #10266

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
33 changes: 33 additions & 0 deletions src/bun.js/api/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,39 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp
}
}

fn writeHeaders(
this: *RequestContext,
headers: *JSC.FetchHeaders,
) void {
ctxLog("writeHeaders", .{});
headers.fastRemove(.ContentLength);
headers.fastRemove(.TransferEncoding);
if (!ssl_enabled) headers.fastRemove(.StrictTransportSecurity);
if (this.resp) |resp| {
headers.toUWSResponse(ssl_enabled, resp);
}
}

pub fn writeStatus(this: *RequestContext, status: u16) void {
var status_text_buf: [48]u8 = undefined;
assert(!this.flags.has_written_status);
this.flags.has_written_status = true;

if (this.resp) |resp| {
var response: *JSC.WebCore.Response = this.response_ptr.?;
const status_text = response.statusText();

if (status_text.length() != 0) {
resp.writeStatus(std.fmt.bufPrint(&status_text_buf, "{d} {s}", .{ status, status_text.byteSlice() }) catch unreachable);
} else if (HTTPStatusText.get(status)) |text| {
resp.writeStatus(text);
} else {
resp.writeStatus(std.fmt.bufPrint(&status_text_buf, "{d} HM", .{status}) catch unreachable);
}
}
}


pub fn endSendFile(this: *RequestContext, writeOffSet: usize, closeConnection: bool) void {
if (this.resp) |resp| {
defer this.deref();
Expand Down
4 changes: 4 additions & 0 deletions src/bun.js/webcore/response.zig
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ pub const Response = struct {
return this.init.status_code;
}

pub inline fn statusText(this: *const Response) bun.String {
return this.init.status_text;
}

pub fn redirectLocation(this: *const Response) ?[]const u8 {
return this.header(.Location);
}
Expand Down
39 changes: 38 additions & 1 deletion test/js/bun/http/bun-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,44 @@ import { bunEnv, bunExe, rejectUnauthorizedScope } from "harness";
import path from "path";

describe("Server", () => {
test("normlizes incoming request URLs", async () => {
test("should set a custom statusText", async () => {
const server = Bun.serve({
fetch(request) {
return new Response(request.url, {
headers: {
"Connection": "close",
},
statusText: "customStatusText",
});
},
port: 0,
});

const response = await fetch(server.url);
expect(response.statusText).toBe("customStatusText");

server.stop(true);
});

test("should set the default statusText if not statusText is defined", async () => {
const server = Bun.serve({
fetch(request) {
return new Response(request.url, {
headers: {
"Connection": "close",
},
});
},
port: 0,
});

const response = await fetch(server.url);
expect(response.statusText).toBe("OK");

server.stop(true);
});

test("normalizes incoming request URLs", async () => {
using server = Bun.serve({
fetch(request) {
return new Response(request.url, {
Expand Down
36 changes: 36 additions & 0 deletions test/js/node/http/node-http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,42 @@ function listen(server: Server, protocol: string = "http"): Promise<URL> {

describe("node:http", () => {
describe("createServer", async () => {
it("should set a custom statusText", async () => {
try {
var server = createServer((req, res) => {
res.writeHead(200, "customStatusText");

res.end("Hello World");
});
const url = await listen(server);
const res = await fetch(new URL(url));

expect(res.statusText).toBe("customStatusText");
} catch (e) {
throw e;
} finally {
server.close();
}
});

it("should set the default statusText if not custom statusText is defined", async () => {
try {
var server = createServer((req, res) => {
res.writeHead(200);

res.end("Hello World");
});
const url = await listen(server);
const res = await fetch(new URL(url));

expect(res.statusText).toBe("OK");
} catch (e) {
throw e;
} finally {
server.close();
}
});

it("hello world", async () => {
try {
var server = createServer((req, res) => {
Expand Down