Ben Copsey

Fixes, start moving block calling onto main thread

@@ -24,7 +24,7 @@ @@ -24,7 +24,7 @@
24 #import "ASIDataCompressor.h" 24 #import "ASIDataCompressor.h"
25 25
26 // Automatically set on build 26 // Automatically set on build
27 -NSString *ASIHTTPRequestVersion = @"v1.7-115 2010-10-18"; 27 +NSString *ASIHTTPRequestVersion = @"v1.7-119 2010-10-18";
28 28
29 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; 29 NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
30 30
@@ -150,8 +150,8 @@ static NSOperationQueue *sharedQueue = nil; @@ -150,8 +150,8 @@ static NSOperationQueue *sharedQueue = nil;
150 - (void)scheduleReadStream; 150 - (void)scheduleReadStream;
151 - (void)unscheduleReadStream; 151 - (void)unscheduleReadStream;
152 152
153 -- (BOOL)askDelegateForCredentials; 153 +- (BOOL)willAskDelegateForCredentials;
154 -- (BOOL)askDelegateForProxyCredentials; 154 +- (BOOL)willAskDelegateForProxyCredentials;
155 + (void)measureBandwidthUsage; 155 + (void)measureBandwidthUsage;
156 + (void)recordBandwidthUsage; 156 + (void)recordBandwidthUsage;
157 - (void)startRequest; 157 - (void)startRequest;
@@ -1577,12 +1577,22 @@ static NSOperationQueue *sharedQueue = nil; @@ -1577,12 +1577,22 @@ static NSOperationQueue *sharedQueue = nil;
1577 #if NS_BLOCKS_AVAILABLE 1577 #if NS_BLOCKS_AVAILABLE
1578 if (bytesReceivedBlock) { 1578 if (bytesReceivedBlock) {
1579 __block ASIHTTPRequest *blockCopy = self; 1579 __block ASIHTTPRequest *blockCopy = self;
1580 - bytesReceivedBlock(blockCopy, bytesReadSoFar, blockCopy->contentLength + blockCopy->partialDownloadSize); 1580 + bytesReceivedBlock(blockCopy, value, blockCopy->contentLength + blockCopy->partialDownloadSize);
1581 } 1581 }
1582 #endif 1582 #endif
1583 [self setLastBytesRead:bytesReadSoFar]; 1583 [self setLastBytesRead:bytesReadSoFar];
1584 } 1584 }
1585 1585
  1586 +#if NS_BLOCKS_AVAILABLE
  1587 +- (void)callBytesReceivedBlockWithBytes:(unsigned long long)bytes ofTotal:(unsigned long long)total
  1588 +{
  1589 + if (bytesReceivedBlock) {
  1590 + __block ASIHTTPRequest *blockCopy = self;
  1591 + bytesReceivedBlock(blockCopy, bytes, blockCopy->contentLength + blockCopy->partialDownloadSize);
  1592 + }
  1593 +}
  1594 +#endif
  1595 +
1586 1596
1587 - (void)updateUploadProgress 1597 - (void)updateUploadProgress
1588 { 1598 {
@@ -1741,8 +1751,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -1741,8 +1751,7 @@ static NSOperationQueue *sharedQueue = nil;
1741 } 1751 }
1742 1752
1743 1753
1744 -#pragma mark talking to delegates 1754 +#pragma mark talking to delegates / calling blocks
1745 -  
1746 1755
1747 /* ALWAYS CALLED ON MAIN THREAD! */ 1756 /* ALWAYS CALLED ON MAIN THREAD! */
1748 - (void)requestStarted 1757 - (void)requestStarted
@@ -1756,9 +1765,34 @@ static NSOperationQueue *sharedQueue = nil; @@ -1756,9 +1765,34 @@ static NSOperationQueue *sharedQueue = nil;
1756 if (queue && [queue respondsToSelector:@selector(requestStarted:)]) { 1765 if (queue && [queue respondsToSelector:@selector(requestStarted:)]) {
1757 [queue performSelector:@selector(requestStarted:) withObject:self]; 1766 [queue performSelector:@selector(requestStarted:) withObject:self];
1758 } 1767 }
  1768 + #if NS_BLOCKS_AVAILABLE
  1769 + if(startedBlock){
  1770 + __block ASIHTTPRequest *blockCopy = self;
  1771 + startedBlock(blockCopy);
  1772 + }
  1773 + #endif
