Repository: grpc/grpc-go
File: internal/resolver/delegatingresolver/delegatingresolver.go (Go)

Code before:
		for _, addr := range endpoint.Addresses {
			if !skipProxy(addr) {
				return true
			}
		}
	}
	return false
}

func maybeAddDefaultPort(target, defaultPort string) (string, error) {
	if target == "" {
		return "", fmt.Errorf("missing address")
	}
	if host, port, err := net.SplitHostPort(target); err == nil {
		if port == "" {
			// If the port field is empty (target ends with colon), e.g. "[::1]:",
			// this is an error.
			return "", fmt.Errorf("missing port after port-separator colon")
		}
		// target has port, i.e ipv4-host:port, [ipv6-host]:port, host-name:port
		if host == "" {
			// Keep consistent with net.Dial(): If the host is empty, as in ":80",
			// the local system is assumed.
			host = "localhost"
		}
		return fmt.Sprintf("%s:%s", host, port), nil
	}
	if host, port, err := net.SplitHostPort(target + ":" + defaultPort); err == nil {
		// target doesn't have port
		return fmt.Sprintf("%s:%s", host, port), nil
	}
	return "", fmt.Errorf("invalid target address %v", target)
}

func skipProxy(address resolver.Address) bool {
	// Avoid proxy when network is not tcp.
	networkType, ok := networktype.Get(address)
	if !ok {
		networkType, _ = transport.ParseDialTarget(address.Addr)
	}
	if networkType != "tcp" {
		return true
	}

	req := &http.Request{URL: &url.URL{
		Scheme: "https",
		Host:   address.Addr,
	}}
	// Avoid proxy when address included in `NO_PROXY` environment variable or
	// fails to get the proxy address.
	url, err := HTTPSProxyFromEnvironment(req)

Review comment (suggestion):
We should use [net.JoinHostPort](https://pkg.go.dev/net#JoinHostPort) to properly format this string. In case of IPv6 addresses for example, the resulting string may be `[%s]:%s` instead.

Code after:
		if !skipProxy(addr) {
			return true
		}
	}
	for _, endpoint := range state.Endpoints {
		for _, addr := range endpoint.Addresses {
			if !skipProxy(addr) {
				return true
			}
		}
	}
	return false
}

// parseTarget takes a target string and ensures it is a valid "host:port" target.
//
// It does the following:
//  1. If the target already has a port (e.g., "host:port", "[ipv6]:port"),
//     it is returned as is.
//  2. If the host part is empty (e.g., ":80"), it defaults to "localhost",
//     returning "localhost:80".
//  3. If the target is missing a port (e.g., "host", "ipv6"), the defaultPort
//     is added.
//
// An error is returned for empty targets or targets with a trailing colon
// but no port (e.g., "host:").
func parseTarget(target string) (string, error) {
	if target == "" {
		return "", fmt.Errorf("missing address")
	}

	host, port, err := net.SplitHostPort(target)
	if err != nil {
		// If SplitHostPort fails, it's likely because the port is missing.
		// We append the default port and return the result.
		return net.JoinHostPort(target, defaultPort), nil
	}

	// If SplitHostPort succeeds, we check for edge cases.
	if port == "" {
		// A success with an empty port means the target had a trailing colon,
		// e.g., "host:", which is an error.
		return "", fmt.Errorf("missing port after port-separator colon")
	}
	if host == "" {
		// A success with an empty host means the target was like ":80".
		// We default the host to "localhost".
		host = "localhost"
	}
	return net.JoinHostPort(host, port), nil
}