Repository: nushell/nushell
File: crates/nu-command/src/network/http/resolver.rs (Rust)

Code before:
use ureq::{
    config::Config,
    unversioned::resolver::{ArrayVec, ResolvedSocketAddrs, Resolver},
    unversioned::transport::NextTimeout,
};

#[derive(Debug)]
pub struct DnsLookupResolver;

impl Resolver for DnsLookupResolver {
    fn resolve(
        &self,
        uri: &Uri,
        config: &Config,
        _timeout: NextTimeout,
    ) -> Result<ResolvedSocketAddrs, ureq::Error> {
        let host = uri.host();
        // Determine the port: use explicit port if provided, otherwise derive from scheme
        let port = uri
            .port_u16()
            .or_else(|| match uri.scheme_str() {
                Some("https") => Some(443),
                Some("http") => Some(80),
                _ => None,
            })
            .unwrap_or(80);

        // Pass None as service to avoid "Service not supported for this socket type" errors
        // in certain environments (e.g., Docker containers on some Linux distributions).
        // We'll set the port manually on each resolved address.
        let addr_info_iter = dns_lookup::getaddrinfo(host, None, None)
            .map_err(|err| ureq::Error::Other(Box::new(LookupError(err))))?;

        let ip_family = config.ip_family();
        let mut resolved = self.empty();
        let capacity = array_vec_capacity(&resolved);
        for addr_info in addr_info_iter {
            let addr_info = addr_info?;
            let sockaddr = addr_info.sockaddr;
            // Filter addresses based on configured IP family (IPv4 only, IPv6 only, or any)
            let is_wanted = match ip_family {
                ureq::config::IpFamily::Any => true,
                ureq::config::IpFamily::Ipv4Only => sockaddr.is_ipv4(),
                ureq::config::IpFamily::Ipv6Only => sockaddr.is_ipv6(),
            };
            if is_wanted {
                // Set the correct port on the resolved address
                let sockaddr_with_port = set_port(sockaddr, port);
                resolved.push(sockaddr_with_port);
                // ArrayVec has a fixed capacity, stop when full
                if resolved.len() >= capacity {

Review comment (suggestion):
We don't need this extra `unwrap_or` call, what about return number directly inside `or_else`?
```rust
            .or_else(|| match uri.scheme_str() {
                Some("https") => 443,
                Some("http") => 80,
                _ => 80,    # limited to the HTTP domain, so setting it to 80 is safe.
            })
```

Code after:
use ureq::{
    config::Config,
    unversioned::resolver::{ArrayVec, ResolvedSocketAddrs, Resolver},
    unversioned::transport::NextTimeout,
};

#[derive(Debug)]
pub struct DnsLookupResolver;

impl Resolver for DnsLookupResolver {
    fn resolve(
        &self,
        uri: &Uri,
        config: &Config,
        _timeout: NextTimeout,
    ) -> Result<ResolvedSocketAddrs, ureq::Error> {
        let host = uri.host();
        // Determine the port: use explicit port if provided, otherwise derive from scheme
        let port = uri.port_u16().unwrap_or_else(|| match uri.scheme_str() {
            Some("https") => 443,
            _ => 80, // http commands only support HTTP/HTTPS, default to port 80
        });

        // Pass None as service to avoid "Service not supported for this socket type" errors
        // in certain environments (e.g., Docker containers on some Linux distributions).
        // We'll set the port manually on each resolved address.
        let addr_info_iter = dns_lookup::getaddrinfo(host, None, None)
            .map_err(|err| ureq::Error::Other(Box::new(LookupError(err))))?;

        let ip_family = config.ip_family();
        let mut resolved = self.empty();
        let capacity = array_vec_capacity(&resolved);
        for addr_info in addr_info_iter {
            let addr_info = addr_info?;
            let sockaddr = addr_info.sockaddr;
            // Filter addresses based on configured IP family (IPv4 only, IPv6 only, or any)
            let is_wanted = match ip_family {
                ureq::config::IpFamily::Any => true,
                ureq::config::IpFamily::Ipv4Only => sockaddr.is_ipv4(),
                ureq::config::IpFamily::Ipv6Only => sockaddr.is_ipv6(),
            };
            if is_wanted {
                // Set the correct port on the resolved address
                let sockaddr_with_port = set_port(sockaddr, port);
                resolved.push(sockaddr_with_port);
                // ArrayVec has a fixed capacity, stop when full
                if resolved.len() >= capacity {
                    break;
                }
            }
        }