Added URL Redirect Block

This commit is contained in:
Kevin Harwood 2012-05-24 14:45:01 -05:00
parent 09a8b1dbcf
commit cd788cea8a
2 changed files with 26 additions and 0 deletions

View file

@ -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

View file

@ -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