FIRNetworkURLSession.m 27 KB

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