Ben Copsey

Stream errors now properly cancel requests, preventing a crash when tracking upload progress

Add missing nil terminator in testRequestMethod
Thanks to Michael Krause for contributing these fixes and the test!
@@ -166,10 +166,11 @@ static NSError *ASIUnableToCreateRequestError; @@ -166,10 +166,11 @@ static NSError *ASIUnableToCreateRequestError;
166 166
167 - (void)cancel 167 - (void)cancel
168 { 168 {
  169 + [super cancel];
169 [self failWithError:ASIRequestCancelledError]; 170 [self failWithError:ASIRequestCancelledError];
170 [self cancelLoad]; 171 [self cancelLoad];
171 complete = YES; 172 complete = YES;
172 - [super cancel]; 173 +
173 } 174 }
174 175
175 176
@@ -516,6 +517,7 @@ static NSError *ASIUnableToCreateRequestError; @@ -516,6 +517,7 @@ static NSError *ASIUnableToCreateRequestError;
516 { 517 {
517 [cancelledLock lock]; 518 [cancelledLock lock];
518 if ([self isCancelled]) { 519 if ([self isCancelled]) {
  520 + [cancelledLock unlock];
519 return; 521 return;
520 } 522 }
521 unsigned long long byteCount = [[(NSNumber *)CFReadStreamCopyProperty (readStream, kCFStreamPropertyHTTPRequestBytesWrittenCount) autorelease] unsignedLongLongValue]; 523 unsigned long long byteCount = [[(NSNumber *)CFReadStreamCopyProperty (readStream, kCFStreamPropertyHTTPRequestBytesWrittenCount) autorelease] unsignedLongLongValue];
@@ -1160,7 +1162,9 @@ static NSError *ASIUnableToCreateRequestError; @@ -1160,7 +1162,9 @@ static NSError *ASIUnableToCreateRequestError;
1160 { 1162 {
1161 NSError *underlyingError = [(NSError *)CFReadStreamCopyError(readStream) autorelease]; 1163 NSError *underlyingError = [(NSError *)CFReadStreamCopyError(readStream) autorelease];
1162 1164
  1165 + [super cancel];
1163 [self cancelLoad]; 1166 [self cancelLoad];
  1167 + complete = YES;
1164 1168
1165 if (!error) { // We may already have handled this error 1169 if (!error) { // We may already have handled this error
1166 1170
@@ -99,7 +99,7 @@ @@ -99,7 +99,7 @@
99 - (void)testRequestMethod 99 - (void)testRequestMethod
100 { 100 {
101 NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/request-method"] autorelease]; 101 NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/request-method"] autorelease];
102 - NSArray *methods = [[[NSArray alloc] initWithObjects:@"GET",@"POST",@"PUT",@"DELETE"] autorelease]; 102 + NSArray *methods = [[[NSArray alloc] initWithObjects:@"GET",@"POST",@"PUT",@"DELETE", nil] autorelease];
103 for (NSString *method in methods) { 103 for (NSString *method in methods) {
104 ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; 104 ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
105 [request setRequestMethod:method]; 105 [request setRequestMethod:method];
@@ -442,9 +442,8 @@ @@ -442,9 +442,8 @@
442 442
443 - (void)testNTLMAuthentication 443 - (void)testNTLMAuthentication
444 { 444 {
445 - /* 445 +
446 - If you want to run this test, set your hostname, username, password and domain below. 446 + // If you want to run this test, set your hostname, username, password and domain below.
447 - */  
448 NSString *theURL = @""; 447 NSString *theURL = @"";
449 NSString *username = @""; 448 NSString *username = @"";
450 NSString *password = @""; 449 NSString *password = @"";
@@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
22 - (void)testFailureCancelsOtherRequests; 22 - (void)testFailureCancelsOtherRequests;
23 - (void)testProgress; 23 - (void)testProgress;
24 - (void)testProgressWithAuthentication; 24 - (void)testProgressWithAuthentication;
  25 +- (void)testWithNoListener;
25 26
26 - (void)setProgress:(float)newProgress; 27 - (void)setProgress:(float)newProgress;
27 28
@@ -265,5 +265,34 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod @@ -265,5 +265,34 @@ static CFStringRef ASIHTTPRequestTestsRunMode = CFSTR("ASIHTTPRequestTestsRunMod
265 265
266 } 266 }
267 267
  268 +//Connect to a port the server isn't listening on, and the read stream won't be created (Test + Fix contributed by Michael Krause)
  269 +- (void)testWithNoListener
  270 +{
  271 + complete = NO;
  272 + networkQueue = [[ASINetworkQueue alloc] init];
  273 + [networkQueue setDownloadProgressDelegate:self];
  274 + [networkQueue setDelegate:self];
  275 + [networkQueue setShowAccurateProgress:YES];
  276 + [networkQueue setQueueDidFinishSelector:@selector(queueFinished:)];
  277 +
  278 + NSURL *url;
  279 + url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com:9999/i/logo.png"] autorelease];
  280 + ASIHTTPRequest *request1 = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
  281 + [networkQueue addOperation:request1];
  282 +
  283 + [networkQueue go];
  284 +
  285 + while (!complete) {
  286 + CFRunLoopRunInMode(ASIHTTPRequestTestsRunMode,0.25,YES);
  287 + }
  288 +
  289 + [networkQueue waitUntilAllOperationsAreFinished];
  290 +
  291 + BOOL success = YES;
  292 + GHAssertTrue(success,@"Should not have crashed");
  293 +
  294 + [networkQueue release];
  295 +}
  296 +
268 297
269 @end 298 @end