Skip to content

Commit bf8b549

Browse files
srikanthkhFacebook Github Bot 1
authored and
Facebook Github Bot 1
committed
android websocket: include cookies with request
Summary: Previously cookie headers weren't sending on websocket connection requests for android. This commit fixes the issue. Closes facebook/react-native#6851 Differential Revision: D3257466 fb-gh-sync-id: 4225d11c8c6efd09493ef938a65f024dcbaff749 fbshipit-source-id: 4225d11c8c6efd09493ef938a65f024dcbaff749
1 parent 861dfb5 commit bf8b549

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/websocket/BUCK

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ android_library(
77
react_native_target('java/com/facebook/react/bridge:bridge'),
88
react_native_target('java/com/facebook/react/common:common'),
99
react_native_target('java/com/facebook/react/modules/core:core'),
10+
react_native_target('java/com/facebook/react/modules/network:network'),
1011
react_native_dep('libraries/fbcore/src/main/java/com/facebook/common/logging:logging'),
1112
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
1213
react_native_dep('third-party/java/jsr-305:jsr-305'),

ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java

+30
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.facebook.react.bridge.WritableMap;
2929
import com.facebook.react.common.ReactConstants;
3030
import com.facebook.react.modules.core.DeviceEventManagerModule;
31+
import com.facebook.react.modules.network.ForwardingCookieHandler;
3132

3233
import com.squareup.okhttp.OkHttpClient;
3334
import com.squareup.okhttp.Request;
@@ -39,6 +40,7 @@
3940
import java.net.URISyntaxException;
4041
import java.net.URI;
4142
import java.util.HashMap;
43+
import java.util.List;
4244
import java.util.Map;
4345
import java.util.concurrent.TimeUnit;
4446

@@ -50,10 +52,12 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
5052

5153
private Map<Integer, WebSocket> mWebSocketConnections = new HashMap<>();
5254
private ReactContext mReactContext;
55+
private ForwardingCookieHandler cookieHandler;
5356

5457
public WebSocketModule(ReactApplicationContext context) {
5558
super(context);
5659
mReactContext = context;
60+
cookieHandler = new ForwardingCookieHandler(context);
5761
}
5862

5963
private void sendEvent(String eventName, WritableMap params) {
@@ -80,6 +84,11 @@ public void connect(final String url, @Nullable final ReadableArray protocols, @
8084
.tag(id)
8185
.url(url);
8286

87+
String cookie = getCookie(url);
88+
if (cookie != null) {
89+
builder.addHeader("Cookie", getCookie(url));
90+
}
91+
8392
if (headers != null) {
8493
ReadableMapKeySetIterator iterator = headers.keySetIterator();
8594

@@ -272,4 +281,25 @@ private static String setDefaultOrigin(String uri) {
272281
}
273282
}
274283

284+
/**
285+
* Get cookie if exists
286+
*
287+
* @param websocket uri
288+
* @return A cookie / null
289+
*/
290+
291+
private String getCookie(String uri){
292+
try {
293+
Map<String, List<String>> cookieMap = cookieHandler.get(new URI(setDefaultOrigin(uri)), new HashMap());
294+
List<String> cookieList = cookieMap.get("Cookie");
295+
if (cookieList != null) {
296+
return cookieList.get(0);
297+
} else {
298+
return null;
299+
}
300+
} catch(URISyntaxException | IOException e) {
301+
throw new IllegalArgumentException("Unable to get cookie from the " + uri);
302+
}
303+
}
304+
275305
}

0 commit comments

Comments
 (0)