Skip to content

Commit a686048

Browse files
corradiofacebook-github-bot
authored andcommitted
Fix cookies not being sent after a redirected response (#22178)
Summary: Fixes #19376 On iOS, the `HTTPShouldHandleCookies` boolean has no effect when custom cookies are set (see https://developer.apple.com/documentation/foundation/nsmutableurlrequest/1415485-httpshouldhandlecookies?preferredLanguage=occ). Setting custom cookies prevents the cookies of a redirect response from being re-used in the subsequent request. This is why issue #19376 is opened. The fix is to intercept the redirect request, and to manually set the cookies. This effectively makes the Android and iOS behaviours converge. Changelog: ---------- [iOS] [Fixed] - Fix cookies not being sent on iOS after redirect Pull Request resolved: #22178 Differential Revision: D13191961 Pulled By: shergin fbshipit-source-id: 4445a2499034a9846c6d7c37c1f1b72c9fd30129
1 parent c68e69c commit a686048

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Libraries/Network/RCTHTTPRequestHandler.mm

+15
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ - (void)URLSession:(NSURLSession *)session
108108
[delegate URLRequest:task didSendDataWithProgress:totalBytesSent];
109109
}
110110

111+
- (void)URLSession:(NSURLSession *)session
112+
task:(NSURLSessionTask *)task
113+
willPerformHTTPRedirection:(NSHTTPURLResponse *)response
114+
newRequest:(NSURLRequest *)request
115+
completionHandler:(void (^)(NSURLRequest *))completionHandler
116+
{
117+
// Reset the cookies on redirect.
118+
// This is necessary because we're not letting iOS handle cookies by itself
119+
NSMutableURLRequest *nextRequest = [request mutableCopy];
120+
121+
NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:request.URL];
122+
nextRequest.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
123+
completionHandler(nextRequest);
124+
}
125+
111126
- (void)URLSession:(NSURLSession *)session
112127
dataTask:(NSURLSessionDataTask *)task
113128
didReceiveResponse:(NSURLResponse *)response

0 commit comments

Comments
 (0)