Ben Copsey

Tweak delegate authentication stuff

Tweak iphone auth sample
@@ -583,6 +583,8 @@ static NSError *ASITooMuchRedirectionError; @@ -583,6 +583,8 @@ static NSError *ASITooMuchRedirectionError;
583 } 583 }
584 [self resetUploadProgress:amount]; 584 [self resetUploadProgress:amount];
585 } 585 }
  586 + // Record when the request started, so we can timeout if nothing happens
  587 + [self setLastActivityTime:[NSDate date]];
586 } 588 }
587 589
588 // This is the 'main loop' for the request. Basically, it runs the runloop that our network stuff is attached to, and checks to see if we should cancel or timeout 590 // This is the 'main loop' for the request. Basically, it runs the runloop that our network stuff is attached to, and checks to see if we should cancel or timeout
@@ -590,8 +592,7 @@ static NSError *ASITooMuchRedirectionError; @@ -590,8 +592,7 @@ static NSError *ASITooMuchRedirectionError;
590 { 592 {
591 [self startRequest]; 593 [self startRequest];
592 594
593 - // Record when the request started, so we can timeout if nothing happens 595 +
594 - [self setLastActivityTime:[NSDate date]];  
595 596
596 // Wait for the request to finish 597 // Wait for the request to finish
597 while (!complete) { 598 while (!complete) {
@@ -1242,9 +1243,9 @@ static NSError *ASITooMuchRedirectionError; @@ -1242,9 +1243,9 @@ static NSError *ASITooMuchRedirectionError;
1242 pass = [[self mainRequest] proxyPassword]; 1243 pass = [[self mainRequest] proxyPassword];
1243 1244
1244 // Let's try to use the ones set in this object 1245 // Let's try to use the ones set in this object
1245 - } else if ([self username] && [self password]) { 1246 + } else if ([self proxyUsername] && [self proxyPassword]) {
1246 - user = [self username]; 1247 + user = [self proxyUsername];
1247 - pass = [self password]; 1248 + pass = [self proxyPassword];
1248 } 1249 }
1249 1250
1250 1251
@@ -1330,8 +1331,8 @@ static NSError *ASITooMuchRedirectionError; @@ -1330,8 +1331,8 @@ static NSError *ASITooMuchRedirectionError;
1330 // Called by delegate to resume loading once authentication info has been populated 1331 // Called by delegate to resume loading once authentication info has been populated
1331 - (void)retryWithAuthentication 1332 - (void)retryWithAuthentication
1332 { 1333 {
1333 - [authenticationLock lockWhenCondition:1]; 1334 + [[self authenticationLock] lockWhenCondition:1];
1334 - [authenticationLock unlockWithCondition:2]; 1335 + [[self authenticationLock] unlockWithCondition:2];
1335 } 1336 }
1336 1337
1337 - (void)attemptToApplyProxyCredentialsAndResume 1338 - (void)attemptToApplyProxyCredentialsAndResume
@@ -1347,6 +1348,7 @@ static NSError *ASITooMuchRedirectionError; @@ -1347,6 +1348,7 @@ static NSError *ASITooMuchRedirectionError;
1347 1348
1348 1349
1349 if (!proxyAuthentication) { 1350 if (!proxyAuthentication) {
  1351 + [self cancelLoad];
1350 [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to get authentication object from response headers",NSLocalizedDescriptionKey,nil]]]; 1352 [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to get authentication object from response headers",NSLocalizedDescriptionKey,nil]]];
1351 return; 1353 return;
1352 } 1354 }
@@ -1374,18 +1376,19 @@ static NSError *ASITooMuchRedirectionError; @@ -1374,18 +1376,19 @@ static NSError *ASITooMuchRedirectionError;
1374 1376
1375 if ([authorizationDelegate respondsToSelector:@selector(proxyAuthorizationNeededForRequest:)]) { 1377 if ([authorizationDelegate respondsToSelector:@selector(proxyAuthorizationNeededForRequest:)]) {
1376 [authorizationDelegate performSelectorOnMainThread:@selector(proxyAuthorizationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; 1378 [authorizationDelegate performSelectorOnMainThread:@selector(proxyAuthorizationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]];
1377 - [authenticationLock lockWhenCondition:2]; 1379 + [[self authenticationLock] lockWhenCondition:2];
1378 - [authenticationLock unlock]; 1380 + [[self authenticationLock] unlockWithCondition:1];
1379 1381
1380 // Hopefully, the delegate gave us some credentials, let's apply them and reload 1382 // Hopefully, the delegate gave us some credentials, let's apply them and reload
1381 [self attemptToApplyProxyCredentialsAndResume]; 1383 [self attemptToApplyProxyCredentialsAndResume];
1382 return; 1384 return;
1383 } 1385 }
1384 } 1386 }
  1387 + [self cancelLoad];
