From 04392f2428dda5424e22bb066897be5dd235b3d5 Mon Sep 17 00:00:00 2001 From: Brad Daily Date: Mon, 19 Sep 2016 15:29:14 -0700 Subject: [PATCH] Android WebSocket: include cookies with request Summary: This commit brings Android in line with iOS WebSockets by sending along cookies from the shared CookieManager. See: https://github.com/facebook/react-native/pull/5630 Closes https://github.com/facebook/react-native/pull/6067 Differential Revision: D3841122 Pulled By: mkonicek fbshipit-source-id: 6607424feeb31c9da4e370ebe4b33dbbedc0a446 --- .../modules/websocket/WebSocketModule.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java index 30de3ac5d..f223c8b44 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java @@ -9,7 +9,9 @@ package com.facebook.react.modules.websocket; +import android.net.Uri; import android.util.Base64; +import android.webkit.CookieManager; import java.io.IOException; import java.lang.IllegalStateException; @@ -87,6 +89,27 @@ public class WebSocketModule extends ReactContextBaseJavaModule { .tag(id) .url(url); + if (url != null) { + // Use the shared CookieManager to access the cookies + // set by WebViews inside the same app + CookieManager cookieManager = CookieManager.getInstance(); + + Uri parsedUrl = Uri.parse(url); + Uri.Builder builtUrl = parsedUrl.buildUpon(); + + // To get HTTPS-only cookies for wss URLs, + // replace wss with http in the URL. + if (parsedUrl.getScheme().equals("wss")) { + builtUrl.scheme("https"); + } + + String cookie = cookieManager.getCookie(builtUrl.build().toString()); + + if (cookie != null) { + builder.addHeader("Cookie", cookie); + } + } + if (headers != null) { ReadableMapKeySetIterator iterator = headers.keySetIterator();