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

refactor(runtime): return iterator from resolve_addr (#8891)

This commit is contained in:
Yusuke Tanaka 2020-12-30 10:19:28 +09:00 committed by GitHub
parent 57b0562957
commit 92a8205d26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 40 deletions

View file

@ -202,7 +202,10 @@ async fn op_datagram_send(
s.borrow::<Permissions>() s.borrow::<Permissions>()
.check_net(&args.hostname, args.port)?; .check_net(&args.hostname, args.port)?;
} }
let addr = resolve_addr(&args.hostname, args.port).await?; let addr = resolve_addr(&args.hostname, args.port)
.await?
.next()
.ok_or_else(|| generic_error("No resolved address found"))?;
let resource = state let resource = state
.borrow_mut() .borrow_mut()
@ -267,7 +270,10 @@ async fn op_connect(
.borrow::<Permissions>() .borrow::<Permissions>()
.check_net(&args.hostname, args.port)?; .check_net(&args.hostname, args.port)?;
} }
let addr = resolve_addr(&args.hostname, args.port).await?; let addr = resolve_addr(&args.hostname, args.port)
.await?
.next()
.ok_or_else(|| generic_error("No resolved address found"))?;
let tcp_stream = TcpStream::connect(&addr).await?; let tcp_stream = TcpStream::connect(&addr).await?;
let local_addr = tcp_stream.local_addr()?; let local_addr = tcp_stream.local_addr()?;
let remote_addr = tcp_stream.peer_addr()?; let remote_addr = tcp_stream.peer_addr()?;
@ -469,7 +475,9 @@ fn op_listen(
} }
permissions.check_net(&args.hostname, args.port)?; permissions.check_net(&args.hostname, args.port)?;
} }
let addr = resolve_addr_sync(&args.hostname, args.port)?; let addr = resolve_addr_sync(&args.hostname, args.port)?
.next()
.ok_or_else(|| generic_error("No resolved address found"))?;
let (rid, local_addr) = if transport == "tcp" { let (rid, local_addr) = if transport == "tcp" {
listen_tcp(state, addr)? listen_tcp(state, addr)?
} else { } else {

View file

@ -8,6 +8,7 @@ use crate::resolve_addr::resolve_addr_sync;
use deno_core::error::bad_resource; use deno_core::error::bad_resource;
use deno_core::error::bad_resource_id; use deno_core::error::bad_resource_id;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::generic_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::serde_json::json; use deno_core::serde_json::json;
@ -156,7 +157,10 @@ async fn op_connect_tls(
domain.push_str("localhost"); domain.push_str("localhost");
} }
let addr = resolve_addr(&args.hostname, args.port).await?; let addr = resolve_addr(&args.hostname, args.port)
.await?
.next()
.ok_or_else(|| generic_error("No resolved address found"))?;
let tcp_stream = TcpStream::connect(&addr).await?; let tcp_stream = TcpStream::connect(&addr).await?;
let local_addr = tcp_stream.local_addr()?; let local_addr = tcp_stream.local_addr()?;
let remote_addr = tcp_stream.peer_addr()?; let remote_addr = tcp_stream.peer_addr()?;
@ -295,7 +299,9 @@ fn op_listen_tls(
.set_single_cert(load_certs(&cert_file)?, load_keys(&key_file)?.remove(0)) .set_single_cert(load_certs(&cert_file)?, load_keys(&key_file)?.remove(0))
.expect("invalid key or certificate"); .expect("invalid key or certificate");
let tls_acceptor = TlsAcceptor::from(Arc::new(config)); let tls_acceptor = TlsAcceptor::from(Arc::new(config));
let addr = resolve_addr_sync(&args.hostname, args.port)?; let addr = resolve_addr_sync(&args.hostname, args.port)?
.next()
.ok_or_else(|| generic_error("No resolved address found"))?;
let std_listener = std::net::TcpListener::bind(&addr)?; let std_listener = std::net::TcpListener::bind(&addr)?;
let listener = TcpListener::from_std(std_listener)?; let listener = TcpListener::from_std(std_listener)?;
let local_addr = listener.local_addr()?; let local_addr = listener.local_addr()?;

View file

@ -1,7 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::error::Context;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use tokio::net::lookup_host; use tokio::net::lookup_host;
@ -10,24 +9,20 @@ use tokio::net::lookup_host;
pub async fn resolve_addr( pub async fn resolve_addr(
hostname: &str, hostname: &str,
port: u16, port: u16,
) -> Result<SocketAddr, AnyError> { ) -> Result<impl Iterator<Item = SocketAddr> + '_, AnyError> {
let addr_port_pair = make_addr_port_pair(hostname, port); let addr_port_pair = make_addr_port_pair(hostname, port);
lookup_host(addr_port_pair) let result = lookup_host(addr_port_pair).await?;
.await? Ok(result)
.next()
.context("No resolved address found")
} }
/// Resolve network address *synchronously*. /// Resolve network address *synchronously*.
pub fn resolve_addr_sync( pub fn resolve_addr_sync(
hostname: &str, hostname: &str,
port: u16, port: u16,
) -> Result<SocketAddr, AnyError> { ) -> Result<impl Iterator<Item = SocketAddr>, AnyError> {
let addr_port_pair = make_addr_port_pair(hostname, port); let addr_port_pair = make_addr_port_pair(hostname, port);
addr_port_pair let result = addr_port_pair.to_socket_addrs()?;
.to_socket_addrs()? Ok(result)
.next()
.context("No resolved address found")
} }
fn make_addr_port_pair(hostname: &str, port: u16) -> (&str, u16) { fn make_addr_port_pair(hostname: &str, port: u16) -> (&str, u16) {
@ -52,37 +47,52 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn resolve_addr1() { async fn resolve_addr1() {
let expected = let expected = vec![SocketAddr::V4(SocketAddrV4::new(
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 80)); Ipv4Addr::new(127, 0, 0, 1),
let actual = resolve_addr("127.0.0.1", 80).await.unwrap(); 80,
))];
let actual = resolve_addr("127.0.0.1", 80)
.await
.unwrap()
.collect::<Vec<_>>();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[tokio::test] #[tokio::test]
async fn resolve_addr2() { async fn resolve_addr2() {
let expected = let expected = vec![SocketAddr::V4(SocketAddrV4::new(
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 80)); Ipv4Addr::new(0, 0, 0, 0),
let actual = resolve_addr("", 80).await.unwrap(); 80,
))];
let actual = resolve_addr("", 80).await.unwrap().collect::<Vec<_>>();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[tokio::test] #[tokio::test]
async fn resolve_addr3() { async fn resolve_addr3() {
let expected = let expected = vec![SocketAddr::V4(SocketAddrV4::new(
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 0, 2, 1), 25)); Ipv4Addr::new(192, 0, 2, 1),
let actual = resolve_addr("192.0.2.1", 25).await.unwrap(); 25,
))];
let actual = resolve_addr("192.0.2.1", 25)
.await
.unwrap()
.collect::<Vec<_>>();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[tokio::test] #[tokio::test]
async fn resolve_addr_ipv6() { async fn resolve_addr_ipv6() {
let expected = SocketAddr::V6(SocketAddrV6::new( let expected = vec![SocketAddr::V6(SocketAddrV6::new(
Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1), Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
8080, 8080,
0, 0,
0, 0,
)); ))];
let actual = resolve_addr("[2001:db8::1]", 8080).await.unwrap(); let actual = resolve_addr("[2001:db8::1]", 8080)
.await
.unwrap()
.collect::<Vec<_>>();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
@ -93,37 +103,49 @@ mod tests {
#[test] #[test]
fn resolve_addr_sync1() { fn resolve_addr_sync1() {
let expected = let expected = vec![SocketAddr::V4(SocketAddrV4::new(
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 80)); Ipv4Addr::new(127, 0, 0, 1),
let actual = resolve_addr_sync("127.0.0.1", 80).unwrap(); 80,
))];
let actual = resolve_addr_sync("127.0.0.1", 80)
.unwrap()
.collect::<Vec<_>>();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[test] #[test]
fn resolve_addr_sync2() { fn resolve_addr_sync2() {
let expected = let expected = vec![SocketAddr::V4(SocketAddrV4::new(
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 80)); Ipv4Addr::new(0, 0, 0, 0),
let actual = resolve_addr_sync("", 80).unwrap(); 80,
))];
let actual = resolve_addr_sync("", 80).unwrap().collect::<Vec<_>>();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[test] #[test]
fn resolve_addr_sync3() { fn resolve_addr_sync3() {
let expected = let expected = vec![SocketAddr::V4(SocketAddrV4::new(
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 0, 2, 1), 25)); Ipv4Addr::new(192, 0, 2, 1),
let actual = resolve_addr_sync("192.0.2.1", 25).unwrap(); 25,
))];
let actual = resolve_addr_sync("192.0.2.1", 25)
.unwrap()
.collect::<Vec<_>>();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
#[test] #[test]
fn resolve_addr_sync_ipv6() { fn resolve_addr_sync_ipv6() {
let expected = SocketAddr::V6(SocketAddrV6::new( let expected = vec![SocketAddr::V6(SocketAddrV6::new(
Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1), Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1),
8080, 8080,
0, 0,
0, 0,
)); ))];
let actual = resolve_addr_sync("[2001:db8::1]", 8080).unwrap(); let actual = resolve_addr_sync("[2001:db8::1]", 8080)
.unwrap()
.collect::<Vec<_>>();
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }