BREAKING: iOS: Support withCredentials flag in XHRs

Summary:
Corresponding Android PR: #12276

Respect the withCredentials XMLHttpRequest flag for sending cookies with requests. This can reduce payload sizes where large cookies are set for domains.

This should fix #5347.

This is a breaking change because it alters the default behavior of XHR. Prior to this change, XHR would send cookies by default. After this change, by default, XHR does not send cookies which is consistent with the default behavior of XHR on web for cross-site requests. Developers can restore the previous behavior by passing `true` for XHR's `withCredentials` argument.

**Test plan (required)**

Verified in a test app that XHR works properly when specifying `withCredentials` as `true`, `false`, and `undefined`. Also, my team uses this change in our app.

Adam Comella
Microsoft Corp.
Closes https://github.com/facebook/react-native/pull/12275

Differential Revision: D4673644

Pulled By: mkonicek

fbshipit-source-id: 2fd8f536d02fb39d872eb849584c5c4f7e7698c5
This commit is contained in:
Adam Comella 2017-03-08 06:00:17 -08:00 committed by Facebook Github Bot
parent 22b3faf1ad
commit 454ab8fc23
3 changed files with 7 additions and 2 deletions

View File

@ -33,7 +33,8 @@ class RCTNetworking extends NativeEventEmitter {
responseType: 'text' | 'base64',
incrementalUpdates: boolean,
timeout: number,
callback: (requestId: number) => any
callback: (requestId: number) => any,
withCredentials: boolean
) {
const body = convertRequestBody(data);
RCTNetworkingNative.sendRequest({
@ -43,7 +44,8 @@ class RCTNetworking extends NativeEventEmitter {
headers,
responseType,
incrementalUpdates,
timeout
timeout,
withCredentials
}, callback);
}

View File

@ -230,6 +230,7 @@ RCT_EXPORT_MODULE()
request.HTTPMethod = [RCTConvert NSString:RCTNilIfNull(query[@"method"])].uppercaseString ?: @"GET";
request.allHTTPHeaderFields = [self stripNullsInRequestHeaders:[RCTConvert NSDictionary:query[@"headers"]]];
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) {

View File

@ -119,6 +119,7 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
status: number = 0;
timeout: number = 0;
responseURL: ?string;
withCredentials: boolean = false
upload: XMLHttpRequestEventTarget = new XMLHttpRequestEventTarget();
@ -501,6 +502,7 @@ class XMLHttpRequest extends EventTarget(...XHR_EVENTS) {
incrementalEvents,
this.timeout,
this.__didCreateRequest.bind(this),
this.withCredentials
);
}