Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +38,7 @@
import java.net.URISyntaxException;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Alphabetize

import java.util.Map;
import java.util.concurrent.TimeUnit;

Expand All @@ -47,10 +49,12 @@ public class WebSocketModule extends ReactContextBaseJavaModule {

private Map<Integer, WebSocket> mWebSocketConnections = new HashMap<>();
private ReactContext mReactContext;
private ForwardingCookieHandler cookieHandler;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be mCookieHandler.


public WebSocketModule(ReactApplicationContext context) {
super(context);
mReactContext = context;
cookieHandler = new ForwardingCookieHandler(context);
}

private void sendEvent(String eventName, WritableMap params) {
Expand Down Expand Up @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use cookie instead of calling getCookie again?

}

if (headers != null) {
ReadableMapKeySetIterator iterator = headers.keySetIterator();

Expand Down Expand Up @@ -234,4 +243,25 @@ private static String setDefaultOrigin(String uri) {
}
}

/**
* Get cookie if exists
Copy link
Contributor

@mkonicek mkonicek May 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get cookie for a specific domain.
@return The cookie header or null if none is set.

*
* @param websocket uri
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is websocket?

* @return A cookie / null
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra line.

private String getCookie(String uri){
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

}