Skip to content

Commit 274ab9c

Browse files
authored
Merge pull request #1075 from pushkarnk/urlsession-warnings-improvement
2 parents 3c8d51e + 5c9014c commit 274ab9c

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

Foundation/NSURLProtocol.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ open class URLProtocol : NSObject {
159159

160160
private static var _registeredProtocolClasses = [AnyClass]()
161161
private static var _classesLock = NSLock()
162+
163+
//TODO: The right way to do this is using URLProtocol.property(forKey:in) and URLProtocol.setProperty(_:forKey:in)
164+
var properties: [URLProtocol._PropertyKey: Any] = [:]
162165
/*!
163166
@method initWithRequest:cachedResponse:client:
164167
@abstract Initializes an NSURLProtocol given request,

Foundation/NSURLSession/NSURLSessionTask.swift

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -543,13 +543,14 @@ extension _ProtocolClient : URLProtocolClient {
543543
task.state = .completed
544544
session.taskRegistry.remove(task)
545545
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+
}
550551
case .downloadCompletionHandler(let completion):
551552
session.delegateQueue.addOperation {
552-
completion(task.currentRequest?.url, task.response, nil)
553+
completion(`protocol`.properties[URLProtocol._PropertyKey.temporaryFileURL] as? URL, task.response, nil)
553554
task.state = .completed
554555
session.taskRegistry.remove(task)
555556
}
@@ -565,18 +566,8 @@ extension _ProtocolClient : URLProtocolClient {
565566
}
566567

567568
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:)
580571
}
581572

582573
func urlProtocol(_ protocol: URLProtocol, didFailWithError error: Error) {
@@ -615,3 +606,10 @@ extension _ProtocolClient : URLProtocolClient {
615606
NSUnimplemented()
616607
}
617608
}
609+
610+
extension URLProtocol {
611+
enum _PropertyKey: String {
612+
case responseData
613+
case temporaryFileURL
614+
}
615+
}

Foundation/NSURLSession/http/HTTPURLProtocol.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,10 @@ extension _HTTPURLProtocol {
679679
}
680680
self.client?.urlProtocol(self, didLoad: data)
681681
self.internalState = .taskCompleted
682-
return
683682
}
684683

685-
if case .toFile(_, let fileHandle?) = bodyDataDrain {
684+
if case .toFile(let url, let fileHandle?) = bodyDataDrain {
685+
self.properties[.temporaryFileURL] = url
686686
fileHandle.closeFile()
687687
}
688688
self.client?.urlProtocolDidFinishLoading(self)

0 commit comments

Comments
 (0)