Skip to content

Commit d2f8c5e

Browse files
authored
Merge pull request #1085 from nethraravindran/protocolClient-branch
2 parents 379f28b + f5b562c commit d2f8c5e

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

Foundation/NSURLSession/NSURLSessionConfiguration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ open class URLSessionConfiguration : NSObject, NSCopying {
4747
self.urlCredentialStorage = nil
4848
self.urlCache = nil
4949
self.shouldUseExtendedBackgroundIdleMode = false
50+
self.protocolClasses = [_HTTPURLProtocol.self]
5051
super.init()
5152
}
5253

Foundation/NSURLSession/NSURLSessionTask.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,12 @@ extension _ProtocolClient : URLProtocolClient {
546546
guard let task = `protocol`.task else { fatalError() }
547547
guard let session = task.session as? URLSession else { fatalError() }
548548
switch session.behaviour(for: task) {
549+
case .taskDelegate(let delegate) where delegate is URLSessionDownloadDelegate:
550+
let downloadDelegate = delegate as! URLSessionDownloadDelegate
551+
let downloadTask = task as! URLSessionDownloadTask
552+
session.delegateQueue.addOperation {
553+
downloadDelegate.urlSession(session, downloadTask: downloadTask, didFinishDownloadingTo: `protocol`.properties[URLProtocol._PropertyKey.temporaryFileURL] as! URL)
554+
}
549555
case .taskDelegate(let delegate):
550556
session.delegateQueue.addOperation {
551557
delegate.urlSession(session, task: task, didCompleteWithError: nil)

Foundation/NSURLSession/http/HTTPURLProtocol.swift

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,10 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
449449
fileprivate func notifyDelegate(aboutReceivedData data: Data) {
450450
guard let t = self.task else { fatalError("Cannot notify") }
451451
if case .taskDelegate(let delegate) = t.session.behaviour(for: self.task!),
452-
let dataDelegate = delegate as? URLSessionDataDelegate,
453-
let task = self.task as? URLSessionDataTask {
454-
// Forward to the delegate:
455-
guard let s = self.task?.session as? URLSession else { fatalError() }
456-
s.delegateQueue.addOperation {
457-
dataDelegate.urlSession(s, dataTask: task, didReceive: data)
458-
}
452+
let _ = delegate as? URLSessionDataDelegate,
453+
let _ = self.task as? URLSessionDataTask {
454+
// Forward to protocol client:
455+
self.client?.urlProtocol(self, didLoad: data)
459456
} else if case .taskDelegate(let delegate) = t.session.behaviour(for: self.task!),
460457
let downloadDelegate = delegate as? URLSessionDownloadDelegate,
461458
let task = self.task as? URLSessionDownloadTask {
@@ -471,9 +468,7 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
471468
}
472469
if Int(self.easyHandle.fileLength) == self.totalDownloaded {
473470
fileHandle.closeFile()
474-
s.delegateQueue.addOperation {
475-
downloadDelegate.urlSession(s, downloadTask: task, didFinishDownloadingTo: self.tempFileURL)
476-
}
471+
self.properties[.temporaryFileURL] = self.tempFileURL
477472
}
478473
}
479474
}
@@ -759,14 +754,14 @@ internal extension _HTTPURLProtocol {
759754
/// Whenever we receive a response (i.e. a complete header) from libcurl,
760755
/// this method gets called.
761756
func didReceiveResponse() {
762-
guard let dt = task as? URLSessionDataTask else { return }
757+
guard let _ = task as? URLSessionDataTask else { return }
763758
guard case .transferInProgress(let ts) = self.internalState else { fatalError("Transfer not in progress.") }
764759
guard let response = ts.response else { fatalError("Header complete, but not URL response.") }
765760
guard let session = task?.session as? URLSession else { fatalError() }
766761
switch session.behaviour(for: self.task!) {
767762
case .noDelegate:
768763
break
769-
case .taskDelegate(let delegate as URLSessionDataDelegate):
764+
case .taskDelegate(_):
770765
//TODO: There's a problem with libcurl / with how we're using it.
771766
// We're currently unable to pause the transfer / the easy handle:
772767
// https://curl.haxx.se/mail/lib-2016-03/0222.html
@@ -777,14 +772,8 @@ internal extension _HTTPURLProtocol {
777772
case 301, 302, 303, 307:
778773
break
779774
default:
780-
session.delegateQueue.addOperation {
781-
delegate.urlSession(session, dataTask: dt, didReceive: response, completionHandler: { _ in
782-
URLSession.printDebug("warning: Ignoring disposition from completion handler.")
783-
})
784-
}
775+
self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
785776
}
786-
case .taskDelegate:
787-
break
788777
case .dataCompletionHandler:
789778
break
790779
case .downloadCompletionHandler:

0 commit comments

Comments
 (0)