From cd788cea8af30ed62b335bf01efa891bf1052229 Mon Sep 17 00:00:00 2001 From: Kevin Harwood Date: Thu, 24 May 2012 14:45:01 -0500 Subject: [PATCH] Added URL Redirect Block --- AFNetworking/AFURLConnectionOperation.h | 7 +++++++ AFNetworking/AFURLConnectionOperation.m | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/AFNetworking/AFURLConnectionOperation.h b/AFNetworking/AFURLConnectionOperation.h index 0266ced..52711ec 100644 --- a/AFNetworking/AFURLConnectionOperation.h +++ b/AFNetworking/AFURLConnectionOperation.h @@ -241,4 +241,11 @@ extern NSString * const AFNetworkingOperationDidFinishNotification; */ - (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block; +/** + Sets a block to be executed to modify the URL request when a redirct is encountered as handled by the `NSURLConnectionDelegate` method `connection:willSendRequest:redirectResponse:`. + + @param block A block object to be executed to determine the properties of a redirected URL request. The block returns an `NSURLRequest` object, the URL request to redirect, and takes three arguments: the URL connection object, the new URL request, the URL redirect response. + */ +- (void)setURLRedirectResponseBlock:(NSURLRequest * (^)(NSURLConnection *inConnection, NSURLRequest *inRequest, NSURLResponse*inRedirectResponse))block; + @end diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index ed0d42d..ebf439f 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -51,6 +51,7 @@ typedef void (^AFURLConnectionOperationProgressBlock)(NSInteger bytes, long long typedef BOOL (^AFURLConnectionOperationAuthenticationAgainstProtectionSpaceBlock)(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace); typedef void (^AFURLConnectionOperationAuthenticationChallengeBlock)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge); typedef NSCachedURLResponse * (^AFURLConnectionOperationCacheResponseBlock)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse); +typedef NSURLRequest * (^AFURLConnectionOperationRedirectResponseBlock)(NSURLConnection *inConnection, NSURLRequest *inRequest, NSURLResponse*inRedirectResponse); static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { switch (state) { @@ -113,6 +114,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat @property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationAgainstProtectionSpaceBlock authenticationAgainstProtectionSpace; @property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationChallengeBlock authenticationChallenge; @property (readwrite, nonatomic, copy) AFURLConnectionOperationCacheResponseBlock cacheResponse; +@property (readwrite, nonatomic, copy) AFURLConnectionOperationRedirectResponseBlock redirectResponse; - (void)operationDidStart; - (void)finish; @@ -137,6 +139,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat @synthesize authenticationAgainstProtectionSpace = _authenticationAgainstProtectionSpace; @synthesize authenticationChallenge = _authenticationChallenge; @synthesize cacheResponse = _cacheResponse; +@synthesize redirectResponse = _redirectResponse; @synthesize lock = _lock; + (void)networkRequestThreadEntryPoint:(id)__unused object { @@ -209,6 +212,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat [_authenticationChallenge release]; [_authenticationAgainstProtectionSpace release]; [_cacheResponse release]; + [_redirectResponse release]; [_connection release]; @@ -302,6 +306,10 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat self.cacheResponse = block; } +- (void)setURLRedirectResponseBlock:(NSURLRequest *(^)(NSURLConnection *, NSURLRequest *, NSURLResponse *))block{ + self.redirectResponse = block; +} + - (void)setState:(AFOperationState)state { [self.lock lock]; if (AFStateTransitionIsValid(self.state, state, [self isCancelled])) { @@ -576,4 +584,15 @@ didReceiveResponse:(NSURLResponse *)response } } +- (NSURLRequest *)connection:(NSURLConnection *)inConnection + willSendRequest:(NSURLRequest *)inRequest + redirectResponse:(NSURLResponse *)inRedirectResponse; +{ + if(self.redirectResponse) + return self.redirectResponse(inConnection, inRequest, inRedirectResponse); + else { + return inRequest; + } +} + @end