Jamie Pinkham

updated to newest ref added block level variables to avoid retain cycles

@@ -65,6 +65,14 @@ extern NSString* const NetworkRequestErrorDomain; @@ -65,6 +65,14 @@ extern NSString* const NetworkRequestErrorDomain;
65 // This number is not official, as far as I know there is no officially documented bandwidth limit 65 // This number is not official, as far as I know there is no officially documented bandwidth limit
66 extern unsigned long const ASIWWANBandwidthThrottleAmount; 66 extern unsigned long const ASIWWANBandwidthThrottleAmount;
67 67
  68 +#if NS_BLOCKS_AVAILABLE
  69 +typedef void (^ASIHTTPRequestBlock)(ASIHTTPRequest *request);
  70 +//typedef BOOL (^ASIHTTPRequestAuthenticationBlock)(ASIHTTPRequest *request);
  71 +typedef void (^ASIHTTPRequestSizeBlock)(ASIHTTPRequest *request, long long size);
  72 +typedef void (^ASIHTTPRequestProgressBlock)(ASIHTTPRequest *request, unsigned long long size, unsigned long long total);
  73 +typedef void (^ASIHTTPRequestDataReceivedBlock)(ASIHTTPRequest *request, NSData *data);
  74 +#endif
  75 +
68 @interface ASIHTTPRequest : NSOperation <NSCopying> { 76 @interface ASIHTTPRequest : NSOperation <NSCopying> {
69 77
70 // The url for this operation, should include GET params in the query string where appropriate 78 // The url for this operation, should include GET params in the query string where appropriate
@@ -427,6 +435,41 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; @@ -427,6 +435,41 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
427 435
428 // Set secondsToCache to use a custom time interval for expiring the response when it is stored in a cache 436 // Set secondsToCache to use a custom time interval for expiring the response when it is stored in a cache
429 NSTimeInterval secondsToCache; 437 NSTimeInterval secondsToCache;
  438 +
  439 +#if NS_BLOCKS_AVAILABLE
  440 + //block to execute when request starts
  441 + ASIHTTPRequestBlock startedBlock;
  442 +
  443 + //block to execute when headers are received
  444 + ASIHTTPRequestBlock headersReceivedBlock;
  445 +
  446 + //block to execute when request completes successfully
  447 + ASIHTTPRequestBlock completionBlock;
  448 +
  449 + //block to execute when request fails
  450 + ASIHTTPRequestBlock failureBlock;
  451 +
  452 + //block for when bytes are received
  453 + ASIHTTPRequestProgressBlock bytesReceivedBlock;
  454 +
  455 + //block for when bytes are sent
  456 + ASIHTTPRequestProgressBlock bytesSentBlock;
  457 +
  458 + //block for when download size is incremented
  459 + ASIHTTPRequestSizeBlock downloadSizeIncrementedBlock;
  460 +
  461 + //block for when upload size is incremented
  462 + ASIHTTPRequestSizeBlock uploadSizeIncrementedBlock;
  463 +
  464 + //block for handling raw bytes received
  465 + ASIHTTPRequestDataReceivedBlock dataReceivedBlock;
  466 +
  467 + //block for handling authentication
  468 + ASIHTTPRequestBlock authenticationNeededBlock;
  469 +
  470 + //block for handling proxy authentication
  471 + ASIHTTPRequestBlock proxyAuthenticationNeededBlock;
  472 +#endif
430 } 473 }
431 474
432 #pragma mark init / dealloc 475 #pragma mark init / dealloc
@@ -440,6 +483,20 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; @@ -440,6 +483,20 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
440 + (id)requestWithURL:(NSURL *)newURL usingCache:(id <ASICacheDelegate>)cache; 483 + (id)requestWithURL:(NSURL *)newURL usingCache:(id <ASICacheDelegate>)cache;
441 + (id)requestWithURL:(NSURL *)newURL usingCache:(id <ASICacheDelegate>)cache andCachePolicy:(ASICachePolicy)policy; 484 + (id)requestWithURL:(NSURL *)newURL usingCache:(id <ASICacheDelegate>)cache andCachePolicy:(ASICachePolicy)policy;
442 485
  486 +#if NS_BLOCKS_AVAILABLE
  487 +- (void)setStartedBlock:(ASIHTTPRequestBlock)aStartedBlock;
  488 +- (void)setHeadersReceivedBlock:(ASIHTTPRequestBlock)aReceivedBlock;
  489 +- (void)setCompletionBlock:(ASIHTTPRequestBlock)aCompletionBlock;
  490 +- (void)setFailedBlock:(ASIHTTPRequestBlock)aFailedBlock;
  491 +- (void)setBytesReceivedBlock:(ASIHTTPRequestProgressBlock) aBytesReceivedBlock;
  492 +- (void)setBytesSentBlock:(ASIHTTPRequestProgressBlock)aBytesSentBlock;
  493 +- (void)setDownloadSizeIncrementedBlock:(ASIHTTPRequestSizeBlock) aDownloadSizeIncrementedBlock;
  494 +- (void)setUploadSizeIncrementedBlock:(ASIHTTPRequestSizeBlock) anUploadSizeIncrementedBlock;
  495 +- (void)setDataReceivedBlock:(ASIHTTPRequestDataReceivedBlock)aReceivedBlock;
  496 +- (void)setAuthenticationNeededBlock:(ASIHTTPRequestBlock)anAuthenticationBlock;
  497 +- (void)setProxyAuthenticationNeededBlock:(ASIHTTPRequestBlock)aProxyAuthenticationBlock;
  498 +#endif
  499 +
