GULNetworkURLSession.m 30 KB

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