diff --git a/Libraries/WebSocket/RCTSRWebSocket.m b/Libraries/WebSocket/RCTSRWebSocket.m index 3d8782c87121ea..bf67c3b14e3748 100644 --- a/Libraries/WebSocket/RCTSRWebSocket.m +++ b/Libraries/WebSocket/RCTSRWebSocket.m @@ -280,7 +280,23 @@ - (instancetype)initWithURL:(NSURL *)URL protocols:(NSArray *)protoc - (instancetype)initWithURL:(NSURL *)URL protocols:(NSArray *)protocols options:(NSDictionary *)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 *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:components.URL]; + [request setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:cookies]]; + } return [self initWithURLRequest:request protocols:protocols options:options]; }