addr.go 703 B

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