1385 [self failWithError:ASIAuthenticationError]; 1388 [self failWithError:ASIAuthenticationError];
1386 return; 1389 return;
1387 } 1390 }
1388 - 1391 +
1389 [self cancelLoad]; 1392 [self cancelLoad];
1390 1393
1391 if (proxyCredentials) { 1394 if (proxyCredentials) {
@@ -1427,8 +1430,8 @@ static NSError *ASITooMuchRedirectionError; @@ -1427,8 +1430,8 @@ static NSError *ASITooMuchRedirectionError;
1427 1430
1428 if ([authorizationDelegate respondsToSelector:@selector(proxyAuthorizationNeededForRequest:)]) { 1431 if ([authorizationDelegate respondsToSelector:@selector(proxyAuthorizationNeededForRequest:)]) {
1429 [authorizationDelegate performSelectorOnMainThread:@selector(proxyAuthorizationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; 1432 [authorizationDelegate performSelectorOnMainThread:@selector(proxyAuthorizationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]];
1430 - [authenticationLock lockWhenCondition:2]; 1433 + [[self authenticationLock] lockWhenCondition:2];
1431 - [authenticationLock unlock]; 1434 + [[self authenticationLock] unlockWithCondition:1];
1432 [self attemptToApplyProxyCredentialsAndResume]; 1435 [self attemptToApplyProxyCredentialsAndResume];
1433 return; 1436 return;
1434 } 1437 }
@@ -1457,6 +1460,7 @@ static NSError *ASITooMuchRedirectionError; @@ -1457,6 +1460,7 @@ static NSError *ASITooMuchRedirectionError;
1457 1460
1458 1461
1459 if (!requestAuthentication) { 1462 if (!requestAuthentication) {
  1463 + [self cancelLoad];
1460 [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to get authentication object from response headers",NSLocalizedDescriptionKey,nil]]]; 1464 [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to get authentication object from response headers",NSLocalizedDescriptionKey,nil]]];
1461 return; 1465 return;
1462 } 1466 }
@@ -1484,14 +1488,16 @@ static NSError *ASITooMuchRedirectionError; @@ -1484,14 +1488,16 @@ static NSError *ASITooMuchRedirectionError;
1484 1488
1485 if ([authorizationDelegate respondsToSelector:@selector(authorizationNeededForRequest:)]) { 1489 if ([authorizationDelegate respondsToSelector:@selector(authorizationNeededForRequest:)]) {
1486 [authorizationDelegate performSelectorOnMainThread:@selector(authorizationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; 1490 [authorizationDelegate performSelectorOnMainThread:@selector(authorizationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]];
1487 - [authenticationLock lockWhenCondition:2]; 1491 + [[self authenticationLock] lockWhenCondition:2];
1488 - [authenticationLock unlock]; 1492 + [[self authenticationLock] unlockWithCondition:1];
1489 1493
1490 // Hopefully, the delegate gave us some credentials, let's apply them and reload 1494 // Hopefully, the delegate gave us some credentials, let's apply them and reload
1491 [self attemptToApplyCredentialsAndResume]; 1495 [self attemptToApplyCredentialsAndResume];
1492 return; 1496 return;
1493 } 1497 }
1494 } 1498 }
  1499 + // The delegate isn't interested, we'll have to give up
  1500 + [self cancelLoad];
1495 [self failWithError:ASIAuthenticationError]; 1501 [self failWithError:ASIAuthenticationError];
1496 return; 1502 return;
1497 } 1503 }
@@ -1537,8 +1543,9 @@ static NSError *ASITooMuchRedirectionError; @@ -1537,8 +1543,9 @@ static NSError *ASITooMuchRedirectionError;
1537 1543
1538 if ([authorizationDelegate respondsToSelector:@selector(authorizationNeededForRequest:)]) { 1544 if ([authorizationDelegate respondsToSelector:@selector(authorizationNeededForRequest:)]) {
1539 [authorizationDelegate performSelectorOnMainThread:@selector(authorizationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; 1545 [authorizationDelegate performSelectorOnMainThread:@selector(authorizationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]];
1540 - [authenticationLock lockWhenCondition:2]; 1546 +
1541 - [authenticationLock unlock]; 1547 + [[self authenticationLock] lockWhenCondition:2];
  1548 + [[self authenticationLock] unlockWithCondition:1];
1542 [self attemptToApplyCredentialsAndResume]; 1549 [self attemptToApplyCredentialsAndResume];
1543 return; 1550 return;
1544 } 1551 }
@@ -21,6 +21,7 @@ @@ -21,6 +21,7 @@
21 { 21 {
22 [networkQueue cancelAllOperations]; 22 [networkQueue cancelAllOperations];
23 [networkQueue setRequestDidFinishSelector:@selector(topSecretFetchComplete:)]; 23 [networkQueue setRequestDidFinishSelector:@selector(topSecretFetchComplete:)];
  24 + [networkQueue setRequestDidFailSelector:@selector(topSecretFetchFailed:)];
24 [networkQueue setDelegate:self]; 25 [networkQueue setDelegate:self];
25 26
26 27
@@ -31,13 +32,16 @@ @@ -31,13 +32,16 @@
31 32
32 } 33 }
33 34
  35 +- (IBAction)topSecretFetchFailed:(ASIHTTPRequest *)request
  36 +{
  37 + [topSecretInfo setText:[[request error] localizedDescription]];
  38 + [topSecretInfo setFont:[UIFont boldSystemFontOfSize:12]];
  39 +}
  40 +
34 - (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)request 41 - (IBAction)topSecretFetchComplete:(ASIHTTPRequest *)request
35 { 42 {
36 - if (![request error]) { 43 + [topSecretInfo setText:[request responseString]];
37 - NSLog(@"foo %@",[request responseString]); 44 + [topSecretInfo setFont:[UIFont boldSystemFontOfSize:12]];
38 - [topSecretInfo setText:[request responseString]];  
39 - [topSecretInfo setFont:[UIFont boldSystemFontOfSize:12]];  
40 - }  
41 } 45 }
42 46
43 - (void)authorizationNeededForRequest:(ASIHTTPRequest *)request 47 - (void)authorizationNeededForRequest:(ASIHTTPRequest *)request