Ben Copsey

Fix bug where requests streaming bodies from disk would remove the temporary fil…

…e when failing authentication
@@ -293,6 +293,9 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; @@ -293,6 +293,9 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
293 293
294 // URL for a PAC (Proxy Auto Configuration) file. If you want to set this yourself, it's probably best if you use a local file 294 // URL for a PAC (Proxy Auto Configuration) file. If you want to set this yourself, it's probably best if you use a local file
295 NSURL *PACurl; 295 NSURL *PACurl;
  296 +
  297 + // True when request is attempting to handle an authentication challenge
  298 + BOOL authenticationChallengeInProgress;
296 } 299 }
297 300
298 #pragma mark init / dealloc 301 #pragma mark init / dealloc
@@ -567,4 +570,5 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; @@ -567,4 +570,5 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
567 @property (retain) NSString *proxyAuthenticationScheme; 570 @property (retain) NSString *proxyAuthenticationScheme;
568 @property (assign) BOOL shouldPresentAuthenticationDialog; 571 @property (assign) BOOL shouldPresentAuthenticationDialog;
569 @property (assign) BOOL shouldPresentProxyAuthenticationDialog; 572 @property (assign) BOOL shouldPresentProxyAuthenticationDialog;
  573 +@property (assign) BOOL authenticationChallengeInProgress;
570 @end 574 @end
@@ -184,6 +184,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -184,6 +184,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
184 184
185 - (void)dealloc 185 - (void)dealloc
186 { 186 {
  187 + [self setAuthenticationChallengeInProgress:NO];
187 if (requestAuthentication) { 188 if (requestAuthentication) {
188 CFRelease(requestAuthentication); 189 CFRelease(requestAuthentication);
189 } 190 }
@@ -774,8 +775,9 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -774,8 +775,9 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
774 } 775 }
775 776
776 // Clean up any temporary file used to store request body for streaming 777 // Clean up any temporary file used to store request body for streaming
777 - if ([self didCreateTemporaryPostDataFile]) { 778 + if (![self authenticationChallengeInProgress] && [self didCreateTemporaryPostDataFile]) {
778 [self removePostDataFile]; 779 [self removePostDataFile];
  780 + [self setDidCreateTemporaryPostDataFile:NO];
779 } 781 }
780 782
781 [self setResponseHeaders:nil]; 783 [self setResponseHeaders:nil];
@@ -1128,6 +1130,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -1128,6 +1130,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
1128 1130
1129 - (BOOL)readResponseHeadersReturningAuthenticationFailure 1131 - (BOOL)readResponseHeadersReturningAuthenticationFailure
1130 { 1132 {
  1133 + [self setAuthenticationChallengeInProgress:NO];
1131 [self setNeedsProxyAuthentication:NO]; 1134 [self setNeedsProxyAuthentication:NO];
1132 BOOL isAuthenticationChallenge = NO; 1135 BOOL isAuthenticationChallenge = NO;
1133 CFHTTPMessageRef headers = (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream, kCFStreamPropertyHTTPResponseHeader); 1136 CFHTTPMessageRef headers = (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream, kCFStreamPropertyHTTPResponseHeader);
@@ -1145,6 +1148,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -1145,6 +1148,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
1145 isAuthenticationChallenge = YES; 1148 isAuthenticationChallenge = YES;
1146 [self setNeedsProxyAuthentication:YES]; 1149 [self setNeedsProxyAuthentication:YES];
1147 } 1150 }
  1151 + [self setAuthenticationChallengeInProgress:isAuthenticationChallenge];
1148 1152
1149 // We won't reset the download progress delegate if we got an authentication challenge 1153 // We won't reset the download progress delegate if we got an authentication challenge
1150 if (!isAuthenticationChallenge) { 1154 if (!isAuthenticationChallenge) {
@@ -2765,6 +2769,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -2765,6 +2769,7 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
2765 @synthesize proxyAuthenticationScheme; 2769 @synthesize proxyAuthenticationScheme;
2766 @synthesize shouldPresentAuthenticationDialog; 2770 @synthesize shouldPresentAuthenticationDialog;
2767 @synthesize shouldPresentProxyAuthenticationDialog; 2771 @synthesize shouldPresentProxyAuthenticationDialog;
  2772 +@synthesize authenticationChallengeInProgress;
2768 @end 2773 @end
2769 2774
2770 2775
@@ -104,4 +104,5 @@ @@ -104,4 +104,5 @@
104 } 104 }
105 105
106 106
  107 +
