Ben Copsey

Dump partial download when no Content-Range header supplied (closes gh-63)

Tweaks to fix more LLVM warnings
... ... @@ -23,7 +23,7 @@
// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.7-22 2010-07-17";
NSString *ASIHTTPRequestVersion = @"v1.7-24 2010-07-18";
NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
... ... @@ -508,7 +508,7 @@ static NSOperationQueue *sharedQueue = nil;
[[self cancelledLock] unlock];
return;
}
[self failWithError:ASIRequestCancelledError];
[self setComplete:YES];
[self cancelLoad];
... ... @@ -1068,7 +1068,7 @@ static NSOperationQueue *sharedQueue = nil;
}
[connectionsLock unlock];
// Schedule the stream
if (![self readStreamIsScheduled] && (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0)) {
[self scheduleReadStream];
... ... @@ -1614,7 +1614,8 @@ static NSOperationQueue *sharedQueue = nil;
[self callSelectorOnMainThread:&didReceiveResponseHeadersSelector forDelegate:&delegate];
// Let the queue know we have started
[self callSelectorOnMainThread:&@selector(requestReceivedResponseHeaders:) forDelegate:&queue];
SEL sel = @selector(requestReceivedResponseHeaders:);
[self callSelectorOnMainThread:&sel forDelegate:&queue];
}
- (void)requestStarted
... ... @@ -1626,7 +1627,8 @@ static NSOperationQueue *sharedQueue = nil;
[self callSelectorOnMainThread:&didStartSelector forDelegate:&delegate];
// Let the queue know we have started
[self callSelectorOnMainThread:&@selector(requestStarted:) forDelegate:&queue];
SEL sel = @selector(requestStarted:);
[self callSelectorOnMainThread:&sel forDelegate:&queue];
}
// Subclasses might override this method to process the result in the same thread
... ... @@ -1643,7 +1645,8 @@ static NSOperationQueue *sharedQueue = nil;
[self callSelectorOnMainThread:&didFinishSelector forDelegate:&delegate];
// Let the queue know we are done
[self callSelectorOnMainThread:&@selector(requestFinished:) forDelegate:&queue];
SEL sel = @selector(requestFinished:);
[self callSelectorOnMainThread:&sel forDelegate:&queue];
}
... ... @@ -1653,7 +1656,8 @@ static NSOperationQueue *sharedQueue = nil;
[self callSelectorOnMainThread:&didFailSelector forDelegate:&delegate];
// Let the queue know something went wrong
[self callSelectorOnMainThread:&@selector(requestFailed:) forDelegate:&queue];
SEL sel = @selector(requestFailed:);
[self callSelectorOnMainThread:&sel forDelegate:&queue];
}
// Subclasses might override this method to perform error handling in the same thread
... ... @@ -2657,10 +2661,15 @@ static NSOperationQueue *sharedQueue = nil;
BOOL append = NO;
if (![self temporaryFileDownloadPath]) {
[self setTemporaryFileDownloadPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]];
} else if ([self allowResumeForFileDownloads]) {
append = YES;
} else if ([self allowResumeForFileDownloads] && [[self requestHeaders] objectForKey:@"Range"]) {
if ([[self responseHeaders] objectForKey:@"Content-Range"]) {
append = YES;
} else {
[self incrementDownloadSizeBy:-[self partialDownloadSize]];
[self setPartialDownloadSize:0];
}
}
[self setFileDownloadOutputStream:[[[NSOutputStream alloc] initToFileAtPath:[self temporaryFileDownloadPath] append:append] autorelease]];
[[self fileDownloadOutputStream] open];
}
... ... @@ -2889,7 +2898,7 @@ static NSOperationQueue *sharedQueue = nil;
{
NSError *underlyingError = NSMakeCollectable([(NSError *)CFReadStreamCopyError((CFReadStreamRef)[self readStream]) autorelease]);
[self cancelLoad];
if (![self error]) { // We may already have handled this error
... ...
... ... @@ -64,7 +64,7 @@
@property (retain) NSString *key;
@property (retain) NSString *sourceBucket;
@property (retain) NSString *sourceKey;
@property (retain) NSString *mimeType;
@property (retain, nonatomic) NSString *mimeType;
@property (retain) NSString *subResource;
@end
... ...
... ... @@ -471,6 +471,72 @@
}
}
- (void)testResumeChecksContentRangeHeader
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/no_resume"];
NSString *temporaryPath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"foo.temp"];
[@"" writeToFile:temporaryPath atomically:NO encoding:NSUTF8StringEncoding error:NULL];
NSString *downloadPath = [[self filePathForTemporaryTestFiles] stringByAppendingPathComponent:@"foo.txt"];
// Download part of a large file that is returned after a redirect
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setTemporaryFileDownloadPath:temporaryPath];
[request setDownloadDestinationPath:downloadPath];
[request setAllowResumeForFileDownloads:YES];
[request setAllowCompressedResponse:NO];
[request setShouldAttemptPersistentConnection:NO];
[request startAsynchronous];
// Cancel the request as soon as it has downloaded 64KB
while (1) {
sleep(0.5);
if ([request totalBytesRead] > 32*1024) {
[request cancel];
break;
}
}
NSNumber *fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:temporaryPath error:NULL] objectForKey:NSFileSize];
unsigned long long partialFileSize = [fileSize unsignedLongLongValue];
BOOL success = (partialFileSize < 1036935);
GHAssertTrue(success,@"Downloaded whole file too quickly, cannot proceed with this test");
// Resume the download
request = [ASIHTTPRequest requestWithURL:url];
[request setTemporaryFileDownloadPath:temporaryPath];
[request setDownloadDestinationPath:downloadPath];
[request setAllowResumeForFileDownloads:YES];
[request setAllowCompressedResponse:NO];
[request buildRequestHeaders];
success = ([request partialDownloadSize] == partialFileSize);
GHAssertTrue(success,@"Failed to obtain correct partial dowload size");
[request startAsynchronous];
while (1) {
sleep(0.5);
if ([request isFinished]) {
break;
}
}
GHAssertNil([request error],@"Request failed, cannot proceed with this test");
success = (![[request responseHeaders] objectForKey:@"Content-Range"]);
GHAssertTrue(success,@"Got range header back, cannot proceed with this test");
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:downloadPath error:NULL];
fileSize = [attributes objectForKey:NSFileSize];
success = ([fileSize intValue] == 1036935);
GHAssertTrue(success,@"Downloaded file has wrong length");
success = ([request partialDownloadSize] == 0);
GHAssertTrue(success,@"Failed to reset download size");
}
- (void)testRedirectedResume
{
[self performSelectorOnMainThread:@selector(runRedirectedResume) withObject:nil waitUntilDone:YES];
... ... @@ -1396,15 +1462,16 @@
- (void)asyncFail:(ASIHTTPRequest *)request
{
int requestNumber = [[[request userInfo] objectForKey:@"RequestNumber"] intValue];
GHAssertEquals(requestNumber,4,@"Wrong request failed");
BOOL success = (requestNumber == 4);
GHAssertTrue(success,@"Wrong request failed");
}
- (void)asyncSuccess:(ASIHTTPRequest *)request
{
int requestNumber = [[[request userInfo] objectForKey:@"RequestNumber"] intValue];
GHAssertNotEquals(requestNumber,4,@"Request succeeded when it should have failed");
BOOL success = (requestNumber != 4);
GHAssertTrue(success,@"Request succeeded when it should have failed");
BOOL success;
switch (requestNumber) {
case 1:
success = [[request responseString] isEqualToString:@"This is the expected content for the first string"];
... ... @@ -1619,7 +1686,6 @@
NSString *dateString = @"Thu, 19 Nov 1981 08:52:01 GMT";
NSDate *date = [ASIHTTPRequest dateFromRFC1123String:dateString];
NSDateComponents *components = [calendar components:dateUnits fromDate:date];
NSLog(@"%i",[components weekday]);
BOOL success = ([components year] == 1981 && [components month] == 11 && [components day] == 19 && [components weekday] == 5 && [components hour] == 8 && [components minute] == 52 && [components second] == 1);
GHAssertTrue(success,@"Failed to parse an RFC1123 date correctly");
... ...
... ... @@ -66,7 +66,7 @@
}
totalTime += times[i];
}
NSLog(@"Ran %hi requests in %f seconds (average time: %f secs / best time: %f secs / worst time: %f secs)",runTimes,totalTime,totalTime/runTimes,bestTime,worstTime);
NSLog(@"Ran %i requests in %f seconds (average time: %f secs / best time: %f secs / worst time: %f secs)",runTimes,totalTime,totalTime/runTimes,bestTime,worstTime);
}
... ... @@ -106,7 +106,7 @@
}
totalTime += times[i];
}
NSLog(@"Ran %hi requests in %f seconds (average time: %f secs / best time: %f secs / worst time: %f secs)",runTimes,totalTime,totalTime/runTimes,bestTime,worstTime);
NSLog(@"Ran %i requests in %f seconds (average time: %f secs / best time: %f secs / worst time: %f secs)",runTimes,totalTime,totalTime/runTimes,bestTime,worstTime);
}
... ...
This diff was suppressed by a .gitattributes entry.