mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-05 09:29:07 +08:00
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 https://github.com/facebook/react-native/pull/6851 Differential Revision: D3257466 fb-gh-sync-id: 4225d11c8c6efd09493ef938a65f024dcbaff749 fbshipit-source-id: 4225d11c8c6efd09493ef938a65f024dcbaff749
This commit is contained in:
committed by
Facebook Github Bot 1
parent
861dfb5d71
commit
bf8b5499bb
@@ -7,6 +7,7 @@ android_library(
|
||||
react_native_target('java/com/facebook/react/bridge:bridge'),
|
||||
react_native_target('java/com/facebook/react/common:common'),
|
||||
react_native_target('java/com/facebook/react/modules/core:core'),
|
||||
react_native_target('java/com/facebook/react/modules/network:network'),
|
||||
react_native_dep('libraries/fbcore/src/main/java/com/facebook/common/logging:logging'),
|
||||
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
|
||||
react_native_dep('third-party/java/jsr-305:jsr-305'),
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.facebook.react.bridge.ReadableType;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.facebook.react.common.ReactConstants;
|
||||
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
||||
import com.facebook.react.modules.network.ForwardingCookieHandler;
|
||||
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
@@ -39,6 +40,7 @@ import com.squareup.okhttp.ws.WebSocketListener;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -50,10 +52,12 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
||||
|
||||
private Map<Integer, WebSocket> mWebSocketConnections = new HashMap<>();
|
||||
private ReactContext mReactContext;
|
||||
private ForwardingCookieHandler cookieHandler;
|
||||
|
||||
public WebSocketModule(ReactApplicationContext context) {
|
||||
super(context);
|
||||
mReactContext = context;
|
||||
cookieHandler = new ForwardingCookieHandler(context);
|
||||
}
|
||||
|
||||
private void sendEvent(String eventName, WritableMap params) {
|
||||
@@ -80,6 +84,11 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
||||
.tag(id)
|
||||
.url(url);
|
||||
|
||||
String cookie = getCookie(url);
|
||||
if (cookie != null) {
|
||||
builder.addHeader("Cookie", getCookie(url));
|
||||
}
|
||||
|
||||
if (headers != null) {
|
||||
ReadableMapKeySetIterator iterator = headers.keySetIterator();
|
||||
|
||||
@@ -272,4 +281,25 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cookie if exists
|
||||
*
|
||||
* @param websocket uri
|
||||
* @return A cookie / null
|
||||
*/
|
||||
|
||||
private String getCookie(String uri){
|
||||
try {
|
||||
Map<String, List<String>> cookieMap = cookieHandler.get(new URI(setDefaultOrigin(uri)), new HashMap());
|
||||
List<String> cookieList = cookieMap.get("Cookie");
|
||||
if (cookieList != null) {
|
||||
return cookieList.get(0);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch(URISyntaxException | IOException e) {
|
||||
throw new IllegalArgumentException("Unable to get cookie from the " + uri);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user