Ben Copsey

More wack-a-mole style bug fixing

@@ -21,7 +21,7 @@ @@ -21,7 +21,7 @@
21 #import "ASIInputStream.h" 21 #import "ASIInputStream.h"
22 22
23 // Automatically set on build 23 // Automatically set on build
24 -NSString *ASIHTTPRequestVersion = @"v1.2-43 2009-12-18"; 24 +NSString *ASIHTTPRequestVersion = @"v1.2-44 2009-12-18";
25 25
26 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; 26 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
27 27
@@ -867,6 +867,16 @@ static BOOL isiPhoneOS2; @@ -867,6 +867,16 @@ static BOOL isiPhoneOS2;
867 return; 867 return;
868 } 868 }
869 869
  870 + // Start the HTTP connection
  871 + if (!CFReadStreamOpen(readStream)) {
  872 + [self destroyReadStream];
  873 + [[self cancelledLock] unlock];
  874 + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to start HTTP connection",NSLocalizedDescriptionKey,nil]]];
  875 + return;
  876 + }
  877 +
  878 + [[self cancelledLock] unlock];
  879 +
870 if (shouldResetProgressIndicators) { 880 if (shouldResetProgressIndicators) {
871 double amount = 1; 881 double amount = 1;
872 if (showAccurateProgress) { 882 if (showAccurateProgress) {
@@ -883,17 +893,16 @@ static BOOL isiPhoneOS2; @@ -883,17 +893,16 @@ static BOOL isiPhoneOS2;
883 // Record when the request started, so we can timeout if nothing happens 893 // Record when the request started, so we can timeout if nothing happens
884 [self setLastActivityTime:[NSDate date]]; 894 [self setLastActivityTime:[NSDate date]];
885 895
886 - 896 +
887 - // Schedule the stream  
888 - if (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0) {  
889 - [self scheduleReadStream];  
890 - }  
891 -  
892 - [[self cancelledLock] unlock];  
893 } 897 }
894 898
895 - (void)loadAsynchronous 899 - (void)loadAsynchronous
896 { 900 {
  901 + // Schedule the stream
  902 + if (!readStreamIsScheduled && (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0)) {
  903 + [self scheduleReadStream];
  904 + }
  905 +
897 [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateStatus:) userInfo:nil repeats:YES]; 906 [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateStatus:) userInfo:nil repeats:YES];
898 907
899 // If we're running asynchronously on the main thread, the runloop will already be running 908 // If we're running asynchronously on the main thread, the runloop will already be running
@@ -907,6 +916,11 @@ static BOOL isiPhoneOS2; @@ -907,6 +916,11 @@ static BOOL isiPhoneOS2;
907 // This is the main loop for synchronous requests. 916 // This is the main loop for synchronous requests.
908 - (void)loadSynchronous 917 - (void)loadSynchronous
909 { 918 {
  919 + // Schedule the stream
  920 + if (!readStreamIsScheduled && (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0)) {
  921 + [self scheduleReadStream];
  922 + }
  923 +
910 while (!complete) { 924 while (!complete) {
911 [self checkRequestStatus]; 925 [self checkRequestStatus];
912 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.25, NO); 926 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.25, NO);
@@ -968,7 +982,6 @@ static BOOL isiPhoneOS2; @@ -968,7 +982,6 @@ static BOOL isiPhoneOS2;
968 } else { 982 } else {
969 [[self cancelledLock] unlock]; 983 [[self cancelledLock] unlock];
970 // Go all the way back to the beginning and build the request again, so that we can apply any new cookies 984 // Go all the way back to the beginning and build the request again, so that we can apply any new cookies
971 -  
972 [self main]; 985 [self main];
973 } 986 }
974 return; 987 return;
@@ -991,10 +1004,6 @@ static BOOL isiPhoneOS2; @@ -991,10 +1004,6 @@ static BOOL isiPhoneOS2;
991 1004
992 } 1005 }
993 1006
994 - // This thread should wait for 1/4 second for the stream to do something  
995 - //CFRunLoopRunInMode(kCFRunLoopDefaultMode,0.25,NO);  
996 -  
997 -  
998 [[self cancelledLock] unlock]; 1007 [[self cancelledLock] unlock];
999 } 1008 }
1000 1009
@@ -2358,7 +2367,9 @@ static BOOL isiPhoneOS2; @@ -2358,7 +2367,9 @@ static BOOL isiPhoneOS2;
2358 { 2367 {
2359 if (readStream) { 2368 if (readStream) {
2360 CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL); 2369 CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);
2361 - CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 2370 + if (readStreamIsScheduled) {
  2371 + CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  2372 + }
2362 CFReadStreamClose(readStream); 2373 CFReadStreamClose(readStream);
2363 CFRelease(readStream); 2374 CFRelease(readStream);
2364 readStream = NULL; 2375 readStream = NULL;
@@ -2370,24 +2381,14 @@ static BOOL isiPhoneOS2; @@ -2370,24 +2381,14 @@ static BOOL isiPhoneOS2;
2370 if (readStream && !readStreamIsScheduled) { 2381 if (readStream && !readStreamIsScheduled) {
2371 // Reset the timeout 2382 // Reset the timeout
2372 [self setLastActivityTime:[NSDate date]]; 2383 [self setLastActivityTime:[NSDate date]];
2373 - CFStreamClientContext ctxt = {0, self, NULL, NULL, NULL};  
2374 - CFReadStreamSetClient(readStream, kNetworkEvents, ReadStreamClientCallBack, &ctxt);  
2375 CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 2384 CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
2376 readStreamIsScheduled = YES; 2385 readStreamIsScheduled = YES;
2377 -  
2378 - // Start the HTTP connection  
2379 - if (!CFReadStreamOpen(readStream)) {  
2380 - [self destroyReadStream];  
2381 - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to start HTTP connection",NSLocalizedDescriptionKey,nil]]];  
2382 - return;  
2383 - }  
2384 } 2386 }
2385 } 2387 }
2386 2388
2387 - (void)unscheduleReadStream 2389 - (void)unscheduleReadStream
2388 { 2390 {
2389 if (readStream && readStreamIsScheduled) { 2391 if (readStream && readStreamIsScheduled) {
2390 - CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);  
2391 CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 2392 CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
2392 readStreamIsScheduled = NO; 2393 readStreamIsScheduled = NO;
2393 } 2394 }
@@ -1044,11 +1044,11 @@ @@ -1044,11 +1044,11 @@
1044 // We'll test first without throttling 1044 // We'll test first without throttling
1045 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_%28abridged%29.txt"]]; 1045 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_%28abridged%29.txt"]];
1046 NSDate *date = [NSDate date]; 1046 NSDate *date = [NSDate date];
1047 - //[request startSynchronous]; 1047 + [request startSynchronous];
1048 1048
1049 NSTimeInterval interval =[date timeIntervalSinceNow]; 1049 NSTimeInterval interval =[date timeIntervalSinceNow];
1050 BOOL success = (interval > -7); 1050 BOOL success = (interval > -7);
1051 - //GHAssertTrue(success,@"Downloaded the file too slowly - either this is a bug, or your internet connection is too slow to run this test (must be able to download 128KB in less than 7 seconds, without throttling)"); 1051 + GHAssertTrue(success,@"Downloaded the file too slowly - either this is a bug, or your internet connection is too slow to run this test (must be able to download 128KB in less than 7 seconds, without throttling)");
1052 1052
1053 // Now we'll test with throttling 1053 // Now we'll test with throttling
1054 [ASIHTTPRequest setMaxBandwidthPerSecond:ASIWWANBandwidthThrottleAmount]; 1054 [ASIHTTPRequest setMaxBandwidthPerSecond:ASIWWANBandwidthThrottleAmount];
@@ -695,6 +695,7 @@ IMPORTANT @@ -695,6 +695,7 @@ IMPORTANT
695 [request setDownloadDestinationPath:downloadPath]; 695 [request setDownloadDestinationPath:downloadPath];
696 [request setTemporaryFileDownloadPath:temporaryPath]; 696 [request setTemporaryFileDownloadPath:temporaryPath];
697 [request setAllowResumeForFileDownloads:YES]; 697 [request setAllowResumeForFileDownloads:YES];
  698 + [networkQueue setRunRequestsInBackgroundThread:YES];
698 [networkQueue addOperation:request]; 699 [networkQueue addOperation:request];
699 [networkQueue go]; 700 [networkQueue go];
700 701
@@ -754,6 +755,7 @@ IMPORTANT @@ -754,6 +755,7 @@ IMPORTANT
754 [networkQueue setDownloadProgressDelegate:self]; 755 [networkQueue setDownloadProgressDelegate:self];
755 [networkQueue setShowAccurateProgress:YES]; 756 [networkQueue setShowAccurateProgress:YES];
756 [networkQueue setDelegate:self]; 757 [networkQueue setDelegate:self];
  758 + [networkQueue setRunRequestsInBackgroundThread:YES];
757 [networkQueue setQueueDidFinishSelector:@selector(queueFinished:)]; 759 [networkQueue setQueueDidFinishSelector:@selector(queueFinished:)];
758 760
759 request = [[[ASIHTTPRequest alloc] initWithURL:downloadURL] autorelease]; 761 request = [[[ASIHTTPRequest alloc] initWithURL:downloadURL] autorelease];