Skip to content

Commit

Permalink
added hooks for request redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Pinkham committed Oct 5, 2010
1 parent d6197f9 commit 4ea1a61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Classes/ASIHTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,10 @@ typedef void (^ASIHTTPRequestDataReceivedBlock)(ASIHTTPRequest *request, NSData
ASIHTTPRequestBlock authenticationNeededBlock;

//block for handling proxy authentication
ASIHTTPRequestBlock proxyAuthenticationNeededBlock;
ASIHTTPRequestBlock proxyAuthenticationNeededBlock;

//block for handling redirections, if you want to
ASIHTTPRequestBlock requestRedirectedBlock;
#endif
}

Expand All @@ -495,6 +498,7 @@ typedef void (^ASIHTTPRequestDataReceivedBlock)(ASIHTTPRequest *request, NSData
- (void)setDataReceivedBlock:(ASIHTTPRequestDataReceivedBlock)aReceivedBlock;
- (void)setAuthenticationNeededBlock:(ASIHTTPRequestBlock)anAuthenticationBlock;
- (void)setProxyAuthenticationNeededBlock:(ASIHTTPRequestBlock)aProxyAuthenticationBlock;
- (void)setRequestRedirectedBlock:(ASIHTTPRequestBlock)aRedirectBlock;
#endif

#pragma mark setup request
Expand Down
15 changes: 15 additions & 0 deletions Classes/ASIHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ - (void)setProxyAuthenticationNeededBlock:(ASIHTTPRequestBlock)aProxyAuthenticat
[proxyAuthenticationNeededBlock release];
proxyAuthenticationNeededBlock = [aProxyAuthenticationBlock copy];
}

- (void)setRequestRedirectedBlock:(ASIHTTPRequestBlock)aRedirectBlock{
[requestRedirectedBlock release];
requestRedirectedBlock = [aRedirectBlock copy];
}
#endif

#pragma mark setup request
Expand Down Expand Up @@ -1995,6 +2000,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
Expand Down
1 change: 1 addition & 0 deletions Classes/ASIHTTPRequestDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- (void)requestReceivedResponseHeaders:(ASIHTTPRequest *)request;
- (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
Expand Down

0 comments on commit 4ea1a61

Please sign in to comment.