@@ -659,7 +659,8 @@ extension _ProtocolClient: URLProtocolClient {
659
659
guard let session = task. session as? URLSession else { fatalError ( " session cannot be nil " ) }
660
660
guard let response = task. response as? HTTPURLResponse else { fatalError ( " No response " ) }
661
661
662
- if response. statusCode == 401 {
662
+
663
+ if response. statusCode == 401 , `protocol`. containsTaskDelegate ( ) {
663
664
// Concat protection space from header with all possibles protection spaces
664
665
if !task. protectionSpacesInited { // init protection spaces
665
666
var allPossibleProtectionSpaces = AuthProtectionSpace . createAllPossible ( using: response)
@@ -683,6 +684,7 @@ extension _ProtocolClient: URLProtocolClient {
683
684
return
684
685
}
685
686
}
687
+
686
688
687
689
switch session. behaviour ( for: task) {
688
690
case . taskDelegate( let delegate) :
@@ -746,7 +748,7 @@ extension _ProtocolClient: URLProtocolClient {
746
748
}
747
749
let certificateErrors = [ NSURLErrorServerCertificateUntrusted, NSURLErrorServerCertificateWrongHost]
748
750
749
- if certificateErrors. contains ( error. _code) {
751
+ if certificateErrors. contains ( error. _code) && `protocol` . containsTaskDelegate ( ) {
750
752
let protectionSpace = URLProtectionSpace ( host: " " ,
751
753
port: 443 ,
752
754
protocol: " https " ,
@@ -763,10 +765,9 @@ extension _ProtocolClient: URLProtocolClient {
763
765
764
766
task. previousFailureCount += 1
765
767
urlProtocol ( `protocol`, didReceive: authenticationChallenge)
768
+ return
766
769
}
767
- return
768
770
}
769
-
770
771
urlProtocol ( task: task, didFailWithError: error)
771
772
}
772
773
@@ -838,6 +839,21 @@ private extension URLSessionTask {
838
839
}
839
840
}
840
841
842
+ fileprivate extension URLProtocol {
843
+ func containsTaskDelegate( ) -> Bool {
844
+ guard let task = self . task else { return false }
845
+ guard let session = task. session as? URLSession else { return false }
846
+
847
+ switch session. behaviour ( for: task) {
848
+ case . taskDelegate( _) :
849
+ return true
850
+
851
+ default :
852
+ return false
853
+ }
854
+ }
855
+ }
856
+
841
857
extension URLProtocol {
842
858
enum _PropertyKey : String {
843
859
case responseData
0 commit comments