443 #pragma mark setup request 500 #pragma mark setup request
444 501
445 // Add a custom header to the request 502 // Add a custom header to the request
@@ -363,9 +363,75 @@ static NSOperationQueue *sharedQueue = nil; @@ -363,9 +363,75 @@ static NSOperationQueue *sharedQueue = nil;
363 [responseStatusMessage release]; 363 [responseStatusMessage release];
364 [connectionInfo release]; 364 [connectionInfo release];
365 [requestID release]; 365 [requestID release];
  366 +#if NS_BLOCKS_AVAILABLE
  367 + [completionBlock release];
  368 + [failureBlock release];
  369 + [startedBlock release];
  370 + [headersReceivedBlock release];
  371 + [bytesReceivedBlock release];
  372 + [bytesSentBlock release];
  373 + [downloadSizeIncrementedBlock release];
  374 + [uploadSizeIncrementedBlock release];
  375 + [dataReceivedBlock release];
  376 + [proxyAuthenticationNeededBlock release];
  377 + [authenticationNeededBlock release];
  378 +#endif
366 [super dealloc]; 379 [super dealloc];
367 } 380 }
368 381
  382 +#pragma mark -
  383 +#pragma mark blocks
  384 +#if NS_BLOCKS_AVAILABLE
  385 +- (void)setStartedBlock:(ASIHTTPRequestBlock)aStartedBlock{
  386 + [startedBlock release];
  387 + startedBlock = [aStartedBlock copy];
  388 +}
  389 +
  390 +- (void)setHeadersReceivedBlock:(ASIHTTPRequestBlock)aReceivedBlock{
  391 + [headersReceivedBlock release];
  392 + headersReceivedBlock = [aReceivedBlock copy];
  393 +}
  394 +
  395 +- (void)setCompletionBlock:(ASIHTTPRequestBlock)aCompletionBlock{
  396 + [completionBlock release];
  397 + completionBlock = [aCompletionBlock copy];
  398 +}
  399 +- (void)setFailedBlock:(ASIHTTPRequestBlock)aFailedBlock{
  400 + [failureBlock release];
  401 + failureBlock = [aFailedBlock copy];
  402 +}
  403 +- (void)setBytesReceivedBlock:(ASIHTTPRequestProgressBlock) aBytesReceivedBlock{
  404 + [bytesReceivedBlock release];
  405 + bytesReceivedBlock = [aBytesReceivedBlock copy];
  406 +}
  407 +- (void)setBytesSentBlock:(ASIHTTPRequestProgressBlock)aBytesSentBlock{
  408 + [bytesSentBlock release];
  409 + bytesSentBlock = [aBytesSentBlock copy];
  410 +}
  411 +- (void)setDownloadSizeIncrementedBlock:(ASIHTTPRequestSizeBlock) aDownloadSizeIncrementedBlock{
  412 + [downloadSizeIncrementedBlock release];
  413 + downloadSizeIncrementedBlock = [aDownloadSizeIncrementedBlock copy];
  414 +}
  415 +- (void)setUploadSizeIncrementedBlock:(ASIHTTPRequestSizeBlock) anUploadSizeIncrementedBlock{
  416 + [uploadSizeIncrementedBlock release];
  417 + uploadSizeIncrementedBlock = [anUploadSizeIncrementedBlock copy];
  418 +}
  419 +
  420 +- (void)setDataReceivedBlock:(ASIHTTPRequestDataReceivedBlock)aReceivedBlock{
  421 + [dataReceivedBlock release];
  422 + dataReceivedBlock = [aReceivedBlock copy];
  423 +}
  424 +
  425 +- (void)setAuthenticationNeededBlock:(ASIHTTPRequestBlock)anAuthenticationBlock{
  426 + [authenticationNeededBlock release];
  427 + authenticationNeededBlock = [anAuthenticationBlock copy];
  428 +}
  429 +- (void)setProxyAuthenticationNeededBlock:(ASIHTTPRequestBlock)aProxyAuthenticationBlock{
  430 + [proxyAuthenticationNeededBlock release];
  431 + proxyAuthenticationNeededBlock = [aProxyAuthenticationBlock copy];
  432 +}
  433 +#endif
  434 +
