1 package challenge 2 3 import ( 4 "net/url" 5 "strings" 6 ) 7 8 // FROM: https://golang.org/src/net/http/http.go 9 // Given a string of the form "host", "host:port", or "[ipv6::address]:port", 10 // return true if the string includes a port. 11 func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") } 12 13 // FROM: http://golang.org/src/net/http/transport.go 14 var portMap = map[string]string{ 15 "http": "80", 16 "https": "443", 17 } 18 19 // canonicalAddr returns url.Host but always with a ":port" suffix 20 // FROM: http://golang.org/src/net/http/transport.go 21 func canonicalAddr(url *url.URL) string { 22 addr := url.Host 23 if !hasPort(addr) { 24 return addr + ":" + portMap[url.Scheme] 25 } 26 return addr 27 } 28