-
Notifications
You must be signed in to change notification settings - Fork 25
/
QCloudHTTPSessionManager.m
531 lines (477 loc) · 22.6 KB
/
QCloudHTTPSessionManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//
// QCloudHTTPSessionManager.m
// QCloudTernimalLab_CommonLogic
//
// Created by tencent on 16/3/30.
// Copyright © 2016年 QCloudTernimalLab. All rights reserved.
//
#import "QCloudHTTPSessionManager.h"
#import "QCloudHTTPRequest.h"
#import "QCloudEnv.h"
#import "QCloudHttpDNS.h"
#import "QCloudHTTPRetryHanlder.h"
#import "QCloudURLSessionTaskData.h"
#import <objc/runtime.h>
#import "QCloudHTTPRequest_RequestID.h"
#import "RNAsyncBenchMark.h"
#import "QCloudNetEnv.h"
#import "QCloudHTTPRequestOperation.h"
#import "QCloudLogger.h"
#import "QCloudNetProfile.h"
#import "QCloudOperationQueue.h"
#import "NSError+QCloudNetworking.h"
#import "QCloudFileUtils.h"
#import "QCloudFileOffsetBody.h"
#import "QCloudRequestData.h"
#import "QCloudService.h"
#import "UIDevice+QCloudFCUUID.h"
#import "QCloudThreadSafeMutableDictionary.h"
#import "QCloudWeakProxy.h"
NSString* TaskDataKey(int64_t identifier){
return [NSString stringWithFormat:@"data-%lld", identifier];
}
NSString* TaskMapKey(NSURLSessionTask* task) {
return [NSString stringWithFormat:@"%p",(void*)task];
}
NSString* const kQCloudRestNetURLUsageNotification = @"kQCloudRestNetURLUsageNotification";
QCloudThreadSafeMutableDictionary* cloudBackGroundSessionManagersCache;
QCloudThreadSafeMutableDictionary* QCloudBackgroundSessionManagerCache()
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cloudBackGroundSessionManagersCache = [QCloudThreadSafeMutableDictionary new];
});
return cloudBackGroundSessionManagersCache;
}
@implementation NSDictionary(QCloudRestNetUsage)
- (NSURL*) bdwt_RestNetCoreUsagedURL
{
return self[@"url"];
}
@end
@interface QCloudHTTPSessionManager() <NSURLSessionDelegate,NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
{
NSMutableDictionary* _taskQueue;
}
@property (nonatomic, strong) NSOperationQueue* sessionTaskQueue;;
@property (nonatomic, strong) NSURLSession* session;
@property (nonatomic, strong) dispatch_queue_t buildDataQueue;
@property (nonatomic, strong) QCloudOperationQueue* operationQueue;
@end
@implementation QCloudHTTPSessionManager
@synthesize maxConcurrencyTask = _maxConcurrencyTask;
+ (QCloudHTTPSessionManager*) shareClient
{
static QCloudHTTPSessionManager* client = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
client = [[QCloudHTTPSessionManager alloc] initWithConfigruation:[NSURLSessionConfiguration defaultSessionConfiguration]];
[QCloudNetEnv shareEnv];
});
return client;
}
+(QCloudHTTPSessionManager *)sessionManagerWithBackgroundIdentifier:(NSString *)backgroundIdentifier{
QCloudHTTPSessionManager *sessionManager = nil;
for (NSString *backgroundIdentifier in cloudBackGroundSessionManagersCache.allKeys) {
return [cloudBackGroundSessionManagersCache objectForKey:backgroundIdentifier];
}
sessionManager = [[QCloudHTTPSessionManager alloc]initWithBackgroundSessionWithBackgroundIdentifier:backgroundIdentifier];
[QCloudBackgroundSessionManagerCache() setObject:sessionManager forKey:backgroundIdentifier];
return sessionManager;
}
- (instancetype) initWithConfigruation:(NSURLSessionConfiguration*)configuration
{
self = [super init];
if (!self) {
return self;
}
//for restful request-response using the default session configuration ,and the most import thing is that you must not set the timeout for session configuration
_configuration = configuration;
_sessionTaskQueue = [[NSOperationQueue alloc] init];
_session = [NSURLSession sessionWithConfiguration:_configuration delegate:self delegateQueue:_sessionTaskQueue];
_buildDataQueue = dispatch_queue_create("com.tencent.qcloud.build.data", NULL);
_taskQueue = [NSMutableDictionary new];
_operationQueue = [QCloudOperationQueue new];
return self;
}
- (QCloudHTTPSessionManager *)initWithBackgroundSessionWithBackgroundIdentifier:(NSString *)backgroundIdentifier{
NSAssert(backgroundIdentifier, @"后台传输的标识符为nil,请检查是否设置了backgroundIdentifier");
NSURLSessionConfiguration* backgroundConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:backgroundIdentifier];
if (self = [self initWithConfigruation:backgroundConfiguration]) {
[QCloudNetEnv shareEnv];
}
return self;
}
- (void) setMaxConcurrencyTask:(int32_t)maxConcurrencyTask
{
if (_maxConcurrencyTask != maxConcurrencyTask) {
_maxConcurrencyTask = maxConcurrencyTask;
_sessionTaskQueue.maxConcurrentOperationCount = maxConcurrencyTask;
}
}
- (void) cancelRequests:(NSArray<NSNumber*>*)requestID
{
}
- (void) cacheTask:(NSURLSessionTask*)task data:(QCloudURLSessionTaskData*)data forSEQ:(int)seq
{
if (!task) {
return;
}
@synchronized (self) {
[_taskQueue setObject:@(seq) forKey:TaskMapKey(task)];
[_taskQueue setObject:task forKey:@(seq)];
[_taskQueue setObject:data forKey:TaskDataKey(seq)];
}
}
- (NSURLSessionTask*) taskForSEQ:(int)seq
{
@synchronized (self) {
return [_taskQueue objectForKey:@(seq)];
}
}
- (QCloudURLSessionTaskData*) taskDataForTask:(NSURLSessionTask*)task
{
@synchronized (self) {
int seq = [_taskQueue[TaskMapKey(task)] intValue];
return [_taskQueue objectForKey:TaskDataKey(seq)];
}
}
- (int) seqForTask:(NSURLSessionTask*)task
{
@synchronized (self) {
return [_taskQueue[TaskMapKey(task)] intValue];
}
}
- (void) removeTaskForSEQ:(int)seq
{
@synchronized (self) {
NSURLSessionTask* task = _taskQueue[@(seq)];
[_taskQueue removeObjectForKey:@(seq)];
[_taskQueue removeObjectForKey:TaskDataKey(seq)];
if (task) {
[_taskQueue removeObjectForKey:TaskMapKey(task)];
}
}
[_operationQueue cancelByRequestID:seq];
}
- (void) removeTask:(NSURLSessionTask*)task
{
@synchronized (self) {
int seq = [_taskQueue[TaskMapKey(task)] intValue];
[_taskQueue removeObjectForKey:@(seq)];
[_taskQueue removeObjectForKey:TaskDataKey(seq)];
[_taskQueue removeObjectForKey:TaskMapKey(task)];
}
}
- (int) performRequest:(QCloudHTTPRequest *)httpRequst withFinishBlock:(QCloudRequestFinishBlock)block
{
httpRequst.finishBlock = block;
return [self performRequest:httpRequst];
}
- (int) performRequest:(QCloudHTTPRequest *)request
{
QCloudHTTPRequestOperation* operation = [[QCloudHTTPRequestOperation alloc] initWithRequest:request];
operation.sessionManager = self;
[_operationQueue addOpreation:operation];
return (int)request.requestID;
}
// only work at iOS 10 and up
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics
{
QCloudURLSessionTaskData* taskData = [self taskDataForTask:task];
// NSAssert(taskData, @"无法获取缓存的TaskData,请检查主动Cache的地方");
NSURLSessionTaskTransactionMetrics* networkMetrics = nil;
for (NSURLSessionTaskTransactionMetrics* m in metrics.transactionMetrics) {
if (m.resourceFetchType == NSURLSessionTaskMetricsResourceFetchTypeNetworkLoad) {
networkMetrics = m;
} else if (m.resourceFetchType == NSURLSessionTaskMetricsResourceFetchTypeLocalCache){
}
}
if (networkMetrics) {
if (!networkMetrics.reusedConnection) {
[taskData.httpRequest.benchMarkMan directSetCost:[networkMetrics.connectEndDate timeIntervalSinceDate:networkMetrics.connectStartDate] forKey:kRNBenchmarkConnectionTime];
if ([taskData.httpRequest.requestData.serverURL.lowercaseString hasPrefix:@"https"]) {
[taskData.httpRequest.benchMarkMan directSetCost:[networkMetrics.secureConnectionEndDate timeIntervalSinceDate:networkMetrics.secureConnectionStartDate] forKey:kRNBenchmarkSecureConnectionTime];
}
[taskData.httpRequest.benchMarkMan directSetCost:[networkMetrics.domainLookupEndDate timeIntervalSinceDate:networkMetrics.domainLookupEndDate] forKey:kRNBenchmarkDNSLoopupTime];
}
[taskData.httpRequest.benchMarkMan directSetCost:[networkMetrics.requestEndDate timeIntervalSinceDate:networkMetrics.requestStartDate] forKey:@"upload"];
[taskData.httpRequest.benchMarkMan directSetCost:[networkMetrics.responseEndDate timeIntervalSinceDate:networkMetrics.responseStartDate] forKey:@"download"];
}
}
- (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
QCloudURLSessionTaskData* taskData = [self taskDataForTask:dataTask];
[taskData.httpRequest.benchMarkMan benginWithKey:kRNBenchmarkServerTime];
taskData.response = (NSHTTPURLResponse*)response;
if (taskData.httpRequest.downloadingURL) {
// if http statue is not found will forbidden write to file
if ([@[@(400), @(403), @(404), @(405)] containsObject:@(taskData.response.statusCode)]) {
taskData.forbidenWirteToFile = YES;
completionHandler([taskData.httpRequest reciveResponse:response]);
} else {
completionHandler([taskData.httpRequest reciveResponse:response]);
}
} else {
completionHandler([taskData.httpRequest reciveResponse:response]);
}
}
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
QCloudURLSessionTaskData* taskData = [self taskDataForTask:task];
if (taskData.httpRequest.sendProcessBlock) {
[taskData.httpRequest notifySendProgressBytesSend:bytesSent totalBytesSend:totalBytesSent totalBytesExpectedToSend:totalBytesExpectedToSend];
}
[[QCloudNetProfile shareProfile] pointUpload:bytesSent];
if (totalBytesSent == totalBytesExpectedToSend) {
[taskData.httpRequest.benchMarkMan markFinishWithKey:kRNBenchmarkUploadTime];
[taskData.httpRequest.benchMarkMan markFinishWithKey:kRNBenchmarkRequest];
}
}
- (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
QCloudURLSessionTaskData* taskData = [self taskDataForTask:dataTask];
if (taskData.totalRecivedLength == 0) {
[taskData.httpRequest.benchMarkMan markFinishWithKey:kRNBenchmarkServerTime];
[taskData.httpRequest.benchMarkMan benginWithKey:kRNBenchmarkDownploadTime];
[taskData.httpRequest.benchMarkMan benginWithKey:kRNBenchmarkResponse];
}
[taskData appendData:data];
if (taskData.httpRequest) {
[taskData.httpRequest notifyDownloadProgressBytesDownload:(int64_t)data.length
totalBytesDownload:(int64_t)taskData.totalRecivedLength
totalBytesExpectedToDownload:(int64_t)[dataTask.response expectedContentLength]];
}
[[QCloudNetProfile shareProfile] pointDownload:data.length];
}
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
[[NSNotificationCenter defaultCenter] postNotificationName:kQCloudRestNetURLUsageNotification object:nil userInfo:@{
@"url":task.originalRequest.URL? task.originalRequest.URL : [NSURL URLWithString:@"http://nullurl.error.com.tencent.qcloud.network"]
}];
QCloudURLSessionTaskData* taskData = [self taskDataForTask:task];
if (!taskData) {
return;
}
[taskData.httpRequest.benchMarkMan markFinishWithKey:kRNBenchmarkDownploadTime];
int seq = [self seqForTask:task];
__weak typeof(self)weakSelf = self;
if (error) {
QCloudLogError(@"Network Error %@",error);
void(^EndRetryFunc)(void) = ^(void) {
[taskData.httpRequest.benchMarkMan markFinishWithKey:kRNBenchmarkOnlyNet];
[taskData.httpRequest onReviveErrorResponse:task.response error:error];
[weakSelf removeTask:task];
};
if (!taskData.retryHandler) {
EndRetryFunc();
} else {
if (![taskData.retryHandler retryFunction:^{
QCloudLogDebug(@"[%i] 错误,开始重试",seq);
QCloudURLSessionTaskData* taskData = [weakSelf taskDataForTask:task];
if (taskData.httpRequest.sendProcessBlock) {
[taskData.httpRequest notifySendProgressBytesSend:-(task.countOfBytesSent) totalBytesSend:task.countOfBytesSent totalBytesExpectedToSend:task.countOfBytesExpectedToSend];
}
QCloudHTTPRequest* httpRequset = taskData.httpRequest;
[taskData restData];
[weakSelf removeTask:task];
[httpRequset.requestData clean];
[weakSelf executeRestHTTPReqeust:httpRequset];
} whenError:error])
{
EndRetryFunc();
}
}
} else {
[taskData.httpRequest.benchMarkMan markFinishWithKey:kRNBenchmarkOnlyNet];
[taskData.httpRequest onReciveRespone:task.response data:taskData.data];
[self removeTask:task];
}
}
- (void) URLSession:(NSURLSession * _Nonnull)session task:(NSURLSessionTask * _Nonnull)task didReceiveChallenge:(NSURLAuthenticationChallenge * _Nonnull)challenge completionHandler:(void (^ _Nonnull)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler
{
QCloudURLSessionTaskData* taskData = [self taskDataForTask:task];
NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
NSURLCredential* credential = nil;
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if (!IS_QCloud_NORMAL_ENV || !taskData.httpRequest.requestSerializer.shouldAuthentication) {
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
credential = [NSURLCredential credentialForTrust:serverTrust];
disposition = NSURLSessionAuthChallengeUseCredential;
} else {
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
if ([[QCloudHttpDNS shareDNS] isTrustIP:task.currentRequest.URL.host]) {
NSMutableArray *policies = [NSMutableArray array];
[policies addObject:(__bridge_transfer id)SecPolicyCreateBasicX509()];
SecTrustSetPolicies(serverTrust, (__bridge CFArrayRef)policies);
}
SecTrustResultType rType;
OSStatus status = SecTrustEvaluate(serverTrust, &rType);
if (status == errSecSuccess &&
(rType == kSecTrustResultProceed || rType == kSecTrustResultUnspecified)) {
credential = [NSURLCredential credentialForTrust:serverTrust];
disposition = NSURLSessionAuthChallengeUseCredential;
} else {
disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}
}
} else {
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
}
if (completionHandler) {
completionHandler(disposition, credential);
}
}
- (void) cancelRequestWithID:(int)requestID
{
NSURLSessionTask* task = [self taskForSEQ:requestID];
[task cancel];
[self removeTaskForSEQ:requestID];
}
- (void) cancelRequestsWithID:(NSArray<NSNumber*>*)requestIDs {
[self.operationQueue cancelByRequestIDs:requestIDs];
for (NSNumber* requestID in requestIDs) {
[self cancelRequestWithID:[requestID intValue]];
}
}
- (void) cancelAllRequest
{
@synchronized (self) {
NSEnumerator* enumertor = [_taskQueue objectEnumerator];
NSURLSessionTask* task = nil;
while (task = [enumertor nextObject]) {
[task cancel];
}
[_taskQueue removeAllObjects];
}
}
- (void) executeRestHTTPReqeust:(QCloudHTTPRequest*)httpRequest
{
[httpRequest willStart];
NSError* error;
NSMutableURLRequest* urlRequest = [[httpRequest buildURLRequest:&error] mutableCopy];
if (error) {
[httpRequest onError:error];
return;
}
NSMutableURLRequest* transformRequest = urlRequest;
if (httpRequest.requestSerializer.HTTPDNSPrefetch) {
transformRequest = [[QCloudHttpDNS shareDNS] resolveURLRequestIfCan:urlRequest];
if (error) {
QCloudLogError(@"DNS转存请求失败%@",error);
}
}
QCloudURLSessionTaskData* taskData = nil;
if (httpRequest.downloadingURL) {
NSError* localError;
if (!QCloudFileExist(httpRequest.downloadingURL.path)) {
[[NSFileManager defaultManager] createFileAtPath:httpRequest.downloadingURL.path contents:nil attributes:nil];
}
NSFileHandle* handler = [NSFileHandle fileHandleForWritingToURL:httpRequest.downloadingURL error:&localError];
if (localError) {
[httpRequest onError:localError];
return;
} else {
[handler seekToFileOffset:httpRequest.localCacheDownloadOffset];
taskData = [[QCloudURLSessionTaskData alloc] initWithDowndingFileHandler:handler];
}
} else {
taskData = [[QCloudURLSessionTaskData alloc] init];
}
NSError* directError;
NSURL* uploadFileURL = nil;
if (httpRequest.requestData.directBody) {
NSMutableURLRequest* mutableRequest = [transformRequest mutableCopy];
id body = httpRequest.requestData.directBody;
if ([body isKindOfClass:[NSData class]]) {
NSData* data = (NSData*)body;
[mutableRequest setHTTPBody:data];
[mutableRequest setValue:[@([data length]) stringValue] forHTTPHeaderField:@"Content-Length"];
} else if ([body isKindOfClass:[NSURL class]]) {
NSURL* fileURL = (NSURL*)body;
if (![fileURL isFileURL]) {
directError = [NSError qcloud_errorWithCode:QCloudNetworkErrorCodeContentError message:@"您输入的body的URL不是本地URL,请检查后使用!!"];
}
NSUInteger fileSize = QCloudFileSize(fileURL.path);
[mutableRequest setValue:[@(fileSize) stringValue] forHTTPHeaderField:@"Content-Length"];
NSInputStream* inputStream = [NSInputStream inputStreamWithURL:fileURL];
[mutableRequest setHTTPBodyStream:inputStream];
uploadFileURL = fileURL;
} else if ([body isKindOfClass:[QCloudFileOffsetBody class]]) {
QCloudFileOffsetBody* fileBody = (QCloudFileOffsetBody*)body;
if (![fileBody.fileURL isFileURL]) {
directError = [NSError qcloud_errorWithCode:QCloudNetworkErrorCodeContentError message:@"您输入的body的URL不是本地URL,请检查后使用!!"];
}
NSFileHandle* handler = [NSFileHandle fileHandleForReadingAtPath:fileBody.fileURL.path];
[handler seekToFileOffset:fileBody.offset];
NSData* data = [handler readDataOfLength:fileBody.sliceLength];
NSString* tempFile = taskData.uploadTempFilePath;
if([data writeToFile:tempFile options:0 error:&directError]) {
NSInputStream* inputstream = [NSInputStream inputStreamWithFileAtPath:tempFile];
[mutableRequest setValue:[@(fileBody.sliceLength) stringValue] forHTTPHeaderField:@"Content-Length"];
[handler closeFile];
[mutableRequest setHTTPBodyStream:inputstream];
uploadFileURL = [NSURL fileURLWithPath:tempFile];
}
} else {
@throw [NSException exceptionWithName:kQCloudNetworkDomain reason:@"不支持设置该类型的body,支持的类型为NSData、QCloudFileOffsetBody、NSURL" userInfo:@{}];
}
transformRequest = mutableRequest;
}
if (directError) {
[httpRequest onError:directError];
return;
}
// 准备发送请求,最后一次机会修改将要发送的HTTP请求
NSError* parepareError;
if (![httpRequest prepareInvokeURLRequest:transformRequest error:&parepareError]) {
[httpRequest onError:parepareError];
return;
}
if (nil == transformRequest) {
NSError* error = [NSError qcloud_errorWithCode:QCloudNetworkErrorCodeContentError message:@"构建Request时候出错,出现空的Request"];
[httpRequest onError:error];
return;
}
NSURLSessionDataTask* task = nil;
//如果是文件上传
if (uploadFileURL) {
task = [self.session uploadTaskWithRequest:transformRequest fromFile:uploadFileURL];
}else{
if (self.session.configuration.identifier) {
if (!transformRequest.HTTPBody) {
if ([[NSFileManager defaultManager] createFileAtPath:taskData.uploadTempFilePath contents:nil attributes:nil]) {
task = [self.session uploadTaskWithRequest:transformRequest fromFile:[NSURL fileURLWithPath:taskData.uploadTempFilePath]];
}
}else{
[transformRequest.HTTPBody writeToFile:taskData.uploadTempFilePath options:0 error:nil];
task = [self.session uploadTaskWithRequest:transformRequest fromFile:[NSURL fileURLWithPath:taskData.uploadTempFilePath]];
}
}else{
task = [self.session dataTaskWithRequest:transformRequest];
}
}
taskData.httpRequest = httpRequest;
QCloudHTTPRetryHanlder* retryHandler = httpRequest.retryPolicy;
taskData.retryHandler = retryHandler;
[self cacheTask:task data:taskData forSEQ:(int)httpRequest.requestID];
[httpRequest.benchMarkMan benginWithKey:kRNBenchmarkOnlyNet];
@try {
int length = [[urlRequest.allHTTPHeaderFields objectForKey:@"Content-Length"] intValue];
[httpRequest.benchMarkMan directSetCost:length forKey:kRNBenchmarkSizeRequeqstBody];
NSUInteger headerLength = [NSJSONSerialization dataWithJSONObject:urlRequest.allHTTPHeaderFields options:0 error:nil].length;
[httpRequest.benchMarkMan directSetCost:headerLength forKey:kRNBenchmarkSizeRequeqstHeader];
}
@catch (NSException *exception) {} @finally {}
[task resume];
[httpRequest.benchMarkMan benginWithKey:kRNBenchmarkUploadTime];
}
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
if (self.didFinishEventsForBackgroundURLSession) {
self.didFinishEventsForBackgroundURLSession();
}
}
-(void)dealloc{
[cloudBackGroundSessionManagersCache removeObject:self];
}
@end