2016-11-08 02:13:56 +01:00
|
|
|
package challenge
|
2016-09-01 23:46:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-09-19 23:14:07 +02:00
|
|
|
// FROM: https://golang.org/src/net/http/http.go
|
2016-09-01 23:46:31 +02:00
|
|
|
// 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, "]") }
|
|
|
|
|
|
|
|
// FROM: http://golang.org/src/net/http/transport.go
|
|
|
|
var portMap = map[string]string{
|
|
|
|
"http": "80",
|
|
|
|
"https": "443",
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:14:07 +02:00
|
|
|
// canonicalAddr returns url.Host but always with a ":port" suffix
|
2016-09-01 23:46:31 +02:00
|
|
|
// FROM: http://golang.org/src/net/http/transport.go
|
2016-09-19 23:14:07 +02:00
|
|
|
func canonicalAddr(url *url.URL) string {
|
2016-09-01 23:46:31 +02:00
|
|
|
addr := url.Host
|
|
|
|
if !hasPort(addr) {
|
|
|
|
return addr + ":" + portMap[url.Scheme]
|
|
|
|
}
|
|
|
|
return addr
|
|
|
|
}
|