github.com/simpleiot/simpleiot@v0.18.3/client/uri.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 // returns protocol, server, port, err 9 func parseURI(uri string) (string, string, string, error) { 10 uri = strings.Trim(uri, " ") 11 parts := strings.Split(uri, "://") 12 if len(parts) < 2 { 13 return "", "", "", fmt.Errorf("URI %v does not contain ://", uri) 14 } 15 16 proto := parts[0] 17 server := parts[1] 18 19 parts = strings.Split(server, ":") 20 21 port := "" 22 if len(parts) > 1 { 23 server = parts[0] 24 port = parts[1] 25 } 26 27 return proto, server, port, nil 28 } 29 30 func sanitizeURI(uri string) (string, error) { 31 // check if port not specified 32 proto, server, port, err := parseURI(uri) 33 if err != nil { 34 return uri, err 35 } 36 37 if port == "" { 38 switch proto { 39 case "ws": 40 port = "80" 41 case "wss": 42 port = "443" 43 default: 44 port = "4222" 45 } 46 } 47 48 return fmt.Sprintf("%v://%v:%v", proto, server, port), nil 49 }