@@ -115,7 +115,7 @@ open class URLSessionTask : NSObject, NSCopying {
115
115
fileprivate var _protocolStorage : ProtocolState = . toBeCreated
116
116
internal var _lastCredentialUsedFromStorageDuringAuthentication : ( protectionSpace: URLProtectionSpace , credential: URLCredential ) ?
117
117
118
- private var _protocolClass : URLProtocol . Type {
118
+ private var _protocolClass : URLProtocol . Type ? {
119
119
guard let request = currentRequest else { fatalError ( " A protocol class was requested, but we do not have a current request " ) }
120
120
let protocolClasses = session. configuration. protocolClasses ?? [ ]
121
121
if let urlProtocolClass = URLProtocol . getProtocolClass ( protocols: protocolClasses, request: request) {
@@ -128,15 +128,19 @@ open class URLSessionTask : NSObject, NSCopying {
128
128
return urlProtocol
129
129
}
130
130
}
131
-
132
- fatalError ( " Couldn't find a protocol appropriate for request: \( request) " )
131
+ return nil
133
132
}
134
133
135
134
func _getProtocol( _ callback: @escaping ( URLProtocol ? ) -> Void ) {
136
135
_protocolLock. lock ( ) // Must be balanced below, before we call out ⬇
137
136
138
137
switch _protocolStorage {
139
138
case . toBeCreated:
139
+ guard let protocolClass = self . _protocolClass else {
140
+ _protocolLock. unlock ( ) // Balances above ⬆
141
+ callback ( nil )
142
+ break
143
+ }
140
144
if let cache = session. configuration. urlCache, let me = self as? URLSessionDataTask {
141
145
let bag : Bag < ( URLProtocol ? ) -> Void > = Bag ( )
142
146
bag. values. append ( callback)
@@ -145,11 +149,11 @@ open class URLSessionTask : NSObject, NSCopying {
145
149
_protocolLock. unlock ( ) // Balances above ⬆
146
150
147
151
cache. getCachedResponse ( for: me) { ( response) in
148
- let urlProtocol = self . _protocolClass . init ( task: self , cachedResponse: response, client: nil )
152
+ let urlProtocol = protocolClass . init ( task: self , cachedResponse: response, client: nil )
149
153
self . _satisfyProtocolRequest ( with: urlProtocol)
150
154
}
151
155
} else {
152
- let urlProtocol = _protocolClass . init ( task: self , cachedResponse: nil , client: nil )
156
+ let urlProtocol = protocolClass . init ( task: self , cachedResponse: nil , client: nil )
153
157
_protocolStorage = . existing( urlProtocol)
154
158
_protocolLock. unlock ( ) // Balances above ⬆
155
159
0 commit comments