369 #pragma mark setup request 435 #pragma mark setup request
370 436
371 - (void)addRequestHeader:(NSString *)header value:(NSString *)value 437 - (void)addRequestHeader:(NSString *)header value:(NSString *)value
@@ -1508,7 +1574,12 @@ static NSOperationQueue *sharedQueue = nil; @@ -1508,7 +1574,12 @@ static NSOperationQueue *sharedQueue = nil;
1508 [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&queue withObject:self amount:&value]; 1574 [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&queue withObject:self amount:&value];
1509 [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&downloadProgressDelegate withObject:self amount:&value]; 1575 [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&downloadProgressDelegate withObject:self amount:&value];
1510 [ASIHTTPRequest updateProgressIndicator:&downloadProgressDelegate withProgress:[self totalBytesRead]+[self partialDownloadSize] ofTotal:[self contentLength]+[self partialDownloadSize]]; 1576 [ASIHTTPRequest updateProgressIndicator:&downloadProgressDelegate withProgress:[self totalBytesRead]+[self partialDownloadSize] ofTotal:[self contentLength]+[self partialDownloadSize]];
1511 - 1577 +#if NS_BLOCKS_AVAILABLE
  1578 + if(bytesReceivedBlock){
  1579 + __block ASIHTTPRequest *blockCopy = self;
  1580 + bytesReceivedBlock(blockCopy, bytesReadSoFar, blockCopy->contentLength + blockCopy->partialDownloadSize);
  1581 + }
  1582 +#endif
1512 [self setLastBytesRead:bytesReadSoFar]; 1583 [self setLastBytesRead:bytesReadSoFar];
1513 } 1584 }
1514 1585
@@ -1547,6 +1618,12 @@ static NSOperationQueue *sharedQueue = nil; @@ -1547,6 +1618,12 @@ static NSOperationQueue *sharedQueue = nil;
1547 [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&queue withObject:self amount:&value]; 1618 [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&queue withObject:self amount:&value];
1548 [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&uploadProgressDelegate withObject:self amount:&value]; 1619 [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&uploadProgressDelegate withObject:self amount:&value];
1549 [ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:[self totalBytesSent]-[self uploadBufferSize] ofTotal:[self postLength]-[self uploadBufferSize]]; 1620 [ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:[self totalBytesSent]-[self uploadBufferSize] ofTotal:[self postLength]-[self uploadBufferSize]];
  1621 +#if NS_BLOCKS_AVAILABLE
  1622 + if(bytesSentBlock){
  1623 + __block ASIHTTPRequest *blockCopy = self;
  1624 + bytesSentBlock(blockCopy, value, blockCopy->postLength);
  1625 + }
  1626 +#endif
