Fix custom instance domains not being parsed correctly

This commit is contained in:
Shadowfacts 2019-12-31 00:13:09 -05:00
parent edb86fc503
commit b9e359ba83
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 28 additions and 4 deletions

View File

@ -74,12 +74,36 @@ class InstanceSelectorTableViewController: UITableViewController {
loadRecommendedInstances() loadRecommendedInstances()
} }
private func updateSpecificInstance(domain: String) { private func parseURLComponents(input: String) -> URLComponents {
var components = URLComponents(string: domain)! // we can't just use the URLComponents(string:) initializer, because when given just a domain (w/o protocol), it interprets it as the path
var input = input
var components = URLComponents()
// extract protocol
if input.contains("://") {
let parts = input.components(separatedBy: "://")
components.scheme = parts.first!
input = parts.last!
}
if components.scheme != "https" && components.scheme != "http" { if components.scheme != "https" && components.scheme != "http" {
components.scheme = "https" components.scheme = "https"
} }
components.path = "/" // drop path
if input.contains("/") {
let parts = input.components(separatedBy: "/")
input = parts.first!
}
// parse port
if input.contains(":") {
let parts = input.components(separatedBy: ":")
input = parts.first!
components.port = Int(parts.last!)
}
components.host = input
return components
}
private func updateSpecificInstance(domain: String) {
let components = parseURLComponents(input: domain)
let client = Client(baseURL: components.url!) let client = Client(baseURL: components.url!)
let request = client.getInstance() let request = client.getInstance()