Skip to content

Commit

Permalink
Android WebSocket: include cookies with request
Browse files Browse the repository at this point in the history
Summary:
This commit brings Android in line with iOS WebSockets by sending along cookies from the shared CookieManager. See: #5630
Closes #6067

Differential Revision: D3841122

Pulled By: mkonicek

fbshipit-source-id: 6607424feeb31c9da4e370ebe4b33dbbedc0a446
  • Loading branch information
bradleyboy authored and Facebook Github Bot 3 committed Sep 19, 2016
1 parent 74c32e2 commit 04392f2
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

package com.facebook.react.modules.websocket;

import android.net.Uri;
import android.util.Base64;
import android.webkit.CookieManager;

import java.io.IOException;
import java.lang.IllegalStateException;
Expand Down Expand Up @@ -87,6 +89,27 @@ public void connect(
.tag(id)
.url(url);

if (url != null) {
// Use the shared CookieManager to access the cookies
// set by WebViews inside the same app
CookieManager cookieManager = CookieManager.getInstance();

Uri parsedUrl = Uri.parse(url);
Uri.Builder builtUrl = parsedUrl.buildUpon();

// To get HTTPS-only cookies for wss URLs,
// replace wss with http in the URL.
if (parsedUrl.getScheme().equals("wss")) {
builtUrl.scheme("https");
}

String cookie = cookieManager.getCookie(builtUrl.build().toString());

if (cookie != null) {
builder.addHeader("Cookie", cookie);
}
}

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

Expand Down

0 comments on commit 04392f2

Please sign in to comment.