Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

(FINALLY!) Merge branch 'jamie-fork' into blocks-merge2

Conflicts:
	Classes/ASIHTTPRequest.h
	Classes/ASIHTTPRequest.m
	iPhone Sample/UploadViewController.m
	iPhone.xcodeproj/project.pbxproj
  • Loading branch information...
commit 0f5c91a51bb8cdd25acfff4bb41cd907812cb059 2 parents 14f2bc8 + 4ea1a61
@pokeb authored
View
18 Classes/ASIHTTPRequest.h
@@ -507,6 +507,9 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
//block for handling proxy authentication
ASIHTTPRequestBlock proxyAuthenticationNeededBlock;
+
+ //block for handling redirections, if you want to
+ ASIHTTPRequestBlock requestRedirectedBlock;
#endif
}
@@ -521,6 +524,21 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
+ (id)requestWithURL:(NSURL *)newURL usingCache:(id <ASICacheDelegate>)cache;
+ (id)requestWithURL:(NSURL *)newURL usingCache:(id <ASICacheDelegate>)cache andCachePolicy:(ASICachePolicy)policy;
+#if NS_BLOCKS_AVAILABLE
+- (void)setStartedBlock:(ASIHTTPRequestBlock)aStartedBlock;
+- (void)setHeadersReceivedBlock:(ASIHTTPRequestBlock)aReceivedBlock;
+- (void)setCompletionBlock:(ASIHTTPRequestBlock)aCompletionBlock;
+- (void)setFailedBlock:(ASIHTTPRequestBlock)aFailedBlock;
+- (void)setBytesReceivedBlock:(ASIHTTPRequestProgressBlock) aBytesReceivedBlock;
+- (void)setBytesSentBlock:(ASIHTTPRequestProgressBlock)aBytesSentBlock;
+- (void)setDownloadSizeIncrementedBlock:(ASIHTTPRequestSizeBlock) aDownloadSizeIncrementedBlock;
+- (void)setUploadSizeIncrementedBlock:(ASIHTTPRequestSizeBlock) anUploadSizeIncrementedBlock;
+- (void)setDataReceivedBlock:(ASIHTTPRequestDataReceivedBlock)aReceivedBlock;
+- (void)setAuthenticationNeededBlock:(ASIHTTPRequestBlock)anAuthenticationBlock;
+- (void)setProxyAuthenticationNeededBlock:(ASIHTTPRequestBlock)aProxyAuthenticationBlock;
+- (void)setRequestRedirectedBlock:(ASIHTTPRequestBlock)aRedirectBlock;
+#endif
+
#pragma mark setup request
// Add a custom header to the request
View
120 Classes/ASIHTTPRequest.m
@@ -24,7 +24,7 @@
#import "ASIDataCompressor.h"
// Automatically set on build
-NSString *ASIHTTPRequestVersion = @"v1.7-113 2010-10-18";
+NSString *ASIHTTPRequestVersion = @"v1.7-115 2010-10-18";
NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
@@ -378,7 +378,6 @@ - (void)dealloc
#pragma mark setup request
-
- (void)addRequestHeader:(NSString *)header value:(NSString *)value
{
if (!requestHeaders) {
@@ -1576,11 +1575,11 @@ - (void)updateDownloadProgress
[ASIHTTPRequest updateProgressIndicator:&downloadProgressDelegate withProgress:[self totalBytesRead]+[self partialDownloadSize] ofTotal:[self contentLength]+[self partialDownloadSize]];
#if NS_BLOCKS_AVAILABLE
- if (bytesReceivedBlock) {
- bytesReceivedBlock(self, bytesReadSoFar, [self contentLength] + [self partialDownloadSize]);
- }
+ if (bytesReceivedBlock) {
+ __block ASIHTTPRequest *blockCopy = self;
+ bytesReceivedBlock(blockCopy, bytesReadSoFar, blockCopy->contentLength + blockCopy->partialDownloadSize);
+ }
#endif
-
[self setLastBytesRead:bytesReadSoFar];
}
@@ -1621,8 +1620,9 @@ - (void)updateUploadProgress
[ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:[self totalBytesSent]-[self uploadBufferSize] ofTotal:[self postLength]-[self uploadBufferSize]];
#if NS_BLOCKS_AVAILABLE
- if (bytesSentBlock) {
- bytesSentBlock(self, value, [self postLength]-[self uploadBufferSize]);
+ if(bytesSentBlock){
+ __block ASIHTTPRequest *blockCopy = self;
+ bytesSentBlock(blockCopy, value, blockCopy->postLength);
}
#endif
}
@@ -1634,9 +1634,10 @@ - (void)incrementDownloadSizeBy:(long long)length
[ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:&downloadProgressDelegate withObject:self amount:&length];
#if NS_BLOCKS_AVAILABLE
- if (downloadSizeIncrementedBlock) {
- downloadSizeIncrementedBlock(self, length);
- }
+ if(downloadSizeIncrementedBlock){
+ __block ASIHTTPRequest *blockCopy = self;
+ downloadSizeIncrementedBlock(blockCopy, length);
+ }
#endif
}
@@ -1647,9 +1648,10 @@ - (void)incrementUploadSizeBy:(long long)length
[ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:&uploadProgressDelegate withObject:self amount:&length];
#if NS_BLOCKS_AVAILABLE
- if (uploadSizeIncrementedBlock) {
- uploadSizeIncrementedBlock(self, length);
- }
+ if(uploadSizeIncrementedBlock){
+ __block ASIHTTPRequest *blockCopy = self;
+ uploadSizeIncrementedBlock(blockCopy, length);
+ }
#endif
}
@@ -1662,8 +1664,9 @@ -(void)removeUploadProgressSoFar
[ASIHTTPRequest updateProgressIndicator:&uploadProgressDelegate withProgress:0 ofTotal:[self postLength]];
#if NS_BLOCKS_AVAILABLE
- if (bytesSentBlock) {
- bytesSentBlock(self, progressToRemove, [self postLength]);
+ if(bytesSentBlock){
+ __block ASIHTTPRequest *blockCopy = self;
+ bytesSentBlock(blockCopy, progressToRemove, blockCopy->postLength);
}
#endif
}
@@ -1768,11 +1771,12 @@ - (void)requestReceivedResponseHeaders:(NSMutableDictionary *)newResponseHeaders
if (queue && [queue respondsToSelector:@selector(request:didReceiveResponseHeaders:)]) {
[queue performSelector:@selector(request:didReceiveResponseHeaders:) withObject:self withObject:newResponseHeaders];
}
-
+
#if NS_BLOCKS_AVAILABLE
- if (headersReceivedBlock) {
- headersReceivedBlock(self);
- }
+ if(headersReceivedBlock){
+ __block ASIHTTPRequest *blockCopy = self;
+ headersReceivedBlock(blockCopy);
+ }
#endif
}
@@ -1782,7 +1786,6 @@ - (void)requestWillRedirectToURL:(NSURL *)newURL
if ([self error] || [self mainRequest]) {
return;
}
-
if (delegate && [delegate respondsToSelector:willRedirectSelector]) {
[delegate performSelector:willRedirectSelector withObject:self withObject:newURL];
}
@@ -1791,8 +1794,9 @@ - (void)requestWillRedirectToURL:(NSURL *)newURL
}
#if NS_BLOCKS_AVAILABLE
- if (startedBlock) {
- startedBlock(self);
+ if(startedBlock){
+ __block ASIHTTPRequest *blockCopy = self;
+ startedBlock(blockCopy);
}
#endif
}
@@ -1816,8 +1820,9 @@ - (void)requestFinished
[queue performSelector:@selector(requestFinished:) withObject:self];
}
#if NS_BLOCKS_AVAILABLE
- if (completionBlock) {
- completionBlock(self);
+ if(completionBlock){
+ __block ASIHTTPRequest *blockCopy = self;
+ completionBlock(blockCopy);
}
#endif
}
@@ -1831,15 +1836,27 @@ - (void)reportFailure
if (queue && [queue respondsToSelector:@selector(requestFailed:)]) {
[queue performSelector:@selector(requestFailed:) withObject:self];
}
+ #if NS_BLOCKS_AVAILABLE
+ if(failureBlock){
+ __block ASIHTTPRequest *blockCopy = self;
+ failureBlock(blockCopy);
+ }
+ #endif
}
/* ALWAYS CALLED ON MAIN THREAD! */
-- (void)passReceivedDataToDelegate:(NSData *)data
+- (void)passOnReceivedData:(NSData *)data
{
if (delegate && [delegate respondsToSelector:didReceiveDataSelector]) {
[delegate performSelector:didReceiveDataSelector withObject:self withObject:data];
- return;
}
+
+ #if NS_BLOCKS_AVAILABLE
+ if (dataReceivedBlock) {
+ __block ASIHTTPRequest *blockCopy = self;
+ dataReceivedBlock(blockCopy, data);
+ }
+ #endif
}
// Subclasses might override this method to perform error handling in the same thread
@@ -1992,6 +2009,16 @@ - (void)readResponseHeaders
// Note that ASIHTTPRequest does not currently support 305 Use Proxy
if ([self shouldRedirect] && [responseHeaders valueForKey:@"Location"]) {
if (([self responseStatusCode] > 300 && [self responseStatusCode] < 304) || [self responseStatusCode] == 307) {
+
+ if([[self delegate] respondsToSelector:@selector(requestRedirected:)]){
+ [[self delegate] performSelectorOnMainThread:@selector(requestRedirected:) withObject:self waitUntilDone:[NSThread isMainThread]];
+ }
+#if NS_BLOCKS_AVAILABLE
+ if(requestRedirectedBlock){
+ __block ASIHTTPRequest *blockCopy = self;
+ requestRedirectedBlock(blockCopy);
+ }
+#endif
// By default, we redirect 301 and 302 response codes as GET requests
// According to RFC 2616 this is wrong, but this is what most browsers do, so it's probably what you're expecting to happen
@@ -2359,16 +2386,19 @@ - (BOOL)askDelegateForProxyCredentials
if (!authenticationDelegate) {
authenticationDelegate = [self queue];
}
- #if NS_BLOCKS_AVAILABLE
- if (proxyAuthenticationNeededBlock) {
- proxyAuthenticationNeededBlock(self);
- }
- #endif
+
if ([authenticationDelegate respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) {
[authenticationDelegate performSelectorOnMainThread:@selector(proxyAuthenticationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]];
return YES;
}
+ #if NS_BLOCKS_AVAILABLE
+ if(proxyAuthenticationNeededBlock){
+ __block ASIHTTPRequest *blockCopy = self;
+ proxyAuthenticationNeededBlock(blockCopy);
+ }
+ #endif
+
return NO;
}
@@ -2859,24 +2889,27 @@ - (void)handleBytesAvailable
// If we need to redirect, and have automatic redirect on, and might be resuming a download, let's do nothing with the content
if ([self needsRedirect] && [self shouldRedirect] && [self allowResumeForFileDownloads]) {
return;
- }
+ }
+
+ BOOL dataWillBeHandledExternally = NO;
+ if ([[self delegate] respondsToSelector:[self didReceiveDataSelector]]) {
+ dataWillBeHandledExternally = YES;
+ }
#if NS_BLOCKS_AVAILABLE
- if (dataReceivedBlock) {
- NSData *data = [NSData dataWithBytes:buffer length:bytesRead];
- dataReceivedBlock(self, data);
- }
+ if (dataReceivedBlock) {
+ dataWillBeHandledExternally = YES;
+ }
#endif
-
// Does the delegate want to handle the data manually?
- if ([[self delegate] respondsToSelector:[self didReceiveDataSelector]]) {
-
+ if (dataWillBeHandledExternally) {
+
NSData *data = nil;
if ([self isResponseCompressed] && ![self shouldWaitToInflateCompressedResponses]) {
data = inflatedData;
} else {
data = [NSData dataWithBytes:buffer length:bytesRead];
}
- [self performSelectorOnMainThread:@selector(passReceivedDataToDelegate:) withObject:data waitUntilDone:[NSThread isMainThread]];
+ [self performSelectorOnMainThread:@selector(passOnReceivedData:) withObject:data waitUntilDone:[NSThread isMainThread]];
// Are we downloading to a file?
} else if ([self downloadDestinationPath]) {
@@ -4232,6 +4265,11 @@ - (void)setProxyAuthenticationNeededBlock:(ASIHTTPRequestBlock)aProxyAuthenticat
[proxyAuthenticationNeededBlock release];
proxyAuthenticationNeededBlock = [aProxyAuthenticationBlock copy];
}
+- (void)setRequestRedirectedBlock:(ASIHTTPRequestBlock)aRedirectBlock
+{
+ [requestRedirectedBlock release];
+ requestRedirectedBlock = [aRedirectBlock copy];
+}
#endif
#pragma mark ===
View
1  Classes/ASIHTTPRequestDelegate.h
@@ -19,6 +19,7 @@
- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL;
- (void)requestFinished:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request;
+- (void)requestRedirected:(ASIHTTPRequest *)request;
// When a delegate implements this method, it is expected to process all incoming data itself
// This means that responseData / responseString / downloadDestinationPath etc are ignored
View
6 iPhone Sample/AuthenticationViewController.m
@@ -19,6 +19,12 @@ - (IBAction)fetchTopSecretInformation:(id)sender
[self setRequest:[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/top_secret/"]]];
[request setUseKeychainPersistence:[useKeychain isOn]];
[request setDelegate:self];
+ [request setAuthenticationNeededBlock:^(ASIHTTPRequest *request){
+ NSLog(@"authentication needed");
+ }];
+ [request setProxyAuthenticationNeededBlock:^(ASIHTTPRequest *request){
+ NSLog(@"proxy authentication needed");
+ }];
[request setShouldPresentAuthenticationDialog:[useBuiltInDialog isOn]];
[request setDidFinishSelector:@selector(topSecretFetchComplete:)];
[request setDidFailSelector:@selector(topSecretFetchFailed:)];
View
19 iPhone Sample/QueueViewController.m
@@ -17,6 +17,16 @@ @implementation QueueViewController
- (IBAction)fetchThreeImages:(id)sender
{
+ ASIHTTPRequestSizeBlock sizeBlock = ^(ASIHTTPRequest *request, long long size){
+ NSDictionary *userInfo = [request userInfo];
+ NSLog(@"request - %@ named = %@ download size incremented %lld", request, [userInfo valueForKey:@"name"], size);
+ };
+
+ ASIHTTPRequestProgressBlock bytesBlock = ^(ASIHTTPRequest *request, unsigned long long size, unsigned long long total){
+ NSDictionary *userInfo = [request userInfo];
+ NSLog(@"request - %@ named - %@ downloaded bytes size %llu of total: %llu", request, [userInfo valueForKey:@"name"], size, total);
+ };
+
[imageView1 setImage:nil];
[imageView2 setImage:nil];
[imageView3 setImage:nil];
@@ -34,18 +44,27 @@ - (IBAction)fetchThreeImages:(id)sender
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/small-image.jpg"]];
+ [request setDownloadSizeIncrementedBlock:sizeBlock];
+ [request setBytesReceivedBlock:bytesBlock];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator1];
+ [request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]];
[networkQueue addOperation:request];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/medium-image.jpg"]] autorelease];
+ [request setDownloadSizeIncrementedBlock:sizeBlock];
+ [request setBytesReceivedBlock:bytesBlock];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"2.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator2];
+ [request setUserInfo:[NSDictionary dictionaryWithObject:@"request2" forKey:@"name"]];
[networkQueue addOperation:request];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/large-image.jpg"]] autorelease];
+ [request setDownloadSizeIncrementedBlock:sizeBlock];
+ [request setBytesReceivedBlock:bytesBlock];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"3.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator3];
+ [request setUserInfo:[NSDictionary dictionaryWithObject:@"request3" forKey:@"name"]];
[networkQueue addOperation:request];
[networkQueue go];
View
29 iPhone Sample/SynchronousViewController.m
@@ -19,12 +19,41 @@ - (IBAction)simpleURLFetch:(id)sender
{
NSURL *url = [NSURL URLWithString:[urlField text]];
+ NSMutableData *recievedData = [NSMutableData data];
// Create a request
// You don't normally need to retain a synchronous request, but we need to in this case because we'll need it later if we reload the table data
[self setRequest:[ASIHTTPRequest requestWithURL:url]];
//Customise our user agent, for no real reason
[request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];
+
+ [request setStartedBlock:^(ASIHTTPRequest *aRequest){
+ NSLog(@"started!");
+ }];
+ [request setHeadersReceivedBlock:^(ASIHTTPRequest *aRequest){
+ NSLog(@"headers recieved");
+ }];
+
+ [request setBytesReceivedBlock:^(ASIHTTPRequest *aRequest, unsigned long long length, unsigned long long total){
+ NSLog(@"bytes received:%llu of total: %llu", length, total);
+ }];
+
+ [request setDownloadSizeIncrementedBlock:^(ASIHTTPRequest *aRequest, long long length){
+ NSLog(@"download size incremented:%lld", length);
+ }];
+
+ [request setDataReceivedBlock:^(ASIHTTPRequest *aRequest, NSData *data){
+ [recievedData appendData:data];
+ NSLog(@"data - %@", recievedData);
+ }];
+
+ [request setCompletionBlock:^(ASIHTTPRequest *aRequest){
+ if ([aRequest error]) {
+ NSLog(@"error from block");
+ } else if ([aRequest responseString]) {
+ NSLog(@"finish from block");
+ }
+ }];
// Start the request
[request startSynchronous];
View
2  iPhone Sample/UploadViewController.m
@@ -20,10 +20,12 @@ - (IBAction)performLargeUpload:(id)sender
[request setPostValue:@"test" forKey:@"value2"];
[request setPostValue:@"test" forKey:@"value3"];
[request setTimeOutSeconds:20];
+
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
[request setShouldContinueWhenAppEntersBackground:YES];
#endif
[request setUploadProgressDelegate:progressIndicator];
+
[request setDelegate:self];
[request setDidFailSelector:@selector(uploadFailed:)];
[request setDidFinishSelector:@selector(uploadFinished:)];
View
1  iPhone Sample/WebPageViewController.m
@@ -116,7 +116,6 @@ - (void)request:(ASIHTTPRequest *)theRequest didReceiveBytes:(long long)newLengt
if (requestNumber != NSNotFound) {
RequestProgressCell *cell = (RequestProgressCell *)[[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:requestNumber inSection:2]];
if ([theRequest contentLength]+[theRequest partialDownloadSize] > 0) {
- unsigned long long totalSize = [theRequest contentLength]+[theRequest partialDownloadSize];
float progressAmount = ([theRequest totalBytesRead]*1.0f)/(([theRequest contentLength]+[theRequest partialDownloadSize])*1.0f);
[[cell progressView] setProgress:progressAmount];
}
Please sign in to comment.
Something went wrong with that request. Please try again.