Skip to content

Make totalDownloaded to be specific to HTTPURLProtocol #1029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Foundation/NSURLSession/NSURLSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ import Dispatch
open class URLSessionTask : NSObject, NSCopying {
/// How many times the task has been suspended, 0 indicating a running task.
internal var suspendCount = 1
internal var totalDownloaded = 0
internal var session: URLSessionProtocol! //change to nil when task completes
internal let body: _Body
internal let tempFileURL: URL
fileprivate var _protocol: URLProtocol! = nil

/// All operations must run on this queue.
Expand All @@ -52,9 +50,6 @@ open class URLSessionTask : NSObject, NSCopying {
originalRequest = nil
body = .none
workQueue = DispatchQueue(label: "URLSessionTask.notused.0")
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
self.tempFileURL = URL(fileURLWithPath: fileName)
super.init()
}
/// Create a data task. If there is a httpBody in the URLRequest, use that as a parameter
Expand All @@ -71,9 +66,6 @@ open class URLSessionTask : NSObject, NSCopying {
self.taskIdentifier = taskIdentifier
self.originalRequest = request
self.body = body
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
self.tempFileURL = URL(fileURLWithPath: fileName)
super.init()
if session.configuration.protocolClasses != nil {
guard let protocolClasses = session.configuration.protocolClasses else { fatalError() }
Expand Down
22 changes: 15 additions & 7 deletions Foundation/NSURLSession/http/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ import Dispatch
internal class _HTTPURLProtocol: URLProtocol {

fileprivate var easyHandle: _EasyHandle!
fileprivate var totalDownloaded = 0
fileprivate var tempFileURL: URL

public override required init(task: URLSessionTask, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) {
self.internalState = _InternalState.initial
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
self.tempFileURL = URL(fileURLWithPath: fileName)
super.init(request: task.originalRequest!, cachedResponse: cachedResponse, client: client)
self.task = task
self.easyHandle = _EasyHandle(delegate: self)
}

public override required init(request: URLRequest, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) {
self.internalState = _InternalState.initial
let fileName = NSTemporaryDirectory() + NSUUID().uuidString + ".tmp"
_ = FileManager.default.createFile(atPath: fileName, contents: nil)
self.tempFileURL = URL(fileURLWithPath: fileName)
super.init(request: request, cachedResponse: cachedResponse, client: client)
self.easyHandle = _EasyHandle(delegate: self)
}
Expand Down Expand Up @@ -399,8 +407,8 @@ internal extension _HTTPURLProtocol {
return .inMemory(nil)
case .downloadCompletionHandler:
// Data needs to be written to a file (i.e. a download task).
let fileHandle = try! FileHandle(forWritingTo: task.tempFileURL)
return .toFile(task.tempFileURL, fileHandle)
let fileHandle = try! FileHandle(forWritingTo: self.tempFileURL)
return .toFile(self.tempFileURL, fileHandle)
}
}
}
Expand Down Expand Up @@ -453,19 +461,19 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
let downloadDelegate = delegate as? URLSessionDownloadDelegate,
let task = self.task as? URLSessionDownloadTask {
guard let s = self.task?.session as? URLSession else { fatalError() }
let fileHandle = try! FileHandle(forWritingTo: task.tempFileURL)
let fileHandle = try! FileHandle(forWritingTo: self.tempFileURL)
_ = fileHandle.seekToEndOfFile()
fileHandle.write(data)
self.task?.totalDownloaded += data.count
self.totalDownloaded += data.count

s.delegateQueue.addOperation {
downloadDelegate.urlSession(s, downloadTask: task, didWriteData: Int64(data.count), totalBytesWritten: Int64(t.totalDownloaded),
downloadDelegate.urlSession(s, downloadTask: task, didWriteData: Int64(data.count), totalBytesWritten: Int64(self.totalDownloaded),
totalBytesExpectedToWrite: Int64(self.easyHandle.fileLength))
}
if Int(self.easyHandle.fileLength) == self.task?.totalDownloaded {
if Int(self.easyHandle.fileLength) == self.totalDownloaded {
fileHandle.closeFile()
s.delegateQueue.addOperation {
downloadDelegate.urlSession(s, downloadTask: task, didFinishDownloadingTo: t.tempFileURL)
downloadDelegate.urlSession(s, downloadTask: task, didFinishDownloadingTo: self.tempFileURL)
}
}
}
Expand Down