1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(unstable): better error for invalid hostname in Deno.serve() (#15529)

This commit is contained in:
Bartek Iwańczuk 2022-08-21 21:15:52 +02:00 committed by GitHub
parent 5ea51702bd
commit 301f6c46ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View file

@ -36,6 +36,18 @@ function onListen<T>(
};
}
Deno.test(async function httpServerInvalidHostname() {
assertThrows(
() =>
Deno.serve({
fetch: (_req) => new Response("ok"),
hostname: "localhost",
}),
TypeError,
"hostname could not be parsed as an IP address",
);
});
Deno.test({ permissions: { net: true } }, async function httpServerBasic() {
const ac = new AbortController();
const promise = deferred();

View file

@ -1244,7 +1244,11 @@ where
state
.borrow_mut::<P>()
.check_net(&(&opts.hostname, Some(opts.port)))?;
let addr = SocketAddr::new(opts.hostname.parse()?, opts.port);
let parsed_hostname = opts
.hostname
.parse()
.map_err(|_| type_error("hostname could not be parsed as an IP address"))?;
let addr = SocketAddr::new(parsed_hostname, opts.port);
let (tx, rx) = mpsc::channel(100);
let (close_tx, close_rx) = mpsc::channel(1);
let (listening_tx, listening_rx) = mpsc::channel(1);