@@ -115,7 +115,7 @@ open class URLSessionTask : NSObject, NSCopying {
115115 fileprivate var _protocolStorage : ProtocolState = . toBeCreated
116116 internal var _lastCredentialUsedFromStorageDuringAuthentication : ( protectionSpace: URLProtectionSpace , credential: URLCredential ) ?
117117
118- private var _protocolClass : URLProtocol . Type {
118+ private var _protocolClass : URLProtocol . Type ? {
119119 guard let request = currentRequest else { fatalError ( " A protocol class was requested, but we do not have a current request " ) }
120120 let protocolClasses = session. configuration. protocolClasses ?? [ ]
121121 if let urlProtocolClass = URLProtocol . getProtocolClass ( protocols: protocolClasses, request: request) {
@@ -128,15 +128,19 @@ open class URLSessionTask : NSObject, NSCopying {
128128 return urlProtocol
129129 }
130130 }
131-
132- fatalError ( " Couldn't find a protocol appropriate for request: \( request) " )
131+ return nil
133132 }
134133
135134 func _getProtocol( _ callback: @escaping ( URLProtocol ? ) -> Void ) {
136135 _protocolLock. lock ( ) // Must be balanced below, before we call out ⬇
137136
138137 switch _protocolStorage {
139138 case . toBeCreated:
139+ guard let protocolClass = self . _protocolClass else {
140+ _protocolLock. unlock ( ) // Balances above ⬆
141+ callback ( nil )
142+ break
143+ }
140144 if let cache = session. configuration. urlCache, let me = self as? URLSessionDataTask {
141145 let bag : Bag < ( URLProtocol ? ) -> Void > = Bag ( )
142146 bag. values. append ( callback)
@@ -145,11 +149,11 @@ open class URLSessionTask : NSObject, NSCopying {
145149 _protocolLock. unlock ( ) // Balances above ⬆
146150
147151 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 )
149153 self . _satisfyProtocolRequest ( with: urlProtocol)
150154 }
151155 } else {
152- let urlProtocol = _protocolClass . init ( task: self , cachedResponse: nil , client: nil )
156+ let urlProtocol = protocolClass . init ( task: self , cachedResponse: nil , client: nil )
153157 _protocolStorage = . existing( urlProtocol)
154158 _protocolLock. unlock ( ) // Balances above ⬆
155159
0 commit comments