@@ -543,13 +543,14 @@ extension _ProtocolClient : URLProtocolClient {
543
543
task. state = . completed
544
544
session. taskRegistry. remove ( task)
545
545
case . dataCompletionHandler( let completion) :
546
- let data = Data ( )
547
- guard let client = `protocol`. client else { fatalError ( ) }
548
- client. urlProtocol ( `protocol`, didLoad: data)
549
- return
546
+ session. delegateQueue. addOperation {
547
+ completion ( `protocol`. properties [ URLProtocol . _PropertyKey. responseData] as? Data ?? Data ( ) , task. response, nil )
548
+ task. state = . completed
549
+ session. taskRegistry. remove ( task)
550
+ }
550
551
case . downloadCompletionHandler( let completion) :
551
552
session. delegateQueue. addOperation {
552
- completion ( task . currentRequest ? . url , task. response, nil )
553
+ completion ( `protocol` . properties [ URLProtocol . _PropertyKey . temporaryFileURL ] as? URL , task. response, nil )
553
554
task. state = . completed
554
555
session. taskRegistry. remove ( task)
555
556
}
@@ -565,18 +566,8 @@ extension _ProtocolClient : URLProtocolClient {
565
566
}
566
567
567
568
func urlProtocol( _ protocol: URLProtocol , didLoad data: Data ) {
568
- guard let task = `protocol`. task else { fatalError ( ) }
569
- guard let session = task. session as? URLSession else { fatalError ( ) }
570
- switch session. behaviour ( for: task) {
571
- case . dataCompletionHandler( let completion) :
572
- guard let s = task. session as? URLSession else { fatalError ( ) }
573
- s. delegateQueue. addOperation {
574
- completion ( data, task. response, nil )
575
- task. state = . completed
576
- s. taskRegistry. remove ( task)
577
- }
578
- default : return
579
- }
569
+ `protocol`. properties [ . responseData] = data
570
+ //TODO: this method needs to be extended to call the urlSession(_:dataTask:didReceive:)
580
571
}
581
572
582
573
func urlProtocol( _ protocol: URLProtocol , didFailWithError error: Error ) {
@@ -615,3 +606,10 @@ extension _ProtocolClient : URLProtocolClient {
615
606
NSUnimplemented ( )
616
607
}
617
608
}
609
+
610
+ extension URLProtocol {
611
+ enum _PropertyKey : String {
612
+ case responseData
613
+ case temporaryFileURL
614
+ }
615
+ }
0 commit comments