You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1947 lines
72 KiB

6 years ago
  1. /* Copyright 2014 Google Inc. All rights reserved.
  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. */
  15. #if !defined(__has_feature) || !__has_feature(objc_arc)
  16. #error "This file requires ARC support."
  17. #endif
  18. #import "GTMSessionUploadFetcher.h"
  19. static NSString *const kGTMSessionIdentifierIsUploadChunkFetcherMetadataKey = @"_upChunk";
  20. static NSString *const kGTMSessionIdentifierUploadFileURLMetadataKey = @"_upFileURL";
  21. static NSString *const kGTMSessionIdentifierUploadFileLengthMetadataKey = @"_upFileLen";
  22. static NSString *const kGTMSessionIdentifierUploadLocationURLMetadataKey = @"_upLocURL";
  23. static NSString *const kGTMSessionIdentifierUploadMIMETypeMetadataKey = @"_uploadMIME";
  24. static NSString *const kGTMSessionIdentifierUploadChunkSizeMetadataKey = @"_upChSize";
  25. static NSString *const kGTMSessionIdentifierUploadCurrentOffsetMetadataKey = @"_upOffset";
  26. static NSString *const kGTMSessionHeaderXGoogUploadChunkGranularity = @"X-Goog-Upload-Chunk-Granularity";
  27. static NSString *const kGTMSessionHeaderXGoogUploadCommand = @"X-Goog-Upload-Command";
  28. static NSString *const kGTMSessionHeaderXGoogUploadContentLength = @"X-Goog-Upload-Content-Length";
  29. static NSString *const kGTMSessionHeaderXGoogUploadContentType = @"X-Goog-Upload-Content-Type";
  30. static NSString *const kGTMSessionHeaderXGoogUploadOffset = @"X-Goog-Upload-Offset";
  31. static NSString *const kGTMSessionHeaderXGoogUploadProtocol = @"X-Goog-Upload-Protocol";
  32. static NSString *const kGTMSessionHeaderXGoogUploadSizeReceived = @"X-Goog-Upload-Size-Received";
  33. static NSString *const kGTMSessionHeaderXGoogUploadStatus = @"X-Goog-Upload-Status";
  34. static NSString *const kGTMSessionHeaderXGoogUploadURL = @"X-Goog-Upload-URL";
  35. // Property of chunk fetchers identifying the parent upload fetcher. Non-retained NSValue.
  36. static NSString *const kGTMSessionUploadFetcherChunkParentKey = @"_uploadFetcherChunkParent";
  37. int64_t const kGTMSessionUploadFetcherUnknownFileSize = -1;
  38. int64_t const kGTMSessionUploadFetcherStandardChunkSize = (int64_t)LLONG_MAX;
  39. #if TARGET_OS_IPHONE
  40. int64_t const kGTMSessionUploadFetcherMaximumDemandBufferSize = 10 * 1024 * 1024; // 10 MB for iOS, watchOS, tvOS
  41. #else
  42. int64_t const kGTMSessionUploadFetcherMaximumDemandBufferSize = 100 * 1024 * 1024; // 100 MB for macOS
  43. #endif
  44. typedef NS_ENUM(NSUInteger, GTMSessionUploadFetcherStatus) {
  45. kStatusUnknown,
  46. kStatusActive,
  47. kStatusFinal,
  48. kStatusCancelled,
  49. };
  50. NSString *const kGTMSessionFetcherUploadLocationObtainedNotification =
  51. @"kGTMSessionFetcherUploadLocationObtainedNotification";
  52. #if !GTMSESSION_BUILD_COMBINED_SOURCES
  53. @interface GTMSessionFetcher (ProtectedMethods)
  54. // Access to non-public method on the parent fetcher class.
  55. - (void)stopFetchReleasingCallbacks:(BOOL)shouldReleaseCallbacks;
  56. - (void)createSessionIdentifierWithMetadata:(NSDictionary *)metadata;
  57. - (GTMSessionFetcherCompletionHandler)completionHandlerWithTarget:(id)target
  58. didFinishSelector:(SEL)finishedSelector;
  59. - (void)invokeOnCallbackQueue:(dispatch_queue_t)callbackQueue
  60. afterUserStopped:(BOOL)afterStopped
  61. block:(void (^)(void))block;
  62. - (NSTimer *)retryTimer;
  63. - (void)beginFetchForRetry;
  64. @property(readwrite, strong) NSData *downloadedData;
  65. - (void)releaseCallbacks;
  66. - (NSInteger)statusCodeUnsynchronized;
  67. - (BOOL)userStoppedFetching;
  68. @end
  69. #endif // !GTMSESSION_BUILD_COMBINED_SOURCES
  70. @interface GTMSessionUploadFetcher ()
  71. // Changing readonly to readwrite.
  72. @property(atomic, strong, readwrite) NSURLRequest *lastChunkRequest;
  73. @property(atomic, readwrite, assign) int64_t currentOffset;
  74. // Internal properties.
  75. @property(strong, atomic, GTM_NULLABLE) GTMSessionFetcher *fetcherInFlight; // Synchronized on self.
  76. @property(assign, atomic, getter=isSubdataGenerating) BOOL subdataGenerating;
  77. @property(assign, atomic) BOOL shouldInitiateOffsetQuery;
  78. @property(assign, atomic) int64_t uploadGranularity;
  79. @end
  80. @implementation GTMSessionUploadFetcher {
  81. GTMSessionFetcher *_chunkFetcher;
  82. // We'll call through to the delegate's completion handler.
  83. GTMSessionFetcherCompletionHandler _delegateCompletionHandler;
  84. dispatch_queue_t _delegateCallbackQueue;
  85. // The initial fetch's body length and bytes actually sent are
  86. // needed for calculating progress during subsequent chunk uploads
  87. int64_t _initialBodyLength;
  88. int64_t _initialBodySent;
  89. // The upload server address for the chunks of this upload session.
  90. NSURL *_uploadLocationURL;
  91. // _uploadData, _uploadDataProvider, or _uploadFileHandle may be set, but only one.
  92. NSData *_uploadData;
  93. NSFileHandle *_uploadFileHandle;
  94. GTMSessionUploadFetcherDataProvider _uploadDataProvider;
  95. NSURL *_uploadFileURL;
  96. int64_t _uploadFileLength;
  97. NSString *_uploadMIMEType;
  98. int64_t _chunkSize;
  99. int64_t _uploadGranularity;
  100. BOOL _isPaused;
  101. BOOL _isRestartedUpload;
  102. BOOL _shouldInitiateOffsetQuery;
  103. // Tied to useBackgroundSession property, since this property is applicable to chunk fetchers.
  104. BOOL _useBackgroundSessionOnChunkFetchers;
  105. // We keep the latest offset into the upload data just for progress reporting.
  106. int64_t _currentOffset;
  107. NSDictionary *_recentChunkReponseHeaders;
  108. NSInteger _recentChunkStatusCode;
  109. // For waiting, we need to know the fetcher in flight, if any, and if subdata generation
  110. // is in progress.
  111. GTMSessionFetcher *_fetcherInFlight;
  112. BOOL _isSubdataGenerating;
  113. BOOL _isCancelInFlight;
  114. GTMSessionUploadFetcherCancellationHandler _cancellationHandler;
  115. }
  116. + (void)load {
  117. [self uploadFetchersForBackgroundSessions];
  118. }
  119. + (instancetype)uploadFetcherWithRequest:(NSURLRequest *)request
  120. uploadMIMEType:(NSString *)uploadMIMEType
  121. chunkSize:(int64_t)chunkSize
  122. fetcherService:(GTMSessionFetcherService *)fetcherService {
  123. GTMSessionUploadFetcher *fetcher = [self uploadFetcherWithRequest:request
  124. fetcherService:fetcherService];
  125. [fetcher setLocationURL:nil
  126. uploadMIMEType:uploadMIMEType
  127. chunkSize:chunkSize];
  128. return fetcher;
  129. }
  130. + (instancetype)uploadFetcherWithLocation:(NSURL * GTM_NULLABLE_TYPE)uploadLocationURL
  131. uploadMIMEType:(NSString *)uploadMIMEType
  132. chunkSize:(int64_t)chunkSize
  133. fetcherService:(GTMSessionFetcherService *)fetcherService {
  134. GTMSessionUploadFetcher *fetcher = [self uploadFetcherWithRequest:nil
  135. fetcherService:fetcherService];
  136. [fetcher setLocationURL:uploadLocationURL
  137. uploadMIMEType:uploadMIMEType
  138. chunkSize:chunkSize];
  139. return fetcher;
  140. }
  141. + (instancetype)uploadFetcherForSessionIdentifierMetadata:(NSDictionary *)metadata {
  142. GTMSESSION_ASSERT_DEBUG(
  143. [metadata[kGTMSessionIdentifierIsUploadChunkFetcherMetadataKey] boolValue],
  144. @"Session identifier metadata is not for an upload fetcher: %@", metadata);
  145. NSNumber *uploadFileLengthNum = metadata[kGTMSessionIdentifierUploadFileLengthMetadataKey];
  146. GTMSESSION_ASSERT_DEBUG(uploadFileLengthNum != nil,
  147. @"Session metadata missing an UploadFileSize");
  148. if (uploadFileLengthNum == nil) return nil;
  149. int64_t uploadFileLength = [uploadFileLengthNum longLongValue];
  150. GTMSESSION_ASSERT_DEBUG(uploadFileLength >= 0, @"Session metadata UploadFileSize is unknown");
  151. NSString *uploadFileURLString = metadata[kGTMSessionIdentifierUploadFileURLMetadataKey];
  152. GTMSESSION_ASSERT_DEBUG(uploadFileURLString, @"Session metadata missing an UploadFileURL");
  153. if (uploadFileURLString == nil) return nil;
  154. NSURL *uploadFileURL = [NSURL URLWithString:uploadFileURLString];
  155. // There used to be a call here to NSURL checkResourceIsReachableAndReturnError: to check for the
  156. // existence of the file (also tried NSFileManager fileExistsAtPath:). We've determined
  157. // empirically that the check can fail at startup even when the upload file does in fact exist.
  158. // For now, we'll go ahead and restore the background upload fetcher. If the file doesn't exist,
  159. // it will fail later.
  160. NSString *uploadLocationURLString = metadata[kGTMSessionIdentifierUploadLocationURLMetadataKey];
  161. NSURL *uploadLocationURL =
  162. uploadLocationURLString ? [NSURL URLWithString:uploadLocationURLString] : nil;
  163. NSString *uploadMIMEType =
  164. metadata[kGTMSessionIdentifierUploadMIMETypeMetadataKey];
  165. int64_t uploadChunkSize =
  166. [metadata[kGTMSessionIdentifierUploadChunkSizeMetadataKey] longLongValue];
  167. if (uploadChunkSize <= 0) {
  168. uploadChunkSize = kGTMSessionUploadFetcherStandardChunkSize;
  169. }
  170. int64_t currentOffset =
  171. [metadata[kGTMSessionIdentifierUploadCurrentOffsetMetadataKey] longLongValue];
  172. GTMSESSION_ASSERT_DEBUG(currentOffset <= uploadFileLength,
  173. @"CurrentOffset (%lld) exceeds UploadFileSize (%lld)",
  174. currentOffset, uploadFileLength);
  175. if (currentOffset > uploadFileLength) return nil;
  176. GTMSessionUploadFetcher *uploadFetcher = [self uploadFetcherWithLocation:uploadLocationURL
  177. uploadMIMEType:uploadMIMEType
  178. chunkSize:uploadChunkSize
  179. fetcherService:nil];
  180. // Set the upload file length before setting the upload file URL tries to determine the length.
  181. [uploadFetcher setUploadFileLength:uploadFileLength];
  182. uploadFetcher.uploadFileURL = uploadFileURL;
  183. uploadFetcher.sessionUserInfo = metadata;
  184. uploadFetcher.useBackgroundSession = YES;
  185. uploadFetcher.currentOffset = currentOffset;
  186. uploadFetcher.delegateCallbackQueue = uploadFetcher.callbackQueue;
  187. uploadFetcher.allowedInsecureSchemes = @[ @"http" ]; // Allowed on restored upload fetcher.
  188. return uploadFetcher;
  189. }
  190. + (instancetype)uploadFetcherWithRequest:(NSURLRequest *)request
  191. fetcherService:(GTMSessionFetcherService *)fetcherService {
  192. // Internal utility method for instantiating fetchers
  193. GTMSessionUploadFetcher *fetcher;
  194. if ([fetcherService isKindOfClass:[GTMSessionFetcherService class]]) {
  195. fetcher = [fetcherService fetcherWithRequest:request
  196. fetcherClass:self];
  197. } else {
  198. fetcher = [self fetcherWithRequest:request];
  199. }
  200. fetcher.useBackgroundSession = YES;
  201. return fetcher;
  202. }
  203. + (NSPointerArray *)uploadFetcherPointerArrayForBackgroundSessions {
  204. static NSPointerArray *gUploadFetcherPointerArrayForBackgroundSessions = nil;
  205. static dispatch_once_t onceToken;
  206. dispatch_once(&onceToken, ^{
  207. gUploadFetcherPointerArrayForBackgroundSessions = [NSPointerArray weakObjectsPointerArray];
  208. });
  209. return gUploadFetcherPointerArrayForBackgroundSessions;
  210. }
  211. + (instancetype)uploadFetcherForSessionIdentifier:(NSString *)sessionIdentifier {
  212. GTMSESSION_ASSERT_DEBUG(sessionIdentifier != nil, @"Invalid session identifier");
  213. NSArray *uploadFetchersForBackgroundSessions = [self uploadFetchersForBackgroundSessions];
  214. for (GTMSessionUploadFetcher *uploadFetcher in uploadFetchersForBackgroundSessions) {
  215. if ([uploadFetcher.chunkFetcher.sessionIdentifier isEqual:sessionIdentifier]) {
  216. return uploadFetcher;
  217. }
  218. }
  219. return nil;
  220. }
  221. + (NSArray *)uploadFetchersForBackgroundSessions {
  222. // Collect the background session upload fetchers that are still in memory.
  223. NSPointerArray *uploadFetcherPointerArray = [self uploadFetcherPointerArrayForBackgroundSessions];
  224. [uploadFetcherPointerArray compact];
  225. NSMutableSet *restoredSessionIdentifiers = [[NSMutableSet alloc] init];
  226. NSMutableArray *uploadFetchers = [[NSMutableArray alloc] init];
  227. for (GTMSessionUploadFetcher *uploadFetcher in uploadFetcherPointerArray) {
  228. NSString *sessionIdentifier = uploadFetcher.chunkFetcher.sessionIdentifier;
  229. if (sessionIdentifier) {
  230. [restoredSessionIdentifiers addObject:sessionIdentifier];
  231. [uploadFetchers addObject:uploadFetcher];
  232. }
  233. }
  234. // The system may have other ongoing background upload sessions. Restore upload fetchers for those
  235. // too.
  236. NSArray *fetchers = [GTMSessionFetcher fetchersForBackgroundSessions];
  237. for (GTMSessionFetcher *fetcher in fetchers) {
  238. NSString *sessionIdentifier = fetcher.sessionIdentifier;
  239. if (!sessionIdentifier || [restoredSessionIdentifiers containsObject:sessionIdentifier]) {
  240. continue;
  241. }
  242. NSDictionary *sessionIdentifierMetadata = [fetcher sessionIdentifierMetadata];
  243. if (sessionIdentifierMetadata == nil) {
  244. continue;
  245. }
  246. if (![sessionIdentifierMetadata[kGTMSessionIdentifierIsUploadChunkFetcherMetadataKey] boolValue]) {
  247. continue;
  248. }
  249. GTMSessionUploadFetcher *uploadFetcher =
  250. [self uploadFetcherForSessionIdentifierMetadata:sessionIdentifierMetadata];
  251. if (uploadFetcher == nil) {
  252. // Something went wrong with this upload fetcher, so kill the restored chunk fetcher.
  253. [fetcher stopFetching];
  254. continue;
  255. }
  256. [uploadFetchers addObject:uploadFetcher];
  257. uploadFetcher->_chunkFetcher = fetcher;
  258. uploadFetcher->_fetcherInFlight = fetcher;
  259. [uploadFetcher attachSendProgressBlockToChunkFetcher:fetcher];
  260. fetcher.completionHandler =
  261. [fetcher completionHandlerWithTarget:uploadFetcher
  262. didFinishSelector:@selector(chunkFetcher:finishedWithData:error:)];
  263. GTMSESSION_LOG_DEBUG(@"%@ restoring upload fetcher %@ for chunk fetcher %@",
  264. [self class], uploadFetcher, fetcher);
  265. }
  266. return uploadFetchers;
  267. }
  268. - (void)setUploadData:(NSData *)data {
  269. BOOL changed = NO;
  270. @synchronized(self) {
  271. GTMSessionMonitorSynchronized(self);
  272. if (_uploadData != data) {
  273. _uploadData = data;
  274. changed = YES;
  275. }
  276. }
  277. if (changed) {
  278. [self setupRequestHeaders];
  279. }
  280. }
  281. - (NSData *)uploadData {
  282. @synchronized(self) {
  283. GTMSessionMonitorSynchronized(self);
  284. return _uploadData;
  285. }
  286. }
  287. - (void)setUploadFileHandle:(NSFileHandle *)fh {
  288. BOOL changed = NO;
  289. @synchronized(self) {
  290. GTMSessionMonitorSynchronized(self);
  291. if (_uploadFileHandle != fh) {
  292. _uploadFileHandle = fh;
  293. changed = YES;
  294. }
  295. }
  296. if (changed) {
  297. [self setupRequestHeaders];
  298. }
  299. }
  300. - (NSFileHandle *)uploadFileHandle {
  301. @synchronized(self) {
  302. GTMSessionMonitorSynchronized(self);
  303. return _uploadFileHandle;
  304. }
  305. }
  306. - (void)setUploadFileURL:(NSURL *)uploadURL {
  307. BOOL changed = NO;
  308. @synchronized(self) {
  309. GTMSessionMonitorSynchronized(self);
  310. if (_uploadFileURL != uploadURL) {
  311. _uploadFileURL = uploadURL;
  312. changed = YES;
  313. }
  314. }
  315. if (changed) {
  316. [self setupRequestHeaders];
  317. }
  318. }
  319. - (NSURL *)uploadFileURL {
  320. @synchronized(self) {
  321. GTMSessionMonitorSynchronized(self);
  322. return _uploadFileURL;
  323. }
  324. }
  325. - (void)setUploadFileLength:(int64_t)fullLength {
  326. @synchronized(self) {
  327. GTMSessionMonitorSynchronized(self);
  328. if (_uploadFileLength == kGTMSessionUploadFetcherUnknownFileSize &&
  329. fullLength != kGTMSessionUploadFetcherUnknownFileSize) {
  330. _uploadFileLength = fullLength;
  331. }
  332. }
  333. }
  334. - (void)setUploadDataLength:(int64_t)fullLength
  335. provider:(GTMSessionUploadFetcherDataProvider)block {
  336. @synchronized(self) {
  337. GTMSessionMonitorSynchronized(self);
  338. _uploadDataProvider = [block copy];
  339. _uploadFileLength = fullLength;
  340. }
  341. [self setupRequestHeaders];
  342. }
  343. - (GTMSessionUploadFetcherDataProvider)uploadDataProvider {
  344. @synchronized(self) {
  345. GTMSessionMonitorSynchronized(self);
  346. return _uploadDataProvider;
  347. }
  348. }
  349. - (void)setUploadMIMEType:(NSString *)uploadMIMEType {
  350. GTMSESSION_ASSERT_DEBUG(0, @"TODO: disallow setUploadMIMEType by making declaration readonly");
  351. // (and uploadMIMEType, chunksize, currentOffset)
  352. @synchronized(self) {
  353. GTMSessionMonitorSynchronized(self);
  354. _uploadMIMEType = uploadMIMEType;
  355. }
  356. }
  357. - (NSString *)uploadMIMEType {
  358. @synchronized(self) {
  359. GTMSessionMonitorSynchronized(self);
  360. return _uploadMIMEType;
  361. }
  362. }
  363. - (void)setChunkSize:(int64_t)chunkSize {
  364. @synchronized(self) {
  365. GTMSessionMonitorSynchronized(self);
  366. _chunkSize = chunkSize;
  367. }
  368. }
  369. - (int64_t)chunkSize {
  370. @synchronized(self) {
  371. GTMSessionMonitorSynchronized(self);
  372. return _chunkSize;
  373. }
  374. }
  375. - (void)setupRequestHeaders {
  376. GTMSessionCheckNotSynchronized(self);
  377. #if DEBUG
  378. @synchronized(self) {
  379. GTMSessionMonitorSynchronized(self);
  380. int hasData = (_uploadData != nil) ? 1 : 0;
  381. int hasFileHandle = (_uploadFileHandle != nil) ? 1 : 0;
  382. int hasFileURL = (_uploadFileURL != nil) ? 1 : 0;
  383. int hasUploadDataProvider = (_uploadDataProvider != nil) ? 1 : 0;
  384. int numberOfSources = hasData + hasFileHandle + hasFileURL + hasUploadDataProvider;
  385. #pragma unused(numberOfSources)
  386. GTMSESSION_ASSERT_DEBUG(numberOfSources == 1,
  387. @"Need just one upload source (%d)", numberOfSources);
  388. } // @synchronized(self)
  389. #endif
  390. // Add our custom headers to the initial request indicating the data
  391. // type and total size to be delivered later in the chunk requests.
  392. NSMutableURLRequest *mutableRequest = [self.request mutableCopy];
  393. GTMSESSION_ASSERT_DEBUG((mutableRequest == nil) != (_uploadLocationURL == nil),
  394. @"Request and location are mutually exclusive");
  395. if (!mutableRequest) return;
  396. [mutableRequest setValue:@"resumable"
  397. forHTTPHeaderField:kGTMSessionHeaderXGoogUploadProtocol];
  398. [mutableRequest setValue:@"start"
  399. forHTTPHeaderField:kGTMSessionHeaderXGoogUploadCommand];
  400. [mutableRequest setValue:_uploadMIMEType
  401. forHTTPHeaderField:kGTMSessionHeaderXGoogUploadContentType];
  402. [mutableRequest setValue:@([self fullUploadLength]).stringValue
  403. forHTTPHeaderField:kGTMSessionHeaderXGoogUploadContentLength];
  404. NSString *method = mutableRequest.HTTPMethod;
  405. if (method == nil || [method caseInsensitiveCompare:@"GET"] == NSOrderedSame) {
  406. [mutableRequest setHTTPMethod:@"POST"];
  407. }
  408. // Ensure the user agent header identifies this to the upload server as a
  409. // GTMSessionUploadFetcher client. The /1 can be incremented in the unlikely circumstance
  410. // we need to make a bug fix in the client that the server can recognize.
  411. NSString *const kUserAgentStub = @"(GTMSUF/1)";
  412. NSString *userAgent = [mutableRequest valueForHTTPHeaderField:@"User-Agent"];
  413. if (userAgent == nil
  414. || [userAgent rangeOfString:kUserAgentStub].location == NSNotFound) {
  415. if (userAgent.length == 0) {
  416. userAgent = GTMFetcherStandardUserAgentString(nil);
  417. }
  418. userAgent = [userAgent stringByAppendingFormat:@" %@", kUserAgentStub];
  419. [mutableRequest setValue:userAgent forHTTPHeaderField:@"User-Agent"];
  420. }
  421. [self setRequest:mutableRequest];
  422. }
  423. - (void)setLocationURL:(NSURL * GTM_NULLABLE_TYPE)location
  424. uploadMIMEType:(NSString *)uploadMIMEType
  425. chunkSize:(int64_t)chunkSize {
  426. @synchronized(self) {
  427. GTMSessionMonitorSynchronized(self);
  428. GTMSESSION_ASSERT_DEBUG(chunkSize > 0, @"chunk size is zero");
  429. // When resuming an upload, set the known upload target URL.
  430. _uploadLocationURL = location;
  431. _uploadMIMEType = uploadMIMEType;
  432. _chunkSize = chunkSize;
  433. // Indicate that we've not yet determined the file handle's length
  434. _uploadFileLength = kGTMSessionUploadFetcherUnknownFileSize;
  435. // Indicate that we've not yet determined the upload fetcher status
  436. _recentChunkStatusCode = -1;
  437. // If this is restarting an upload begun by another fetcher,
  438. // the location is specified but the request is nil
  439. _isRestartedUpload = (location != nil);
  440. } // @synchronized(self)
  441. }
  442. - (int64_t)fullUploadLength {
  443. int64_t result;
  444. @synchronized(self) {
  445. GTMSessionMonitorSynchronized(self);
  446. if (_uploadData) {
  447. result = (int64_t)_uploadData.length;
  448. } else {
  449. if (_uploadFileLength == kGTMSessionUploadFetcherUnknownFileSize) {
  450. if (_uploadFileHandle) {
  451. // First time through, seek to end to determine file length
  452. _uploadFileLength = (int64_t)[_uploadFileHandle seekToEndOfFile];
  453. } else if (_uploadDataProvider) {
  454. // _uploadFileLength is set when the _uploadDataProvider is set.
  455. GTMSESSION_ASSERT_DEBUG(_uploadFileLength >= 0, @"No uploadDataProvider length set");
  456. } else {
  457. NSNumber *filesizeNum;
  458. NSError *valueError;
  459. if ([_uploadFileURL getResourceValue:&filesizeNum
  460. forKey:NSURLFileSizeKey
  461. error:&valueError]) {
  462. _uploadFileLength = filesizeNum.longLongValue;
  463. } else {
  464. GTMSESSION_ASSERT_DEBUG(NO, @"Cannot get file size: %@\n %@",
  465. valueError, _uploadFileURL.path);
  466. _uploadFileLength = 0;
  467. }
  468. }
  469. }
  470. result = _uploadFileLength;
  471. }
  472. } // @synchronized(self)
  473. return result;
  474. }
  475. // Make a subdata of the upload data.
  476. - (void)generateChunkSubdataWithOffset:(int64_t)offset
  477. length:(int64_t)length
  478. response:(GTMSessionUploadFetcherDataProviderResponse)response {
  479. GTMSessionUploadFetcherDataProvider uploadDataProvider = self.uploadDataProvider;
  480. if (uploadDataProvider) {
  481. uploadDataProvider(offset, length, response);
  482. return;
  483. }
  484. NSData *uploadData = self.uploadData;
  485. if (uploadData) {
  486. // NSData provided.
  487. NSData *resultData;
  488. if (offset == 0 && length == (int64_t)uploadData.length) {
  489. resultData = uploadData;
  490. } else {
  491. int64_t dataLength = (int64_t)uploadData.length;
  492. // Ensure our range is valid. b/18007814
  493. if (offset + length > dataLength) {
  494. NSString *errorMessage = [NSString stringWithFormat:
  495. @"Range invalid for upload data. offset: %lld\tlength: %lld\tdataLength: %lld",
  496. offset, length, dataLength];
  497. GTMSESSION_ASSERT_DEBUG(NO, @"%@", errorMessage);
  498. response(nil,
  499. kGTMSessionUploadFetcherUnknownFileSize,
  500. [self uploadChunkUnavailableErrorWithDescription:errorMessage]);
  501. return;
  502. }
  503. NSRange range = NSMakeRange((NSUInteger)offset, (NSUInteger)length);
  504. @try {
  505. resultData = [uploadData subdataWithRange:range];
  506. }
  507. @catch (NSException *exception) {
  508. NSString *errorMessage = exception.description;
  509. GTMSESSION_ASSERT_DEBUG(NO, @"%@", errorMessage);
  510. response(nil,
  511. kGTMSessionUploadFetcherUnknownFileSize,
  512. [self uploadChunkUnavailableErrorWithDescription:errorMessage]);
  513. return;
  514. }
  515. }
  516. response(resultData, kGTMSessionUploadFetcherUnknownFileSize, nil);
  517. return;
  518. }
  519. NSURL *uploadFileURL = self.uploadFileURL;
  520. if (uploadFileURL) {
  521. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  522. [self generateChunkSubdataFromFileURL:uploadFileURL
  523. offset:offset
  524. length:length
  525. response:response];
  526. });
  527. return;
  528. }
  529. GTMSESSION_ASSERT_DEBUG(_uploadFileHandle, @"Unexpectedly missing upload data package");
  530. NSFileHandle *uploadFileHandle = self.uploadFileHandle;
  531. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  532. [self generateChunkSubdataFromFileHandle:uploadFileHandle
  533. offset:offset
  534. length:length
  535. response:response];
  536. });
  537. }
  538. - (void)generateChunkSubdataFromFileHandle:(NSFileHandle *)fileHandle
  539. offset:(int64_t)offset
  540. length:(int64_t)length
  541. response:(GTMSessionUploadFetcherDataProviderResponse)response {
  542. NSData *resultData;
  543. NSError *error;
  544. @try {
  545. [fileHandle seekToFileOffset:(unsigned long long)offset];
  546. resultData = [fileHandle readDataOfLength:(NSUInteger)length];
  547. }
  548. @catch (NSException *exception) {
  549. GTMSESSION_ASSERT_DEBUG(NO, @"uploadFileHandle failed to read, %@", exception);
  550. error = [self uploadChunkUnavailableErrorWithDescription:exception.description];
  551. }
  552. // The response always re-dispatches to the main thread, so we skip doing that here.
  553. response(resultData, kGTMSessionUploadFetcherUnknownFileSize, error);
  554. }
  555. - (void)generateChunkSubdataFromFileURL:(NSURL *)fileURL
  556. offset:(int64_t)offset
  557. length:(int64_t)length
  558. response:(GTMSessionUploadFetcherDataProviderResponse)response {
  559. GTMSessionCheckNotSynchronized(self);
  560. NSData *resultData;
  561. NSError *error;
  562. int64_t fullUploadLength = [self fullUploadLength];
  563. NSData *mappedData =
  564. [NSData dataWithContentsOfURL:fileURL
  565. options:NSDataReadingMappedAlways + NSDataReadingUncached
  566. error:&error];
  567. if (!mappedData) {
  568. // We could not create an NSData by memory-mapping the file.
  569. #if TARGET_IPHONE_SIMULATOR
  570. // NSTemporaryDirectory() can differ in the simulator between app restarts,
  571. // yet the contents for the new path remains unchanged, so try the latest temp path.
  572. if ([error.domain isEqual:NSCocoaErrorDomain] && (error.code == NSFileReadNoSuchFileError)) {
  573. NSString *filename = [fileURL lastPathComponent];
  574. NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
  575. NSURL *newFileURL = [NSURL fileURLWithPath:filePath];
  576. if (![newFileURL isEqual:fileURL]) {
  577. [self generateChunkSubdataFromFileURL:newFileURL
  578. offset:offset
  579. length:length
  580. response:response];
  581. return;
  582. }
  583. }
  584. #endif
  585. // If the file is just too large to create an NSData for, or if for some other reason we can't
  586. // map it, create an NSFileHandle instead to read a subset into an NSData.
  587. #if DEBUG
  588. NSNumber *fileSizeNum;
  589. BOOL hasFileSize = [fileURL getResourceValue:&fileSizeNum forKey:NSURLFileSizeKey error:NULL];
  590. GTMSESSION_LOG_DEBUG(@"Note: uploadFileURL is falling back to creating upload chunks by reading"
  591. @" an NSFileHandle since uploadFileURL failed to map the upload file,"
  592. @" file size %@, %@",
  593. hasFileSize ? fileSizeNum : @"unknown", error);
  594. #endif
  595. NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:fileURL
  596. error:&error];
  597. if (fileHandle != nil) {
  598. [self generateChunkSubdataFromFileHandle:fileHandle
  599. offset:offset
  600. length:length
  601. response:response];
  602. return;
  603. }
  604. GTMSESSION_ASSERT_DEBUG(NO, @"uploadFileURL failed to read, %@", error);
  605. // Fall through with the error.
  606. } else {
  607. // Successfully created an NSData by memory-mapping the file.
  608. if ((NSUInteger)(offset + length) > mappedData.length) {
  609. NSString *errorMessage = [NSString stringWithFormat:
  610. @"Range invalid for upload data. offset: %lld\tlength: %lld\tdataLength: %lld\texpected UploadLength: %lld",
  611. offset, length, (long long)mappedData.length, fullUploadLength];
  612. GTMSESSION_ASSERT_DEBUG(NO, @"%@", errorMessage);
  613. response(nil,
  614. kGTMSessionUploadFetcherUnknownFileSize,
  615. [self uploadChunkUnavailableErrorWithDescription:errorMessage]);
  616. return;
  617. }
  618. if (offset > 0 || length < fullUploadLength) {
  619. NSRange range = NSMakeRange((NSUInteger)offset, (NSUInteger)length);
  620. resultData = [mappedData subdataWithRange:range];
  621. } else {
  622. resultData = mappedData;
  623. }
  624. }
  625. // The response always re-dispatches to the main thread, so we skip re-dispatching here.
  626. response(resultData, kGTMSessionUploadFetcherUnknownFileSize, error);
  627. }
  628. - (NSError *)uploadChunkUnavailableErrorWithDescription:(NSString *)description {
  629. // The description in the userInfo is intended as a clue to programmers, not
  630. // for client code to examine or rely on.
  631. NSDictionary *userInfo = @{ @"description" : description };
  632. return [NSError errorWithDomain:kGTMSessionFetcherErrorDomain
  633. code:GTMSessionFetcherErrorUploadChunkUnavailable
  634. userInfo:userInfo];
  635. }
  636. - (NSError *)prematureFailureErrorWithUserInfo:(NSDictionary *)userInfo {
  637. // An error for if we get an unexpected status from the upload server or
  638. // otherwise cannot continue. This is an issue beyond the upload protocol;
  639. // there's no way the client can do anything useful except give up.
  640. NSError *error = [NSError errorWithDomain:kGTMSessionFetcherStatusDomain
  641. code:501 // Not implemented
  642. userInfo:userInfo];
  643. return error;
  644. }
  645. + (GTMSessionUploadFetcherStatus)uploadStatusFromResponseHeaders:(NSDictionary *)responseHeaders {
  646. NSString *statusString = [responseHeaders objectForKey:kGTMSessionHeaderXGoogUploadStatus];
  647. if ([statusString isEqual:@"active"]) {
  648. return kStatusActive;
  649. }
  650. if ([statusString isEqual:@"final"]) {
  651. return kStatusFinal;
  652. }
  653. if ([statusString isEqual:@"cancelled"]) {
  654. return kStatusCancelled;
  655. }
  656. return kStatusUnknown;
  657. }
  658. #pragma mark Method overrides affecting the initial fetch only
  659. - (void)setCompletionHandler:(GTMSessionFetcherCompletionHandler)handler {
  660. @synchronized(self) {
  661. GTMSessionMonitorSynchronized(self);
  662. _delegateCompletionHandler = handler;
  663. }
  664. }
  665. - (void)setDelegateCallbackQueue:(dispatch_queue_t GTM_NULLABLE_TYPE)queue {
  666. @synchronized(self) {
  667. GTMSessionMonitorSynchronized(self);
  668. _delegateCallbackQueue = queue;
  669. }
  670. }
  671. - (dispatch_queue_t GTM_NULLABLE_TYPE)delegateCallbackQueue {
  672. @synchronized(self) {
  673. GTMSessionMonitorSynchronized(self);
  674. return _delegateCallbackQueue;
  675. }
  676. }
  677. - (BOOL)isRestartedUpload {
  678. @synchronized(self) {
  679. GTMSessionMonitorSynchronized(self);
  680. return _isRestartedUpload;
  681. }
  682. }
  683. - (GTMSessionFetcher * GTM_NULLABLE_TYPE)chunkFetcher {
  684. @synchronized(self) {
  685. GTMSessionMonitorSynchronized(self);
  686. return _chunkFetcher;
  687. }
  688. }
  689. - (void)setChunkFetcher:(GTMSessionFetcher * GTM_NULLABLE_TYPE)fetcher {
  690. @synchronized(self) {
  691. GTMSessionMonitorSynchronized(self);
  692. _chunkFetcher = fetcher;
  693. }
  694. }
  695. - (void)setFetcherInFlight:(GTMSessionFetcher * GTM_NULLABLE_TYPE)fetcher {
  696. @synchronized(self) {
  697. GTMSessionMonitorSynchronized(self);
  698. _fetcherInFlight = fetcher;
  699. }
  700. }
  701. - (GTMSessionFetcher * GTM_NULLABLE_TYPE)fetcherInFlight {
  702. @synchronized(self) {
  703. GTMSessionMonitorSynchronized(self);
  704. return _fetcherInFlight;
  705. }
  706. }
  707. - (void)setCancellationHandler:(GTMSessionUploadFetcherCancellationHandler GTM_NULLABLE_TYPE)
  708. cancellationHandler {
  709. @synchronized(self) {
  710. GTMSessionMonitorSynchronized(self);
  711. _cancellationHandler = cancellationHandler;
  712. }
  713. }
  714. - (GTMSessionUploadFetcherCancellationHandler GTM_NULLABLE_TYPE)cancellationHandler {
  715. @synchronized(self) {
  716. GTMSessionMonitorSynchronized(self);
  717. return _cancellationHandler;
  718. }
  719. }
  720. - (void)beginFetchForRetry {
  721. GTMSessionCheckNotSynchronized(self);
  722. // Override the superclass to reset the initial body length and fetcher-in-flight,
  723. // then call the superclass implementation.
  724. [self setInitialBodyLength:[self bodyLength]];
  725. GTMSESSION_ASSERT_DEBUG(self.fetcherInFlight == nil, @"unexpected fetcher in flight: %@",
  726. self.fetcherInFlight);
  727. self.fetcherInFlight = self;
  728. [super beginFetchForRetry];
  729. }
  730. - (void)beginFetchWithCompletionHandler:(GTMSessionFetcherCompletionHandler)handler {
  731. GTMSessionCheckNotSynchronized(self);
  732. [self setInitialBodyLength:[self bodyLength]];
  733. // We'll hold onto the superclass's callback queue so we can invoke the handler
  734. // even after the superclass has released the queue and its callback handler, as
  735. // happens during auth failure.
  736. [self setDelegateCallbackQueue:self.callbackQueue];
  737. self.completionHandler = handler;
  738. if ([self isRestartedUpload]) {
  739. // When restarting an upload, we know the destination location for chunk fetches,
  740. // but we need to query to find the initial offset.
  741. if (![self isPaused]) {
  742. [self sendQueryForUploadOffsetWithFetcherProperties:self.properties];
  743. }
  744. return;
  745. }
  746. // We don't want to call into the client's completion block immediately
  747. // after the finish of the initial connection (the delegate is called only
  748. // when uploading finishes), so we substitute our own completion block to be
  749. // called when the initial connection finishes
  750. GTMSESSION_ASSERT_DEBUG(self.fetcherInFlight == nil, @"unexpected fetcher in flight: %@",
  751. self.fetcherInFlight);
  752. self.fetcherInFlight = self;
  753. [super beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  754. self.fetcherInFlight = nil;
  755. // callback
  756. BOOL hasTestBlock = (self.testBlock != nil);
  757. if (![self isRestartedUpload] && !hasTestBlock) {
  758. if (error == nil) {
  759. [self beginChunkFetches];
  760. } else {
  761. if ([self retryTimer] == nil) {
  762. [self invokeFinalCallbackWithData:nil
  763. error:error
  764. shouldInvalidateLocation:YES];
  765. }
  766. }
  767. } else {
  768. // If there was no initial request, then this fetch is resuming some
  769. // other uploadFetcher's initial request, and the superclass's connection
  770. // is never used, so at this point we call the user's actual completion
  771. // block.
  772. if (!hasTestBlock) {
  773. [self invokeFinalCallbackWithData:data
  774. error:error
  775. shouldInvalidateLocation:YES];
  776. } else {
  777. // There was a test block, so we won't do chunk fetches, but we simulate obtaining
  778. // the data to be uploaded from the upload data provider block or the file handle,
  779. // and then call back.
  780. [self generateChunkSubdataWithOffset:0
  781. length:[self fullUploadLength]
  782. response:^(NSData *generateData, int64_t fullUploadLength, NSError *generateError) {
  783. [self invokeFinalCallbackWithData:data
  784. error:error
  785. shouldInvalidateLocation:YES];
  786. }];
  787. }
  788. }
  789. }];
  790. }
  791. - (void)beginChunkFetches {
  792. GTMSessionCheckNotSynchronized(self);
  793. #if DEBUG
  794. // The initial response of the resumable upload protocol should have an
  795. // empty body
  796. //
  797. // This assert typically happens because the upload create/edit link URL was
  798. // not supplied with the request, and the server is thus expecting a non-
  799. // resumable request/response.
  800. if (self.downloadedData.length > 0) {
  801. NSData *downloadedData = self.downloadedData;
  802. NSString *str = [[NSString alloc] initWithData:downloadedData
  803. encoding:NSUTF8StringEncoding];
  804. #pragma unused(str)
  805. GTMSESSION_ASSERT_DEBUG(NO, @"unexpected response data (uploading to the wrong URL?)\n%@", str);
  806. }
  807. #endif
  808. // We need to get the upload URL from the location header to continue.
  809. NSDictionary *responseHeaders = [self responseHeaders];
  810. [self retrieveUploadChunkGranularityFromResponseHeaders:responseHeaders];
  811. GTMSessionUploadFetcherStatus uploadStatus =
  812. [[self class] uploadStatusFromResponseHeaders:responseHeaders];
  813. GTMSESSION_ASSERT_DEBUG(uploadStatus != kStatusUnknown,
  814. @"beginChunkFetches has unexpected upload status for headers %@", responseHeaders);
  815. BOOL isPrematureStop = (uploadStatus == kStatusFinal) || (uploadStatus == kStatusCancelled);
  816. NSString *uploadLocationURLStr = [responseHeaders objectForKey:kGTMSessionHeaderXGoogUploadURL];
  817. BOOL hasUploadLocation = (uploadLocationURLStr.length > 0);
  818. if (isPrematureStop || !hasUploadLocation) {
  819. GTMSESSION_ASSERT_DEBUG(NO, @"Premature failure: upload-status:\"%@\" location:%@",
  820. [responseHeaders objectForKey:kGTMSessionHeaderXGoogUploadStatus], uploadLocationURLStr);
  821. // We cannot continue since we do not know the location to use
  822. // as our upload destination.
  823. NSDictionary *userInfo = nil;
  824. NSData *downloadedData = self.downloadedData;
  825. if (downloadedData.length > 0) {
  826. userInfo = @{ kGTMSessionFetcherStatusDataKey : downloadedData };
  827. }
  828. NSError *failureError = [self prematureFailureErrorWithUserInfo:userInfo];
  829. [self invokeFinalCallbackWithData:nil
  830. error:failureError
  831. shouldInvalidateLocation:YES];
  832. return;
  833. }
  834. self.uploadLocationURL = [NSURL URLWithString:uploadLocationURLStr];
  835. NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  836. [nc postNotificationName:kGTMSessionFetcherUploadLocationObtainedNotification
  837. object:self];
  838. // we've now sent all of the initial post body data, so we need to include
  839. // its size in future progress indicator callbacks
  840. [self setInitialBodySent:[self initialBodyLength]];
  841. // just in case the user paused us during the initial fetch...
  842. if (![self isPaused]) {
  843. [self uploadNextChunkWithOffset:0];
  844. }
  845. }
  846. - (void)URLSession:(NSURLSession *)session
  847. task:(NSURLSessionTask *)task
  848. didSendBodyData:(int64_t)bytesSent
  849. totalBytesSent:(int64_t)totalBytesSent
  850. totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
  851. // Overrides the superclass.
  852. [self invokeDelegateWithDidSendBytes:bytesSent
  853. totalBytesSent:totalBytesSent
  854. totalBytesExpectedToSend:totalBytesExpectedToSend + [self fullUploadLength]];
  855. }
  856. - (BOOL)shouldReleaseCallbacksUponCompletion {
  857. // Overrides the superclass.
  858. // We don't want the superclass to release the delegate and callback
  859. // blocks once the initial fetch has finished
  860. //
  861. // This is invoked for only successful completion of the connection;
  862. // an error always will invoke and release the callbacks
  863. return NO;
  864. }
  865. - (void)invokeFinalCallbackWithData:(NSData *)data
  866. error:(NSError *)error
  867. shouldInvalidateLocation:(BOOL)shouldInvalidateLocation {
  868. @synchronized(self) {
  869. GTMSessionMonitorSynchronized(self);
  870. if (shouldInvalidateLocation) {
  871. _uploadLocationURL = nil;
  872. }
  873. dispatch_queue_t queue = _delegateCallbackQueue;
  874. GTMSessionFetcherCompletionHandler handler = _delegateCompletionHandler;
  875. if (queue && handler) {
  876. [self invokeOnCallbackQueue:queue
  877. afterUserStopped:NO
  878. block:^{
  879. handler(data, error);
  880. }];
  881. }
  882. } // @synchronized(self)
  883. [self releaseUploadAndBaseCallbacks:!self.userStoppedFetching];
  884. }
  885. - (void)releaseUploadAndBaseCallbacks:(BOOL)shouldReleaseCancellation {
  886. @synchronized(self) {
  887. GTMSessionMonitorSynchronized(self);
  888. _delegateCallbackQueue = nil;
  889. _delegateCompletionHandler = nil;
  890. _uploadDataProvider = nil;
  891. if (shouldReleaseCancellation) {
  892. _cancellationHandler = nil;
  893. }
  894. }
  895. // Release the base class's callbacks, too, if needed.
  896. [self releaseCallbacks];
  897. }
  898. - (void)stopFetchReleasingCallbacks:(BOOL)shouldReleaseCallbacks {
  899. GTMSessionCheckNotSynchronized(self);
  900. // Clear _fetcherInFlight when stopped. Moved from stopFetching, since that's a public method,
  901. // where this method does the work. Fixes issue clearing value when retryBlock included.
  902. GTMSessionFetcher *fetcherInFlight = self.fetcherInFlight;
  903. if (fetcherInFlight == self) {
  904. self.fetcherInFlight = nil;
  905. }
  906. [super stopFetchReleasingCallbacks:shouldReleaseCallbacks];
  907. if (shouldReleaseCallbacks) {
  908. [self releaseUploadAndBaseCallbacks:NO];
  909. }
  910. }
  911. #pragma mark Chunk fetching methods
  912. - (void)uploadNextChunkWithOffset:(int64_t)offset {
  913. // use the properties in each chunk fetcher
  914. NSDictionary *props = [self properties];
  915. [self uploadNextChunkWithOffset:offset
  916. fetcherProperties:props];
  917. }
  918. - (void)sendQueryForUploadOffsetWithFetcherProperties:(NSDictionary *)props {
  919. GTMSessionFetcher *queryFetcher = [self uploadFetcherWithProperties:props
  920. isQueryFetch:YES];
  921. queryFetcher.bodyData = [NSData data];
  922. NSString *originalComment = self.comment;
  923. [queryFetcher setCommentWithFormat:@"%@ (query offset)",
  924. originalComment ? originalComment : @"upload"];
  925. [queryFetcher setRequestValue:@"query" forHTTPHeaderField:kGTMSessionHeaderXGoogUploadCommand];
  926. self.fetcherInFlight = queryFetcher;
  927. [queryFetcher beginFetchWithDelegate:self
  928. didFinishSelector:@selector(queryFetcher:finishedWithData:error:)];
  929. }
  930. - (void)queryFetcher:(GTMSessionFetcher *)queryFetcher
  931. finishedWithData:(NSData *)data
  932. error:(NSError *)error {
  933. self.fetcherInFlight = nil;
  934. NSDictionary *responseHeaders = [queryFetcher responseHeaders];
  935. NSString *sizeReceivedHeader;
  936. GTMSessionUploadFetcherStatus uploadStatus =
  937. [[self class] uploadStatusFromResponseHeaders:responseHeaders];
  938. GTMSESSION_ASSERT_DEBUG(uploadStatus != kStatusUnknown || error != nil,
  939. @"query fetcher completion has unexpected upload status for headers %@", responseHeaders);
  940. if (error == nil) {
  941. sizeReceivedHeader = [responseHeaders objectForKey:kGTMSessionHeaderXGoogUploadSizeReceived];
  942. if (uploadStatus == kStatusCancelled ||
  943. (uploadStatus == kStatusActive && sizeReceivedHeader == nil)) {
  944. NSDictionary *userInfo = nil;
  945. if (data.length > 0) {
  946. userInfo = @{ kGTMSessionFetcherStatusDataKey : data };
  947. }
  948. error = [self prematureFailureErrorWithUserInfo:userInfo];
  949. }
  950. }
  951. if (error == nil) {
  952. int64_t offset = [sizeReceivedHeader longLongValue];
  953. int64_t fullUploadLength = [self fullUploadLength];
  954. if (uploadStatus == kStatusFinal ||
  955. (offset >= fullUploadLength &&
  956. fullUploadLength != kGTMSessionUploadFetcherUnknownFileSize)) {
  957. // Handle we're done
  958. [self chunkFetcher:queryFetcher finishedWithData:data error:nil];
  959. } else {
  960. [self retrieveUploadChunkGranularityFromResponseHeaders:responseHeaders];
  961. [self uploadNextChunkWithOffset:offset];
  962. }
  963. } else {
  964. // Handle query error
  965. [self chunkFetcher:queryFetcher finishedWithData:data error:error];
  966. }
  967. }
  968. - (void)sendCancelUploadWithFetcherProperties:(NSDictionary *)props {
  969. @synchronized(self) {
  970. _isCancelInFlight = YES;
  971. }
  972. GTMSessionFetcher *cancelFetcher = [self uploadFetcherWithProperties:props
  973. isQueryFetch:YES];
  974. cancelFetcher.bodyData = [NSData data];
  975. NSString *originalComment = self.comment;
  976. [cancelFetcher setCommentWithFormat:@"%@ (cancel)",
  977. originalComment ? originalComment : @"upload"];
  978. [cancelFetcher setRequestValue:@"cancel" forHTTPHeaderField:kGTMSessionHeaderXGoogUploadCommand];
  979. self.fetcherInFlight = cancelFetcher;
  980. [cancelFetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
  981. self.fetcherInFlight = nil;
  982. if (![self triggerCancellationHandlerForFetch:cancelFetcher data:data error:error]) {
  983. if (error) {
  984. GTMSESSION_LOG_DEBUG(@"cancelFetcher %@", error);
  985. }
  986. }
  987. @synchronized(self) {
  988. self->_isCancelInFlight = NO;
  989. }
  990. }];
  991. }
  992. - (void)uploadNextChunkWithOffset:(int64_t)offset
  993. fetcherProperties:(NSDictionary *)props {
  994. GTMSessionCheckNotSynchronized(self);
  995. // Example chunk headers:
  996. // X-Goog-Upload-Command: upload, finalize
  997. // X-Goog-Upload-Offset: 0
  998. // Content-Length: 2000000
  999. // Content-Type: image/jpeg
  1000. //
  1001. // {bytes 0-1999999}
  1002. // The chunk upload URL requires no authentication header.
  1003. GTMSessionFetcher *chunkFetcher = [self uploadFetcherWithProperties:props
  1004. isQueryFetch:NO];
  1005. [self attachSendProgressBlockToChunkFetcher:chunkFetcher];
  1006. int64_t chunkSize = [self updateChunkFetcher:chunkFetcher
  1007. forChunkAtOffset:offset];
  1008. BOOL isUploadingFileURL = (self.uploadFileURL != nil);
  1009. int64_t fullUploadLength = [self fullUploadLength];
  1010. // The chunk size may have changed, so determine again if we're uploading the full file.
  1011. BOOL isUploadingFullFile = (offset == 0 &&
  1012. fullUploadLength != kGTMSessionUploadFetcherUnknownFileSize &&
  1013. chunkSize >= fullUploadLength);
  1014. if (isUploadingFullFile && isUploadingFileURL) {
  1015. // The data is the full upload file URL.
  1016. chunkFetcher.bodyFileURL = self.uploadFileURL;
  1017. [self beginChunkFetcher:chunkFetcher
  1018. offset:offset];
  1019. } else {
  1020. // Make an NSData for the subset for this upload chunk.
  1021. self.subdataGenerating = YES;
  1022. [self generateChunkSubdataWithOffset:offset
  1023. length:chunkSize
  1024. response:^(NSData *chunkData, int64_t uploadFileLength, NSError *chunkError) {
  1025. // The subdata methods may leave us on a background thread.
  1026. dispatch_async(dispatch_get_main_queue(), ^{
  1027. self.subdataGenerating = NO;
  1028. // dont allow the updating of fileLength for uploads not using a data provider as they
  1029. // should know the file length before the upload starts.
  1030. if (self->_uploadDataProvider != nil && uploadFileLength > 0) {
  1031. [self setUploadFileLength:uploadFileLength];
  1032. // Update the command and content-length headers if this is the last chunk to be sent.
  1033. if (offset + chunkSize >= uploadFileLength) {
  1034. int64_t updatedChunkSize = [self updateChunkFetcher:chunkFetcher
  1035. forChunkAtOffset:offset];
  1036. if (updatedChunkSize == 0) {
  1037. // Calling beginChunkFetcher early when there is no more data to send allows us to
  1038. // properly handle nil chunkData below without having to account for the case where
  1039. // we are just finalizing the file.
  1040. chunkFetcher.bodyData = [[NSData alloc] init];
  1041. [self beginChunkFetcher:chunkFetcher
  1042. offset:offset];
  1043. return;
  1044. }
  1045. }
  1046. }
  1047. if (chunkData == nil) {
  1048. NSError *responseError = chunkError;
  1049. if (!responseError) {
  1050. responseError = [self uploadChunkUnavailableErrorWithDescription:@"chunkData is nil"];
  1051. }
  1052. [self invokeFinalCallbackWithData:nil
  1053. error:responseError
  1054. shouldInvalidateLocation:YES];
  1055. return;
  1056. }
  1057. BOOL didWriteFile = NO;
  1058. if (isUploadingFileURL) {
  1059. // Make a temporary file with the data subset.
  1060. NSString *tempName =
  1061. [NSString stringWithFormat:@"GTMUpload_temp_%@", [[NSUUID UUID] UUIDString]];
  1062. NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:tempName];
  1063. NSError *writeError;
  1064. didWriteFile = [chunkData writeToFile:tempPath
  1065. options:NSDataWritingAtomic
  1066. error:&writeError];
  1067. if (didWriteFile) {
  1068. chunkFetcher.bodyFileURL = [NSURL fileURLWithPath:tempPath];
  1069. } else {
  1070. GTMSESSION_LOG_DEBUG(@"writeToFile failed: %@\n%@", writeError, tempPath);
  1071. }
  1072. }
  1073. if (!didWriteFile) {
  1074. chunkFetcher.bodyData = [chunkData copy];
  1075. }
  1076. [self beginChunkFetcher:chunkFetcher
  1077. offset:offset];
  1078. });
  1079. }];
  1080. }
  1081. }
  1082. - (void)beginChunkFetcher:(GTMSessionFetcher *)chunkFetcher
  1083. offset:(int64_t)offset {
  1084. // Track the current offset for progress reporting
  1085. self.currentOffset = offset;
  1086. // Hang on to the fetcher in case we need to cancel it. We set these before beginning the
  1087. // chunk fetch so the observers notified of chunk fetches can inspect the upload fetcher to
  1088. // match to the chunk.
  1089. self.chunkFetcher = chunkFetcher;
  1090. self.fetcherInFlight = chunkFetcher;
  1091. // Update the last chunk request, including any request headers.
  1092. self.lastChunkRequest = chunkFetcher.request;
  1093. [chunkFetcher beginFetchWithDelegate:self
  1094. didFinishSelector:@selector(chunkFetcher:finishedWithData:error:)];
  1095. }
  1096. - (void)attachSendProgressBlockToChunkFetcher:(GTMSessionFetcher *)chunkFetcher {
  1097. chunkFetcher.sendProgressBlock = ^(int64_t bytesSent, int64_t totalBytesSent,
  1098. int64_t totalBytesExpectedToSend) {
  1099. // The total bytes expected include the initial body and the full chunked
  1100. // data, independent of how big this fetcher's chunk is.
  1101. int64_t initialBodySent = [self bodyLength]; // TODO(grobbins) use [self initialBodySent]
  1102. int64_t totalSent = initialBodySent + self.currentOffset + totalBytesSent;
  1103. int64_t totalExpected = initialBodySent + [self fullUploadLength];
  1104. [self invokeDelegateWithDidSendBytes:bytesSent
  1105. totalBytesSent:totalSent
  1106. totalBytesExpectedToSend:totalExpected];
  1107. };
  1108. }
  1109. - (NSDictionary *)uploadSessionIdentifierMetadata {
  1110. NSMutableDictionary *metadata = [NSMutableDictionary dictionary];
  1111. metadata[kGTMSessionIdentifierIsUploadChunkFetcherMetadataKey] = @YES;
  1112. GTMSESSION_ASSERT_DEBUG(self.uploadFileURL,
  1113. @"Invalid upload fetcher to create session identifier for metadata");
  1114. metadata[kGTMSessionIdentifierUploadFileURLMetadataKey] = [self.uploadFileURL absoluteString];
  1115. metadata[kGTMSessionIdentifierUploadFileLengthMetadataKey] = @([self fullUploadLength]);
  1116. if (self.uploadLocationURL) {
  1117. metadata[kGTMSessionIdentifierUploadLocationURLMetadataKey] =
  1118. [self.uploadLocationURL absoluteString];
  1119. }
  1120. if (self.uploadMIMEType) {
  1121. metadata[kGTMSessionIdentifierUploadMIMETypeMetadataKey] = self.uploadMIMEType;
  1122. }
  1123. metadata[kGTMSessionIdentifierUploadChunkSizeMetadataKey] = @(self.chunkSize);
  1124. metadata[kGTMSessionIdentifierUploadCurrentOffsetMetadataKey] = @(self.currentOffset);
  1125. return metadata;
  1126. }
  1127. - (GTMSessionFetcher *)uploadFetcherWithProperties:(NSDictionary *)properties
  1128. isQueryFetch:(BOOL)isQueryFetch {
  1129. GTMSessionCheckNotSynchronized(self);
  1130. // Common code to make a request for a query command or for a chunk upload.
  1131. NSURL *uploadLocationURL = self.uploadLocationURL;
  1132. NSMutableURLRequest *chunkRequest = [NSMutableURLRequest requestWithURL:uploadLocationURL];
  1133. [chunkRequest setHTTPMethod:@"PUT"];
  1134. // copy the user-agent from the original connection
  1135. NSURLRequest *origRequest = self.request;
  1136. NSString *userAgent = [origRequest valueForHTTPHeaderField:@"User-Agent"];
  1137. if (userAgent.length > 0) {
  1138. [chunkRequest setValue:userAgent forHTTPHeaderField:@"User-Agent"];
  1139. }
  1140. // To avoid timeouts when debugging, copy the timeout of the initial fetcher.
  1141. NSTimeInterval origTimeout = [origRequest timeoutInterval];
  1142. [chunkRequest setTimeoutInterval:origTimeout];
  1143. //
  1144. // Make a new chunk fetcher.
  1145. //
  1146. GTMSessionFetcher *chunkFetcher = [GTMSessionFetcher fetcherWithRequest:chunkRequest];
  1147. chunkFetcher.callbackQueue = self.callbackQueue;
  1148. chunkFetcher.sessionUserInfo = self.sessionUserInfo;
  1149. chunkFetcher.configurationBlock = self.configurationBlock;
  1150. chunkFetcher.allowedInsecureSchemes = self.allowedInsecureSchemes;
  1151. chunkFetcher.allowLocalhostRequest = self.allowLocalhostRequest;
  1152. chunkFetcher.allowInvalidServerCertificates = self.allowInvalidServerCertificates;
  1153. chunkFetcher.useUploadTask = !isQueryFetch;
  1154. if (self.uploadFileURL && !isQueryFetch && self.useBackgroundSession) {
  1155. [chunkFetcher createSessionIdentifierWithMetadata:[self uploadSessionIdentifierMetadata]];
  1156. }
  1157. // Give the chunk fetcher the same properties as the previous chunk fetcher
  1158. chunkFetcher.properties = [properties mutableCopy];
  1159. [chunkFetcher setProperty:[NSValue valueWithNonretainedObject:self]
  1160. forKey:kGTMSessionUploadFetcherChunkParentKey];
  1161. // copy other fetcher settings to the new fetcher
  1162. chunkFetcher.retryEnabled = self.retryEnabled;
  1163. chunkFetcher.maxRetryInterval = self.maxRetryInterval;
  1164. if ([self isRetryEnabled]) {
  1165. // We interpose our own retry method both so we can change the request to ask the server to
  1166. // tell us where to resume the chunk.
  1167. chunkFetcher.retryBlock = ^(BOOL suggestedWillRetry, NSError *chunkError,
  1168. GTMSessionFetcherRetryResponse response) {
  1169. void (^finish)(BOOL) = ^(BOOL shouldRetry){
  1170. // We'll retry by sending an offset query.
  1171. if (shouldRetry) {
  1172. self.shouldInitiateOffsetQuery = !isQueryFetch;
  1173. // We don't know what our actual offset is anymore, but the server will tell us.
  1174. self.currentOffset = 0;
  1175. }
  1176. // We don't actually want to retry this specific fetcher.
  1177. response(NO);
  1178. };
  1179. GTMSessionFetcherRetryBlock retryBlock = self.retryBlock;
  1180. if (retryBlock) {
  1181. // Ask the client, then call the finish block above.
  1182. retryBlock(suggestedWillRetry, chunkError, finish);
  1183. } else {
  1184. finish(suggestedWillRetry);
  1185. }
  1186. };
  1187. }
  1188. return chunkFetcher;
  1189. }
  1190. - (void)chunkFetcher:(GTMSessionFetcher *)chunkFetcher
  1191. finishedWithData:(NSData *)data
  1192. error:(NSError *)error {
  1193. BOOL hasDestroyedOldChunkFetcher = NO;
  1194. self.fetcherInFlight = nil;
  1195. NSDictionary *responseHeaders = [chunkFetcher responseHeaders];
  1196. GTMSessionUploadFetcherStatus uploadStatus =
  1197. [[self class] uploadStatusFromResponseHeaders:responseHeaders];
  1198. GTMSESSION_ASSERT_DEBUG(uploadStatus != kStatusUnknown
  1199. || error != nil
  1200. || self.wasCreatedFromBackgroundSession,
  1201. @"chunk fetcher completion has kStatusUnknown upload status for headers %@ fetcher %@",
  1202. responseHeaders, self);
  1203. BOOL isUploadStatusStopped = (uploadStatus == kStatusFinal || uploadStatus == kStatusCancelled);
  1204. // Check if the fetcher was actually querying. If it failed, do not retry,
  1205. // as it would enter an infinite retry loop.
  1206. NSString *uploadCommand =
  1207. chunkFetcher.request.allHTTPHeaderFields[kGTMSessionHeaderXGoogUploadCommand];
  1208. BOOL isQueryFetch = [uploadCommand isEqual:@"query"];
  1209. // TODO
  1210. // Maybe here we can check to see if the request had x goog content length set. (the file length one).
  1211. int64_t previousContentLength =
  1212. [[chunkFetcher.request valueForHTTPHeaderField:@"Content-Length"] longLongValue];
  1213. // The Content-Length header may not be present if the chunk fetcher was recreated from
  1214. // a background session.
  1215. BOOL hasKnownChunkSize = (previousContentLength > 0);
  1216. BOOL needsQuery = (!hasKnownChunkSize && !isUploadStatusStopped);
  1217. if (error || (needsQuery && !isQueryFetch)) {
  1218. NSInteger status = error.code;
  1219. // Status 4xx indicates a bad offset in the Google upload protocol. However, do not retry status
  1220. // 404 per spec, nor if the upload size appears to have been zero (since the server will just
  1221. // keep asking us to retry.)
  1222. if (self.shouldInitiateOffsetQuery ||
  1223. (needsQuery && !isQueryFetch) ||
  1224. ([error.domain isEqual:kGTMSessionFetcherStatusDomain] &&
  1225. status >= 400 && status <= 499 &&
  1226. status != 404 &&
  1227. uploadStatus == kStatusActive &&
  1228. previousContentLength > 0)) {
  1229. self.shouldInitiateOffsetQuery = NO;
  1230. [self destroyChunkFetcher];
  1231. hasDestroyedOldChunkFetcher = YES;
  1232. [self sendQueryForUploadOffsetWithFetcherProperties:chunkFetcher.properties];
  1233. } else {
  1234. // Some unexpected status has occurred; handle it as we would a regular
  1235. // object fetcher failure.
  1236. [self invokeFinalCallbackWithData:data
  1237. error:error
  1238. shouldInvalidateLocation:NO];
  1239. }
  1240. } else {
  1241. // The chunk has uploaded successfully.
  1242. int64_t newOffset = self.currentOffset + previousContentLength;
  1243. #if DEBUG
  1244. // Verify that if we think all of the uploading data has been sent, the server responded with
  1245. // the "final" upload status.
  1246. BOOL hasUploadAllData = (newOffset == [self fullUploadLength]);
  1247. BOOL isFinalStatus = (uploadStatus == kStatusFinal);
  1248. #pragma unused(hasUploadAllData,isFinalStatus)
  1249. GTMSESSION_ASSERT_DEBUG(hasUploadAllData == isFinalStatus || !hasKnownChunkSize,
  1250. @"uploadStatus:%@ newOffset:%lld (%lld + %lld) fullUploadLength:%lld"
  1251. @" chunkFetcher:%@ requestHeaders:%@ responseHeaders:%@",
  1252. [responseHeaders objectForKey:kGTMSessionHeaderXGoogUploadStatus],
  1253. newOffset, self.currentOffset, previousContentLength,
  1254. [self fullUploadLength],
  1255. chunkFetcher, chunkFetcher.request.allHTTPHeaderFields,
  1256. responseHeaders);
  1257. #endif
  1258. if (isUploadStatusStopped || (_currentOffset > _uploadFileLength && _uploadFileLength > 0)) {
  1259. // This was the last chunk.
  1260. if (error == nil && uploadStatus == kStatusCancelled) {
  1261. // Report cancelled status as an error.
  1262. NSDictionary *userInfo = nil;
  1263. if (data.length > 0) {
  1264. userInfo = @{ kGTMSessionFetcherStatusDataKey : data };
  1265. }
  1266. data = nil;
  1267. error = [self prematureFailureErrorWithUserInfo:userInfo];
  1268. } else {
  1269. // The upload is in final status.
  1270. //
  1271. // Take the chunk fetcher's data as the superclass data.
  1272. self.downloadedData = data;
  1273. self.statusCode = chunkFetcher.statusCode;
  1274. }
  1275. // we're done
  1276. [self invokeFinalCallbackWithData:data
  1277. error:error
  1278. shouldInvalidateLocation:YES];
  1279. } else {
  1280. // Start the next chunk.
  1281. self.currentOffset = newOffset;
  1282. // We want to destroy this chunk fetcher before creating the next one, but
  1283. // we want to pass on its properties
  1284. NSDictionary *props = [chunkFetcher properties];
  1285. // We no longer need to be able to cancel this chunkFetcher. Destroy it
  1286. // before we create a new chunk fetcher.
  1287. [self destroyChunkFetcher];
  1288. hasDestroyedOldChunkFetcher = YES;
  1289. [self uploadNextChunkWithOffset:newOffset
  1290. fetcherProperties:props];
  1291. }
  1292. }
  1293. if (!hasDestroyedOldChunkFetcher) {
  1294. [self destroyChunkFetcher];
  1295. }
  1296. }
  1297. - (void)destroyChunkFetcher {
  1298. @synchronized(self) {
  1299. GTMSessionMonitorSynchronized(self);
  1300. if (_fetcherInFlight == _chunkFetcher) {
  1301. _fetcherInFlight = nil;
  1302. }
  1303. [_chunkFetcher stopFetching];
  1304. NSURL *chunkFileURL = _chunkFetcher.bodyFileURL;
  1305. BOOL wasTemporaryUploadFile = ![chunkFileURL isEqual:_uploadFileURL];
  1306. if (wasTemporaryUploadFile) {
  1307. NSError *error;
  1308. [[NSFileManager defaultManager] removeItemAtURL:chunkFileURL
  1309. error:&error];
  1310. if (error) {
  1311. GTMSESSION_LOG_DEBUG(@"removingItemAtURL failed: %@\n%@", error, chunkFileURL);
  1312. }
  1313. }
  1314. _recentChunkReponseHeaders = _chunkFetcher.responseHeaders;
  1315. // To avoid retain cycles, remove all properties except the parent identifier.
  1316. _chunkFetcher.properties =
  1317. @{ kGTMSessionUploadFetcherChunkParentKey : [NSValue valueWithNonretainedObject:self] };
  1318. _chunkFetcher.retryBlock = nil;
  1319. _chunkFetcher.sendProgressBlock = nil;
  1320. _chunkFetcher = nil;
  1321. } // @synchronized(self)
  1322. }
  1323. // This method calculates the proper values to pass to the client's send progress block.
  1324. //
  1325. // The actual total bytes sent include the initial body sent, plus the
  1326. // offset into the batched data prior to the current chunk fetcher
  1327. - (void)invokeDelegateWithDidSendBytes:(int64_t)bytesSent
  1328. totalBytesSent:(int64_t)totalBytesSent
  1329. totalBytesExpectedToSend:(int64_t)totalBytesExpected {
  1330. GTMSessionCheckNotSynchronized(self);
  1331. // Ensure the chunk fetcher survives the callback in case the user pauses the upload process.
  1332. __block GTMSessionFetcher *holdFetcher = self.chunkFetcher;
  1333. [self invokeOnCallbackQueue:self.delegateCallbackQueue
  1334. afterUserStopped:NO
  1335. block:^{
  1336. GTMSessionFetcherSendProgressBlock sendProgressBlock = self.sendProgressBlock;
  1337. if (sendProgressBlock) {
  1338. sendProgressBlock(bytesSent, totalBytesSent, totalBytesExpected);
  1339. }
  1340. holdFetcher = nil;
  1341. }];
  1342. }
  1343. - (void)retrieveUploadChunkGranularityFromResponseHeaders:(NSDictionary *)responseHeaders {
  1344. GTMSessionCheckNotSynchronized(self);
  1345. // Standard granularity for Google uploads is 256K.
  1346. NSString *chunkGranularityHeader =
  1347. [responseHeaders objectForKey:kGTMSessionHeaderXGoogUploadChunkGranularity];
  1348. self.uploadGranularity = chunkGranularityHeader.longLongValue;
  1349. }
  1350. #pragma mark -
  1351. - (BOOL)isPaused {
  1352. @synchronized(self) {
  1353. GTMSessionMonitorSynchronized(self);
  1354. return _isPaused;
  1355. } // @synchronized(self)
  1356. }
  1357. - (void)pauseFetching {
  1358. @synchronized(self) {
  1359. GTMSessionMonitorSynchronized(self);
  1360. _isPaused = YES;
  1361. } // @synchronized(self)
  1362. // Pausing just means stopping the current chunk from uploading;
  1363. // when we resume, we will send a query request to the server to
  1364. // figure out what bytes to resume sending.
  1365. //
  1366. // We won't try to cancel the initial data upload, but rather will check
  1367. // for being paused in beginChunkFetches.
  1368. [self destroyChunkFetcher];
  1369. }
  1370. - (void)resumeFetching {
  1371. BOOL wasPaused;
  1372. @synchronized(self) {
  1373. GTMSessionMonitorSynchronized(self);
  1374. wasPaused = _isPaused;
  1375. _isPaused = NO;
  1376. } // @synchronized(self)
  1377. if (wasPaused) {
  1378. [self sendQueryForUploadOffsetWithFetcherProperties:self.properties];
  1379. }
  1380. }
  1381. - (void)stopFetching {
  1382. // Overrides the superclass
  1383. [self destroyChunkFetcher];
  1384. // If we think the server is waiting for more data, then tell it there won't be more.
  1385. if (self.uploadLocationURL) {
  1386. [self sendCancelUploadWithFetcherProperties:[self properties]];
  1387. self.uploadLocationURL = nil;
  1388. } else {
  1389. [self invokeOnCallbackQueue:self.callbackQueue
  1390. afterUserStopped:YES
  1391. block:^{
  1392. // Repeated calls to stopFetching may cause this path to be reached despite having sent a real
  1393. // cancel request, check here to ensure that the cancellation handler invocation which fires
  1394. // will definitely be for the real request sent previously.
  1395. @synchronized(self) {
  1396. if (self->_isCancelInFlight) {
  1397. return;
  1398. }
  1399. }
  1400. [self triggerCancellationHandlerForFetch:nil data:nil error:nil];
  1401. }];
  1402. }
  1403. [super stopFetching];
  1404. }
  1405. // Fires the cancellation handler, returning whether there was a handler to be fired.
  1406. - (BOOL)triggerCancellationHandlerForFetch:(GTMSessionFetcher *)fetcher
  1407. data:(NSData *)data
  1408. error:(NSError *)error {
  1409. GTMSessionUploadFetcherCancellationHandler handler = self.cancellationHandler;
  1410. if (handler) {
  1411. handler(fetcher, data, error);
  1412. self.cancellationHandler = nil;
  1413. return YES;
  1414. }
  1415. return NO;
  1416. }
  1417. #pragma mark -
  1418. - (int64_t)updateChunkFetcher:(GTMSessionFetcher *)chunkFetcher
  1419. forChunkAtOffset:(int64_t)offset {
  1420. BOOL isUploadingFileURL = (self.uploadFileURL != nil);
  1421. // Upload another chunk, meeting server-required granularity.
  1422. int64_t chunkSize = self.chunkSize;
  1423. int64_t fullUploadLength = [self fullUploadLength];
  1424. BOOL isFileLengthKnown = fullUploadLength >= 0;
  1425. BOOL isUploadingFullFile = (offset == 0 && isFileLengthKnown && chunkSize >= fullUploadLength);
  1426. if (!isUploadingFileURL || !isUploadingFullFile) {
  1427. // We're not uploading the entire file and given the file URL. Since we'll be
  1428. // allocating a subdata block for a chunk, we need to bound it to something that
  1429. // won't blow the process's memory.
  1430. if (chunkSize > kGTMSessionUploadFetcherMaximumDemandBufferSize) {
  1431. chunkSize = kGTMSessionUploadFetcherMaximumDemandBufferSize;
  1432. }
  1433. }
  1434. int64_t granularity = self.uploadGranularity;
  1435. if (granularity > 0) {
  1436. if (chunkSize < granularity) {
  1437. chunkSize = granularity;
  1438. } else {
  1439. chunkSize = chunkSize - (chunkSize % granularity);
  1440. }
  1441. }
  1442. GTMSESSION_ASSERT_DEBUG(offset < fullUploadLength || fullUploadLength == 0,
  1443. @"offset %lld exceeds data length %lld", offset, fullUploadLength);
  1444. if (granularity > 0) {
  1445. offset = offset - (offset % granularity);
  1446. }
  1447. // If the chunk size is bigger than the remaining data, or else
  1448. // it's close enough in size to the remaining data that we'd rather
  1449. // avoid having a whole extra http fetch for the leftover bit, then make
  1450. // this chunk size exactly match the remaining data size
  1451. NSString *command;
  1452. int64_t thisChunkSize = chunkSize;
  1453. BOOL isChunkTooBig = (thisChunkSize >= (fullUploadLength - offset));
  1454. BOOL isChunkAlmostBigEnough = (fullUploadLength - offset - 2500 < thisChunkSize);
  1455. BOOL isFinalChunk = (isChunkTooBig || isChunkAlmostBigEnough) && isFileLengthKnown;
  1456. if (isFinalChunk) {
  1457. thisChunkSize = fullUploadLength - offset;
  1458. if (thisChunkSize > 0) {
  1459. command = @"upload, finalize";
  1460. } else {
  1461. command = @"finalize";
  1462. }
  1463. } else {
  1464. command = @"upload";
  1465. }
  1466. NSString *lengthStr = @(thisChunkSize).stringValue;
  1467. NSString *offsetStr = @(offset).stringValue;
  1468. [chunkFetcher setRequestValue:command forHTTPHeaderField:kGTMSessionHeaderXGoogUploadCommand];
  1469. [chunkFetcher setRequestValue:lengthStr forHTTPHeaderField:@"Content-Length"];
  1470. [chunkFetcher setRequestValue:offsetStr forHTTPHeaderField:kGTMSessionHeaderXGoogUploadOffset];
  1471. if (_uploadFileLength != kGTMSessionUploadFetcherUnknownFileSize) {
  1472. [chunkFetcher setRequestValue:@([self fullUploadLength]).stringValue
  1473. forHTTPHeaderField:kGTMSessionHeaderXGoogUploadContentLength];
  1474. }
  1475. // Append the range of bytes in this chunk to the fetcher comment.
  1476. NSString *baseComment = self.comment;
  1477. [chunkFetcher setCommentWithFormat:@"%@ (%lld-%lld)",
  1478. baseComment ? baseComment : @"upload", offset, MAX(0, offset + thisChunkSize - 1)];
  1479. return thisChunkSize;
  1480. }
  1481. // Public properties.
  1482. @synthesize currentOffset = _currentOffset,
  1483. delegateCompletionHandler = _delegateCompletionHandler,
  1484. chunkFetcher = _chunkFetcher,
  1485. lastChunkRequest = _lastChunkRequest,
  1486. subdataGenerating = _subdataGenerating,
  1487. shouldInitiateOffsetQuery = _shouldInitiateOffsetQuery,
  1488. uploadGranularity = _uploadGranularity;
  1489. // Internal properties.
  1490. @dynamic fetcherInFlight;
  1491. @dynamic activeFetcher;
  1492. @dynamic statusCode;
  1493. @dynamic delegateCallbackQueue;
  1494. + (void)removePointer:(void *)pointer fromPointerArray:(NSPointerArray *)pointerArray {
  1495. for (NSUInteger index = 0, count = pointerArray.count; index < count; ++index) {
  1496. void *pointerAtIndex = [pointerArray pointerAtIndex:index];
  1497. if (pointerAtIndex == pointer) {
  1498. [pointerArray removePointerAtIndex:index];
  1499. return;
  1500. }
  1501. }
  1502. }
  1503. - (BOOL)useBackgroundSession {
  1504. @synchronized(self) {
  1505. GTMSessionMonitorSynchronized(self);
  1506. return _useBackgroundSessionOnChunkFetchers;
  1507. } // @synchronized(self
  1508. }
  1509. - (void)setUseBackgroundSession:(BOOL)useBackgroundSession {
  1510. @synchronized(self) {
  1511. GTMSessionMonitorSynchronized(self);
  1512. if (_useBackgroundSessionOnChunkFetchers != useBackgroundSession) {
  1513. _useBackgroundSessionOnChunkFetchers = useBackgroundSession;
  1514. NSPointerArray *uploadFetcherPointerArrayForBackgroundSessions =
  1515. [[self class] uploadFetcherPointerArrayForBackgroundSessions];
  1516. if (_useBackgroundSessionOnChunkFetchers) {
  1517. [uploadFetcherPointerArrayForBackgroundSessions addPointer:(__bridge void *)self];
  1518. } else {
  1519. [[self class] removePointer:(__bridge void *)self
  1520. fromPointerArray:uploadFetcherPointerArrayForBackgroundSessions];
  1521. }
  1522. }
  1523. } // @synchronized(self
  1524. }
  1525. - (BOOL)canFetchWithBackgroundSession {
  1526. // The initial upload fetcher is always a foreground session; the
  1527. // useBackgroundSession property will apply only to chunk fetchers,
  1528. // not to queries.
  1529. return NO;
  1530. }
  1531. - (NSDictionary *)responseHeaders {
  1532. GTMSessionCheckNotSynchronized(self);
  1533. // Overrides the superclass
  1534. // If asked for the fetcher's response, use the most recent chunk fetcher's response,
  1535. // since the original request's response lacks useful information like the actual
  1536. // Content-Type.
  1537. NSDictionary *dict = self.chunkFetcher.responseHeaders;
  1538. if (dict) {
  1539. return dict;
  1540. }
  1541. @synchronized(self) {
  1542. GTMSessionMonitorSynchronized(self);
  1543. if (_recentChunkReponseHeaders) {
  1544. return _recentChunkReponseHeaders;
  1545. }
  1546. } // @synchronized(self
  1547. // No chunk fetcher yet completed, so return whatever we have from the initial fetch.
  1548. return [super responseHeaders];
  1549. }
  1550. - (NSInteger)statusCodeUnsynchronized {
  1551. GTMSessionCheckSynchronized(self);
  1552. if (_recentChunkStatusCode != -1) {
  1553. // Overrides the superclass to indicate status appropriate to the initial
  1554. // or latest chunk fetch
  1555. return _recentChunkStatusCode;
  1556. } else {
  1557. return [super statusCodeUnsynchronized];
  1558. }
  1559. }
  1560. - (void)setStatusCode:(NSInteger)val {
  1561. @synchronized(self) {
  1562. GTMSessionMonitorSynchronized(self);
  1563. _recentChunkStatusCode = val;
  1564. }
  1565. }
  1566. - (int64_t)initialBodyLength {
  1567. @synchronized(self) {
  1568. GTMSessionMonitorSynchronized(self);
  1569. return _initialBodyLength;
  1570. }
  1571. }
  1572. - (void)setInitialBodyLength:(int64_t)length {
  1573. @synchronized(self) {
  1574. GTMSessionMonitorSynchronized(self);
  1575. _initialBodyLength = length;
  1576. }
  1577. }
  1578. - (int64_t)initialBodySent {
  1579. @synchronized(self) {
  1580. GTMSessionMonitorSynchronized(self);
  1581. return _initialBodySent;
  1582. }
  1583. }
  1584. - (void)setInitialBodySent:(int64_t)length {
  1585. @synchronized(self) {
  1586. GTMSessionMonitorSynchronized(self);
  1587. _initialBodySent = length;
  1588. }
  1589. }
  1590. - (NSURL *)uploadLocationURL {
  1591. @synchronized(self) {
  1592. GTMSessionMonitorSynchronized(self);
  1593. return _uploadLocationURL;
  1594. }
  1595. }
  1596. - (void)setUploadLocationURL:(NSURL *)locationURL {
  1597. @synchronized(self) {
  1598. GTMSessionMonitorSynchronized(self);
  1599. _uploadLocationURL = locationURL;
  1600. }
  1601. }
  1602. - (GTMSessionFetcher *)activeFetcher {
  1603. GTMSessionFetcher *result = self.fetcherInFlight;
  1604. if (result) return result;
  1605. return self;
  1606. }
  1607. - (BOOL)isFetching {
  1608. // If there is an active chunk fetcher, then the upload fetcher is considered
  1609. // to still be fetching.
  1610. if (self.fetcherInFlight != nil) return YES;
  1611. return [super isFetching];
  1612. }
  1613. - (BOOL)waitForCompletionWithTimeout:(NSTimeInterval)timeoutInSeconds {
  1614. NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeoutInSeconds];
  1615. while (self.fetcherInFlight || self.subdataGenerating) {
  1616. if ([timeoutDate timeIntervalSinceNow] < 0) return NO;
  1617. if (self.subdataGenerating) {
  1618. // Allow time for subdata generation.
  1619. NSDate *stopDate = [NSDate dateWithTimeIntervalSinceNow:0.001];
  1620. [[NSRunLoop currentRunLoop] runUntilDate:stopDate];
  1621. } else {
  1622. // Wait for any chunk or query fetchers that still have pending callbacks or
  1623. // notifications.
  1624. BOOL timedOut;
  1625. if (self.fetcherInFlight == self) {
  1626. timedOut = ![super waitForCompletionWithTimeout:timeoutInSeconds];
  1627. } else {
  1628. timedOut = ![self.fetcherInFlight waitForCompletionWithTimeout:timeoutInSeconds];
  1629. }
  1630. if (timedOut) return NO;
  1631. }
  1632. }
  1633. return YES;
  1634. }
  1635. @end
  1636. @implementation GTMSessionFetcher (GTMSessionUploadFetcherMethods)
  1637. - (GTMSessionUploadFetcher *)parentUploadFetcher {
  1638. NSValue *property = [self propertyForKey:kGTMSessionUploadFetcherChunkParentKey];
  1639. if (!property) return nil;
  1640. GTMSessionUploadFetcher *uploadFetcher = property.nonretainedObjectValue;
  1641. GTMSESSION_ASSERT_DEBUG([uploadFetcher isKindOfClass:[GTMSessionUploadFetcher class]],
  1642. @"Unexpected parent upload fetcher class: %@", [uploadFetcher class]);
  1643. return uploadFetcher;
  1644. }
  1645. @end