From ede43bb368c099e29805f09edd0f9974d62005c9 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 26 May 2024 22:18:06 -0700 Subject: [PATCH] Fix a few bugs. --- Web/Sources/Web/DownloadSession.swift | 28 +++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Web/Sources/Web/DownloadSession.swift b/Web/Sources/Web/DownloadSession.swift index 900642357..e5c176a3a 100755 --- a/Web/Sources/Web/DownloadSession.swift +++ b/Web/Sources/Web/DownloadSession.swift @@ -107,7 +107,10 @@ extension DownloadSession: URLSessionTaskDelegate { 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) } } @@ -146,10 +149,7 @@ extension DownloadSession: URLSessionDataDelegate { if let identifier { delegate?.downloadSession(self, didReceiveNotModifiedResponse: response, identifier: identifier) } - - completionHandler(.cancel) - removeTask(dataTask) - + completionHandler(.allow) return } @@ -158,10 +158,7 @@ extension DownloadSession: URLSessionDataDelegate { if let identifier { delegate?.downloadSession(self, didReceiveUnexpectedResponse: response, identifier: identifier) } - completionHandler(.cancel) - removeTask(dataTask) - return } @@ -195,12 +192,15 @@ private extension DownloadSession { func addDataTask(_ identifier: String) { + downloadProgress.addTask() + guard tasksPending.count < 500 else { queue.insert(identifier, at: 0) return } guard let request = delegate?.downloadSession(self, requestForIdentifier: identifier) else { + downloadProgress.completeTask() return } @@ -251,9 +251,8 @@ private extension DownloadSession { downloadProgress.completeTask() updateDownloadProgress() - if tasksInProgress.count + tasksPending.count < 1 { - assert(allIdentifiers.isEmpty) - assert(queue.isEmpty) + if tasksInProgress.count + tasksPending.count + queue.count < 1 { // Finished? + allIdentifiers = Set() delegate?.downloadSessionDidComplete(self) clearDownloadProgress() } @@ -269,14 +268,15 @@ private extension DownloadSession { downloadProgress = DownloadProgress(numberOfTasks: 0) } + static let badRedirectStrings = ["solutionip", "lodgenet", "monzoon", "landingpage", "btopenzone", "register", "login", "authentic"] + func urlStringIsDisallowedRedirect(_ urlString: String) -> Bool { // Hotels and similar often do permanent redirects. We can catch some of those. let s = urlString.lowercased() - let badStrings = ["solutionip", "lodgenet", "monzoon", "landingpage", "btopenzone", "register", "login", "authentic"] - - for oneBadString in badStrings { + + for oneBadString in Self.badRedirectStrings { if s.contains(oneBadString) { return true }