Skip to content

Commit

Permalink
Allow users to override the redirect handler
Browse files Browse the repository at this point in the history
  • Loading branch information
russellhancox committed Mar 16, 2017
1 parent 21b0a06 commit a48ff19
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Source/MOLAuthenticatingURLSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Source/MOLAuthenticatingURLSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a48ff19

Please sign in to comment.