What is the appropriate way to split apart a host and port from a "server-based naming authority?" This is a string that can be in the form "host:port" or just "host". This is the type of string in URL.Host and http.Request.Host.
net.SplitHostPort() doesn't work because it requires the port to be present. I'd be fine if there was some way to add a default port if no port was present, but I don't find a function to do that either.
You can see the behavior of SplitHostPort in:
This doesn't seem all that strange of a need; any protocol that uses URLs would need something like this in order to be able to Dial(). Given that the following code is in net/http, I guess I'm expected to implement the handling myself. Should I file an issue?
// canonicalAddr returns url.Host but always with a ":port" suffix
func canonicalAddr(url *url.URL) string {
addr := url.Host
if !hasPort(addr) {
return addr + ":" + portMap[url.Scheme]
}
return addr
}
// Given a string of the form "host", "host:port", or "[ipv6::address]:port",
// return true if the string includes a port.
func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }