@@ -29,8 +29,8 @@ open class URLSessionTask : NSObject, NSCopying {
29
29
internal var suspendCount = 1
30
30
internal var session : URLSessionProtocol ! //change to nil when task completes
31
31
internal let body : _Body
32
- fileprivate var _protocol : URLProtocol ! = nil
33
-
32
+ fileprivate var _protocol : URLProtocol ? = nil
33
+
34
34
/// All operations must run on this queue.
35
35
internal let workQueue : DispatchQueue
36
36
/// Using dispatch semaphore to make public attributes thread safe.
@@ -200,8 +200,8 @@ open class URLSessionTask : NSObject, NSCopying {
200
200
self . workQueue. async {
201
201
let urlError = URLError ( _nsError: NSError ( domain: NSURLErrorDomain, code: NSURLErrorCancelled, userInfo: nil ) )
202
202
self . error = urlError
203
- self . _protocol. stopLoading ( )
204
- self . _protocol. client? . urlProtocol ( self . _protocol, didFailWithError: urlError)
203
+ self . _protocol? . stopLoading ( )
204
+ self . _protocol? . client? . urlProtocol ( self . _protocol! , didFailWithError: urlError)
205
205
}
206
206
}
207
207
}
@@ -263,7 +263,7 @@ open class URLSessionTask : NSObject, NSCopying {
263
263
264
264
if self . suspendCount == 1 {
265
265
self . workQueue. async {
266
- self . _protocol. stopLoading ( )
266
+ self . _protocol? . stopLoading ( )
267
267
}
268
268
}
269
269
}
@@ -278,7 +278,21 @@ open class URLSessionTask : NSObject, NSCopying {
278
278
self . updateTaskState ( )
279
279
if self . suspendCount == 0 {
280
280
self . workQueue. async {
281
- self . _protocol. startLoading ( )
281
+ if let _protocol = self . _protocol {
282
+ _protocol. startLoading ( )
283
+ }
284
+ else if self . error == nil {
285
+ var userInfo : [ String : Any ] = [ NSLocalizedDescriptionKey: " unsupported URL " ]
286
+ if let url = self . originalRequest? . url {
287
+ userInfo [ NSURLErrorFailingURLErrorKey] = url
288
+ userInfo [ NSURLErrorFailingURLStringErrorKey] = url. absoluteString
289
+ }
290
+ let urlError = URLError ( _nsError: NSError ( domain: NSURLErrorDomain,
291
+ code: NSURLErrorUnsupportedURL,
292
+ userInfo: userInfo) )
293
+ self . error = urlError
294
+ _ProtocolClient ( ) . urlProtocol ( task: self , didFailWithError: urlError)
295
+ }
282
296
}
283
297
}
284
298
}
@@ -573,6 +587,7 @@ extension _ProtocolClient : URLProtocolClient {
573
587
session. taskRegistry. remove ( task)
574
588
}
575
589
}
590
+ task. _protocol = nil
576
591
}
577
592
578
593
func urlProtocol( _ protocol: URLProtocol , didCancel challenge: URLAuthenticationChallenge ) {
@@ -600,6 +615,10 @@ extension _ProtocolClient : URLProtocolClient {
600
615
601
616
func urlProtocol( _ protocol: URLProtocol , didFailWithError error: Error ) {
602
617
guard let task = `protocol`. task else { fatalError ( ) }
618
+ urlProtocol ( task: task, didFailWithError: error)
619
+ }
620
+
621
+ func urlProtocol( task: URLSessionTask , didFailWithError error: Error ) {
603
622
guard let session = task. session as? URLSession else { fatalError ( ) }
604
623
switch session. behaviour ( for: task) {
605
624
case . taskDelegate( let delegate) :
@@ -624,6 +643,7 @@ extension _ProtocolClient : URLProtocolClient {
624
643
session. taskRegistry. remove ( task)
625
644
}
626
645
}
646
+ task. _protocol = nil
627
647
}
628
648
629
649
func urlProtocol( _ protocol: URLProtocol , cachedResponseIsValid cachedResponse: CachedURLResponse ) {
0 commit comments