iOS websocket: include cookies with request

Summary:
This PR modifies the Websocket implementation on iOS to pass cookies to the server. Sending cookies is useful for clients that wish to access protected Websocket endpoints without creating a new authentication protocol.
Closes https://github.com/facebook/react-native/pull/5630

Reviewed By: svcscm

Differential Revision: D2881815

Pulled By: martinbigio

fb-gh-sync-id: 31c1640626cd15447bdb4f2058ae4e34dfa52f88
This commit is contained in:
Jelle van den Hooff 2016-02-01 14:18:26 -08:00 committed by facebook-github-bot-4
parent 6ecfb61147
commit debcac59d0
1 changed files with 17 additions and 1 deletions

View File

@ -280,7 +280,23 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (instancetype)initWithURL:(NSURL *)URL protocols:(NSArray<NSString *> *)protocols options:(NSDictionary<NSString *, id> *)options
{
NSURLRequest *request = URL ? [NSURLRequest requestWithURL:URL] : nil;
NSMutableURLRequest *request;
if (URL) {
// Build a mutable request so we can fill the cookie header.
request = [NSMutableURLRequest requestWithURL:URL];
// We load cookies from sharedHTTPCookieStorage (shared with XHR and
// fetch). To get HTTPS-only cookies for wss URLs, replace wss with https
// in the URL.
NSURLComponents *components = [NSURLComponents componentsWithURL:URL resolvingAgainstBaseURL:true];
if ([components.scheme isEqualToString:@"wss"]) {
components.scheme = @"https";
}
// Load and set the cookie header.
NSArray<NSHTTPCookie *> *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:components.URL];
[request setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:cookies]];
}
return [self initWithURLRequest:request protocols:protocols options:options];
}