Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when connection URL contains invalid scheme #1277

Merged
merged 2 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 1 addition & 16 deletions Sources/Shared/API/ConnectionInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,22 +407,7 @@ public class ConnectionInfo: Codable {

/// Rewrites the given URL to ensure that it points to the active API URL.
public func adaptAPIURL(_ existingURL: URL) -> URL? {
guard var components = URLComponents(url: existingURL, resolvingAgainstBaseURL: false) else { return nil }

components.scheme = self.activeURL.scheme
components.host = self.activeURL.host
components.port = self.activeURL.port

return components.url
}

/// Check if the provided URL uses the active URL.
public func ensureURL(_ url: URL, _ apiURL: Bool = false) -> Bool {
if apiURL {
return url.scheme == self.activeAPIURL.scheme && url.host == self.activeAPIURL.host &&
url.port == self.activeAPIURL.port
}
return url == self.webhookURL
activeURL.adapting(url: existingURL)
}

// MARK: - RequestAdapter
Expand Down
17 changes: 17 additions & 0 deletions Sources/Shared/Common/Extensions/URL+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,21 @@ extension URL {
&& user == otherURL.user
&& password == otherURL.password
}

func adapting(url: URL) -> URL {
guard
let components = URLComponents(url: self, resolvingAgainstBaseURL: false),
var futureComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
else {
return url
}

futureComponents.host = components.host
futureComponents.port = components.port
futureComponents.scheme = components.scheme
futureComponents.user = components.user
futureComponents.password = components.password

return futureComponents.url ?? url
}
}