1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

fix(unstable): Deno.serve() can parse hostnames (#15579)

This commit is contained in:
Bartek Iwańczuk 2022-08-24 16:38:51 +02:00 committed by GitHub
parent 5268fa0e0f
commit a4cc09447e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 15 deletions

View file

@ -34,16 +34,27 @@ function onListen<T>(
};
}
Deno.test(async function httpServerInvalidHostname() {
await assertRejects(
() =>
Deno.serve({
handler: (_req) => new Response("ok"),
hostname: "localhost",
}),
TypeError,
"hostname could not be parsed as an IP address",
);
Deno.test(async function httpServerCanResolveHostnames() {
const ac = new AbortController();
const listeningPromise = deferred();
const server = Deno.serve({
handler: (_req) => new Response("ok"),
hostname: "localhost",
port: 4501,
signal: ac.signal,
onListen: onListen(listeningPromise),
onError: createOnErrorCb(ac),
});
await listeningPromise;
const resp = await fetch("http://localhost:4501/", {
headers: { "connection": "close" },
});
const text = await resp.text();
assertEquals(text, "ok");
ac.abort();
await server;
});
Deno.test({ permissions: { net: true } }, async function httpServerBasic() {

View file

@ -4,6 +4,7 @@
// https://github.com/rust-lang/rust-clippy/issues/6446
#![allow(clippy::await_holding_lock)]
use deno_core::error::generic_error;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op;
@ -47,6 +48,7 @@ use std::io::Write;
use std::marker::PhantomPinned;
use std::mem::replace;
use std::net::SocketAddr;
use std::net::ToSocketAddrs;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::Arc;
@ -1232,6 +1234,28 @@ fn run_server(
Ok(())
}
fn make_addr_port_pair(hostname: &str, port: u16) -> (&str, u16) {
// Default to localhost if given just the port. Example: ":80"
if hostname.is_empty() {
return ("0.0.0.0", port);
}
// If this looks like an ipv6 IP address. Example: "[2001:db8::1]"
// Then we remove the brackets.
let addr = hostname.trim_start_matches('[').trim_end_matches(']');
(addr, port)
}
/// Resolve network address *synchronously*.
pub fn resolve_addr_sync(
hostname: &str,
port: u16,
) -> Result<impl Iterator<Item = SocketAddr>, AnyError> {
let addr_port_pair = make_addr_port_pair(hostname, port);
let result = addr_port_pair.to_socket_addrs()?;
Ok(result)
}
#[op]
fn op_flash_serve<P>(
state: &mut OpState,
@ -1244,11 +1268,10 @@ where
state
.borrow_mut::<P>()
.check_net(&(&opts.hostname, Some(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 addr = resolve_addr_sync(&opts.hostname, opts.port)?
.next()
.ok_or_else(|| generic_error("No resolved address found"))?;
let (tx, rx) = mpsc::channel(100);
let (close_tx, close_rx) = mpsc::channel(1);
let (listening_tx, listening_rx) = mpsc::channel(1);