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(ext/http): do not set localhost to hostname unnecessarily #24777

Merged
19 changes: 9 additions & 10 deletions ext/http/00_serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,22 +668,21 @@ function serve(arg1, arg2) {
}

const addr = listener.addr;
// If the hostname is "0.0.0.0", we display "localhost" in console
// because browsers in Windows don't resolve "0.0.0.0".
// See the discussion in https://github.com/denoland/deno_std/issues/1165
const hostname = (addr.hostname == "0.0.0.0" || addr.hostname == "::") &&
(Deno.build.os === "windows")
? "localhost"
: addr.hostname;
addr.hostname = hostname;

const onListen = (scheme) => {
if (options.onListen) {
options.onListen(addr);
} else {
const host = StringPrototypeIncludes(addr.hostname, ":")
? `[${addr.hostname}]`
// If the hostname is "0.0.0.0", we display "localhost" in console
// because browsers in Windows don't resolve "0.0.0.0".
// See the discussion in https://github.com/denoland/deno_std/issues/1165
const hostname = (addr.hostname == "0.0.0.0" || addr.hostname == "::") &&
(Deno.build.os === "windows")
? "localhost"
: addr.hostname;
const host = StringPrototypeIncludes(hostname, ":")
? `[${hostname}]`
: hostname;
console.log(`Listening on ${scheme}${host}:${addr.port}/`);
}
};
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/serve_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4106,3 +4106,19 @@ Deno.test({
'Operation `"op_net_listen_unix"` not supported on non-unix platforms.',
);
});

Deno.test({
name: "onListen callback gets 0.0.0.0 hostname as is",
}, async () => {
const { promise, resolve } = Promise.withResolvers<{ hostname: string }>();

const server = Deno.serve({
handler: (_) => new Response("ok"),
hostname: "0.0.0.0",
port: 0,
onListen: resolve,
});
const { hostname } = await promise;
assertEquals(hostname, "0.0.0.0");
await server.shutdown();
});
Loading