ParseURI parses a connection URI and returns the connection type, host/path, and parameters.
(uri string)
| 92 | |
| 93 | // ParseURI parses a connection URI and returns the connection type, host/path, and parameters. |
| 94 | func ParseURI(uri string) (*Connection, error) { |
| 95 | isWshShorthand := strings.HasPrefix(uri, "//") |
| 96 | split := strings.SplitN(uri, "://", 2) |
| 97 | var scheme string |
| 98 | var rest string |
| 99 | if isWshShorthand { |
| 100 | rest = strings.TrimPrefix(uri, "//") |
| 101 | } else if len(split) > 1 { |
| 102 | scheme = split[0] |
| 103 | rest = strings.TrimPrefix(split[1], "//") |
| 104 | } else { |
| 105 | rest = split[0] |
| 106 | } |
| 107 | |
| 108 | var host string |
| 109 | var remotePath string |
| 110 | |
| 111 | parseGenericPath := func() { |
| 112 | split = strings.SplitN(rest, "/", 2) |
| 113 | host = split[0] |
| 114 | if len(split) > 1 && split[1] != "" { |
| 115 | remotePath = split[1] |
| 116 | } else if strings.HasSuffix(rest, "/") { |
| 117 | // preserve trailing slash |
| 118 | remotePath = "/" |
| 119 | } else { |
| 120 | remotePath = "" |
| 121 | } |
| 122 | } |
| 123 | parseWshPath := func() { |
| 124 | if strings.HasPrefix(rest, "wsl://") { |
| 125 | host = wslConnRegex.FindString(rest) |
| 126 | remotePath = strings.TrimPrefix(rest, host) |
| 127 | } else { |
| 128 | parseGenericPath() |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | addPrecedingSlash := true |
| 133 | |
| 134 | if scheme == "" { |
| 135 | scheme = ConnectionTypeWsh |
| 136 | addPrecedingSlash = false |
| 137 | if isWshShorthand { |
| 138 | parseWshPath() |
| 139 | } else if strings.HasPrefix(rest, "/~") { |
| 140 | host = wshrpc.LocalConnName |
| 141 | remotePath = rest |
| 142 | } else { |
| 143 | host = ConnHostCurrent |
| 144 | remotePath = rest |
| 145 | } |
| 146 | } else if scheme == ConnectionTypeWsh { |
| 147 | parseWshPath() |
| 148 | } else { |
| 149 | parseGenericPath() |
| 150 | } |
| 151 |
no outgoing calls