107 @end 108 @end
@@ -34,6 +34,8 @@ IMPORTANT @@ -34,6 +34,8 @@ IMPORTANT
34 ASINetworkQueue *cancelQueue; 34 ASINetworkQueue *cancelQueue;
35 35
36 int authenticationPromptCount; 36 int authenticationPromptCount;
  37 +
  38 + ASINetworkQueue *postQueue;
37 } 39 }
38 40
39 - (void)testFailure; 41 - (void)testFailure;
@@ -58,10 +60,13 @@ IMPORTANT @@ -58,10 +60,13 @@ IMPORTANT
58 */ 60 */
59 61
60 - (void)testDelegateAuthenticationCredentialsReuse; 62 - (void)testDelegateAuthenticationCredentialsReuse;
  63 +- (void)testPOSTWithAuthentication;
61 64
62 @property (retain) NSOperationQueue *immediateCancelQueue; 65 @property (retain) NSOperationQueue *immediateCancelQueue;
63 @property (retain) NSMutableArray *failedRequests; 66 @property (retain) NSMutableArray *failedRequests;
64 @property (retain) NSMutableArray *finishedRequests; 67 @property (retain) NSMutableArray *finishedRequests;
65 @property (retain) ASINetworkQueue *releaseTestQueue; 68 @property (retain) ASINetworkQueue *releaseTestQueue;
66 @property (retain) ASINetworkQueue *cancelQueue; 69 @property (retain) ASINetworkQueue *cancelQueue;
  70 +@property (retain) ASINetworkQueue *postQueue;
  71 +
67 @end 72 @end
@@ -720,7 +720,7 @@ IMPORTANT @@ -720,7 +720,7 @@ IMPORTANT
720 BOOL success = (interval > -6); 720 BOOL success = (interval > -6);
721 GHAssertTrue(success,@"Downloaded the data too slowly - either this is a bug, or your internet connection is too slow to run this test (must be able to download 90KB in less than 6 seconds, without throttling)"); 721 GHAssertTrue(success,@"Downloaded the data too slowly - either this is a bug, or your internet connection is too slow to run this test (must be able to download 90KB in less than 6 seconds, without throttling)");
722 722
723 - NSLog(@"Throttle"); 723 + //NSLog(@"Throttle");
724 724
725 // Reset the queue 725 // Reset the queue
726 [networkQueue cancelAllOperations]; 726 [networkQueue cancelAllOperations];
@@ -826,10 +826,30 @@ IMPORTANT @@ -826,10 +826,30 @@ IMPORTANT
826 GHAssertTrue(NO,@"Request failed"); 826 GHAssertTrue(NO,@"Request failed");
827 } 827 }
828 828
  829 +// Test for a bug that used to exist where the temporary file used to store the request body would be removed when authentication failed
  830 +- (void)testPOSTWithAuthentication
  831 +{
  832 + [self setPostQueue:[ASINetworkQueue queue]];
  833 + [[self postQueue] setRequestDidFinishSelector:@selector(postDone:)];
  834 + [[self postQueue] setDelegate:self];
  835 +
  836 + ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/post_with_authentication"]];
  837 + [request setPostValue:@"This is the first item" forKey:@"first"];
  838 + [request setData:[@"This is the second item" dataUsingEncoding:NSUTF8StringEncoding] forKey:@"second"];
  839 + [[self postQueue] addOperation:request];
  840 + [[self postQueue] go];
  841 +}
  842 +
  843 +- (void)postDone:(ASIHTTPRequest *)request
  844 +{
  845 + BOOL success = [[request responseString] isEqualToString:@"This is the first item\r\nThis is the second item"];
  846 + GHAssertTrue(success,@"Didn't post correct data");
  847 +}
829 848
830 @synthesize immediateCancelQueue; 849 @synthesize immediateCancelQueue;
831 @synthesize failedRequests; 850 @synthesize failedRequests;
832 @synthesize finishedRequests; 851 @synthesize finishedRequests;
833 @synthesize releaseTestQueue; 852 @synthesize releaseTestQueue;
834 @synthesize cancelQueue; 853 @synthesize cancelQueue;
  854 +@synthesize postQueue;
835 @end 855 @end