Skip to content

Commit 9c08115

Browse files
authored
Merge pull request #1195 from johnno1962e/master
2 parents 7a5af0f + a8018e7 commit 9c08115

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

Foundation/URLSession/URLSessionTask.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ open class URLSessionTask : NSObject, NSCopying {
3333
internal var suspendCount = 1
3434
internal var session: URLSessionProtocol! //change to nil when task completes
3535
internal let body: _Body
36-
fileprivate var _protocol: URLProtocol! = nil
36+
fileprivate var _protocol: URLProtocol? = nil
3737
private let syncQ = DispatchQueue(label: "org.swift.URLSessionTask.SyncQ")
3838

3939
/// All operations must run on this queue.
@@ -178,8 +178,8 @@ open class URLSessionTask : NSObject, NSCopying {
178178
self.workQueue.async {
179179
let urlError = URLError(_nsError: NSError(domain: NSURLErrorDomain, code: NSURLErrorCancelled, userInfo: nil))
180180
self.error = urlError
181-
self._protocol.stopLoading()
182-
self._protocol.client?.urlProtocol(self._protocol, didFailWithError: urlError)
181+
self._protocol?.stopLoading()
182+
self._protocol?.client?.urlProtocol(self._protocol!, didFailWithError: urlError)
183183
}
184184
}
185185
}
@@ -235,7 +235,7 @@ open class URLSessionTask : NSObject, NSCopying {
235235

236236
if self.suspendCount == 1 {
237237
self.workQueue.async {
238-
self._protocol.stopLoading()
238+
self._protocol?.stopLoading()
239239
}
240240
}
241241
}
@@ -250,7 +250,21 @@ open class URLSessionTask : NSObject, NSCopying {
250250
self.updateTaskState()
251251
if self.suspendCount == 0 {
252252
self.workQueue.async {
253-
self._protocol.startLoading()
253+
if let _protocol = self._protocol {
254+
_protocol.startLoading()
255+
}
256+
else if self.error == nil {
257+
var userInfo: [String: Any] = [NSLocalizedDescriptionKey: "unsupported URL"]
258+
if let url = self.originalRequest?.url {
259+
userInfo[NSURLErrorFailingURLErrorKey] = url
260+
userInfo[NSURLErrorFailingURLStringErrorKey] = url.absoluteString
261+
}
262+
let urlError = URLError(_nsError: NSError(domain: NSURLErrorDomain,
263+
code: NSURLErrorUnsupportedURL,
264+
userInfo: userInfo))
265+
self.error = urlError
266+
_ProtocolClient().urlProtocol(task: self, didFailWithError: urlError)
267+
}
254268
}
255269
}
256270
}
@@ -547,6 +561,7 @@ extension _ProtocolClient : URLProtocolClient {
547561
session.taskRegistry.remove(task)
548562
}
549563
}
564+
task._protocol = nil
550565
}
551566

552567
func urlProtocol(_ protocol: URLProtocol, didCancel challenge: URLAuthenticationChallenge) {
@@ -574,6 +589,10 @@ extension _ProtocolClient : URLProtocolClient {
574589

575590
func urlProtocol(_ protocol: URLProtocol, didFailWithError error: Error) {
576591
guard let task = `protocol`.task else { fatalError() }
592+
urlProtocol(task: task, didFailWithError: error)
593+
}
594+
595+
func urlProtocol(task: URLSessionTask, didFailWithError error: Error) {
577596
guard let session = task.session as? URLSession else { fatalError() }
578597
switch session.behaviour(for: task) {
579598
case .taskDelegate(let delegate):
@@ -602,6 +621,7 @@ extension _ProtocolClient : URLProtocolClient {
602621
session.taskRegistry.remove(task)
603622
}
604623
}
624+
task._protocol = nil
605625
}
606626

607627
func urlProtocol(_ protocol: URLProtocol, cachedResponseIsValid cachedResponse: CachedURLResponse) {

Foundation/URLSession/http/HTTPURLProtocol.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,21 @@ import Dispatch
1313
internal class _HTTPURLProtocol: URLProtocol {
1414

1515
fileprivate var easyHandle: _EasyHandle!
16-
fileprivate var tempFileURL: URL
16+
fileprivate lazy var tempFileURL: URL = {
17+
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
18+
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
19+
return URL(fileURLWithPath: fileName)
20+
}()
1721

1822
public required init(task: URLSessionTask, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) {
1923
self.internalState = _InternalState.initial
20-
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
21-
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
22-
self.tempFileURL = URL(fileURLWithPath: fileName)
2324
super.init(request: task.originalRequest!, cachedResponse: cachedResponse, client: client)
2425
self.task = task
2526
self.easyHandle = _EasyHandle(delegate: self)
2627
}
2728

2829
public required init(request: URLRequest, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) {
2930
self.internalState = _InternalState.initial
30-
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
31-
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
32-
self.tempFileURL = URL(fileURLWithPath: fileName)
3331
super.init(request: request, cachedResponse: cachedResponse, client: client)
3432
self.easyHandle = _EasyHandle(delegate: self)
3533
}

0 commit comments

Comments
 (0)