From a48ff199d173ce9761b8b595afda666f389bd3a2 Mon Sep 17 00:00:00 2001 From: Russell Hancox Date: Thu, 16 Mar 2017 15:49:27 -0400 Subject: [PATCH] Allow users to override the redirect handler --- Source/MOLAuthenticatingURLSession.h | 13 +++++++++++++ Source/MOLAuthenticatingURLSession.m | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Source/MOLAuthenticatingURLSession.h b/Source/MOLAuthenticatingURLSession.h index 95836ab..070686a 100644 --- a/Source/MOLAuthenticatingURLSession.h +++ b/Source/MOLAuthenticatingURLSession.h @@ -97,6 +97,19 @@ @property(copy) void (^dataTaskDidReceiveDataBlock)(NSURLSession *, NSURLSessionDataTask *, NSData *); +/** + If set, this block will be called when a redirect is attempted. This overrides the + refusesRedirects property as you are taking responsibility for handling redirects. + + @param task, The task this redirect is related to. + @param request, The new request, pre-filled. + @param response, The response from the server to the request that caused the redirect. + @return request, A valid request to make or nil to refuse the redirect. Returning the request + passed as the third parameter is valid. +*/ +@property(copy) NSURLRequest * + (^redirectHandlerBlock)(NSURLSessionTask *, NSHTTPURLResponse *, NSURLRequest *); + /** This method should be called with PEM data containing one or more certificates to use to verify the server's certificate chain. This will override the trusted system roots. If there are no usable diff --git a/Source/MOLAuthenticatingURLSession.m b/Source/MOLAuthenticatingURLSession.m index ef56fca..39cb173 100644 --- a/Source/MOLAuthenticatingURLSession.m +++ b/Source/MOLAuthenticatingURLSession.m @@ -156,9 +156,9 @@ - (void)URLSession:(NSURLSession *)session willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler { - if (self.refusesRedirects) { - [self log:@"Rejected redirection to: %@", request.URL]; - [task cancel]; // without this, the connection hangs until timeout!?! + if (self.redirectHandlerBlock) { + completionHandler(self.redirectHandlerBlock(task, response, request)); + } else if (self.refusesRedirects) { completionHandler(NULL); } else { completionHandler(request);