Skip to content

Commit c520710

Browse files
Use AuthChallenge if delegate is presented
1 parent c577fda commit c520710

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

Foundation/URLSession/URLSessionTask.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ extension _ProtocolClient: URLProtocolClient {
650650
}
651651
}
652652
}
653-
default: return
653+
default: return
654654
}
655655
}
656656

@@ -659,7 +659,8 @@ extension _ProtocolClient: URLProtocolClient {
659659
guard let session = task.session as? URLSession else { fatalError("session cannot be nil") }
660660
guard let response = task.response as? HTTPURLResponse else { fatalError("No response") }
661661

662-
if response.statusCode == 401 {
662+
663+
if response.statusCode == 401, `protocol`.containsTaskDelegate() {
663664
// Concat protection space from header with all possibles protection spaces
664665
if !task.protectionSpacesInited { // init protection spaces
665666
var allPossibleProtectionSpaces = AuthProtectionSpace.createAllPossible(using: response)
@@ -683,6 +684,7 @@ extension _ProtocolClient: URLProtocolClient {
683684
return
684685
}
685686
}
687+
686688

687689
switch session.behaviour(for: task) {
688690
case .taskDelegate(let delegate):
@@ -746,7 +748,7 @@ extension _ProtocolClient: URLProtocolClient {
746748
}
747749
let certificateErrors = [NSURLErrorServerCertificateUntrusted, NSURLErrorServerCertificateWrongHost]
748750

749-
if certificateErrors.contains(error._code) {
751+
if certificateErrors.contains(error._code) && `protocol`.containsTaskDelegate() {
750752
let protectionSpace = URLProtectionSpace(host: "",
751753
port: 443,
752754
protocol: "https",
@@ -763,10 +765,9 @@ extension _ProtocolClient: URLProtocolClient {
763765

764766
task.previousFailureCount += 1
765767
urlProtocol(`protocol`, didReceive: authenticationChallenge)
768+
return
766769
}
767-
return
768770
}
769-
770771
urlProtocol(task: task, didFailWithError: error)
771772
}
772773

@@ -838,6 +839,21 @@ private extension URLSessionTask {
838839
}
839840
}
840841

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+
841857
extension URLProtocol {
842858
enum _PropertyKey: String {
843859
case responseData

0 commit comments

Comments
 (0)