From 804c95279729b6a883b037bb68e66a4ad14a7209 Mon Sep 17 00:00:00 2001 From: Srikanth K Hari Date: Wed, 6 Apr 2016 17:27:22 -0700 Subject: [PATCH 1/3] android websocket: include cookies with request --- .../com/facebook/react/modules/websocket/BUCK | 1 + .../modules/websocket/WebSocketModule.java | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/BUCK index 7c8af9cca7a7db..6b534fc90df4f9 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/BUCK @@ -13,6 +13,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'), ], visibility = [ 'PUBLIC', 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 5f318cab69c06b..c36926ca73643f 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 @@ -28,6 +28,7 @@ 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 okhttp3.OkHttpClient; import okhttp3.Request; @@ -41,6 +42,7 @@ 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; @@ -51,10 +53,12 @@ public class WebSocketModule extends ReactContextBaseJavaModule { private Map 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 void connect(final String url, @Nullable final ReadableArray protocols, @ .tag(id) .url(url); + String cookie = getCookie(url); + if (cookie != null) { + builder.addHeader("Cookie", getCookie(url)); + } + if (headers != null) { ReadableMapKeySetIterator iterator = headers.keySetIterator(); @@ -268,4 +277,25 @@ private static String setDefaultOrigin(String uri) { } } + /** + * Get cookie if exists + * + * @param websocket uri + * @return A cookie / null + */ + + private String getCookie(String uri){ + try { + Map> cookieMap = cookieHandler.get(new URI(setDefaultOrigin(uri)), new HashMap()); + List 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); + } + } + } From 7269f677757c9d3a3a06ce3e9550ee00b7d5542c Mon Sep 17 00:00:00 2001 From: Antoine Rousseau Date: Tue, 7 Jun 2016 14:01:35 -0500 Subject: [PATCH 2/3] coding conventions and javadoc --- .../modules/websocket/WebSocketModule.java | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) 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 c36926ca73643f..8b7b49f248ea03 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 @@ -53,12 +53,12 @@ public class WebSocketModule extends ReactContextBaseJavaModule { private Map mWebSocketConnections = new HashMap<>(); private ReactContext mReactContext; - private ForwardingCookieHandler cookieHandler; + private ForwardingCookieHandler mCookieHandler; public WebSocketModule(ReactApplicationContext context) { super(context); mReactContext = context; - cookieHandler = new ForwardingCookieHandler(context); + mCookieHandler = new ForwardingCookieHandler(context); } private void sendEvent(String eventName, WritableMap params) { @@ -86,14 +86,14 @@ public void connect(final String url, @Nullable final ReadableArray protocols, @ String cookie = getCookie(url); if (cookie != null) { - builder.addHeader("Cookie", getCookie(url)); + builder.addHeader("Cookie", cookie); } if (headers != null) { ReadableMapKeySetIterator iterator = headers.keySetIterator(); if (!headers.hasKey("origin")) { - builder.addHeader("origin", setDefaultOrigin(url)); + builder.addHeader("origin", getDefaultOrigin(url)); } while (iterator.hasNextKey()) { @@ -107,7 +107,7 @@ public void connect(final String url, @Nullable final ReadableArray protocols, @ } } } else { - builder.addHeader("origin", setDefaultOrigin(url)); + builder.addHeader("origin", getDefaultOrigin(url)); } if (protocols != null && protocols.size() > 0) { @@ -246,13 +246,12 @@ private void notifyWebSocketFailed(int id, String message) { } /** - * Set a default origin + * Get the default HTTP(S) origin for a specific WebSocket URI * - * @param Websocket connection endpoint - * @return A string of the endpoint converted to HTTP protocol + * @param String uri + * @return A string of the endpoint converted to HTTP protocol (http[s]://host[:port]) */ - - private static String setDefaultOrigin(String uri) { + private static String getDefaultOrigin(String uri) { try { String defaultOrigin; String scheme = ""; @@ -271,30 +270,30 @@ private static String setDefaultOrigin(String uri) { } return defaultOrigin; - } catch(URISyntaxException e) { - throw new IllegalArgumentException("Unable to set " + uri + " as default origin header."); + throw new IllegalArgumentException("Unable to set " + uri + " as default origin header"); } } /** - * Get cookie if exists + * Get the cookie for a specific domain * - * @param websocket uri - * @return A cookie / null + * @param String uri + * @return The cookie header or null if none is set */ - - private String getCookie(String uri){ + private String getCookie(String uri) { try { - Map> cookieMap = cookieHandler.get(new URI(setDefaultOrigin(uri)), new HashMap()); + URI origin = new URI(getDefaultOrigin(uri)); + Map> cookieMap = mCookieHandler.get(origin, new HashMap()); List cookieList = cookieMap.get("Cookie"); - if (cookieList != null) { - return cookieList.get(0); - } else { + + if (cookieList == null || cookieList.isEmpty()) { return null; } - } catch(URISyntaxException | IOException e) { - throw new IllegalArgumentException("Unable to get cookie from the " + uri); + + return cookieList.get(0); + } catch (URISyntaxException | IOException e) { + throw new IllegalArgumentException("Unable to get cookie from " + uri); } } From 70a9cba4ea1f579d08284df1e7fd0514b60803f4 Mon Sep 17 00:00:00 2001 From: Antoine Rousseau Date: Wed, 8 Jun 2016 14:40:01 -0500 Subject: [PATCH 3/3] Add test function for WS cookies in WS example of UIExplorer --- Examples/UIExplorer/WebSocketExample.js | 79 +++++++++++++++----- Examples/UIExplorer/http_test_server.js | 43 +++++++++++ Examples/UIExplorer/websocket_test_server.js | 5 +- 3 files changed, 107 insertions(+), 20 deletions(-) create mode 100755 Examples/UIExplorer/http_test_server.js diff --git a/Examples/UIExplorer/WebSocketExample.js b/Examples/UIExplorer/WebSocketExample.js index c43079162a31ee..970fecbc6b5e07 100644 --- a/Examples/UIExplorer/WebSocketExample.js +++ b/Examples/UIExplorer/WebSocketExample.js @@ -25,10 +25,12 @@ const { Text, TextInput, TouchableOpacity, + ScrollView, View, } = ReactNative; const DEFAULT_WS_URL = 'ws://localhost:5555/'; +const DEFAULT_HTTP_URL = 'http://localhost:5556/'; const WS_EVENTS = [ 'close', 'error', @@ -88,11 +90,13 @@ function showValue(value) { type State = { url: string; + httpServerUrl: string; socket: ?WebSocket; socketState: ?number; lastSocketEvent: ?string; lastMessage: ?string | ?ArrayBuffer; outgoingMessage: string; + fetchStatus: ?string; }; class WebSocketExample extends React.Component { @@ -102,11 +106,13 @@ class WebSocketExample extends React.Component { state: State = { url: DEFAULT_WS_URL, + httpServerUrl: DEFAULT_HTTP_URL, socket: null, socketState: null, lastSocketEvent: null, lastMessage: null, outgoingMessage: '', + fetchStatus: null, }; _connect = () => { @@ -162,6 +168,19 @@ class WebSocketExample extends React.Component { this.setState({outgoingMessage: ''}); }; + _sendHttp = () => { + this.setState({ + fetchStatus: 'fetching', + }); + fetch(this.state.httpServerUrl).then((response) => { + if (response.status >= 200 && response.status < 400) { + this.setState({ + fetchStatus: 'OK', + }); + } + }); + }; + render(): ReactElement { const socketState = WS_STATES[this.state.socketState || -1]; const canConnect = @@ -169,16 +188,11 @@ class WebSocketExample extends React.Component { this.state.socket.readyState >= WebSocket.CLOSING; const canSend = !!this.state.socket; return ( - + - Pro tip: + To start the WS test server: - node Examples/UIExplorer/websocket_test_server.js - - - {' in the '} - react-native - {' directory starts a test server.'} + ./Examples/UIExplorer/websocket_test_server.js { onChangeText={(url) => this.setState({url})} value={this.state.url} /> -