Sfoglia il codice sorgente

Merge pull request #985 from zhongwuzw/fix-invalid-status-code-cancellation

Cancel download task when response status code invalid
Wei Wang 7 anni fa
parent
commit
c8c5baef2f
1 ha cambiato i file con 4 aggiunte e 1 eliminazioni
  1. 4 1
      Sources/ImageDownloader.swift

+ 4 - 1
Sources/ImageDownloader.swift

@@ -490,6 +490,8 @@ final class ImageDownloaderSessionHandler: NSObject, URLSessionDataDelegate, Aut
             return
         }
         
+        var disposition = URLSession.ResponseDisposition.allow
+        
         if let statusCode = (response as? HTTPURLResponse)?.statusCode,
            let url = dataTask.originalRequest?.url,
             !(downloader.delegate ?? downloader).isValidStatusCode(statusCode, for: downloader)
@@ -504,9 +506,10 @@ final class ImageDownloaderSessionHandler: NSObject, URLSessionDataDelegate, Aut
             }
             
             callCompletionHandlerFailure(error: error, url: url)
+            disposition = .cancel
         }
         
-        completionHandler(.allow)
+        completionHandler(disposition)
     }
     
     func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {