No Description

GULNetworkURLSession.m 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. #import "Private/GULNetworkURLSession.h"
  16. #import <GoogleUtilities/GULLogger.h>
  17. #import "Private/GULMutableDictionary.h"
  18. #import "Private/GULNetworkConstants.h"
  19. #import "Private/GULNetworkMessageCode.h"
  20. @interface GULNetworkURLSession () <NSURLSessionDelegate,
  21. NSURLSessionDataDelegate,
  22. NSURLSessionDownloadDelegate,
  23. NSURLSessionTaskDelegate>
  24. @end
  25. @implementation GULNetworkURLSession {
  26. /// The handler to be called when the request completes or error has occurs.
  27. GULNetworkURLSessionCompletionHandler _completionHandler;
  28. /// Session ID generated randomly with a fixed prefix.
  29. NSString *_sessionID;
  30. #pragma clang diagnostic push
  31. #pragma clang diagnostic ignored "-Wunguarded-availability"
  32. /// The session configuration. NSURLSessionConfiguration' is only available on iOS 7.0 or newer.
  33. NSURLSessionConfiguration *_sessionConfig;
  34. /// The current NSURLSession.
  35. NSURLSession *__weak _Nullable _URLSession;
  36. #pragma clang diagnostic pop
  37. /// The path to the directory where all temporary files are stored before uploading.
  38. NSURL *_networkDirectoryURL;
  39. /// The downloaded data from fetching.
  40. NSData *_downloadedData;
  41. /// The path to the temporary file which stores the uploading data.
  42. NSURL *_uploadingFileURL;
  43. /// The current request.
  44. NSURLRequest *_request;
  45. }
  46. #pragma mark - Init
  47. - (instancetype)initWithNetworkLoggerDelegate:(id<GULNetworkLoggerDelegate>)networkLoggerDelegate {
  48. self = [super init];
  49. if (self) {
  50. // Create URL to the directory where all temporary files to upload have to be stored.
  51. NSArray *paths =
  52. NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
  53. NSString *applicationSupportDirectory = paths.firstObject;
  54. NSArray *tempPathComponents = @[
  55. applicationSupportDirectory, kGULNetworkApplicationSupportSubdirectory,
  56. kGULNetworkTempDirectoryName
  57. ];
  58. _networkDirectoryURL = [NSURL fileURLWithPathComponents:tempPathComponents];
  59. _sessionID = [NSString stringWithFormat:@"%@-%@", kGULNetworkBackgroundSessionConfigIDPrefix,
  60. [[NSUUID UUID] UUIDString]];
  61. _loggerDelegate = networkLoggerDelegate;
  62. }
  63. return self;
  64. }
  65. #pragma mark - External Methods
  66. #pragma mark - To be called from AppDelegate
  67. + (void)handleEventsForBackgroundURLSessionID:(NSString *)sessionID
  68. completionHandler:
  69. (GULNetworkSystemCompletionHandler)systemCompletionHandler {
  70. // The session may not be Analytics background. Ignore those that do not have the prefix.
  71. if (![sessionID hasPrefix:kGULNetworkBackgroundSessionConfigIDPrefix]) {
  72. return;
  73. }
  74. GULNetworkURLSession *fetcher = [self fetcherWithSessionIdentifier:sessionID];
  75. if (fetcher != nil) {
  76. [fetcher addSystemCompletionHandler:systemCompletionHandler forSession:sessionID];
  77. } else {
  78. GULLogError(kGULLoggerNetwork, NO,
  79. [NSString stringWithFormat:@"I-NET%06ld", (long)kGULNetworkMessageCodeNetwork003],
  80. @"Failed to retrieve background session with ID %@ after app is relaunched.",
  81. sessionID);
  82. }
  83. }
  84. #pragma mark - External Methods
  85. /// Sends an async POST request using NSURLSession for iOS >= 7.0, and returns an ID of the
  86. /// connection.
  87. - (nullable NSString *)sessionIDFromAsyncPOSTRequest:(NSURLRequest *)request
  88. completionHandler:(GULNetworkURLSessionCompletionHandler)handler
  89. API_AVAILABLE(ios(7.0)) {
  90. // NSURLSessionUploadTask does not work with NSData in the background.
  91. // To avoid this issue, write the data to a temporary file to upload it.
  92. // Make a temporary file with the data subset.
  93. _uploadingFileURL = [self temporaryFilePathWithSessionID:_sessionID];
  94. NSError *writeError;
  95. NSURLSessionUploadTask *postRequestTask;
  96. NSURLSession *session;
  97. BOOL didWriteFile = NO;
  98. // Clean up the entire temp folder to avoid temp files that remain in case the previous session
  99. // crashed and did not clean up.
  100. [self maybeRemoveTempFilesAtURL:_networkDirectoryURL
  101. expiringTime:kGULNetworkTempFolderExpireTime];
  102. // If there is no background network enabled, no need to write to file. This will allow default
  103. // network session which runs on the foreground.
  104. if (_backgroundNetworkEnabled && [self ensureTemporaryDirectoryExists]) {
  105. didWriteFile = [request.HTTPBody writeToFile:_uploadingFileURL.path
  106. options:NSDataWritingAtomic
  107. error:&writeError];
  108. if (writeError) {
  109. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
  110. messageCode:kGULNetworkMessageCodeURLSession000
  111. message:@"Failed to write request data to file"
  112. context:writeError];
  113. }
  114. }
  115. if (didWriteFile) {
  116. // Exclude this file from backing up to iTunes. There are conflicting reports that excluding
  117. // directory from backing up does not exclude files of that directory from backing up.
  118. [self excludeFromBackupForURL:_uploadingFileURL];
  119. _sessionConfig = [self backgroundSessionConfigWithSessionID:_sessionID];
  120. [self populateSessionConfig:_sessionConfig withRequest:request];
  121. session = [NSURLSession sessionWithConfiguration:_sessionConfig
  122. delegate:self
  123. delegateQueue:[NSOperationQueue mainQueue]];
  124. postRequestTask = [session uploadTaskWithRequest:request fromFile:_uploadingFileURL];
  125. } else {
  126. // If we cannot write to file, just send it in the foreground.
  127. _sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
  128. [self populateSessionConfig:_sessionConfig withRequest:request];
  129. session = [NSURLSession sessionWithConfiguration:_sessionConfig
  130. delegate:self
  131. delegateQueue:[NSOperationQueue mainQueue]];
  132. postRequestTask = [session uploadTaskWithRequest:request fromData:request.HTTPBody];
  133. }
  134. if (!session || !postRequestTask) {
  135. NSError *error = [[NSError alloc]
  136. initWithDomain:kGULNetworkErrorDomain
  137. code:GULErrorCodeNetworkRequestCreation
  138. userInfo:@{kGULNetworkErrorContext : @"Cannot create network session"}];
  139. [self callCompletionHandler:handler withResponse:nil data:nil error:error];
  140. return nil;
  141. }
  142. _URLSession = session;
  143. // Save the session into memory.
  144. [[self class] setSessionInFetcherMap:self forSessionID:_sessionID];
  145. _request = [request copy];
  146. // Store completion handler because background session does not accept handler block but custom
  147. // delegate.
  148. _completionHandler = [handler copy];
  149. [postRequestTask resume];
  150. return _sessionID;
  151. }
  152. /// Sends an async GET request using NSURLSession for iOS >= 7.0, and returns an ID of the session.
  153. - (nullable NSString *)sessionIDFromAsyncGETRequest:(NSURLRequest *)request
  154. completionHandler:(GULNetworkURLSessionCompletionHandler)handler
  155. API_AVAILABLE(ios(7.0)) {
  156. if (_backgroundNetworkEnabled) {
  157. _sessionConfig = [self backgroundSessionConfigWithSessionID:_sessionID];
  158. } else {
  159. _sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
  160. }
  161. [self populateSessionConfig:_sessionConfig withRequest:request];
  162. // Do not cache the GET request.
  163. _sessionConfig.URLCache = nil;
  164. NSURLSession *session = [NSURLSession sessionWithConfiguration:_sessionConfig
  165. delegate:self
  166. delegateQueue:[NSOperationQueue mainQueue]];
  167. NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
  168. if (!session || !downloadTask) {
  169. NSError *error = [[NSError alloc]
  170. initWithDomain:kGULNetworkErrorDomain
  171. code:GULErrorCodeNetworkRequestCreation
  172. userInfo:@{kGULNetworkErrorContext : @"Cannot create network session"}];
  173. [self callCompletionHandler:handler withResponse:nil data:nil error:error];
  174. return nil;
  175. }
  176. _URLSession = session;
  177. // Save the session into memory.
  178. [[self class] setSessionInFetcherMap:self forSessionID:_sessionID];
  179. _request = [request copy];
  180. _completionHandler = [handler copy];
  181. [downloadTask resume];
  182. return _sessionID;
  183. }
  184. #pragma mark - NSURLSessionDataDelegate
  185. /// Called by the NSURLSession when the data task has received some of the expected data.
  186. /// Once the session is completed, URLSession:task:didCompleteWithError will be called and the
  187. /// completion handler will be called with the downloaded data.
  188. - (void)URLSession:(NSURLSession *)session
  189. dataTask:(NSURLSessionDataTask *)dataTask
  190. didReceiveData:(NSData *)data {
  191. @synchronized(self) {
  192. NSMutableData *mutableData = [[NSMutableData alloc] init];
  193. if (_downloadedData) {
  194. mutableData = _downloadedData.mutableCopy;
  195. }
  196. [mutableData appendData:data];
  197. _downloadedData = mutableData;
  198. }
  199. }
  200. #pragma mark - NSURLSessionTaskDelegate
  201. /// Called by the NSURLSession once the download task is completed. The file is saved in the
  202. /// provided URL so we need to read the data and store into _downloadedData. Once the session is
  203. /// completed, URLSession:task:didCompleteWithError will be called and the completion handler will
  204. /// be called with the downloaded data.
  205. - (void)URLSession:(NSURLSession *)session
  206. downloadTask:(NSURLSessionDownloadTask *)task
  207. didFinishDownloadingToURL:(NSURL *)url API_AVAILABLE(ios(7.0)) {
  208. if (!url.path) {
  209. [_loggerDelegate
  210. GULNetwork_logWithLevel:kGULNetworkLogLevelError
  211. messageCode:kGULNetworkMessageCodeURLSession001
  212. message:@"Unable to read downloaded data from empty temp path"];
  213. _downloadedData = nil;
  214. return;
  215. }
  216. NSError *error;
  217. _downloadedData = [NSData dataWithContentsOfFile:url.path options:0 error:&error];
  218. if (error) {
  219. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
  220. messageCode:kGULNetworkMessageCodeURLSession002
  221. message:@"Cannot read the content of downloaded data"
  222. context:error];
  223. _downloadedData = nil;
  224. }
  225. }
  226. #if TARGET_OS_IOS || TARGET_OS_TV
  227. - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
  228. API_AVAILABLE(ios(7.0)) {
  229. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelDebug
  230. messageCode:kGULNetworkMessageCodeURLSession003
  231. message:@"Background session finished"
  232. context:session.configuration.identifier];
  233. [self callSystemCompletionHandler:session.configuration.identifier];
  234. }
  235. #endif
  236. - (void)URLSession:(NSURLSession *)session
  237. task:(NSURLSessionTask *)task
  238. didCompleteWithError:(NSError *)error API_AVAILABLE(ios(7.0)) {
  239. // Avoid any chance of recursive behavior leading to it being used repeatedly.
  240. GULNetworkURLSessionCompletionHandler handler = _completionHandler;
  241. _completionHandler = nil;
  242. if (task.response) {
  243. // The following assertion should always be true for HTTP requests, see https://goo.gl/gVLxT7.
  244. NSAssert([task.response isKindOfClass:[NSHTTPURLResponse class]], @"URL response must be HTTP");
  245. // The server responded so ignore the error created by the system.
  246. error = nil;
  247. } else if (!error) {
  248. error = [[NSError alloc]
  249. initWithDomain:kGULNetworkErrorDomain
  250. code:GULErrorCodeNetworkInvalidResponse
  251. userInfo:@{kGULNetworkErrorContext : @"Network Error: Empty network response"}];
  252. }
  253. [self callCompletionHandler:handler
  254. withResponse:(NSHTTPURLResponse *)task.response
  255. data:_downloadedData
  256. error:error];
  257. // Remove the temp file to avoid trashing devices with lots of temp files.
  258. [self removeTempItemAtURL:_uploadingFileURL];
  259. // Try to clean up stale files again.
  260. [self maybeRemoveTempFilesAtURL:_networkDirectoryURL
  261. expiringTime:kGULNetworkTempFolderExpireTime];
  262. // This is called without checking the sessionID here since non-background sessions
  263. // won't have an ID.
  264. [session finishTasksAndInvalidate];
  265. // Explicitly remove the session so it won't be reused. The weak map table should
  266. // remove the session on deallocation, but dealloc may not happen immediately after
  267. // calling `finishTasksAndInvalidate`.
  268. NSString *sessionID = session.configuration.identifier;
  269. [[self class] setSessionInFetcherMap:nil forSessionID:sessionID];
  270. }
  271. - (void)URLSession:(NSURLSession *)session
  272. task:(NSURLSessionTask *)task
  273. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
  274. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition,
  275. NSURLCredential *credential))completionHandler
  276. API_AVAILABLE(ios(7.0)) {
  277. // The handling is modeled after GTMSessionFetcher.
  278. if ([challenge.protectionSpace.authenticationMethod
  279. isEqualToString:NSURLAuthenticationMethodServerTrust]) {
  280. SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
  281. if (serverTrust == NULL) {
  282. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelDebug
  283. messageCode:kGULNetworkMessageCodeURLSession004
  284. message:@"Received empty server trust for host. Host"
  285. context:_request.URL];
  286. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  287. return;
  288. }
  289. NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];
  290. if (!credential) {
  291. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelWarning
  292. messageCode:kGULNetworkMessageCodeURLSession005
  293. message:@"Unable to verify server identity. Host"
  294. context:_request.URL];
  295. completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
  296. return;
  297. }
  298. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelDebug
  299. messageCode:kGULNetworkMessageCodeURLSession006
  300. message:@"Received SSL challenge for host. Host"
  301. context:_request.URL];
  302. void (^callback)(BOOL) = ^(BOOL allow) {
  303. if (allow) {
  304. completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
  305. } else {
  306. [self->_loggerDelegate
  307. GULNetwork_logWithLevel:kGULNetworkLogLevelDebug
  308. messageCode:kGULNetworkMessageCodeURLSession007
  309. message:@"Cancelling authentication challenge for host. Host"
  310. context:self->_request.URL];
  311. completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
  312. }
  313. };
  314. // Retain the trust object to avoid a SecTrustEvaluate() crash on iOS 7.
  315. CFRetain(serverTrust);
  316. // Evaluate the certificate chain.
  317. //
  318. // The delegate queue may be the main thread. Trust evaluation could cause some
  319. // blocking network activity, so we must evaluate async, as documented at
  320. // https://developer.apple.com/library/ios/technotes/tn2232/
  321. dispatch_queue_t evaluateBackgroundQueue =
  322. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  323. dispatch_async(evaluateBackgroundQueue, ^{
  324. SecTrustResultType trustEval = kSecTrustResultInvalid;
  325. BOOL shouldAllow;
  326. OSStatus trustError;
  327. @synchronized([GULNetworkURLSession class]) {
  328. trustError = SecTrustEvaluate(serverTrust, &trustEval);
  329. }
  330. if (trustError != errSecSuccess) {
  331. [self->_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
  332. messageCode:kGULNetworkMessageCodeURLSession008
  333. message:@"Cannot evaluate server trust. Error, host"
  334. contexts:@[ @(trustError), self->_request.URL ]];
  335. shouldAllow = NO;
  336. } else {
  337. // Having a trust level "unspecified" by the user is the usual result, described at
  338. // https://developer.apple.com/library/mac/qa/qa1360
  339. shouldAllow =
  340. (trustEval == kSecTrustResultUnspecified || trustEval == kSecTrustResultProceed);
  341. }
  342. // Call the call back with the permission.
  343. callback(shouldAllow);
  344. CFRelease(serverTrust);
  345. });
  346. return;
  347. }
  348. // Default handling for other Auth Challenges.
  349. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  350. }
  351. #pragma mark - Internal Methods
  352. /// Stores system completion handler with session ID as key.
  353. - (void)addSystemCompletionHandler:(GULNetworkSystemCompletionHandler)handler
  354. forSession:(NSString *)identifier {
  355. if (!handler) {
  356. [_loggerDelegate
  357. GULNetwork_logWithLevel:kGULNetworkLogLevelError
  358. messageCode:kGULNetworkMessageCodeURLSession009
  359. message:@"Cannot store nil system completion handler in network"];
  360. return;
  361. }
  362. if (!identifier.length) {
  363. [_loggerDelegate
  364. GULNetwork_logWithLevel:kGULNetworkLogLevelError
  365. messageCode:kGULNetworkMessageCodeURLSession010
  366. message:@"Cannot store system completion handler with empty network "
  367. "session identifier"];
  368. return;
  369. }
  370. GULMutableDictionary *systemCompletionHandlers =
  371. [[self class] sessionIDToSystemCompletionHandlerDictionary];
  372. if (systemCompletionHandlers[identifier]) {
  373. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelWarning
  374. messageCode:kGULNetworkMessageCodeURLSession011
  375. message:@"Got multiple system handlers for a single session ID"
  376. context:identifier];
  377. }
  378. systemCompletionHandlers[identifier] = handler;
  379. }
  380. /// Calls the system provided completion handler with the session ID stored in the dictionary.
  381. /// The handler will be removed from the dictionary after being called.
  382. - (void)callSystemCompletionHandler:(NSString *)identifier {
  383. GULMutableDictionary *systemCompletionHandlers =
  384. [[self class] sessionIDToSystemCompletionHandlerDictionary];
  385. GULNetworkSystemCompletionHandler handler = [systemCompletionHandlers objectForKey:identifier];
  386. if (handler) {
  387. [systemCompletionHandlers removeObjectForKey:identifier];
  388. dispatch_async(dispatch_get_main_queue(), ^{
  389. handler();
  390. });
  391. }
  392. }
  393. /// Sets or updates the session ID of this session.
  394. - (void)setSessionID:(NSString *)sessionID {
  395. _sessionID = [sessionID copy];
  396. }
  397. /// Creates a background session configuration with the session ID using the supported method.
  398. - (NSURLSessionConfiguration *)backgroundSessionConfigWithSessionID:(NSString *)sessionID
  399. API_AVAILABLE(ios(7.0)) {
  400. #if (TARGET_OS_OSX && defined(MAC_OS_X_VERSION_10_10) && \
  401. MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_10) || \
  402. TARGET_OS_TV || \
  403. (TARGET_OS_IOS && defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0)
  404. // iOS 8/10.10 builds require the new backgroundSessionConfiguration method name.
  405. return [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:sessionID];
  406. #elif (TARGET_OS_OSX && defined(MAC_OS_X_VERSION_10_10) && \
  407. MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10) || \
  408. (TARGET_OS_IOS && defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0)
  409. // Do a runtime check to avoid a deprecation warning about using
  410. // +backgroundSessionConfiguration: on iOS 8.
  411. if ([NSURLSessionConfiguration
  412. respondsToSelector:@selector(backgroundSessionConfigurationWithIdentifier:)]) {
  413. // Running on iOS 8+/OS X 10.10+.
  414. #pragma clang diagnostic push
  415. #pragma clang diagnostic ignored "-Wunguarded-availability"
  416. return [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:sessionID];
  417. #pragma clang diagnostic pop
  418. } else {
  419. // Running on iOS 7/OS X 10.9.
  420. return [NSURLSessionConfiguration backgroundSessionConfiguration:sessionID];
  421. }
  422. #else
  423. // Building with an SDK earlier than iOS 8/OS X 10.10.
  424. return [NSURLSessionConfiguration backgroundSessionConfiguration:sessionID];
  425. #endif
  426. }
  427. - (void)maybeRemoveTempFilesAtURL:(NSURL *)folderURL expiringTime:(NSTimeInterval)staleTime {
  428. if (!folderURL.absoluteString.length) {
  429. return;
  430. }
  431. NSFileManager *fileManager = [NSFileManager defaultManager];
  432. NSError *error = nil;
  433. NSArray *properties = @[ NSURLCreationDateKey ];
  434. NSArray *directoryContent =
  435. [fileManager contentsOfDirectoryAtURL:folderURL
  436. includingPropertiesForKeys:properties
  437. options:NSDirectoryEnumerationSkipsSubdirectoryDescendants
  438. error:&error];
  439. if (error && error.code != NSFileReadNoSuchFileError) {
  440. [_loggerDelegate
  441. GULNetwork_logWithLevel:kGULNetworkLogLevelDebug
  442. messageCode:kGULNetworkMessageCodeURLSession012
  443. message:@"Cannot get files from the temporary network folder. Error"
  444. context:error];
  445. return;
  446. }
  447. if (!directoryContent.count) {
  448. return;
  449. }
  450. NSTimeInterval now = [NSDate date].timeIntervalSince1970;
  451. for (NSURL *tempFile in directoryContent) {
  452. NSDate *creationDate;
  453. BOOL getCreationDate = [tempFile getResourceValue:&creationDate
  454. forKey:NSURLCreationDateKey
  455. error:NULL];
  456. if (!getCreationDate) {
  457. continue;
  458. }
  459. NSTimeInterval creationTimeInterval = creationDate.timeIntervalSince1970;
  460. if (fabs(now - creationTimeInterval) > staleTime) {
  461. [self removeTempItemAtURL:tempFile];
  462. }
  463. }
  464. }
  465. /// Removes the temporary file written to disk for sending the request. It has to be cleaned up
  466. /// after the session is done.
  467. - (void)removeTempItemAtURL:(NSURL *)fileURL {
  468. if (!fileURL.absoluteString.length) {
  469. return;
  470. }
  471. NSFileManager *fileManager = [NSFileManager defaultManager];
  472. NSError *error = nil;
  473. if (![fileManager removeItemAtURL:fileURL error:&error] && error.code != NSFileNoSuchFileError) {
  474. [_loggerDelegate
  475. GULNetwork_logWithLevel:kGULNetworkLogLevelError
  476. messageCode:kGULNetworkMessageCodeURLSession013
  477. message:@"Failed to remove temporary uploading data file. Error"
  478. context:error.localizedDescription];
  479. }
  480. }
  481. /// Gets the fetcher with the session ID.
  482. + (instancetype)fetcherWithSessionIdentifier:(NSString *)sessionIdentifier {
  483. GULNetworkURLSession *session = [self sessionFromFetcherMapForSessionID:sessionIdentifier];
  484. if (!session && [sessionIdentifier hasPrefix:kGULNetworkBackgroundSessionConfigIDPrefix]) {
  485. session = [[GULNetworkURLSession alloc] initWithNetworkLoggerDelegate:nil];
  486. [session setSessionID:sessionIdentifier];
  487. [self setSessionInFetcherMap:session forSessionID:sessionIdentifier];
  488. }
  489. return session;
  490. }
  491. /// Returns a map of the fetcher by session ID. Creates a map if it is not created.
  492. /// When reading and writing from/to the session map, don't use this method directly.
  493. /// To avoid thread safety issues, use one of the helper methods at the bottom of the
  494. /// file: setSessionInFetcherMap:forSessionID:, sessionFromFetcherMapForSessionID:
  495. + (NSMapTable<NSString *, GULNetworkURLSession *> *)sessionIDToFetcherMap {
  496. static NSMapTable *sessionIDToFetcherMap;
  497. static dispatch_once_t sessionMapOnceToken;
  498. dispatch_once(&sessionMapOnceToken, ^{
  499. sessionIDToFetcherMap = [NSMapTable strongToWeakObjectsMapTable];
  500. });
  501. return sessionIDToFetcherMap;
  502. }
  503. + (NSLock *)sessionIDToFetcherMapReadWriteLock {
  504. static NSLock *lock;
  505. static dispatch_once_t onceToken;
  506. dispatch_once(&onceToken, ^{
  507. lock = [[NSLock alloc] init];
  508. });
  509. return lock;
  510. }
  511. /// Returns a map of system provided completion handler by session ID. Creates a map if it is not
  512. /// created.
  513. + (GULMutableDictionary *)sessionIDToSystemCompletionHandlerDictionary {
  514. static GULMutableDictionary *systemCompletionHandlers;
  515. static dispatch_once_t systemCompletionHandlerOnceToken;
  516. dispatch_once(&systemCompletionHandlerOnceToken, ^{
  517. systemCompletionHandlers = [[GULMutableDictionary alloc] init];
  518. });
  519. return systemCompletionHandlers;
  520. }
  521. - (NSURL *)temporaryFilePathWithSessionID:(NSString *)sessionID {
  522. NSString *tempName = [NSString stringWithFormat:@"GULUpload_temp_%@", sessionID];
  523. return [_networkDirectoryURL URLByAppendingPathComponent:tempName];
  524. }
  525. /// Makes sure that the directory to store temp files exists. If not, tries to create it and returns
  526. /// YES. If there is anything wrong, returns NO.
  527. - (BOOL)ensureTemporaryDirectoryExists {
  528. NSFileManager *fileManager = [NSFileManager defaultManager];
  529. NSError *error = nil;
  530. // Create a temporary directory if it does not exist or was deleted.
  531. if ([_networkDirectoryURL checkResourceIsReachableAndReturnError:&error]) {
  532. return YES;
  533. }
  534. if (error && error.code != NSFileReadNoSuchFileError) {
  535. [_loggerDelegate
  536. GULNetwork_logWithLevel:kGULNetworkLogLevelWarning
  537. messageCode:kGULNetworkMessageCodeURLSession014
  538. message:@"Error while trying to access Network temp folder. Error"
  539. context:error];
  540. }
  541. NSError *writeError = nil;
  542. [fileManager createDirectoryAtURL:_networkDirectoryURL
  543. withIntermediateDirectories:YES
  544. attributes:nil
  545. error:&writeError];
  546. if (writeError) {
  547. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
  548. messageCode:kGULNetworkMessageCodeURLSession015
  549. message:@"Cannot create temporary directory. Error"
  550. context:writeError];
  551. return NO;
  552. }
  553. // Set the iCloud exclusion attribute on the Documents URL.
  554. [self excludeFromBackupForURL:_networkDirectoryURL];
  555. return YES;
  556. }
  557. - (void)excludeFromBackupForURL:(NSURL *)url {
  558. if (!url.path) {
  559. return;
  560. }
  561. // Set the iCloud exclusion attribute on the Documents URL.
  562. NSError *preventBackupError = nil;
  563. [url setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:&preventBackupError];
  564. if (preventBackupError) {
  565. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
  566. messageCode:kGULNetworkMessageCodeURLSession016
  567. message:@"Cannot exclude temporary folder from iTunes backup"];
  568. }
  569. }
  570. - (void)URLSession:(NSURLSession *)session
  571. task:(NSURLSessionTask *)task
  572. willPerformHTTPRedirection:(NSHTTPURLResponse *)response
  573. newRequest:(NSURLRequest *)request
  574. completionHandler:(void (^)(NSURLRequest *))completionHandler API_AVAILABLE(ios(7.0)) {
  575. NSArray *nonAllowedRedirectionCodes = @[
  576. @(kGULNetworkHTTPStatusCodeFound), @(kGULNetworkHTTPStatusCodeMovedPermanently),
  577. @(kGULNetworkHTTPStatusCodeMovedTemporarily), @(kGULNetworkHTTPStatusCodeMultipleChoices)
  578. ];
  579. // Allow those not in the non allowed list to be followed.
  580. if (![nonAllowedRedirectionCodes containsObject:@(response.statusCode)]) {
  581. completionHandler(request);
  582. return;
  583. }
  584. // Do not allow redirection if the response code is in the non-allowed list.
  585. NSURLRequest *newRequest = request;
  586. if (response) {
  587. newRequest = nil;
  588. }
  589. completionHandler(newRequest);
  590. }
  591. #pragma mark - Helper Methods
  592. + (void)setSessionInFetcherMap:(GULNetworkURLSession *)session forSessionID:(NSString *)sessionID {
  593. [[self sessionIDToFetcherMapReadWriteLock] lock];
  594. GULNetworkURLSession *existingSession =
  595. [[[self class] sessionIDToFetcherMap] objectForKey:sessionID];
  596. if (existingSession) {
  597. if (session) {
  598. NSString *message = [NSString stringWithFormat:@"Discarding session: %@", existingSession];
  599. [existingSession->_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelInfo
  600. messageCode:kGULNetworkMessageCodeURLSession019
  601. message:message];
  602. }
  603. [existingSession->_URLSession finishTasksAndInvalidate];
  604. }
  605. if (session) {
  606. [[[self class] sessionIDToFetcherMap] setObject:session forKey:sessionID];
  607. } else {
  608. [[[self class] sessionIDToFetcherMap] removeObjectForKey:sessionID];
  609. }
  610. [[self sessionIDToFetcherMapReadWriteLock] unlock];
  611. }
  612. + (nullable GULNetworkURLSession *)sessionFromFetcherMapForSessionID:(NSString *)sessionID {
  613. [[self sessionIDToFetcherMapReadWriteLock] lock];
  614. GULNetworkURLSession *session = [[[self class] sessionIDToFetcherMap] objectForKey:sessionID];
  615. [[self sessionIDToFetcherMapReadWriteLock] unlock];
  616. return session;
  617. }
  618. - (void)callCompletionHandler:(GULNetworkURLSessionCompletionHandler)handler
  619. withResponse:(NSHTTPURLResponse *)response
  620. data:(NSData *)data
  621. error:(NSError *)error {
  622. if (error) {
  623. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
  624. messageCode:kGULNetworkMessageCodeURLSession017
  625. message:@"Encounter network error. Code, error"
  626. contexts:@[ @(error.code), error ]];
  627. }
  628. if (handler) {
  629. dispatch_async(dispatch_get_main_queue(), ^{
  630. handler(response, data, self->_sessionID, error);
  631. });
  632. }
  633. }
  634. // Always use the request parameters even if the default session configuration is more restrictive.
  635. - (void)populateSessionConfig:(NSURLSessionConfiguration *)sessionConfig
  636. withRequest:(NSURLRequest *)request API_AVAILABLE(ios(7.0)) {
  637. sessionConfig.HTTPAdditionalHeaders = request.allHTTPHeaderFields;
  638. sessionConfig.timeoutIntervalForRequest = request.timeoutInterval;
  639. sessionConfig.timeoutIntervalForResource = request.timeoutInterval;
  640. sessionConfig.requestCachePolicy = request.cachePolicy;
  641. }
  642. @end