Ben Copsey

Improve locking behaviour

Tweak SSL test
@@ -79,8 +79,8 @@ BOOL isBandwidthThrottled = NO; @@ -79,8 +79,8 @@ BOOL isBandwidthThrottled = NO;
79 79
80 BOOL shouldThrottleBandwithForWWANOnly = NO; 80 BOOL shouldThrottleBandwithForWWANOnly = NO;
81 81
82 -// Mediates access to the session cookies so requests can't modify them when they are in use 82 +// Mediates access to the session cookies so requests
83 -static NSLock *sessionCookiesLock = nil; 83 +static NSRecursiveLock *sessionCookiesLock = nil;
84 84
85 // This lock ensures delegates only receive one notification that authentication is required at once 85 // This lock ensures delegates only receive one notification that authentication is required at once
86 // When using ASIAuthenticationDialogs, it also ensures only one dialog is shown at once 86 // When using ASIAuthenticationDialogs, it also ensures only one dialog is shown at once
@@ -141,7 +141,8 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -141,7 +141,8 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
141 if (self == [ASIHTTPRequest class]) { 141 if (self == [ASIHTTPRequest class]) {
142 progressLock = [[NSRecursiveLock alloc] init]; 142 progressLock = [[NSRecursiveLock alloc] init];
143 bandwidthThrottlingLock = [[NSLock alloc] init]; 143 bandwidthThrottlingLock = [[NSLock alloc] init];
144 - sessionCookiesLock = [[NSLock alloc] init]; 144 + sessionCookiesLock = [[NSRecursiveLock alloc] init];
  145 + sessionCredentialsLock = [[NSRecursiveLock alloc] init];
145 delegateAuthenticationLock = [[NSRecursiveLock alloc] init]; 146 delegateAuthenticationLock = [[NSRecursiveLock alloc] init];
146 bandwidthUsageTracker = [[NSMutableArray alloc] initWithCapacity:5]; 147 bandwidthUsageTracker = [[NSMutableArray alloc] initWithCapacity:5];
147 ASIRequestTimedOutError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIRequestTimedOutErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request timed out",NSLocalizedDescriptionKey,nil]] retain]; 148 ASIRequestTimedOutError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIRequestTimedOutErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request timed out",NSLocalizedDescriptionKey,nil]] retain];
@@ -2088,9 +2089,6 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -2088,9 +2089,6 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
2088 if (!sessionProxyCredentialsStore) { 2089 if (!sessionProxyCredentialsStore) {
2089 sessionProxyCredentialsStore = [[NSMutableArray alloc] init]; 2090 sessionProxyCredentialsStore = [[NSMutableArray alloc] init];
2090 } 2091 }
2091 - if (!sessionCredentialsLock) {  
2092 - sessionCredentialsLock = [[NSRecursiveLock alloc] init];  
2093 - }  
2094 return sessionProxyCredentialsStore; 2092 return sessionProxyCredentialsStore;
2095 } 2093 }
2096 2094
@@ -2099,9 +2097,6 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -2099,9 +2097,6 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
2099 if (!sessionCredentialsStore) { 2097 if (!sessionCredentialsStore) {
2100 sessionCredentialsStore = [[NSMutableArray alloc] init]; 2098 sessionCredentialsStore = [[NSMutableArray alloc] init];
2101 } 2099 }
2102 - if (!sessionCredentialsLock) {  
2103 - sessionCredentialsLock = [[NSRecursiveLock alloc] init];  
2104 - }  
2105 return sessionCredentialsStore; 2100 return sessionCredentialsStore;
2106 } 2101 }
2107 2102
@@ -2259,9 +2254,6 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -2259,9 +2254,6 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
2259 2254
2260 + (void)addSessionCookie:(NSHTTPCookie *)newCookie 2255 + (void)addSessionCookie:(NSHTTPCookie *)newCookie
2261 { 2256 {
2262 - // Called to ensure sessionCookies exists first, as we won't be able to create it when we have the lock  
2263 - [[ASIHTTPRequest sessionCookies] count];  
2264 -  
2265 [sessionCookiesLock lock]; 2257 [sessionCookiesLock lock];
2266 NSHTTPCookie *cookie; 2258 NSHTTPCookie *cookie;
2267 int i; 2259 int i;
@@ -2280,7 +2272,9 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; @@ -2280,7 +2272,9 @@ static NSRecursiveLock *delegateAuthenticationLock = nil;
2280 // Dump all session data (authentication and cookies) 2272 // Dump all session data (authentication and cookies)
2281 + (void)clearSession 2273 + (void)clearSession
2282 { 2274 {
  2275 + [sessionCredentialsLock lock];
2283 [[[self class] sessionCredentialsStore] removeAllObjects]; 2276 [[[self class] sessionCredentialsStore] removeAllObjects];
  2277 + [sessionCredentialsLock unlock];
2284 [[self class] setSessionCookies:nil]; 2278 [[self class] setSessionCookies:nil];
2285 } 2279 }
2286 2280
@@ -29,7 +29,7 @@ @@ -29,7 +29,7 @@
29 - (void)testCharacterEncoding; 29 - (void)testCharacterEncoding;
30 - (void)testCompressedResponse; 30 - (void)testCompressedResponse;
31 - (void)testCompressedResponseDownloadToFile; 31 - (void)testCompressedResponseDownloadToFile;
32 -- (void)testSSL; 32 +- (void)test000SSL;
33 - (void)testRedirectPreservesSession; 33 - (void)testRedirectPreservesSession;
34 - (void)testTooMuchRedirection; 34 - (void)testTooMuchRedirection;
35 - (void)testRedirectToNewDomain; 35 - (void)testRedirectToNewDomain;
@@ -694,13 +694,14 @@ @@ -694,13 +694,14 @@
694 GHAssertTrue(success,@"Failed to correctly display increment progress for a partial download"); 694 GHAssertTrue(success,@"Failed to correctly display increment progress for a partial download");
695 } 695 }
696 696
697 -- (void)testSSL 697 +// The '000' is to ensure this test runs first, as another test may connect to https://selfsigned.allseeing-i.com and accept the certificate
  698 +- (void)test000SSL
698 { 699 {
699 NSURL *url = [NSURL URLWithString:@"https://selfsigned.allseeing-i.com"]; 700 NSURL *url = [NSURL URLWithString:@"https://selfsigned.allseeing-i.com"];
700 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 701 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
701 [request start]; 702 [request start];
702 703
703 - GHAssertNotNil([request error],@"Failed to generate an error for a self-signed certificate"); 704 + GHAssertNotNil([request error],@"Failed to generate an error for a self-signed certificate (Will fail on the second run in the same session!)");
704 705
705 // Just for testing the request generated a custom error description - don't do this! You should look at the domain / code of the underlyingError in your own programs. 706 // Just for testing the request generated a custom error description - don't do this! You should look at the domain / code of the underlyingError in your own programs.
706 BOOL success = ([[[request error] localizedDescription] isEqualToString:@"A connection failure occurred: SSL problem (possibily a bad/expired/self-signed certificate)"]); 707 BOOL success = ([[[request error] localizedDescription] isEqualToString:@"A connection failure occurred: SSL problem (possibily a bad/expired/self-signed certificate)"]);