GULNetworkURLSession.m 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 "GoogleUtilities/Network/Private/GULNetworkURLSession.h"
  16. #import <GoogleUtilities/GULLogger.h>
  17. #import "GoogleUtilities/Network/Private/GULMutableDictionary.h"
  18. #import "GoogleUtilities/Network/Private/GULNetworkConstants.h"
  19. #import "GoogleUtilities/Network/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. #pragma clang diagnostic push
  329. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  330. trustError = SecTrustEvaluate(serverTrust, &trustEval);
  331. #pragma clang dianostic pop
  332. }
  333. if (trustError != errSecSuccess) {
  334. [self->_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
  335. messageCode:kGULNetworkMessageCodeURLSession008
  336. message:@"Cannot evaluate server trust. Error, host"
  337. contexts:@[ @(trustError), self->_request.URL ]];
  338. shouldAllow = NO;
  339. } else {
  340. // Having a trust level "unspecified" by the user is the usual result, described at
  341. // https://developer.apple.com/library/mac/qa/qa1360
  342. shouldAllow =
  343. (trustEval == kSecTrustResultUnspecified || trustEval == kSecTrustResultProceed);
  344. }
  345. // Call the call back with the permission.
  346. callback(shouldAllow);
  347. CFRelease(serverTrust);
  348. });
  349. return;
  350. }
  351. // Default handling for other Auth Challenges.
  352. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  353. }
  354. #pragma mark - Internal Methods
  355. /// Stores system completion handler with session ID as key.
  356. - (void)addSystemCompletionHandler:(GULNetworkSystemCompletionHandler)handler
  357. forSession:(NSString *)identifier {
  358. if (!handler) {
  359. [_loggerDelegate
  360. GULNetwork_logWithLevel:kGULNetworkLogLevelError
  361. messageCode:kGULNetworkMessageCodeURLSession009
  362. message:@"Cannot store nil system completion handler in network"];
  363. return;
  364. }
  365. if (!identifier.length) {
  366. [_loggerDelegate
  367. GULNetwork_logWithLevel:kGULNetworkLogLevelError
  368. messageCode:kGULNetworkMessageCodeURLSession010
  369. message:@"Cannot store system completion handler with empty network "
  370. "session identifier"];
  371. return;
  372. }
  373. GULMutableDictionary *systemCompletionHandlers =
  374. [[self class] sessionIDToSystemCompletionHandlerDictionary];
  375. if (systemCompletionHandlers[identifier]) {
  376. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelWarning
  377. messageCode:kGULNetworkMessageCodeURLSession011
  378. message:@"Got multiple system handlers for a single session ID"
  379. context:identifier];
  380. }
  381. systemCompletionHandlers[identifier] = handler;
  382. }
  383. /// Calls the system provided completion handler with the session ID stored in the dictionary.
  384. /// The handler will be removed from the dictionary after being called.
  385. - (void)callSystemCompletionHandler:(NSString *)identifier {
  386. GULMutableDictionary *systemCompletionHandlers =
  387. [[self class] sessionIDToSystemCompletionHandlerDictionary];
  388. GULNetworkSystemCompletionHandler handler = [systemCompletionHandlers objectForKey:identifier];
  389. if (handler) {
  390. [systemCompletionHandlers removeObjectForKey:identifier];
  391. dispatch_async(dispatch_get_main_queue(), ^{
  392. handler();
  393. });
  394. }
  395. }
  396. /// Sets or updates the session ID of this session.
  397. - (void)setSessionID:(NSString *)sessionID {
  398. _sessionID = [sessionID copy];
  399. }
  400. /// Creates a background session configuration with the session ID using the supported method.
  401. - (NSURLSessionConfiguration *)backgroundSessionConfigWithSessionID:(NSString *)sessionID
  402. API_AVAILABLE(ios(7.0)) {
  403. #if (TARGET_OS_OSX && defined(MAC_OS_X_VERSION_10_10) && \
  404. MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_10) || \
  405. TARGET_OS_TV || \
  406. (TARGET_OS_IOS && defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0)
  407. // iOS 8/10.10 builds require the new backgroundSessionConfiguration method name.
  408. return [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:sessionID];
  409. #elif (TARGET_OS_OSX && defined(MAC_OS_X_VERSION_10_10) && \
  410. MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_10) || \
  411. (TARGET_OS_IOS && defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0)
  412. // Do a runtime check to avoid a deprecation warning about using
  413. // +backgroundSessionConfiguration: on iOS 8.
  414. if ([NSURLSessionConfiguration
  415. respondsToSelector:@selector(backgroundSessionConfigurationWithIdentifier:)]) {
  416. // Running on iOS 8+/OS X 10.10+.
  417. #pragma clang diagnostic push
  418. #pragma clang diagnostic ignored "-Wunguarded-availability"
  419. return [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:sessionID];
  420. #pragma clang diagnostic pop
  421. } else {
  422. // Running on iOS 7/OS X 10.9.
  423. return [NSURLSessionConfiguration backgroundSessionConfiguration:sessionID];
  424. }
  425. #else
  426. // Building with an SDK earlier than iOS 8/OS X 10.10.
  427. return [NSURLSessionConfiguration backgroundSessionConfiguration:sessionID];
  428. #endif
  429. }
  430. - (void)maybeRemoveTempFilesAtURL:(NSURL *)folderURL expiringTime:(NSTimeInterval)staleTime {
  431. if (!folderURL.absoluteString.length) {
  432. return;
  433. }
  434. NSFileManager *fileManager = [NSFileManager defaultManager];
  435. NSError *error = nil;
  436. NSArray *properties = @[ NSURLCreationDateKey ];
  437. NSArray *directoryContent =
  438. [fileManager contentsOfDirectoryAtURL:folderURL
  439. includingPropertiesForKeys:properties
  440. options:NSDirectoryEnumerationSkipsSubdirectoryDescendants
  441. error:&error];
  442. if (error && error.code != NSFileReadNoSuchFileError) {
  443. [_loggerDelegate
  444. GULNetwork_logWithLevel:kGULNetworkLogLevelDebug
  445. messageCode:kGULNetworkMessageCodeURLSession012
  446. message:@"Cannot get files from the temporary network folder. Error"
  447. context:error];
  448. return;
  449. }
  450. if (!directoryContent.count) {
  451. return;
  452. }
  453. NSTimeInterval now = [NSDate date].timeIntervalSince1970;
  454. for (NSURL *tempFile in directoryContent) {
  455. NSDate *creationDate;
  456. BOOL getCreationDate = [tempFile getResourceValue:&creationDate
  457. forKey:NSURLCreationDateKey
  458. error:NULL];
  459. if (!getCreationDate) {
  460. continue;
  461. }
  462. NSTimeInterval creationTimeInterval = creationDate.timeIntervalSince1970;
  463. if (fabs(now - creationTimeInterval) > staleTime) {
  464. [self removeTempItemAtURL:tempFile];
  465. }
  466. }
  467. }
  468. /// Removes the temporary file written to disk for sending the request. It has to be cleaned up
  469. /// after the session is done.
  470. - (void)removeTempItemAtURL:(NSURL *)fileURL {
  471. if (!fileURL.absoluteString.length) {
  472. return;
  473. }
  474. NSFileManager *fileManager = [NSFileManager defaultManager];
  475. NSError *error = nil;
  476. if (![fileManager removeItemAtURL:fileURL error:&error] && error.code != NSFileNoSuchFileError) {
  477. [_loggerDelegate
  478. GULNetwork_logWithLevel:kGULNetworkLogLevelError
  479. messageCode:kGULNetworkMessageCodeURLSession013
  480. message:@"Failed to remove temporary uploading data file. Error"
  481. context:error.localizedDescription];
  482. }
  483. }
  484. /// Gets the fetcher with the session ID.
  485. + (instancetype)fetcherWithSessionIdentifier:(NSString *)sessionIdentifier {
  486. GULNetworkURLSession *session = [self sessionFromFetcherMapForSessionID:sessionIdentifier];
  487. if (!session && [sessionIdentifier hasPrefix:kGULNetworkBackgroundSessionConfigIDPrefix]) {
  488. session = [[GULNetworkURLSession alloc] initWithNetworkLoggerDelegate:nil];
  489. [session setSessionID:sessionIdentifier];
  490. [self setSessionInFetcherMap:session forSessionID:sessionIdentifier];
  491. }
  492. return session;
  493. }
  494. /// Returns a map of the fetcher by session ID. Creates a map if it is not created.
  495. /// When reading and writing from/to the session map, don't use this method directly.
  496. /// To avoid thread safety issues, use one of the helper methods at the bottom of the
  497. /// file: setSessionInFetcherMap:forSessionID:, sessionFromFetcherMapForSessionID:
  498. + (NSMapTable<NSString *, GULNetworkURLSession *> *)sessionIDToFetcherMap {
  499. static NSMapTable *sessionIDToFetcherMap;
  500. static dispatch_once_t sessionMapOnceToken;
  501. dispatch_once(&sessionMapOnceToken, ^{
  502. sessionIDToFetcherMap = [NSMapTable strongToWeakObjectsMapTable];
  503. });
  504. return sessionIDToFetcherMap;
  505. }
  506. + (NSLock *)sessionIDToFetcherMapReadWriteLock {
  507. static NSLock *lock;
  508. static dispatch_once_t onceToken;
  509. dispatch_once(&onceToken, ^{
  510. lock = [[NSLock alloc] init];
  511. });
  512. return lock;
  513. }
  514. /// Returns a map of system provided completion handler by session ID. Creates a map if it is not
  515. /// created.
  516. + (GULMutableDictionary *)sessionIDToSystemCompletionHandlerDictionary {
  517. static GULMutableDictionary *systemCompletionHandlers;
  518. static dispatch_once_t systemCompletionHandlerOnceToken;
  519. dispatch_once(&systemCompletionHandlerOnceToken, ^{
  520. systemCompletionHandlers = [[GULMutableDictionary alloc] init];
  521. });
  522. return systemCompletionHandlers;
  523. }
  524. - (NSURL *)temporaryFilePathWithSessionID:(NSString *)sessionID {
  525. NSString *tempName = [NSString stringWithFormat:@"GULUpload_temp_%@", sessionID];
  526. return [_networkDirectoryURL URLByAppendingPathComponent:tempName];
  527. }
  528. /// Makes sure that the directory to store temp files exists. If not, tries to create it and returns
  529. /// YES. If there is anything wrong, returns NO.
  530. - (BOOL)ensureTemporaryDirectoryExists {
  531. NSFileManager *fileManager = [NSFileManager defaultManager];
  532. NSError *error = nil;
  533. // Create a temporary directory if it does not exist or was deleted.
  534. if ([_networkDirectoryURL checkResourceIsReachableAndReturnError:&error]) {
  535. return YES;
  536. }
  537. if (error && error.code != NSFileReadNoSuchFileError) {
  538. [_loggerDelegate
  539. GULNetwork_logWithLevel:kGULNetworkLogLevelWarning
  540. messageCode:kGULNetworkMessageCodeURLSession014
  541. message:@"Error while trying to access Network temp folder. Error"
  542. context:error];
  543. }
  544. NSError *writeError = nil;
  545. [fileManager createDirectoryAtURL:_networkDirectoryURL
  546. withIntermediateDirectories:YES
  547. attributes:nil
  548. error:&writeError];
  549. if (writeError) {
  550. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
  551. messageCode:kGULNetworkMessageCodeURLSession015
  552. message:@"Cannot create temporary directory. Error"
  553. context:writeError];
  554. return NO;
  555. }
  556. // Set the iCloud exclusion attribute on the Documents URL.
  557. [self excludeFromBackupForURL:_networkDirectoryURL];
  558. return YES;
  559. }
  560. - (void)excludeFromBackupForURL:(NSURL *)url {
  561. if (!url.path) {
  562. return;
  563. }
  564. // Set the iCloud exclusion attribute on the Documents URL.
  565. NSError *preventBackupError = nil;
  566. [url setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:&preventBackupError];
  567. if (preventBackupError) {
  568. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
  569. messageCode:kGULNetworkMessageCodeURLSession016
  570. message:@"Cannot exclude temporary folder from iTunes backup"];
  571. }
  572. }
  573. - (void)URLSession:(NSURLSession *)session
  574. task:(NSURLSessionTask *)task
  575. willPerformHTTPRedirection:(NSHTTPURLResponse *)response
  576. newRequest:(NSURLRequest *)request
  577. completionHandler:(void (^)(NSURLRequest *))completionHandler API_AVAILABLE(ios(7.0)) {
  578. NSArray *nonAllowedRedirectionCodes = @[
  579. @(kGULNetworkHTTPStatusCodeFound), @(kGULNetworkHTTPStatusCodeMovedPermanently),
  580. @(kGULNetworkHTTPStatusCodeMovedTemporarily), @(kGULNetworkHTTPStatusCodeMultipleChoices)
  581. ];
  582. // Allow those not in the non allowed list to be followed.
  583. if (![nonAllowedRedirectionCodes containsObject:@(response.statusCode)]) {
  584. completionHandler(request);
  585. return;
  586. }
  587. // Do not allow redirection if the response code is in the non-allowed list.
  588. NSURLRequest *newRequest = request;
  589. if (response) {
  590. newRequest = nil;
  591. }
  592. completionHandler(newRequest);
  593. }
  594. #pragma mark - Helper Methods
  595. + (void)setSessionInFetcherMap:(GULNetworkURLSession *)session forSessionID:(NSString *)sessionID {
  596. [[self sessionIDToFetcherMapReadWriteLock] lock];
  597. GULNetworkURLSession *existingSession =
  598. [[[self class] sessionIDToFetcherMap] objectForKey:sessionID];
  599. if (existingSession) {
  600. if (session) {
  601. NSString *message = [NSString stringWithFormat:@"Discarding session: %@", existingSession];
  602. [existingSession->_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelInfo
  603. messageCode:kGULNetworkMessageCodeURLSession019
  604. message:message];
  605. }
  606. [existingSession->_URLSession finishTasksAndInvalidate];
  607. }
  608. if (session) {
  609. [[[self class] sessionIDToFetcherMap] setObject:session forKey:sessionID];
  610. } else {
  611. [[[self class] sessionIDToFetcherMap] removeObjectForKey:sessionID];
  612. }
  613. [[self sessionIDToFetcherMapReadWriteLock] unlock];
  614. }
  615. + (nullable GULNetworkURLSession *)sessionFromFetcherMapForSessionID:(NSString *)sessionID {
  616. [[self sessionIDToFetcherMapReadWriteLock] lock];
  617. GULNetworkURLSession *session = [[[self class] sessionIDToFetcherMap] objectForKey:sessionID];
  618. [[self sessionIDToFetcherMapReadWriteLock] unlock];
  619. return session;
  620. }
  621. - (void)callCompletionHandler:(GULNetworkURLSessionCompletionHandler)handler
  622. withResponse:(NSHTTPURLResponse *)response
  623. data:(NSData *)data
  624. error:(NSError *)error {
  625. if (error) {
  626. [_loggerDelegate GULNetwork_logWithLevel:kGULNetworkLogLevelError
  627. messageCode:kGULNetworkMessageCodeURLSession017
  628. message:@"Encounter network error. Code, error"
  629. contexts:@[ @(error.code), error ]];
  630. }
  631. if (handler) {
  632. dispatch_async(dispatch_get_main_queue(), ^{
  633. handler(response, data, self->_sessionID, error);
  634. });
  635. }
  636. }
  637. // Always use the request parameters even if the default session configuration is more restrictive.
  638. - (void)populateSessionConfig:(NSURLSessionConfiguration *)sessionConfig
  639. withRequest:(NSURLRequest *)request API_AVAILABLE(ios(7.0)) {
  640. sessionConfig.HTTPAdditionalHeaders = request.allHTTPHeaderFields;
  641. sessionConfig.timeoutIntervalForRequest = request.timeoutInterval;
  642. sessionConfig.timeoutIntervalForResource = request.timeoutInterval;
  643. sessionConfig.requestCachePolicy = request.cachePolicy;
  644. }
  645. @end