1550 } 1627 }
1551 1628
1552 1629
@@ -1554,6 +1631,12 @@ static NSOperationQueue *sharedQueue = nil; @@ -1554,6 +1631,12 @@ static NSOperationQueue *sharedQueue = nil;
1554 { 1631 {
1555 [ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:&queue withObject:self amount:&length]; 1632 [ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:&queue withObject:self amount:&length];
1556 [ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:&downloadProgressDelegate withObject:self amount:&length]; 1633 [ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:&downloadProgressDelegate withObject:self amount:&length];
  1634 +#if NS_BLOCKS_AVAILABLE
  1635 + if(downloadSizeIncrementedBlock){
  1636 + __block ASIHTTPRequest *blockCopy = self;
  1637 + downloadSizeIncrementedBlock(blockCopy, length);
  1638 + }
  1639 +#endif
1557 } 1640 }
1558 1641
1559 1642
@@ -1561,6 +1644,12 @@ static NSOperationQueue *sharedQueue = nil; @@ -1561,6 +1644,12 @@ static NSOperationQueue *sharedQueue = nil;
1561 { 1644 {
1562 [ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:&queue withObject:self amount:&length]; 1645 [ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:&queue withObject:self amount:&length];
1563 [ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:&uploadProgressDelegate withObject:self amount:&length]; 1646 [ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:&uploadProgressDelegate withObject:self amount:&length];
  1647 +#if NS_BLOCKS_AVAILABLE
  1648 + if(uploadSizeIncrementedBlock){
  1649 + __block ASIHTTPRequest *blockCopy = self;
  1650 + uploadSizeIncrementedBlock(blockCopy, length);
  1651 + }
  1652 +#endif
1564 } 1653 }
1565 1654
1566 1655
@@ -1570,6 +1659,12 @@ static NSOperationQueue *sharedQueue = nil; @@ -1570,6 +1659,12 @@ static NSOperationQueue *sharedQueue = nil;
1570 [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&queue withObject:self amount:&progressToRemove]; 1659 [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&queue withObject:self amount:&progressToRemove];
1571 [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&uploadProgressDelegate withObject:self amount:&progressToRemove]; 1660 [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:&uploadProgressDelegate withObject:self amount:&progressToRemove];
1572 [ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:0 ofTotal:[self postLength]]; 1661 [ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:0 ofTotal:[self postLength]];
  1662 +#if NS_BLOCKS_AVAILABLE
  1663 + if(bytesSentBlock){
  1664 + __block ASIHTTPRequest *blockCopy = self;
  1665 + bytesSentBlock(blockCopy, progressToRemove, blockCopy->postLength);
  1666 + }
  1667 +#endif
1573 } 1668 }
1574 1669
1575 + (void)performInvocation:(NSInvocation *)invocation onTarget:(id *)target 1670 + (void)performInvocation:(NSInvocation *)invocation onTarget:(id *)target
@@ -1686,6 +1781,13 @@ static NSOperationQueue *sharedQueue = nil; @@ -1686,6 +1781,13 @@ static NSOperationQueue *sharedQueue = nil;
1686 1781
1687 // Let the queue know we have started 1782 // Let the queue know we have started
1688 [self callSelectorOnMainThread:&queueRequestReceivedResponseHeadersSelector forDelegate:&queue]; 1783 [self callSelectorOnMainThread:&queueRequestReceivedResponseHeadersSelector forDelegate:&queue];
  1784 +
  1785 +#if NS_BLOCKS_AVAILABLE
  1786 + if(headersReceivedBlock){
  1787 + __block ASIHTTPRequest *blockCopy = self;
  1788 + headersReceivedBlock(blockCopy);
  1789 + }
  1790 +#endif
1689 } 1791 }
1690 1792
1691 - (void)requestStarted 1793 - (void)requestStarted
@@ -1698,6 +1800,13 @@ static NSOperationQueue *sharedQueue = nil; @@ -1698,6 +1800,13 @@ static NSOperationQueue *sharedQueue = nil;
1698 1800
1699 // Let the queue know we have started 1801 // Let the queue know we have started
1700 [self callSelectorOnMainThread:&queueRequestStartedSelector forDelegate:&queue]; 1802 [self callSelectorOnMainThread:&queueRequestStartedSelector forDelegate:&queue];
  1803 +
  1804 +#if NS_BLOCKS_AVAILABLE
  1805 + if(startedBlock){
  1806 + __block ASIHTTPRequest *blockCopy = self;
  1807 + startedBlock(blockCopy);
  1808 + }
  1809 +#endif
