(host string, scheme string, fallbackPort string)
| 153 | } |
| 154 | |
| 155 | func canonicalOriginFromHost(host string, scheme string, fallbackPort string) (canonicalOrigin, bool) { |
| 156 | trimmedHost := strings.TrimSpace(host) |
| 157 | scheme = strings.ToLower(strings.TrimSpace(scheme)) |
| 158 | if trimmedHost == "" || scheme == "" { |
| 159 | return canonicalOrigin{}, false |
| 160 | } |
| 161 | |
| 162 | hostname := canonicalHost(trimmedHost) |
| 163 | port := "" |
| 164 | if parsedHost, parsedPort, err := net.SplitHostPort(trimmedHost); err == nil { |
| 165 | hostname = canonicalHost(parsedHost) |
| 166 | port = parsedPort |
| 167 | } |
| 168 | if hostname == "" { |
| 169 | return canonicalOrigin{}, false |
| 170 | } |
| 171 | |
| 172 | port = normalizePort(scheme, firstNonEmptyString(port, fallbackPort)) |
| 173 | if port == "" { |
| 174 | return canonicalOrigin{}, false |
| 175 | } |
| 176 | |
| 177 | return canonicalOrigin{ |
| 178 | scheme: scheme, |
| 179 | hostname: hostname, |
| 180 | port: port, |
| 181 | canonical: scheme + "://" + net.JoinHostPort(hostname, port), |
| 182 | loopback: isLoopbackHost(hostname), |
| 183 | wildcard: isWildcardHost(hostname), |
| 184 | }, true |
| 185 | } |
| 186 | |
| 187 | func normalizePort(scheme string, port string) string { |
| 188 | trimmed := strings.TrimSpace(port) |
no test coverage detected