Use URLComponents to parse out scheme and host if possible

This commit is contained in:
Angelo Stavrow 2021-02-01 17:03:40 -05:00
parent c00b71cd43
commit 1a8951a3e0
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 16 additions and 9 deletions

View File

@ -58,15 +58,22 @@ struct AccountLoginView: View {
#if os(iOS)
hideKeyboard()
#endif
// If logging in to Write.as, trim any path in the hostname.
if server.hasPrefix("https://write.as") ||
server.hasPrefix("http://write.as") ||
server.hasPrefix("write.as") {
server = "https://write.as"
// If the server string is not prefixed with a scheme, prepend "https://" to it.
if !(server.hasPrefix("https://") || server.hasPrefix("http://")) {
server = "https://\(server)"
}
// Trim any trailing slashes that would cause the request to fail.
if server.hasSuffix("/") {
server = String(server.dropLast(1))
print(server)
// We only need the protocol and host from the URL, so drop anything else.
let url = URLComponents(string: server)
if let validURL = url {
let scheme = validURL.scheme
let host = validURL.host
var hostURL = URLComponents()
hostURL.scheme = scheme
hostURL.host = host
server = hostURL.string ?? server
} else {
// TODO: - throw an error if this is an invalid URL.
}
model.login(
to: URL(string: server)!,

View File

@ -268,8 +268,8 @@
1756AE7F24CB841200FD7257 /* Extensions */ = {
isa = PBXGroup;
children = (
17C42E6F250AA12200072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift */,
17480CA4251272EE00EB7765 /* Bundle+AppVersion.swift */,
17C42E6F250AA12200072984 /* NSManagedObjectContext+ExecuteAndMergeChanges.swift */,
17B37C5525C8679800FE75E9 /* WriteFreelyModel+API.swift */,
17B37C5C25C8698900FE75E9 /* WriteFreelyModel+APIHandlers.swift */,
17B37C4A25C8661300FE75E9 /* WriteFreelyModel+Keychain.swift */,