Fix: respect "withCredentials: false" in RCTNetworking iOS (#24629)

Summary:
Fixes https://github.com/facebook/react-native/issues/24080.

Even with `withCredentials: false` network requests still sending cookies. Fix this behaviour according to https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials.

[iOS] [Fixed] - Respect "withCredentials: false" in network requests
Pull Request resolved: https://github.com/facebook/react-native/pull/24629

Differential Revision: D15120420

Pulled By: cpojer

fbshipit-source-id: 78b9924436b02584c4fc1aa04763dff085eea78c
This commit is contained in:
Dmitry Dushkin
2019-04-29 03:43:28 -07:00
committed by Facebook Github Bot
parent 6ab249f220
commit 382f088fb0

View File

@@ -259,10 +259,13 @@ RCT_EXPORT_MODULE()
NSURL *URL = [RCTConvert NSURL:query[@"url"]]; // this is marked as nullable in JS, but should not be null
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
request.HTTPMethod = [RCTConvert NSString:RCTNilIfNull(query[@"method"])].uppercaseString ?: @"GET";
request.HTTPShouldHandleCookies = [RCTConvert BOOL:query[@"withCredentials"]];
// Load and set the cookie header.
NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:URL];
request.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
if (request.HTTPShouldHandleCookies == YES) {
// Load and set the cookie header.
NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:URL];
request.allHTTPHeaderFields = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
}
// Set supplied headers.
NSDictionary *headers = [RCTConvert NSDictionary:query[@"headers"]];
@@ -273,7 +276,6 @@ RCT_EXPORT_MODULE()
}];
request.timeoutInterval = [RCTConvert NSTimeInterval:query[@"timeout"]];
request.HTTPShouldHandleCookies = [RCTConvert BOOL:query[@"withCredentials"]];
NSDictionary<NSString *, id> *data = [RCTConvert NSDictionary:RCTNilIfNull(query[@"data"])];
NSString *trackingName = data[@"trackingName"];
if (trackingName) {