1701 } 1810 }
1702 1811
1703 // Subclasses might override this method to process the result in the same thread 1812 // Subclasses might override this method to process the result in the same thread
@@ -1715,6 +1824,12 @@ static NSOperationQueue *sharedQueue = nil; @@ -1715,6 +1824,12 @@ static NSOperationQueue *sharedQueue = nil;
1715 1824
1716 // Let the queue know we are done 1825 // Let the queue know we are done
1717 [self callSelectorOnMainThread:&queueRequestFinishedSelector forDelegate:&queue]; 1826 [self callSelectorOnMainThread:&queueRequestFinishedSelector forDelegate:&queue];
  1827 +#if NS_BLOCKS_AVAILABLE
  1828 + if(completionBlock){
  1829 + __block ASIHTTPRequest *blockCopy = self;
  1830 + completionBlock(blockCopy);
  1831 + }
  1832 +#endif
1718 } 1833 }
1719 1834
1720 1835
@@ -1725,6 +1840,13 @@ static NSOperationQueue *sharedQueue = nil; @@ -1725,6 +1840,13 @@ static NSOperationQueue *sharedQueue = nil;
1725 1840
1726 // Let the queue know something went wrong 1841 // Let the queue know something went wrong
1727 [self callSelectorOnMainThread:&queueRequestFailedSelector forDelegate:&queue]; 1842 [self callSelectorOnMainThread:&queueRequestFailedSelector forDelegate:&queue];
  1843 +
  1844 +#if NS_BLOCKS_AVAILABLE
  1845 + if(failureBlock){
  1846 + __block ASIHTTPRequest *blockCopy = self;
  1847 + failureBlock(blockCopy);
  1848 + }
  1849 +#endif
1728 } 1850 }
1729 1851
1730 // Subclasses might override this method to perform error handling in the same thread 1852 // Subclasses might override this method to perform error handling in the same thread
@@ -2255,6 +2377,12 @@ static NSOperationQueue *sharedQueue = nil; @@ -2255,6 +2377,12 @@ static NSOperationQueue *sharedQueue = nil;
2255 [authenticationDelegate performSelectorOnMainThread:@selector(proxyAuthenticationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; 2377 [authenticationDelegate performSelectorOnMainThread:@selector(proxyAuthenticationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]];
2256 return YES; 2378 return YES;
2257 } 2379 }
  2380 +#if NS_BLOCKS_AVAILABLE
  2381 + if(proxyAuthenticationNeededBlock){
  2382 + __block ASIHTTPRequest *blockCopy = self;
  2383 + proxyAuthenticationNeededBlock(blockCopy);
  2384 + }
  2385 +#endif
2258 return NO; 2386 return NO;
2259 } 2387 }
2260 2388
@@ -2724,6 +2852,13 @@ static NSOperationQueue *sharedQueue = nil; @@ -2724,6 +2852,13 @@ static NSOperationQueue *sharedQueue = nil;
2724 if ([self needsRedirect] && [self shouldRedirect] && [self allowResumeForFileDownloads]) { 2852 if ([self needsRedirect] && [self shouldRedirect] && [self allowResumeForFileDownloads]) {
2725 return; 2853 return;
2726 } 2854 }
  2855 +#if NS_BLOCKS_AVAILABLE
  2856 + if(dataReceivedBlock){
  2857 + NSData *data = [NSData dataWithBytes:buffer length:bytesRead];
  2858 + __block ASIHTTPRequest *blockCopy = self;
  2859 + dataReceivedBlock(blockCopy, data);
  2860 + }
  2861 +#endif
2727 2862
2728 // Does the delegate want to handle the data manually? 2863 // Does the delegate want to handle the data manually?
2729 if ([[self delegate] respondsToSelector:[self didReceiveDataSelector]]) { 2864 if ([[self delegate] respondsToSelector:[self didReceiveDataSelector]]) {
This diff was suppressed by a .gitattributes entry.