From b9e359ba835e11f0d0c533e0b321a23fd3493a7e Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 31 Dec 2019 00:13:09 -0500 Subject: [PATCH] Fix custom instance domains not being parsed correctly --- .../InstanceSelectorTableViewController.swift | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift b/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift index 468ce836..572e2b2f 100644 --- a/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift +++ b/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift @@ -74,13 +74,37 @@ class InstanceSelectorTableViewController: UITableViewController { loadRecommendedInstances() } - private func updateSpecificInstance(domain: String) { - var components = URLComponents(string: domain)! + private func parseURLComponents(input: String) -> URLComponents { + // 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" { 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 request = client.getInstance() client.run(request) { (response) in