Skip to content

Commit

Permalink
Auto-remove trailing slash and other unwanted errors from Nightscout URL
Browse files Browse the repository at this point in the history
When users enter a trailing / in their Nightscout URL (Eg: "https://url/" rather than "https://url"), this will immediately result in a "Not found!" (404) error, because a / is already being appended. (Resulting in "https://url//").

This check removes the trailing slash, capital characters, symbols, etc. when the user enters it in to the URL field, to prevent 404's.

Commit inspired by both my own as well as @bjorkert's commit to LoopFollow. Might not be the cleanest way, but it works.
  • Loading branch information
LiroyvH committed May 14, 2024
1 parent f23b38a commit a5c488c
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ extension NightscoutConfig {
}

func connect() {
if let CheckURL = url.last, CheckURL == "/" {
let fixedURL = url.dropLast()
.replacingOccurrences(of: "[^A-Za-z0-9:/._-]", with: "", options: .regularExpression)
.lowercased()
url = String(fixedURL)
} else {
let fixedURL = url.replacingOccurrences(of: "[^A-Za-z0-9:/._-]", with: "", options: .regularExpression)
.lowercased()
url = String(fixedURL)
}
guard let url = URL(string: url) else {
message = "Invalid URL"
return
Expand Down

0 comments on commit a5c488c

Please sign in to comment.