parseServerURL parses a server URL string into a Server struct. It handles scheme defaults and port defaults. Does not validate websocket consistency. Returns the parsed Server and whether it's a websocket URL.
(sURL string, implicit, saveTLSName bool)
| 2060 | // It handles scheme defaults and port defaults. Does not validate websocket consistency. |
| 2061 | // Returns the parsed Server and whether it's a websocket URL. |
| 2062 | func (nc *Conn) parseServerURL(sURL string, implicit, saveTLSName bool) (*Server, error) { |
| 2063 | if !strings.Contains(sURL, "://") { |
| 2064 | sURL = fmt.Sprintf("%s://%s", nc.connScheme(), sURL) |
| 2065 | } |
| 2066 | var ( |
| 2067 | u *url.URL |
| 2068 | err error |
| 2069 | ) |
| 2070 | for i := 0; i < 2; i++ { |
| 2071 | u, err = url.Parse(sURL) |
| 2072 | if err != nil { |
| 2073 | return nil, err |
| 2074 | } |
| 2075 | if u.Port() != "" { |
| 2076 | break |
| 2077 | } |
| 2078 | // In case given URL is of the form "localhost:", just add |
| 2079 | // the port number at the end, otherwise, add ":4222". |
| 2080 | if sURL[len(sURL)-1] != ':' { |
| 2081 | sURL += ":" |
| 2082 | } |
| 2083 | switch u.Scheme { |
| 2084 | case wsScheme: |
| 2085 | sURL += defaultWSPortString |
| 2086 | case wsSchemeTLS: |
| 2087 | sURL += defaultWSSPortString |
| 2088 | default: |
| 2089 | sURL += defaultPortString |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | var tlsName string |
| 2094 | if implicit { |
| 2095 | curl := nc.current.URL |
| 2096 | // Check to see if we do not have a url.User but current connected |
| 2097 | // url does. If so copy over. |
| 2098 | if u.User == nil && curl.User != nil { |
| 2099 | u.User = curl.User |
| 2100 | } |
| 2101 | // We are checking to see if we have a secure connection and are |
| 2102 | // adding an implicit server that just has an IP. If so we will remember |
| 2103 | // the current hostname we are connected to. |
| 2104 | if saveTLSName && hostIsIP(u) { |
| 2105 | tlsName = curl.Hostname() |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | s := &Server{URL: u, isImplicit: implicit, tlsName: tlsName} |
| 2110 | return s, nil |
| 2111 | } |
| 2112 | |
| 2113 | // addURLToPool adds an entry to the server pool |
| 2114 | func (nc *Conn) addURLToPool(sURL string, implicit, saveTLSName bool) error { |
no test coverage detected