diff --git a/Classes/ASIHTTPRequest.m b/Classes/ASIHTTPRequest.m index 9f83f97c..c6ba5178 100644 --- a/Classes/ASIHTTPRequest.m +++ b/Classes/ASIHTTPRequest.m @@ -21,7 +21,7 @@ #import "ASIInputStream.h" // Automatically set on build -NSString *ASIHTTPRequestVersion = @"v1.2-43 2009-12-18"; +NSString *ASIHTTPRequestVersion = @"v1.2-44 2009-12-18"; NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; @@ -867,6 +867,16 @@ - (void)startRequest return; } + // Start the HTTP connection + if (!CFReadStreamOpen(readStream)) { + [self destroyReadStream]; + [[self cancelledLock] unlock]; + [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to start HTTP connection",NSLocalizedDescriptionKey,nil]]]; + return; + } + + [[self cancelledLock] unlock]; + if (shouldResetProgressIndicators) { double amount = 1; if (showAccurateProgress) { @@ -883,17 +893,16 @@ - (void)startRequest // Record when the request started, so we can timeout if nothing happens [self setLastActivityTime:[NSDate date]]; - - // Schedule the stream - if (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0) { - [self scheduleReadStream]; - } - - [[self cancelledLock] unlock]; + } - (void)loadAsynchronous { + // Schedule the stream + if (!readStreamIsScheduled && (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0)) { + [self scheduleReadStream]; + } + [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateStatus:) userInfo:nil repeats:YES]; // If we're running asynchronously on the main thread, the runloop will already be running @@ -907,6 +916,11 @@ - (void)loadAsynchronous // This is the main loop for synchronous requests. - (void)loadSynchronous { + // Schedule the stream + if (!readStreamIsScheduled && (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0)) { + [self scheduleReadStream]; + } + while (!complete) { [self checkRequestStatus]; CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.25, NO); @@ -968,7 +982,6 @@ - (void)checkRequestStatus } else { [[self cancelledLock] unlock]; // Go all the way back to the beginning and build the request again, so that we can apply any new cookies - [self main]; } return; @@ -991,10 +1004,6 @@ - (void)checkRequestStatus } - // This thread should wait for 1/4 second for the stream to do something - //CFRunLoopRunInMode(kCFRunLoopDefaultMode,0.25,NO); - - [[self cancelledLock] unlock]; } @@ -2358,7 +2367,9 @@ - (void)destroyReadStream { if (readStream) { CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL); - CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + if (readStreamIsScheduled) { + CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + } CFReadStreamClose(readStream); CFRelease(readStream); readStream = NULL; @@ -2370,24 +2381,14 @@ - (void)scheduleReadStream if (readStream && !readStreamIsScheduled) { // Reset the timeout [self setLastActivityTime:[NSDate date]]; - CFStreamClientContext ctxt = {0, self, NULL, NULL, NULL}; - CFReadStreamSetClient(readStream, kNetworkEvents, ReadStreamClientCallBack, &ctxt); CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); readStreamIsScheduled = YES; - - // Start the HTTP connection - if (!CFReadStreamOpen(readStream)) { - [self destroyReadStream]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to start HTTP connection",NSLocalizedDescriptionKey,nil]]]; - return; - } } } - (void)unscheduleReadStream { if (readStream && readStreamIsScheduled) { - CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL); CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); readStreamIsScheduled = NO; } diff --git a/Classes/Tests/ASIHTTPRequestTests.m b/Classes/Tests/ASIHTTPRequestTests.m index aaf6ea84..c15e6eba 100644 --- a/Classes/Tests/ASIHTTPRequestTests.m +++ b/Classes/Tests/ASIHTTPRequestTests.m @@ -1044,11 +1044,11 @@ - (void)testThrottlingDownloadBandwidth // We'll test first without throttling ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/the_great_american_novel_%28abridged%29.txt"]]; NSDate *date = [NSDate date]; - //[request startSynchronous]; + [request startSynchronous]; NSTimeInterval interval =[date timeIntervalSinceNow]; BOOL success = (interval > -7); - //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)"); + 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)"); // Now we'll test with throttling [ASIHTTPRequest setMaxBandwidthPerSecond:ASIWWANBandwidthThrottleAmount]; diff --git a/Classes/Tests/ASINetworkQueueTests.m b/Classes/Tests/ASINetworkQueueTests.m index 9c42a65a..ac1343a5 100755 --- a/Classes/Tests/ASINetworkQueueTests.m +++ b/Classes/Tests/ASINetworkQueueTests.m @@ -695,6 +695,7 @@ - (void)testPartialResume [request setDownloadDestinationPath:downloadPath]; [request setTemporaryFileDownloadPath:temporaryPath]; [request setAllowResumeForFileDownloads:YES]; + [networkQueue setRunRequestsInBackgroundThread:YES]; [networkQueue addOperation:request]; [networkQueue go]; @@ -754,6 +755,7 @@ - (void)testPartialResume [networkQueue setDownloadProgressDelegate:self]; [networkQueue setShowAccurateProgress:YES]; [networkQueue setDelegate:self]; + [networkQueue setRunRequestsInBackgroundThread:YES]; [networkQueue setQueueDidFinishSelector:@selector(queueFinished:)]; request = [[[ASIHTTPRequest alloc] initWithURL:downloadURL] autorelease];