1759 } 1774 }
1760 1775
1761 /* ALWAYS CALLED ON MAIN THREAD! */ 1776 /* ALWAYS CALLED ON MAIN THREAD! */
  1777 +- (void)requestRedirected
  1778 +{
  1779 + if ([self error] || [self mainRequest]) {
  1780 + return;
  1781 + }
  1782 +
  1783 + if([[self delegate] respondsToSelector:@selector(requestRedirected:)]){
  1784 + [[self delegate] performSelector:@selector(requestRedirected:) withObject:self];
  1785 + }
  1786 + #if NS_BLOCKS_AVAILABLE
  1787 + if(requestRedirectedBlock){
  1788 + __block ASIHTTPRequest *blockCopy = self;
  1789 + requestRedirectedBlock(blockCopy);
  1790 + }
  1791 + #endif
  1792 +}
  1793 +
  1794 +
  1795 +/* ALWAYS CALLED ON MAIN THREAD! */
1762 - (void)requestReceivedResponseHeaders:(NSMutableDictionary *)newResponseHeaders 1796 - (void)requestReceivedResponseHeaders:(NSMutableDictionary *)newResponseHeaders
1763 { 1797 {
1764 if ([self error] || [self mainRequest]) { 1798 if ([self error] || [self mainRequest]) {
@@ -2010,15 +2044,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -2010,15 +2044,7 @@ static NSOperationQueue *sharedQueue = nil;
2010 if ([self shouldRedirect] && [responseHeaders valueForKey:@"Location"]) { 2044 if ([self shouldRedirect] && [responseHeaders valueForKey:@"Location"]) {
2011 if (([self responseStatusCode] > 300 && [self responseStatusCode] < 304) || [self responseStatusCode] == 307) { 2045 if (([self responseStatusCode] > 300 && [self responseStatusCode] < 304) || [self responseStatusCode] == 307) {
2012 2046
2013 - if([[self delegate] respondsToSelector:@selector(requestRedirected:)]){ 2047 + [self performSelectorOnMainThread:@selector(requestRedirected) withObject:nil waitUntilDone:[NSThread isMainThread]];
2014 - [[self delegate] performSelectorOnMainThread:@selector(requestRedirected:) withObject:self waitUntilDone:[NSThread isMainThread]];  
2015 - }  
2016 -#if NS_BLOCKS_AVAILABLE  
2017 - if(requestRedirectedBlock){  
2018 - __block ASIHTTPRequest *blockCopy = self;  
2019 - requestRedirectedBlock(blockCopy);  
2020 - }  
2021 -#endif  
2022 2048
2023 // By default, we redirect 301 and 302 response codes as GET requests 2049 // By default, we redirect 301 and 302 response codes as GET requests
2024 // According to RFC 2616 this is wrong, but this is what most browsers do, so it's probably what you're expecting to happen 2050 // According to RFC 2616 this is wrong, but this is what most browsers do, so it's probably what you're expecting to happen
@@ -2378,7 +2404,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -2378,7 +2404,7 @@ static NSOperationQueue *sharedQueue = nil;
2378 } 2404 }
2379 2405
2380 2406
2381 -- (BOOL)askDelegateForProxyCredentials 2407 +- (BOOL)willAskDelegateForProxyCredentials
2382 { 2408 {
2383 // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:. 2409 // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:.
2384 // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate 2410 // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate
@@ -2386,20 +2412,91 @@ static NSOperationQueue *sharedQueue = nil; @@ -2386,20 +2412,91 @@ static NSOperationQueue *sharedQueue = nil;
2386 if (!authenticationDelegate) { 2412 if (!authenticationDelegate) {
2387 authenticationDelegate = [self queue]; 2413 authenticationDelegate = [self queue];
2388 } 2414 }
  2415 +
  2416 + BOOL delegateOrBlockWillHandleAuthentication = NO;
2389 2417
2390 if ([authenticationDelegate respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { 2418 if ([authenticationDelegate respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) {
2391 - [authenticationDelegate performSelectorOnMainThread:@selector(proxyAuthenticationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; 2419 + delegateOrBlockWillHandleAuthentication = YES;
2392 - return YES;  
2393 } 2420 }
2394 2421
2395 #if NS_BLOCKS_AVAILABLE 2422 #if NS_BLOCKS_AVAILABLE
2396 if(proxyAuthenticationNeededBlock){ 2423 if(proxyAuthenticationNeededBlock){
  2424 + delegateOrBlockWillHandleAuthentication = YES;
  2425 + }
  2426 + #endif
  2427 +
  2428 + if (delegateOrBlockWillHandleAuthentication) {
  2429 + [self performSelectorOnMainThread:@selector(askDelegateForProxyCredentials) withObject:nil waitUntilDone:NO];
  2430 + }
  2431 +
  2432 + return delegateOrBlockWillHandleAuthentication;
  2433 +}
  2434 +
  2435 +/* ALWAYS CALLED ON MAIN THREAD! */
  2436 +- (void)askDelegateForProxyCredentials
  2437 +{
  2438 + id authenticationDelegate = [self delegate];
  2439 + if (!authenticationDelegate) {
  2440 + authenticationDelegate = [self queue];
  2441 + }
  2442 + if ([authenticationDelegate respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) {
  2443 + [authenticationDelegate performSelector:@selector(proxyAuthenticationNeededForRequest:) withObject:self];
  2444 + return;
  2445 + }
  2446 + #if NS_BLOCKS_AVAILABLE
  2447 + if(proxyAuthenticationNeededBlock){
2397 __block ASIHTTPRequest *blockCopy = self; 2448 __block ASIHTTPRequest *blockCopy = self;
2398 proxyAuthenticationNeededBlock(blockCopy); 2449 proxyAuthenticationNeededBlock(blockCopy);
2399 } 2450 }
2400 #endif 2451 #endif
  2452 +}
2401 2453
2402 - return NO; 2454 +
  2455 +- (BOOL)willAskDelegateForCredentials
  2456 +{
  2457 + // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:.
  2458 + // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate
  2459 + id authenticationDelegate = [self delegate];
  2460 + if (!authenticationDelegate) {
  2461 + authenticationDelegate = [self queue];
  2462 + }
  2463 +
  2464 + BOOL delegateOrBlockWillHandleAuthentication = NO;
  2465 +
  2466 + if ([authenticationDelegate respondsToSelector:@selector(authenticationNeededForRequest:)]) {
  2467 + delegateOrBlockWillHandleAuthentication = YES;
  2468 + }
  2469 +
  2470 + #if NS_BLOCKS_AVAILABLE
  2471 + if (authenticationNeededBlock) {
  2472 + delegateOrBlockWillHandleAuthentication = YES;
  2473 + }
  2474 + #endif
  2475 +
  2476 + if (delegateOrBlockWillHandleAuthentication) {
  2477 + [self performSelectorOnMainThread:@selector(askDelegateForCredentials) withObject:nil waitUntilDone:NO];
  2478 + }
  2479 + return delegateOrBlockWillHandleAuthentication;
  2480 +}
  2481 +
  2482 +/* ALWAYS CALLED ON MAIN THREAD! */
  2483 +- (void)askDelegateForCredentials
  2484 +{
  2485 + id authenticationDelegate = [self delegate];
  2486 + if (!authenticationDelegate) {
  2487 + authenticationDelegate = [self queue];
  2488 + }
  2489 +
  2490 + if ([authenticationDelegate respondsToSelector:@selector(authenticationNeededForRequest:)]) {
  2491 + [authenticationDelegate performSelector:@selector(authenticationNeededForRequest:) withObject:self];
  2492 + return;
  2493 + }
  2494 +
  2495 + #if NS_BLOCKS_AVAILABLE
  2496 + if (authenticationNeededBlock) {
  2497 + authenticationNeededBlock(self);
  2498 + }
  2499 + #endif
2403 } 2500 }
2404 2501
2405 - (void)attemptToApplyProxyCredentialsAndResume 2502 - (void)attemptToApplyProxyCredentialsAndResume
@@ -2467,7 +2564,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -2467,7 +2564,7 @@ static NSOperationQueue *sharedQueue = nil;
2467 2564
2468 [self setLastActivityTime:nil]; 2565 [self setLastActivityTime:nil];
2469 2566
2470 - if ([self askDelegateForProxyCredentials]) { 2567 + if ([self willAskDelegateForProxyCredentials]) {
2471 [self attemptToApplyProxyCredentialsAndResume]; 2568 [self attemptToApplyProxyCredentialsAndResume];
2472 [delegateAuthenticationLock unlock]; 2569 [delegateAuthenticationLock unlock];
2473 return; 2570 return;
@@ -2539,7 +2636,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -2539,7 +2636,7 @@ static NSOperationQueue *sharedQueue = nil;
2539 return; 2636 return;
2540 } 2637 }
2541 2638
2542 - if ([self askDelegateForProxyCredentials]) { 2639 + if ([self willAskDelegateForProxyCredentials]) {
2543 [delegateAuthenticationLock unlock]; 2640 [delegateAuthenticationLock unlock];
2544 return; 2641 return;
2545 } 2642 }
@@ -2571,27 +2668,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -2571,27 +2668,7 @@ static NSOperationQueue *sharedQueue = nil;
2571 #endif 2668 #endif
2572 } 2669 }
2573 2670
2574 -- (BOOL)askDelegateForCredentials  
2575 -{  
2576 - // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:.  
2577 - // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate  
2578 - id authenticationDelegate = [self delegate];  
2579 - if (!authenticationDelegate) {  
2580 - authenticationDelegate = [self queue];  
2581 - }  
2582 - #if NS_BLOCKS_AVAILABLE  
2583 - if (authenticationNeededBlock) {  
2584 - authenticationNeededBlock(self);  
2585 - }  
2586 - #endif  
2587 2671
2588 - if ([authenticationDelegate respondsToSelector:@selector(authenticationNeededForRequest:)]) {  
2589 - [authenticationDelegate performSelectorOnMainThread:@selector(authenticationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]];  
2590 - return YES;  
2591 - }  
2592 -  
2593 - return NO;  
2594 -}  
2595 2672
2596 - (void)attemptToApplyCredentialsAndResume 2673 - (void)attemptToApplyCredentialsAndResume
2597 { 2674 {
@@ -2661,7 +2738,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -2661,7 +2738,7 @@ static NSOperationQueue *sharedQueue = nil;
2661 2738
2662 [self setLastActivityTime:nil]; 2739 [self setLastActivityTime:nil];
2663 2740
2664 - if ([self askDelegateForCredentials]) { 2741 + if ([self willAskDelegateForCredentials]) {
2665 [delegateAuthenticationLock unlock]; 2742 [delegateAuthenticationLock unlock];
2666 return; 2743 return;
2667 } 2744 }
@@ -2728,7 +2805,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -2728,7 +2805,7 @@ static NSOperationQueue *sharedQueue = nil;
2728 } 2805 }
2729 return; 2806 return;
2730 } 2807 }
2731 - if ([self askDelegateForCredentials]) { 2808 + if ([self willAskDelegateForCredentials]) {
2732 [delegateAuthenticationLock unlock]; 2809 [delegateAuthenticationLock unlock];
2733 return; 2810 return;
2734 } 2811 }
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.