Browse Source

Fix custom instance domains not being parsed correctly

multiple-accounts
Shadowfacts 1 month ago
parent
commit
b9e359ba83
Signed by: Shadowfacts <me@shadowfacts.net> GPG Key ID: 94A5AB95422746E5
1 changed files with 28 additions and 4 deletions
  1. 28
    4
      Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift

+ 28
- 4
Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift View File

@@ -74,13 +74,37 @@ class InstanceSelectorTableViewController: UITableViewController {
74 74
         loadRecommendedInstances()
75 75
     }
76 76
     
77
-    private func updateSpecificInstance(domain: String) {
78
-        var components = URLComponents(string: domain)!
77
+    private func parseURLComponents(input: String) -> URLComponents {
78
+        // we can't just use the URLComponents(string:) initializer, because when given just a domain (w/o protocol), it interprets it as the path
79
+        var input = input
80
+        var components = URLComponents()
81
+        // extract protocol
82
+        if input.contains("://") {
83
+            let parts = input.components(separatedBy: "://")
84
+            components.scheme = parts.first!
85
+            input = parts.last!
86
+        }
79 87
         if components.scheme != "https" && components.scheme != "http" {
80 88
             components.scheme = "https"
81 89
         }
82
-        components.path = "/"
83
-        
90
+        // drop path
91
+        if input.contains("/") {
92
+            let parts = input.components(separatedBy: "/")
93
+            input = parts.first!
94
+        }
95
+        // parse port
96
+        if input.contains(":") {
97
+            let parts = input.components(separatedBy: ":")
98
+            input = parts.first!
99
+            components.port = Int(parts.last!)
100
+        }
101
+        components.host = input
102
+        return components
103
+    }
104
+    
105
+    private func updateSpecificInstance(domain: String) {
106
+        let components = parseURLComponents(input: domain)
107
+
84 108
         let client = Client(baseURL: components.url!)
85 109
         let request = client.getInstance()
86 110
         client.run(request) { (response) in

Loading…
Cancel
Save