From e3f68b70bf11afe976c6cd0b017e84f8bfb88b74 Mon Sep 17 00:00:00 2001 From: Zorg Date: Thu, 29 Dec 2016 11:11:05 -0500 Subject: [PATCH] Prefer using HTTP expected content length over appcast item length Also use (u)int64_t instead of NS(U)Integer --- Downloader/SPUDownloaderDelegate.h | 2 +- Sparkle/SPUCoreBasedUpdateDriver.h | 4 ++-- Sparkle/SPUCoreBasedUpdateDriver.m | 4 ++-- Sparkle/SPUDownloadDriver.h | 4 ++-- Sparkle/SPUDownloadDriver.m | 19 +++++++------------ Sparkle/SPUStandardUserDriver.m | 4 ++-- Sparkle/SPUUIBasedUpdateDriver.m | 4 ++-- Sparkle/SPUURLDownload.m | 2 +- Sparkle/SPUUserDriver.h | 4 ++-- Sparkle/SUAppcastItem.h | 2 +- Sparkle/SUAppcastItem.m | 15 +++++---------- TestApplication/SUPopUpTitlebarUserDriver.m | 8 ++++---- sparkle-cli/SPUCommandLineUserDriver.m | 12 ++++++------ 13 files changed, 37 insertions(+), 47 deletions(-) diff --git a/Downloader/SPUDownloaderDelegate.h b/Downloader/SPUDownloaderDelegate.h index 9e1eb472..760db6c3 100644 --- a/Downloader/SPUDownloaderDelegate.h +++ b/Downloader/SPUDownloaderDelegate.h @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloaderDidReceiveExpectedContentLength:(int64_t)expectedContentLength; // This is only invoked for persistent downloads -- (void)downloaderDidReceiveDataOfLength:(NSUInteger)length; +- (void)downloaderDidReceiveDataOfLength:(uint64_t)length; // downloadData is nil if this is a persisent download, otherwise it's non-nil if it's a temporary download - (void)downloaderDidFinishWithTemporaryDownloadData:(SPUDownloadData * _Nullable)downloadData; diff --git a/Sparkle/SPUCoreBasedUpdateDriver.h b/Sparkle/SPUCoreBasedUpdateDriver.h index a483f212..cdf49b6e 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.h +++ b/Sparkle/SPUCoreBasedUpdateDriver.h @@ -31,9 +31,9 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloadDriverWillBeginDownload; -- (void)downloadDriverDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength; +- (void)downloadDriverDidReceiveExpectedContentLength:(uint64_t)expectedContentLength; -- (void)downloadDriverDidReceiveDataOfLength:(NSUInteger)length; +- (void)downloadDriverDidReceiveDataOfLength:(uint64_t)length; - (void)coreDriverDidStartExtractingUpdate; diff --git a/Sparkle/SPUCoreBasedUpdateDriver.m b/Sparkle/SPUCoreBasedUpdateDriver.m index cfb82834..e46e2fee 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.m +++ b/Sparkle/SPUCoreBasedUpdateDriver.m @@ -183,14 +183,14 @@ } } -- (void)downloadDriverDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength +- (void)downloadDriverDidReceiveExpectedContentLength:(uint64_t)expectedContentLength { if ([self.delegate respondsToSelector:@selector(downloadDriverDidReceiveExpectedContentLength:)]) { [self.delegate downloadDriverDidReceiveExpectedContentLength:expectedContentLength]; } } -- (void)downloadDriverDidReceiveDataOfLength:(NSUInteger)length +- (void)downloadDriverDidReceiveDataOfLength:(uint64_t)length { if ([self.delegate respondsToSelector:@selector(downloadDriverDidReceiveDataOfLength:)]) { [self.delegate downloadDriverDidReceiveDataOfLength:length]; diff --git a/Sparkle/SPUDownloadDriver.h b/Sparkle/SPUDownloadDriver.h index b022f736..2f9c9194 100644 --- a/Sparkle/SPUDownloadDriver.h +++ b/Sparkle/SPUDownloadDriver.h @@ -16,9 +16,9 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloadDriverWillBeginDownload; -- (void)downloadDriverDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength; +- (void)downloadDriverDidReceiveExpectedContentLength:(uint64_t)expectedContentLength; -- (void)downloadDriverDidReceiveDataOfLength:(NSUInteger)length; +- (void)downloadDriverDidReceiveDataOfLength:(uint64_t)length; - (void)downloadDriverDidDownloadUpdate:(SPUDownloadedUpdate *)downloadedUpdate; diff --git a/Sparkle/SPUDownloadDriver.m b/Sparkle/SPUDownloadDriver.m index db3ec0d1..bf0f50a4 100644 --- a/Sparkle/SPUDownloadDriver.m +++ b/Sparkle/SPUDownloadDriver.m @@ -34,8 +34,7 @@ @property (nonatomic, copy) NSString *downloadName; @property (nonatomic, weak) id delegate; @property (nonatomic) BOOL retrievedDownloadResult; -@property (nonatomic) BOOL retrievedDownloadResponse; -@property (nonatomic) NSUInteger expectedContentLength; +@property (nonatomic) uint64_t expectedContentLength; @property (nonatomic) BOOL cleaningUp; @end @@ -51,7 +50,6 @@ @synthesize downloadName = _downloadName; @synthesize delegate = _delegate; @synthesize retrievedDownloadResult = _retrievedDownloadResult; -@synthesize retrievedDownloadResponse = _retrievedDownloadResponse; @synthesize expectedContentLength = _expectedContentLength; @synthesize cleaningUp = _cleaningUp; @@ -140,7 +138,7 @@ self.retrievedDownloadResult = YES; if (self.expectedContentLength > 0 && self.updateItem.contentLength > 0 && self.expectedContentLength != self.updateItem.contentLength) { - SULog(@"Warning: Downloader's expected content length (%lu) != Appcast item's length (%lu)", self.expectedContentLength, self.updateItem.contentLength); + SULog(@"Warning: Downloader's expected content length (%llu) != Appcast item's length (%llu)", self.expectedContentLength, self.updateItem.contentLength); } SPUDownloadedUpdate *downloadedUpdate = [[SPUDownloadedUpdate alloc] initWithAppcastItem:self.updateItem downloadName:self.downloadName temporaryDirectory:self.temporaryDirectory]; @@ -186,21 +184,18 @@ - (void)downloaderDidReceiveExpectedContentLength:(int64_t)expectedContentLength { dispatch_async(dispatch_get_main_queue(), ^{ - // We only notify the expected content length once and we try to use the content length from the appcast instead of from the downloader - if (!self.retrievedDownloadResponse) { - [self.delegate downloadDriverDidReceiveExpectedContentLength:(self.updateItem.contentLength > 0 ? self.updateItem.contentLength : (NSUInteger)expectedContentLength)]; - self.retrievedDownloadResponse = YES; - } + // Fallback to appcast item's content length if we don't get the length from HTTP header + [self.delegate downloadDriverDidReceiveExpectedContentLength:expectedContentLength > 0 ? (uint64_t)expectedContentLength : self.updateItem.contentLength]; // Reset expected content length from downloader // Later we verify if the total length matches with the content length from the appcast - if (expectedContentLength > 0 && expectedContentLength != NSURLResponseUnknownLength) { - self.expectedContentLength = (NSUInteger)expectedContentLength; + if (expectedContentLength > 0) { + self.expectedContentLength = (uint64_t)expectedContentLength; } }); } -- (void)downloaderDidReceiveDataOfLength:(NSUInteger)length +- (void)downloaderDidReceiveDataOfLength:(uint64_t)length { dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate downloadDriverDidReceiveDataOfLength:length]; diff --git a/Sparkle/SPUStandardUserDriver.m b/Sparkle/SPUStandardUserDriver.m index 1234e75c..687b4a45 100644 --- a/Sparkle/SPUStandardUserDriver.m +++ b/Sparkle/SPUStandardUserDriver.m @@ -320,7 +320,7 @@ [self.coreComponent cancelDownloadStatus]; } -- (void)showDownloadDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength +- (void)showDownloadDidReceiveExpectedContentLength:(uint64_t)expectedContentLength { dispatch_async(dispatch_get_main_queue(), ^{ [self.statusController setMaxProgressValue:expectedContentLength]; @@ -333,7 +333,7 @@ countStyle:NSByteCountFormatterCountStyleFile]; } -- (void)showDownloadDidReceiveDataOfLength:(NSUInteger)length +- (void)showDownloadDidReceiveDataOfLength:(uint64_t)length { dispatch_async(dispatch_get_main_queue(), ^{ [self.statusController setProgressValue:[self.statusController progressValue] + (double)length]; diff --git a/Sparkle/SPUUIBasedUpdateDriver.m b/Sparkle/SPUUIBasedUpdateDriver.m index cd8d96fb..b58802ce 100644 --- a/Sparkle/SPUUIBasedUpdateDriver.m +++ b/Sparkle/SPUUIBasedUpdateDriver.m @@ -181,12 +181,12 @@ }]; } -- (void)downloadDriverDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength +- (void)downloadDriverDidReceiveExpectedContentLength:(uint64_t)expectedContentLength { [self.userDriver showDownloadDidReceiveExpectedContentLength:expectedContentLength]; } -- (void)downloadDriverDidReceiveDataOfLength:(NSUInteger)length +- (void)downloadDriverDidReceiveDataOfLength:(uint64_t)length { [self.userDriver showDownloadDidReceiveDataOfLength:length]; } diff --git a/Sparkle/SPUURLDownload.m b/Sparkle/SPUURLDownload.m index 8274f245..a7bdcd18 100644 --- a/Sparkle/SPUURLDownload.m +++ b/Sparkle/SPUURLDownload.m @@ -46,7 +46,7 @@ { } -- (void)downloaderDidReceiveDataOfLength:(NSUInteger)__unused length +- (void)downloaderDidReceiveDataOfLength:(uint64_t)__unused length { } diff --git a/Sparkle/SPUUserDriver.h b/Sparkle/SPUUserDriver.h index 24538a0e..22531d32 100644 --- a/Sparkle/SPUUserDriver.h +++ b/Sparkle/SPUUserDriver.h @@ -202,7 +202,7 @@ SU_EXPORT @protocol SPUUserDriver * * This can be called from any thread */ -- (void)showDownloadDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength; +- (void)showDownloadDidReceiveExpectedContentLength:(uint64_t)expectedContentLength; /*! * Show the user that the update download received more data @@ -212,7 +212,7 @@ SU_EXPORT @protocol SPUUserDriver * * This can be called from any thread */ -- (void)showDownloadDidReceiveDataOfLength:(NSUInteger)length; +- (void)showDownloadDidReceiveDataOfLength:(uint64_t)length; /*! * Show the user that the update finished downloading and started extracting diff --git a/Sparkle/SUAppcastItem.h b/Sparkle/SUAppcastItem.h index 54bbf509..9d7ef34a 100644 --- a/Sparkle/SUAppcastItem.h +++ b/Sparkle/SUAppcastItem.h @@ -25,7 +25,7 @@ SU_EXPORT @interface SUAppcastItem : NSObject @property (copy, readonly) NSString *minimumSystemVersion; @property (copy, readonly) NSString *maximumSystemVersion; @property (strong, readonly) NSURL *fileURL; -@property (nonatomic, readonly) NSUInteger contentLength; +@property (nonatomic, readonly) uint64_t contentLength; @property (copy, readonly) NSString *versionString; @property (copy, readonly) NSString *displayVersionString; @property (copy, readonly) NSDictionary *deltaUpdates; diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index 87ec4b7a..c2cf0f86 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -65,7 +65,7 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT _fileURL = [decoder decodeObjectOfClass:[NSURL class] forKey:SUAppcastItemFileURLKey]; _infoURL = [decoder decodeObjectOfClass:[NSURL class] forKey:SUAppcastItemInfoURLKey]; - _contentLength = (NSUInteger)[decoder decodeIntegerForKey:SUAppcastItemContentLengthKey]; + _contentLength = (uint64_t)[decoder decodeInt64ForKey:SUAppcastItemContentLengthKey]; _installationType = [decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastItemInstallationTypeKey]; if (!SPUValidInstallationType(_installationType)) { @@ -106,7 +106,7 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT [encoder encodeObject:self.infoURL forKey:SUAppcastItemInfoURLKey]; } - [encoder encodeInteger:(NSInteger)self.contentLength forKey:SUAppcastItemContentLengthKey]; + [encoder encodeInt64:(int64_t)self.contentLength forKey:SUAppcastItemContentLengthKey]; if (self.itemDescription != nil) { [encoder encodeObject:self.itemDescription forKey:SUAppcastItemDescriptionKey]; @@ -232,16 +232,11 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT if (enclosureURLString) { NSString *enclosureLengthString = [enclosure objectForKey:SURSSAttributeLength]; - NSInteger contentLength = 0; + long long contentLength = 0; if (enclosureLengthString != nil) { - contentLength = [enclosureLengthString integerValue]; - } else { - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - SULog(@"warning: <%@> for URL '%@' is missing %@ attribute. Downloading progress may be unreliable. Please always specify %@", SURSSElementEnclosure, [enclosure objectForKey:SURSSAttributeURL], SURSSAttributeLength, SURSSAttributeLength); - }); + contentLength = [enclosureLengthString longLongValue]; } - _contentLength = (contentLength > 0) ? (NSUInteger)contentLength : 0; + _contentLength = (contentLength > 0) ? (uint64_t)contentLength : 0; } if (enclosureURLString) { diff --git a/TestApplication/SUPopUpTitlebarUserDriver.m b/TestApplication/SUPopUpTitlebarUserDriver.m index 9aca5b1b..a0f77410 100644 --- a/TestApplication/SUPopUpTitlebarUserDriver.m +++ b/TestApplication/SUPopUpTitlebarUserDriver.m @@ -18,8 +18,8 @@ @property (nonatomic) BOOL addedAccessory; @property (nonatomic) NSButton *updateButton; @property (nonatomic, copy) void (^updateButtonAction)(NSButton *); -@property (nonatomic) NSUInteger expectedContentLength; -@property (nonatomic) NSUInteger contentLengthDownloaded; +@property (nonatomic) uint64_t expectedContentLength; +@property (nonatomic) uint64_t contentLengthDownloaded; @end @@ -268,7 +268,7 @@ }); } -- (void)showDownloadDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength +- (void)showDownloadDidReceiveExpectedContentLength:(uint64_t)expectedContentLength { dispatch_async(dispatch_get_main_queue(), ^{ [self addUpdateButtonWithTitle:@"Downloading…"]; @@ -277,7 +277,7 @@ }); } -- (void)showDownloadDidReceiveDataOfLength:(NSUInteger)length +- (void)showDownloadDidReceiveDataOfLength:(uint64_t)length { dispatch_async(dispatch_get_main_queue(), ^{ self.contentLengthDownloaded += length; diff --git a/sparkle-cli/SPUCommandLineUserDriver.m b/sparkle-cli/SPUCommandLineUserDriver.m index 7296ac4b..b0027c54 100644 --- a/sparkle-cli/SPUCommandLineUserDriver.m +++ b/sparkle-cli/SPUCommandLineUserDriver.m @@ -19,8 +19,8 @@ @property (nonatomic, readonly) BOOL deferInstallation; @property (nonatomic, readonly) BOOL verbose; @property (nonatomic, readonly) SPUUserDriverCoreComponent *coreComponent; -@property (nonatomic) NSUInteger bytesDownloaded; -@property (nonatomic) NSUInteger bytesToDownload; +@property (nonatomic) uint64_t bytesDownloaded; +@property (nonatomic) uint64_t bytesToDownload; @end @@ -215,23 +215,23 @@ }); } -- (void)showDownloadDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength +- (void)showDownloadDidReceiveExpectedContentLength:(uint64_t)expectedContentLength { dispatch_async(dispatch_get_main_queue(), ^{ if (self.verbose) { - fprintf(stderr, "Downloading %lu bytes...\n", (unsigned long)expectedContentLength); + fprintf(stderr, "Downloading %llu bytes...\n", expectedContentLength); } self.bytesDownloaded = 0; self.bytesToDownload = expectedContentLength; }); } -- (void)showDownloadDidReceiveDataOfLength:(NSUInteger)length +- (void)showDownloadDidReceiveDataOfLength:(uint64_t)length { dispatch_async(dispatch_get_main_queue(), ^{ self.bytesDownloaded += length; if (self.bytesToDownload > 0 && self.verbose) { - fprintf(stderr, "Downloaded %lu out of %lu bytes (%.0f%%)\n", (unsigned long)self.bytesDownloaded, (unsigned long)self.bytesToDownload, (self.bytesDownloaded * 100.0 / self.bytesToDownload)); + fprintf(stderr, "Downloaded %llu out of %llu bytes (%.0f%%)\n", self.bytesDownloaded, self.bytesToDownload, (self.bytesDownloaded * 100.0 / self.bytesToDownload)); } }); }