Deliverable for D3.2

address.go 568B

123456789101112131415161718192021222324252627282930313233
  1. package utils
  2. import (
  3. "net/url"
  4. "strconv"
  5. "strings"
  6. )
  7. // HostAndIpToBits parses an address parsing out the host and port
  8. // Example input - http://abc.com:1234 returns { true, http, abc.com, 1234 }
  9. func HostAndIpToBits(address string) (bool, string, string, int) {
  10. url, err := url.Parse(address)
  11. if err != nil {
  12. return false, "", "", 0
  13. }
  14. bits := strings.Split(url.Host, ":")
  15. if len(bits) != 2 {
  16. return false, "", "", 0
  17. }
  18. port, err := strconv.Atoi(bits[1])
  19. if err != nil {
  20. return false, "", "", 0
  21. }
  22. return true, url.Scheme, bits[0], port
  23. }