Fix a few bugs.

This commit is contained in:
Brent Simmons 2024-05-26 22:18:06 -07:00
parent 0ab4eac333
commit ede43bb368

View File

@ -107,7 +107,10 @@ extension DownloadSession: URLSessionTaskDelegate {
return return
} }
delegate?.downloadSession(self, downloadDidCompleteForIdentifier: info.identifier, response: info.urlResponse, data: info.data, error: error) if let response = info.urlResponse, response.statusIsOK {
delegate?.downloadSession(self, downloadDidCompleteForIdentifier: info.identifier, response: info.urlResponse, data: info.data, error: error)
}
removeTask(task) removeTask(task)
} }
} }
@ -146,10 +149,7 @@ extension DownloadSession: URLSessionDataDelegate {
if let identifier { if let identifier {
delegate?.downloadSession(self, didReceiveNotModifiedResponse: response, identifier: identifier) delegate?.downloadSession(self, didReceiveNotModifiedResponse: response, identifier: identifier)
} }
completionHandler(.allow)
completionHandler(.cancel)
removeTask(dataTask)
return return
} }
@ -158,10 +158,7 @@ extension DownloadSession: URLSessionDataDelegate {
if let identifier { if let identifier {
delegate?.downloadSession(self, didReceiveUnexpectedResponse: response, identifier: identifier) delegate?.downloadSession(self, didReceiveUnexpectedResponse: response, identifier: identifier)
} }
completionHandler(.cancel) completionHandler(.cancel)
removeTask(dataTask)
return return
} }
@ -195,12 +192,15 @@ private extension DownloadSession {
func addDataTask(_ identifier: String) { func addDataTask(_ identifier: String) {
downloadProgress.addTask()
guard tasksPending.count < 500 else { guard tasksPending.count < 500 else {
queue.insert(identifier, at: 0) queue.insert(identifier, at: 0)
return return
} }
guard let request = delegate?.downloadSession(self, requestForIdentifier: identifier) else { guard let request = delegate?.downloadSession(self, requestForIdentifier: identifier) else {
downloadProgress.completeTask()
return return
} }
@ -251,9 +251,8 @@ private extension DownloadSession {
downloadProgress.completeTask() downloadProgress.completeTask()
updateDownloadProgress() updateDownloadProgress()
if tasksInProgress.count + tasksPending.count < 1 { if tasksInProgress.count + tasksPending.count + queue.count < 1 { // Finished?
assert(allIdentifiers.isEmpty) allIdentifiers = Set<String>()
assert(queue.isEmpty)
delegate?.downloadSessionDidComplete(self) delegate?.downloadSessionDidComplete(self)
clearDownloadProgress() clearDownloadProgress()
} }
@ -269,14 +268,15 @@ private extension DownloadSession {
downloadProgress = DownloadProgress(numberOfTasks: 0) downloadProgress = DownloadProgress(numberOfTasks: 0)
} }
static let badRedirectStrings = ["solutionip", "lodgenet", "monzoon", "landingpage", "btopenzone", "register", "login", "authentic"]
func urlStringIsDisallowedRedirect(_ urlString: String) -> Bool { func urlStringIsDisallowedRedirect(_ urlString: String) -> Bool {
// Hotels and similar often do permanent redirects. We can catch some of those. // Hotels and similar often do permanent redirects. We can catch some of those.
let s = urlString.lowercased() let s = urlString.lowercased()
let badStrings = ["solutionip", "lodgenet", "monzoon", "landingpage", "btopenzone", "register", "login", "authentic"]
for oneBadString in Self.badRedirectStrings {
for oneBadString in badStrings {
if s.contains(oneBadString) { if s.contains(oneBadString) {
return true return true
} }