Skip to content

Commit

Permalink
Fix a bug where requests might erroneously timeout when not using a d…
Browse files Browse the repository at this point in the history
…ownloadProgressDelegate

Thanks to Martin Destagnol for his assistance in finding this issue!
  • Loading branch information
pokeb committed Jul 30, 2009
1 parent c5b610b commit 6b97579
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
8 changes: 2 additions & 6 deletions Classes/ASIHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ - (void)loadRequest
// Prevent timeouts before 128KB* has been sent when the size of data to upload is greater than 128KB* (*32KB on iPhone 3.0 SDK)
// This is to workaround the fact that kCFStreamPropertyHTTPRequestBytesWrittenCount is the amount written to the buffer, not the amount actually sent
// This workaround prevents erroneous timeouts in low bandwidth situations (eg iPhone)
if (contentLength <= uploadBufferSize || (uploadBufferSize > 0 && totalBytesSent > uploadBufferSize)) {
if (totalBytesSent || postLength <= uploadBufferSize || (uploadBufferSize > 0 && totalBytesSent > uploadBufferSize)) {
[self failWithError:ASIRequestTimedOutError];
[self cancelLoad];
[self setComplete:YES];
Expand Down Expand Up @@ -848,11 +848,6 @@ - (void)updateDownloadProgress
if (responseHeaders) {

unsigned long long bytesReadSoFar = totalBytesRead+partialDownloadSize;

if (bytesReadSoFar > lastBytesRead) {
[self setLastActivityTime:[NSDate date]];
}


// We're using a progress queue or compatible controller to handle progress
SEL selector = @selector(incrementDownloadProgressBy:);
Expand Down Expand Up @@ -1365,6 +1360,7 @@ - (void)handleBytesAvailable
} else if (bytesRead) {

[self setTotalBytesRead:[self totalBytesRead]+bytesRead];
[self setLastActivityTime:[NSDate date]];

// Are we downloading to a file?
if ([self downloadDestinationPath]) {
Expand Down
2 changes: 1 addition & 1 deletion Classes/Tests/ASIHTTPRequestTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
- (void)test303Redirect;
- (void)testCompression;
- (void)testSubclass;

- (void)testTimeOutWithoutDownloadDelegate;
@end
13 changes: 13 additions & 0 deletions Classes/Tests/ASIHTTPRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,19 @@ - (void)testTimeOut

BOOL success = [[request error] code] == ASIRequestTimedOutErrorType;
GHAssertTrue(success,@"Timeout didn't generate the correct error");
}


// Test fix for a bug that might have caused timeouts when posting data
- (void)testTimeOutWithoutDownloadDelegate
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://trails-network.net/Downloads/MemexTrails_1.0b1.zip"]];
[request setTimeOutSeconds:5];
[request setShowAccurateProgress:NO];
[request setPostBody:[NSMutableData dataWithData:[@"Small Body" dataUsingEncoding:NSUTF8StringEncoding]]];
[request start];

GHAssertNil([request error],@"Generated an error (most likely a timeout) - this test might fail on high latency connections");
}


Expand Down Expand Up @@ -782,6 +794,7 @@ - (void)testSubclass
}



@end


10 changes: 5 additions & 5 deletions Classes/Tests/ASINetworkQueueTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ - (void)testProgress

[networkQueue go];

while (!complete) {
while (!complete) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
}

}
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
BOOL success = (progress > 0.95);
GHAssertTrue(success,@"Failed to increment progress properly");

Expand Down Expand Up @@ -117,7 +117,7 @@ - (void)testUploadProgress
while (!complete) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
}

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
BOOL success = (progress > 0.95);
GHAssertTrue(success,@"Failed to increment progress properly");

Expand All @@ -141,7 +141,7 @@ - (void)testUploadProgress
while (!complete) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
}

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
success = (progress > 0.95);
GHAssertTrue(success,@"Failed to increment progress properly");

Expand Down
4 changes: 3 additions & 1 deletion Mac Sample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ - (IBAction)simpleURLFetch:(id)sender
[request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];

[request start];
if ([request responseString]) {
if ([request error]) {
[htmlSource setString:[[request error] localizedDescription]];
} else if ([request responseString]) {
[htmlSource setString:[request responseString]];
}
}
Expand Down

0 comments on commit 6b97579

Please sign in to comment.