@@ -13,9 +13,6 @@ import Dispatch
13
13
internal class _HTTPURLProtocol : URLProtocol {
14
14
15
15
fileprivate var easyHandle : _EasyHandle !
16
- fileprivate var totalDownloaded = 0
17
- fileprivate var totalUploaded : Int64 = 0
18
- fileprivate var requestBodyLength : Int64 = 0
19
16
fileprivate var tempFileURL : URL
20
17
21
18
public required init ( task: URLSessionTask , cachedResponse: CachedURLResponse ? , client: URLProtocolClient ? ) {
@@ -129,7 +126,7 @@ fileprivate extension _HTTPURLProtocol {
129
126
set ( requestBodyLength: . noBody)
130
127
case ( _, . some( let length) ) :
131
128
set ( requestBodyLength: . length( length) )
132
- requestBodyLength = Int64 ( length)
129
+ task! . countOfBytesExpectedToSend = Int64 ( length)
133
130
case ( _, . none) :
134
131
set ( requestBodyLength: . unknown)
135
132
}
@@ -469,21 +466,23 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
469
466
let fileHandle = try ! FileHandle ( forWritingTo: self . tempFileURL)
470
467
_ = fileHandle. seekToEndOfFile ( )
471
468
fileHandle. write ( data)
472
- self . totalDownloaded += data. count
469
+ task . countOfBytesReceived += Int64 ( data. count)
473
470
474
471
s. delegateQueue. addOperation {
475
- downloadDelegate. urlSession ( s, downloadTask: task, didWriteData: Int64 ( data. count) , totalBytesWritten: Int64 ( self . totalDownloaded ) ,
476
- totalBytesExpectedToWrite: Int64 ( self . easyHandle . fileLength ) )
472
+ downloadDelegate. urlSession ( s, downloadTask: task, didWriteData: Int64 ( data. count) , totalBytesWritten: task . countOfBytesReceived ,
473
+ totalBytesExpectedToWrite: task . countOfBytesExpectedToReceive )
477
474
}
478
- if Int ( self . easyHandle . fileLength ) == self . totalDownloaded {
475
+ if task . countOfBytesExpectedToReceive == task . countOfBytesReceived {
479
476
fileHandle. closeFile ( )
480
477
self . properties [ . temporaryFileURL] = self . tempFileURL
481
478
}
482
479
}
483
480
}
484
481
485
- func didReceive( headerData data: Data ) -> _EasyHandle . _Action {
482
+ func didReceive( headerData data: Data , contentLength : Int64 ) -> _EasyHandle . _Action {
486
483
guard case . transferInProgress( let ts) = internalState else { fatalError ( " Received body data, but no transfer in progress. " ) }
484
+ guard let task = task else { fatalError ( " Receieved header data but no task available " ) }
485
+ task. countOfBytesExpectedToReceive = contentLength > 0 ? contentLength : NSURLSessionTransferSizeUnknown
487
486
do {
488
487
let newTS = try ts. byAppending ( headerLine: data)
489
488
internalState = . transferInProgress( newTS)
@@ -501,10 +500,10 @@ extension _HTTPURLProtocol: _EasyHandleDelegate {
501
500
fileprivate func notifyDelegate( aboutUploadedData count: Int64 ) {
502
501
let session = self . task? . session as! URLSession
503
502
guard case . taskDelegate( let delegate) = session. behaviour ( for: self . task!) , self . task is URLSessionUploadTask else { return }
504
- totalUploaded += count
503
+ task! . countOfBytesSent += count
505
504
session. delegateQueue. addOperation {
506
505
delegate. urlSession ( session, task: self . task!, didSendBodyData: count,
507
- totalBytesSent: self . totalUploaded , totalBytesExpectedToSend: self . requestBodyLength )
506
+ totalBytesSent: self . task! . countOfBytesSent , totalBytesExpectedToSend: self . task! . countOfBytesExpectedToSend )
508
507
}
509
508
}
510
509
0 commit comments