-
Notifications
You must be signed in to change notification settings - Fork 24k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
android websocket: include cookies with request #6851
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,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 com.squareup.okhttp.OkHttpClient; | ||
import com.squareup.okhttp.Request; | ||
|
@@ -37,6 +38,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; | ||
|
||
|
@@ -47,10 +49,12 @@ public class WebSocketModule extends ReactContextBaseJavaModule { | |
|
||
private Map<Integer, WebSocket> mWebSocketConnections = new HashMap<>(); | ||
private ReactContext mReactContext; | ||
private ForwardingCookieHandler cookieHandler; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
|
||
public WebSocketModule(ReactApplicationContext context) { | ||
super(context); | ||
mReactContext = context; | ||
cookieHandler = new ForwardingCookieHandler(context); | ||
} | ||
|
||
private void sendEvent(String eventName, WritableMap params) { | ||
|
@@ -78,6 +82,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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just use |
||
} | ||
|
||
if (headers != null) { | ||
ReadableMapKeySetIterator iterator = headers.keySetIterator(); | ||
|
||
|
@@ -234,4 +243,25 @@ private static String setDefaultOrigin(String uri) { | |
} | ||
} | ||
|
||
/** | ||
* Get cookie if exists | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* | ||
* @param websocket uri | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is |
||
* @return A cookie / null | ||
*/ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove extra line. |
||
private String getCookie(String uri){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space after |
||
try { | ||
Map<String, List<String>> cookieMap = cookieHandler.get(new URI(setDefaultOrigin(uri)), new HashMap()); | ||
List<String> cookieList = cookieMap.get("Cookie"); | ||
if (cookieList != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the list is empty? |
||
return cookieList.get(0); | ||
} else { | ||
return null; | ||
} | ||
} catch(URISyntaxException | IOException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space after catch |
||
throw new IllegalArgumentException("Unable to get cookie from the " + uri); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove empty line |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Alphabetize