Ben Copsey

Fix a bug where requests might erroneously timeout when not using a downloadProgressDelegate

Thanks to Martin Destagnol for his assistance in finding this issue!
@@ -566,7 +566,7 @@ static NSError *ASITooMuchRedirectionError; @@ -566,7 +566,7 @@ static NSError *ASITooMuchRedirectionError;
566 // Prevent timeouts before 128KB* has been sent when the size of data to upload is greater than 128KB* (*32KB on iPhone 3.0 SDK) 566 // Prevent timeouts before 128KB* has been sent when the size of data to upload is greater than 128KB* (*32KB on iPhone 3.0 SDK)
567 // This is to workaround the fact that kCFStreamPropertyHTTPRequestBytesWrittenCount is the amount written to the buffer, not the amount actually sent 567 // This is to workaround the fact that kCFStreamPropertyHTTPRequestBytesWrittenCount is the amount written to the buffer, not the amount actually sent
568 // This workaround prevents erroneous timeouts in low bandwidth situations (eg iPhone) 568 // This workaround prevents erroneous timeouts in low bandwidth situations (eg iPhone)
569 - if (contentLength <= uploadBufferSize || (uploadBufferSize > 0 && totalBytesSent > uploadBufferSize)) { 569 + if (totalBytesSent || postLength <= uploadBufferSize || (uploadBufferSize > 0 && totalBytesSent > uploadBufferSize)) {
570 [self failWithError:ASIRequestTimedOutError]; 570 [self failWithError:ASIRequestTimedOutError];
571 [self cancelLoad]; 571 [self cancelLoad];
572 [self setComplete:YES]; 572 [self setComplete:YES];
@@ -848,11 +848,6 @@ static NSError *ASITooMuchRedirectionError; @@ -848,11 +848,6 @@ static NSError *ASITooMuchRedirectionError;
848 if (responseHeaders) { 848 if (responseHeaders) {
849 849
850 unsigned long long bytesReadSoFar = totalBytesRead+partialDownloadSize; 850 unsigned long long bytesReadSoFar = totalBytesRead+partialDownloadSize;
851 -  
852 - if (bytesReadSoFar > lastBytesRead) {  
853 - [self setLastActivityTime:[NSDate date]];  
854 - }  
855 -  
856 851
857 // We're using a progress queue or compatible controller to handle progress 852 // We're using a progress queue or compatible controller to handle progress
858 SEL selector = @selector(incrementDownloadProgressBy:); 853 SEL selector = @selector(incrementDownloadProgressBy:);
@@ -1365,6 +1360,7 @@ static NSError *ASITooMuchRedirectionError; @@ -1365,6 +1360,7 @@ static NSError *ASITooMuchRedirectionError;
1365 } else if (bytesRead) { 1360 } else if (bytesRead) {
1366 1361
1367 [self setTotalBytesRead:[self totalBytesRead]+bytesRead]; 1362 [self setTotalBytesRead:[self totalBytesRead]+bytesRead];
  1363 + [self setLastActivityTime:[NSDate date]];
1368 1364
1369 // Are we downloading to a file? 1365 // Are we downloading to a file?
1370 if ([self downloadDestinationPath]) { 1366 if ([self downloadDestinationPath]) {
@@ -36,5 +36,5 @@ @@ -36,5 +36,5 @@
36 - (void)test303Redirect; 36 - (void)test303Redirect;
37 - (void)testCompression; 37 - (void)testCompression;
38 - (void)testSubclass; 38 - (void)testSubclass;
39 - 39 +- (void)testTimeOutWithoutDownloadDelegate;
40 @end 40 @end
@@ -97,7 +97,19 @@ @@ -97,7 +97,19 @@
97 97
98 BOOL success = [[request error] code] == ASIRequestTimedOutErrorType; 98 BOOL success = [[request error] code] == ASIRequestTimedOutErrorType;
99 GHAssertTrue(success,@"Timeout didn't generate the correct error"); 99 GHAssertTrue(success,@"Timeout didn't generate the correct error");
  100 +}
  101 +
  102 +
  103 +// Test fix for a bug that might have caused timeouts when posting data
  104 +- (void)testTimeOutWithoutDownloadDelegate
  105 +{
  106 + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://trails-network.net/Downloads/MemexTrails_1.0b1.zip"]];
  107 + [request setTimeOutSeconds:5];
  108 + [request setShowAccurateProgress:NO];
  109 + [request setPostBody:[NSMutableData dataWithData:[@"Small Body" dataUsingEncoding:NSUTF8StringEncoding]]];
  110 + [request start];
100 111
  112 + GHAssertNil([request error],@"Generated an error (most likely a timeout) - this test might fail on high latency connections");
101 } 113 }
102 114
103 115
@@ -782,6 +794,7 @@ @@ -782,6 +794,7 @@
782 } 794 }
783 795
784 796
  797 +
785 @end 798 @end
786 799
787 800
@@ -53,10 +53,10 @@ IMPORTANT @@ -53,10 +53,10 @@ IMPORTANT
53 53
54 [networkQueue go]; 54 [networkQueue go];
55 55
56 - while (!complete) { 56 + while (!complete) {
57 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]]; 57 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
58 - } 58 + }
59 - 59 + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
60 BOOL success = (progress > 0.95); 60 BOOL success = (progress > 0.95);
61 GHAssertTrue(success,@"Failed to increment progress properly"); 61 GHAssertTrue(success,@"Failed to increment progress properly");
62 62
@@ -117,7 +117,7 @@ IMPORTANT @@ -117,7 +117,7 @@ IMPORTANT
117 while (!complete) { 117 while (!complete) {
118 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]]; 118 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
119 } 119 }
120 - 120 + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
121 BOOL success = (progress > 0.95); 121 BOOL success = (progress > 0.95);
122 GHAssertTrue(success,@"Failed to increment progress properly"); 122 GHAssertTrue(success,@"Failed to increment progress properly");
123 123
@@ -141,7 +141,7 @@ IMPORTANT @@ -141,7 +141,7 @@ IMPORTANT
141 while (!complete) { 141 while (!complete) {
142 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]]; 142 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
143 } 143 }
144 - 144 + [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
145 success = (progress > 0.95); 145 success = (progress > 0.95);
146 GHAssertTrue(success,@"Failed to increment progress properly"); 146 GHAssertTrue(success,@"Failed to increment progress properly");
147 147
@@ -34,7 +34,9 @@ @@ -34,7 +34,9 @@
34 [request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"]; 34 [request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];
35 35
36 [request start]; 36 [request start];
37 - if ([request responseString]) { 37 + if ([request error]) {
  38 + [htmlSource setString:[[request error] localizedDescription]];
  39 + } else if ([request responseString]) {
38 [htmlSource setString:[request responseString]]; 40 [htmlSource setString:[request responseString]];
39 } 41 }
40 } 42 }