Autodetect proxy username/password should now be possible on Mac OS
Various changes to proxy code to fix possible problems
Showing
3 changed files
with
67 additions
and
55 deletions
| @@ -426,14 +426,17 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; | @@ -426,14 +426,17 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; | ||
| 426 | // Save credentials for this request to the keychain | 426 | // Save credentials for this request to the keychain |
| 427 | - (void)saveCredentialsToKeychain:(NSMutableDictionary *)newCredentials; | 427 | - (void)saveCredentialsToKeychain:(NSMutableDictionary *)newCredentials; |
| 428 | 428 | ||
| 429 | -// Save creddentials to the keychain | 429 | +// Save credentials to the keychain |
| 430 | + (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; | 430 | + (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; |
| 431 | ++ (void)saveCredentials:(NSURLCredential *)credentials forProxy:(NSString *)host port:(int)port realm:(NSString *)realm; | ||
| 431 | 432 | ||
| 432 | // Return credentials from the keychain | 433 | // Return credentials from the keychain |
| 433 | + (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; | 434 | + (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; |
| 435 | ++ (NSURLCredential *)savedCredentialsForProxy:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; | ||
| 434 | 436 | ||
| 435 | // Remove credentials from the keychain | 437 | // Remove credentials from the keychain |
| 436 | + (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; | 438 | + (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; |
| 439 | ++ (void)removeCredentialsForProxy:(NSString *)host port:(int)port realm:(NSString *)realm; | ||
| 437 | 440 | ||
| 438 | // We keep track of any cookies we accept, so that we can remove them from the persistent store later | 441 | // We keep track of any cookies we accept, so that we can remove them from the persistent store later |
| 439 | + (void)setSessionCookies:(NSMutableArray *)newSessionCookies; | 442 | + (void)setSessionCookies:(NSMutableArray *)newSessionCookies; |
| @@ -436,6 +436,12 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | @@ -436,6 +436,12 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | ||
| 436 | [ASIHTTPRequest setSessionCredentials:nil]; | 436 | [ASIHTTPRequest setSessionCredentials:nil]; |
| 437 | } | 437 | } |
| 438 | } | 438 | } |
| 439 | + if (sessionProxyCredentials && sessionProxyAuthentication) { | ||
| 440 | + if (!CFHTTPMessageApplyCredentialDictionary(request, sessionProxyAuthentication, (CFMutableDictionaryRef)sessionProxyCredentials, NULL)) { | ||
| 441 | + [ASIHTTPRequest setSessionProxyAuthentication:NULL]; | ||
| 442 | + [ASIHTTPRequest setSessionProxyCredentials:nil]; | ||
| 443 | + } | ||
| 444 | + } | ||
| 439 | } | 445 | } |
| 440 | 446 | ||
| 441 | // Add cookies from the persistant (mac os global) store | 447 | // Add cookies from the persistant (mac os global) store |
| @@ -1251,21 +1257,16 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | @@ -1251,21 +1257,16 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | ||
| 1251 | 1257 | ||
| 1252 | - (void)saveProxyCredentialsToKeychain:(NSMutableDictionary *)newCredentials | 1258 | - (void)saveProxyCredentialsToKeychain:(NSMutableDictionary *)newCredentials |
| 1253 | { | 1259 | { |
| 1254 | - NSURLCredential *authenticationCredentials = [NSURLCredential credentialWithUser:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationUsername] | 1260 | + NSURLCredential *authenticationCredentials = [NSURLCredential credentialWithUser:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationUsername] password:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationPassword] persistence:NSURLCredentialPersistencePermanent]; |
| 1255 | - password:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationPassword] | ||
| 1256 | - persistence:NSURLCredentialPersistencePermanent]; | ||
| 1257 | - | ||
| 1258 | if (authenticationCredentials) { | 1261 | if (authenticationCredentials) { |
| 1259 | - [ASIHTTPRequest saveCredentials:authenticationCredentials forHost:[self proxyHost] port:[self proxyPort] protocol:[[self url] scheme] realm:[self proxyAuthenticationRealm]]; | 1262 | + [ASIHTTPRequest saveCredentials:authenticationCredentials forProxy:[self proxyHost] port:[self proxyPort] realm:[self proxyAuthenticationRealm]]; |
| 1260 | } | 1263 | } |
| 1261 | } | 1264 | } |
| 1262 | 1265 | ||
| 1263 | 1266 | ||
| 1264 | - (void)saveCredentialsToKeychain:(NSMutableDictionary *)newCredentials | 1267 | - (void)saveCredentialsToKeychain:(NSMutableDictionary *)newCredentials |
| 1265 | { | 1268 | { |
| 1266 | - NSURLCredential *authenticationCredentials = [NSURLCredential credentialWithUser:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationUsername] | 1269 | + NSURLCredential *authenticationCredentials = [NSURLCredential credentialWithUser:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationUsername] password:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationPassword] persistence:NSURLCredentialPersistencePermanent]; |
| 1267 | - password:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationPassword] | ||
| 1268 | - persistence:NSURLCredentialPersistencePermanent]; | ||
| 1269 | 1270 | ||
| 1270 | if (authenticationCredentials) { | 1271 | if (authenticationCredentials) { |
| 1271 | [ASIHTTPRequest saveCredentials:authenticationCredentials forHost:[[self url] host] port:[[[self url] port] intValue] protocol:[[self url] scheme] realm:[self authenticationRealm]]; | 1272 | [ASIHTTPRequest saveCredentials:authenticationCredentials forHost:[[self url] host] port:[[[self url] port] intValue] protocol:[[self url] scheme] realm:[self authenticationRealm]]; |
| @@ -1357,8 +1358,9 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | @@ -1357,8 +1358,9 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | ||
| 1357 | 1358 | ||
| 1358 | 1359 | ||
| 1359 | // Ok, that didn't work, let's try the keychain | 1360 | // Ok, that didn't work, let's try the keychain |
| 1360 | - if ((!user || !pass) && useKeychainPersistance) { | 1361 | + // For authenticating proxies, we'll look in the keychain regardless of the value of useKeychainPersistance |
| 1361 | - NSURLCredential *authenticationCredentials = [ASIHTTPRequest savedCredentialsForHost:[self proxyHost] port:[self proxyPort] protocol:[[self url] scheme] realm:[self proxyAuthenticationRealm]]; | 1362 | + if ((!user || !pass)) { |
| 1363 | + NSURLCredential *authenticationCredentials = [ASIHTTPRequest savedCredentialsForProxy:[self proxyHost] port:[self proxyPort] protocol:[[self url] scheme] realm:[self proxyAuthenticationRealm]]; | ||
| 1362 | if (authenticationCredentials) { | 1364 | if (authenticationCredentials) { |
| 1363 | user = [authenticationCredentials user]; | 1365 | user = [authenticationCredentials user]; |
| 1364 | pass = [authenticationCredentials password]; | 1366 | pass = [authenticationCredentials password]; |
| @@ -2054,35 +2056,43 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | @@ -2054,35 +2056,43 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | ||
| 2054 | 2056 | ||
| 2055 | + (void)setSessionCredentials:(NSMutableDictionary *)newCredentials | 2057 | + (void)setSessionCredentials:(NSMutableDictionary *)newCredentials |
| 2056 | { | 2058 | { |
| 2057 | - [sessionCredentials release]; | 2059 | + if (newCredentials != sessionCredentials) { |
| 2058 | - sessionCredentials = [newCredentials retain]; | 2060 | + [sessionCredentials release]; |
| 2061 | + sessionCredentials = [newCredentials retain]; | ||
| 2062 | + } | ||
| 2059 | } | 2063 | } |
| 2060 | 2064 | ||
| 2061 | + (void)setSessionAuthentication:(CFHTTPAuthenticationRef)newAuthentication | 2065 | + (void)setSessionAuthentication:(CFHTTPAuthenticationRef)newAuthentication |
| 2062 | { | 2066 | { |
| 2063 | - if (sessionAuthentication) { | 2067 | + if (newAuthentication != sessionAuthentication) { |
| 2064 | - CFRelease(sessionAuthentication); | 2068 | + if (sessionAuthentication) { |
| 2065 | - } | 2069 | + CFRelease(sessionAuthentication); |
| 2066 | - sessionAuthentication = newAuthentication; | 2070 | + } |
| 2067 | - if (newAuthentication) { | 2071 | + sessionAuthentication = newAuthentication; |
| 2068 | - CFRetain(sessionAuthentication); | 2072 | + if (newAuthentication) { |
| 2073 | + CFRetain(sessionAuthentication); | ||
| 2074 | + } | ||
| 2069 | } | 2075 | } |
| 2070 | } | 2076 | } |
| 2071 | 2077 | ||
| 2072 | + (void)setSessionProxyCredentials:(NSMutableDictionary *)newCredentials | 2078 | + (void)setSessionProxyCredentials:(NSMutableDictionary *)newCredentials |
| 2073 | { | 2079 | { |
| 2074 | - [sessionProxyCredentials release]; | 2080 | + if (newCredentials != sessionProxyCredentials) { |
| 2075 | - sessionProxyCredentials = [newCredentials retain]; | 2081 | + [sessionProxyCredentials release]; |
| 2082 | + sessionProxyCredentials = [newCredentials retain]; | ||
| 2083 | + } | ||
| 2076 | } | 2084 | } |
| 2077 | 2085 | ||
| 2078 | + (void)setSessionProxyAuthentication:(CFHTTPAuthenticationRef)newAuthentication | 2086 | + (void)setSessionProxyAuthentication:(CFHTTPAuthenticationRef)newAuthentication |
| 2079 | { | 2087 | { |
| 2080 | - if (sessionProxyAuthentication) { | 2088 | + if (sessionProxyAuthentication != newAuthentication) { |
| 2081 | - CFRelease(sessionProxyAuthentication); | 2089 | + if (sessionProxyAuthentication) { |
| 2082 | - } | 2090 | + CFRelease(sessionProxyAuthentication); |
| 2083 | - sessionProxyAuthentication = newAuthentication; | 2091 | + } |
| 2084 | - if (newAuthentication) { | 2092 | + sessionProxyAuthentication = newAuthentication; |
| 2085 | - CFRetain(sessionProxyAuthentication); | 2093 | + if (newAuthentication) { |
| 2094 | + CFRetain(sessionProxyAuthentication); | ||
| 2095 | + } | ||
| 2086 | } | 2096 | } |
| 2087 | } | 2097 | } |
| 2088 | 2098 | ||
| @@ -2091,42 +2101,40 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | @@ -2091,42 +2101,40 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | ||
| 2091 | 2101 | ||
| 2092 | + (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm | 2102 | + (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm |
| 2093 | { | 2103 | { |
| 2094 | - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host | 2104 | + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; |
| 2095 | - port:port | 2105 | + [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credentials forProtectionSpace:protectionSpace]; |
| 2096 | - protocol:protocol | 2106 | +} |
| 2097 | - realm:realm | 2107 | + |
| 2098 | - authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; | 2108 | ++ (void)saveCredentials:(NSURLCredential *)credentials forProxy:(NSString *)host port:(int)port realm:(NSString *)realm |
| 2099 | - | 2109 | +{ |
| 2100 | - | 2110 | + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; |
| 2101 | - NSURLCredentialStorage *storage = [NSURLCredentialStorage sharedCredentialStorage]; | 2111 | + [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credentials forProtectionSpace:protectionSpace]; |
| 2102 | - [storage setDefaultCredential:credentials forProtectionSpace:protectionSpace]; | ||
| 2103 | } | 2112 | } |
| 2104 | 2113 | ||
| 2105 | + (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm | 2114 | + (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm |
| 2106 | { | 2115 | { |
| 2107 | - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host | 2116 | + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; |
| 2108 | - port:port | 2117 | + return [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; |
| 2109 | - protocol:protocol | 2118 | +} |
| 2110 | - realm:realm | 2119 | + |
| 2111 | - authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; | 2120 | ++ (NSURLCredential *)savedCredentialsForProxy:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm |
| 2112 | - | 2121 | +{ |
| 2113 | - | 2122 | + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; |
| 2114 | - NSURLCredentialStorage *storage = [NSURLCredentialStorage sharedCredentialStorage]; | 2123 | + return [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; |
| 2115 | - return [storage defaultCredentialForProtectionSpace:protectionSpace]; | ||
| 2116 | } | 2124 | } |
| 2117 | 2125 | ||
| 2118 | + (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm | 2126 | + (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm |
| 2119 | { | 2127 | { |
| 2120 | - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host | 2128 | + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; |
| 2121 | - port:port | 2129 | + NSURLCredentialStorage *storage = [NSURLCredentialStorage sharedCredentialStorage]; |
| 2122 | - protocol:protocol | 2130 | + [storage removeCredential:[storage defaultCredentialForProtectionSpace:protectionSpace] forProtectionSpace:protectionSpace]; |
| 2123 | - realm:realm | 2131 | +} |
| 2124 | - authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; | 2132 | + |
| 2125 | - | 2133 | ++ (void)removeCredentialsForProxy:(NSString *)host port:(int)port realm:(NSString *)realm |
| 2126 | - | 2134 | +{ |
| 2135 | + NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; | ||
| 2127 | NSURLCredentialStorage *storage = [NSURLCredentialStorage sharedCredentialStorage]; | 2136 | NSURLCredentialStorage *storage = [NSURLCredentialStorage sharedCredentialStorage]; |
| 2128 | [storage removeCredential:[storage defaultCredentialForProtectionSpace:protectionSpace] forProtectionSpace:protectionSpace]; | 2137 | [storage removeCredential:[storage defaultCredentialForProtectionSpace:protectionSpace] forProtectionSpace:protectionSpace]; |
| 2129 | - | ||
| 2130 | } | 2138 | } |
| 2131 | 2139 | ||
| 2132 | 2140 |
| @@ -133,15 +133,16 @@ | @@ -133,15 +133,16 @@ | ||
| 133 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | 133 | ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
| 134 | [request start]; | 134 | [request start]; |
| 135 | 135 | ||
| 136 | + NSLog([request responseString]); | ||
| 136 | BOOL success = [[request responseString] isEqualToString:@"HTTP/1.1"]; | 137 | BOOL success = [[request responseString] isEqualToString:@"HTTP/1.1"]; |
| 137 | - GHAssertTrue(success,@"Wrong HTTP version used"); | 138 | + GHAssertTrue(success,@"Wrong HTTP version used (May fail when using a proxy that changes the HTTP version!)"); |
| 138 | 139 | ||
| 139 | request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; | 140 | request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; |
| 140 | [request setUseHTTPVersionOne:YES]; | 141 | [request setUseHTTPVersionOne:YES]; |
| 141 | [request start]; | 142 | [request start]; |
| 142 | 143 | ||
| 143 | success = [[request responseString] isEqualToString:@"HTTP/1.0"]; | 144 | success = [[request responseString] isEqualToString:@"HTTP/1.0"]; |
| 144 | - GHAssertTrue(success,@"Wrong HTTP version used"); | 145 | + GHAssertTrue(success,@"Wrong HTTP version used (May fail when using a proxy that changes the HTTP version!)"); |
| 145 | } | 146 | } |
| 146 | 147 | ||
| 147 | - (void)testUserAgent | 148 | - (void)testUserAgent |
-
Please register or login to post a comment