diff --git a/Sparkle/SUAppcastItem.h b/Sparkle/SUAppcastItem.h index 2db02c65..c8a41117 100644 --- a/Sparkle/SUAppcastItem.h +++ b/Sparkle/SUAppcastItem.h @@ -21,6 +21,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 (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 02278f8e..9ba75c69 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -24,6 +24,7 @@ @property (copy, readwrite) NSString *minimumSystemVersion; @property (copy, readwrite) NSString *maximumSystemVersion; @property (strong, readwrite) NSURL *fileURL; +@property (nonatomic) NSUInteger contentLength; @property (copy, readwrite) NSString *versionString; @property (copy, readwrite) NSString *displayVersionString; @property (copy, readwrite) NSDictionary *deltaUpdates; @@ -37,6 +38,7 @@ @synthesize displayVersionString; @synthesize DSASignature; @synthesize fileURL; +@synthesize contentLength = _contentLength; @synthesize infoURL; @synthesize itemDescription; @synthesize maximumSystemVersion; @@ -61,6 +63,12 @@ self.DSASignature = [decoder decodeObjectOfClass:[NSString class] forKey:@"DSASignature"]; self.fileURL = [decoder decodeObjectOfClass:[NSURL class] forKey:@"fileURL"]; self.infoURL = [decoder decodeObjectOfClass:[NSURL class] forKey:@"infoURL"]; + + self.contentLength = (NSUInteger)[decoder decodeIntegerForKey:@"contentLength"]; + if (self.contentLength == 0) { + return nil; + } + self.itemDescription = [decoder decodeObjectOfClass:[NSString class] forKey:@"itemDescription"]; self.maximumSystemVersion = [decoder decodeObjectOfClass:[NSString class] forKey:@"maximumSystemVersion"]; self.minimumSystemVersion = [decoder decodeObjectOfClass:[NSString class] forKey:@"minimumSystemVersion"]; @@ -95,6 +103,8 @@ [encoder encodeObject:self.infoURL forKey:@"infoURL"]; } + [encoder encodeInteger:(NSInteger)self.contentLength forKey:@"contentLength"]; + if (self.itemDescription != nil) { [encoder encodeObject:self.itemDescription forKey:@"itemDescription"]; } @@ -212,6 +222,23 @@ } return nil; } + + NSString *enclosureLengthString = [enclosure objectForKey:SURSSAttributeLength]; + NSInteger contentLength = 0; + if (enclosureLengthString != nil) { + contentLength = [enclosureLengthString integerValue]; + } + if (contentLength > 0) { + self.contentLength = (NSUInteger)contentLength; + } else { + // content length is important enough to require + // developers copying the sample appcast should be using it already, + // and we can get away with assuming the content length provided is valid + if (error != NULL) { + *error = @"Feed item's enclosure lacks length"; + } + return nil; + } if (enclosureURLString) { // Sparkle used to always URL-encode, so for backwards compatibility spaces in URLs must be forgiven. diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index 4d482494..ff36ab3b 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -75,6 +75,7 @@ extern NSString *const SUAppcastElementReleaseNotesLink; extern NSString *const SUAppcastElementTags; extern NSString *const SURSSAttributeURL; +extern NSString *const SURSSAttributeLength; extern NSString *const SURSSElementDescription; extern NSString *const SURSSElementEnclosure; diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index d3081230..2c69d8ae 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -73,6 +73,7 @@ NSString *const SUAppcastElementReleaseNotesLink = @"sparkle:releaseNotesLink"; NSString *const SUAppcastElementTags = @"sparkle:tags"; NSString *const SURSSAttributeURL = @"url"; +NSString *const SURSSAttributeLength = @"length"; NSString *const SURSSElementDescription = @"description"; NSString *const SURSSElementEnclosure = @"enclosure"; diff --git a/Sparkle/SUCoreBasedUpdateDriver.h b/Sparkle/SUCoreBasedUpdateDriver.h index 7cf988de..4a483260 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.h +++ b/Sparkle/SUCoreBasedUpdateDriver.h @@ -33,7 +33,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloadDriverWillBeginDownload; -- (void)downloadDriverDidReceiveExpectedContentLength:(int64_t)expectedContentLength; +- (void)downloadDriverDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength; - (void)downloadDriverDidReceiveDataOfLength:(NSUInteger)length; diff --git a/Sparkle/SUCoreBasedUpdateDriver.m b/Sparkle/SUCoreBasedUpdateDriver.m index f82dc3bc..d01bcdb0 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.m +++ b/Sparkle/SUCoreBasedUpdateDriver.m @@ -141,7 +141,7 @@ } } -- (void)downloadDriverDidReceiveExpectedContentLength:(int64_t)expectedContentLength +- (void)downloadDriverDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength { if ([self.delegate respondsToSelector:@selector(downloadDriverDidReceiveExpectedContentLength:)]) { [self.delegate downloadDriverDidReceiveExpectedContentLength:expectedContentLength]; diff --git a/Sparkle/SUDownloadDriver.h b/Sparkle/SUDownloadDriver.h index 3354341d..cecc3a11 100644 --- a/Sparkle/SUDownloadDriver.h +++ b/Sparkle/SUDownloadDriver.h @@ -14,7 +14,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloadDriverWillBeginDownload; -- (void)downloadDriverDidReceiveExpectedContentLength:(int64_t)expectedContentLength; +- (void)downloadDriverDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength; - (void)downloadDriverDidReceiveDataOfLength:(NSUInteger)length; diff --git a/Sparkle/SUDownloadDriver.m b/Sparkle/SUDownloadDriver.m index e9b832d1..6b04b0c9 100644 --- a/Sparkle/SUDownloadDriver.m +++ b/Sparkle/SUDownloadDriver.m @@ -181,7 +181,20 @@ - (void)downloaderDidReceiveExpectedContentLength:(int64_t)expectedContentLength { dispatch_async(dispatch_get_main_queue(), ^{ - [self.delegate downloadDriverDidReceiveExpectedContentLength:expectedContentLength]; + BOOL validExpectedContentLength = (expectedContentLength > 0 && expectedContentLength != NSURLResponseUnknownLength); + NSUInteger expectedLength = (NSUInteger)expectedContentLength; + NSUInteger updateItemContentLength = self.updateItem.contentLength; + + NSUInteger contentLength = + validExpectedContentLength ? + updateItemContentLength : + expectedLength; + + if (validExpectedContentLength && updateItemContentLength != expectedLength) { + SULog(@"Warning: Downloader's expected content length (%lu) != Appcast item's length (%lu)", expectedLength, updateItemContentLength); + } + + [self.delegate downloadDriverDidReceiveExpectedContentLength:contentLength]; }); } diff --git a/Sparkle/SUStandardUserDriver.m b/Sparkle/SUStandardUserDriver.m index e8e52642..fe215c5b 100644 --- a/Sparkle/SUStandardUserDriver.m +++ b/Sparkle/SUStandardUserDriver.m @@ -317,7 +317,7 @@ [self.coreComponent cancelDownloadStatus]; } -- (void)showDownloadDidReceiveExpectedContentLength:(int64_t)expectedContentLength +- (void)showDownloadDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength { dispatch_async(dispatch_get_main_queue(), ^{ [self.statusController setMaxProgressValue:expectedContentLength]; diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 14db4ef4..1d0970dc 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -147,7 +147,7 @@ }]; } -- (void)downloadDriverDidReceiveExpectedContentLength:(int64_t)expectedContentLength +- (void)downloadDriverDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength { [self.userDriver showDownloadDidReceiveExpectedContentLength:expectedContentLength]; } diff --git a/Sparkle/SUUserDriver.h b/Sparkle/SUUserDriver.h index c372fe04..076e7be3 100644 --- a/Sparkle/SUUserDriver.h +++ b/Sparkle/SUUserDriver.h @@ -203,12 +203,11 @@ NS_ASSUME_NONNULL_BEGIN /*! * Show the user the content length of the new update that will be downloaded * - * @param expectedContentLength The expected content length of the new update being downloaded. - * This is NSURLResponseUnknownLength if the length is unknown. + * @param expectedContentLength The expected content length of the new update being downloaded. This will be greater than 0. * * This can be called from any thread */ -- (void)showDownloadDidReceiveExpectedContentLength:(int64_t)expectedContentLength; +- (void)showDownloadDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength; /*! * Show the user that the update download received more data diff --git a/TestApplication/SUPopUpTitlebarUserDriver.m b/TestApplication/SUPopUpTitlebarUserDriver.m index 04bda33c..9ba2f425 100644 --- a/TestApplication/SUPopUpTitlebarUserDriver.m +++ b/TestApplication/SUPopUpTitlebarUserDriver.m @@ -268,16 +268,12 @@ }); } -- (void)showDownloadDidReceiveExpectedContentLength:(int64_t)expectedContentLength +- (void)showDownloadDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength { dispatch_async(dispatch_get_main_queue(), ^{ [self addUpdateButtonWithTitle:@"Downloading…"]; self.contentLengthDownloaded = 0; - self.expectedContentLength = (NSUInteger)expectedContentLength; - if (self.expectedContentLength == 0) { - // don't divide by zero ever - self.expectedContentLength = 1; - } + self.expectedContentLength = expectedContentLength; }); } diff --git a/Tests/SUUpdaterTest.m b/Tests/SUUpdaterTest.m index dd28333b..e5129f2a 100644 --- a/Tests/SUUpdaterTest.m +++ b/Tests/SUUpdaterTest.m @@ -45,7 +45,7 @@ - (void)showDownloadInitiatedWithCompletion:(void (^)(SUDownloadUpdateStatus))__unused downloadUpdateStatusCompletion {} -- (void)showDownloadDidReceiveExpectedContentLength:(int64_t)__unused expectedContentLength {} +- (void)showDownloadDidReceiveExpectedContentLength:(NSUInteger)__unused expectedContentLength {} - (void)showDownloadDidReceiveDataOfLength:(NSUInteger)__unused length {} diff --git a/sparkle-cli/SUCommandLineUserDriver.m b/sparkle-cli/SUCommandLineUserDriver.m index 7f3d321a..fd4d2786 100644 --- a/sparkle-cli/SUCommandLineUserDriver.m +++ b/sparkle-cli/SUCommandLineUserDriver.m @@ -190,14 +190,14 @@ }); } -- (void)showDownloadDidReceiveExpectedContentLength:(int64_t)expectedContentLength +- (void)showDownloadDidReceiveExpectedContentLength:(NSUInteger)expectedContentLength { dispatch_async(dispatch_get_main_queue(), ^{ if (self.verbose) { fprintf(stderr, "Downloading %llu bytes...\n", expectedContentLength); } self.bytesDownloaded = 0; - self.bytesToDownload = (NSUInteger)expectedContentLength; + self.bytesToDownload = expectedContentLength; }); }