Backed out changeset 183744d2415b

Reviewed By: nicklockwood

Differential Revision: D3053067

fb-gh-sync-id: de20718b5bf82eae433637847143e32b7a4bb216
shipit-source-id: de20718b5bf82eae433637847143e32b7a4bb216
This commit is contained in:
Alexey Dodonov
2016-03-15 11:48:42 -07:00
committed by Facebook Github Bot 6
parent fe7fcdb227
commit cc2068e201
5 changed files with 73 additions and 30 deletions

View File

@@ -34,6 +34,8 @@ import com.squareup.okhttp.ws.WebSocket;
import com.squareup.okhttp.ws.WebSocketCall;
import com.squareup.okhttp.ws.WebSocketListener;
import java.net.URISyntaxException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@@ -63,7 +65,7 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
}
@ReactMethod
public void connect(final String url, @Nullable final ReadableArray protocols, @Nullable final ReadableMap options, final int id) {
public void connect(final String url, @Nullable final ReadableArray protocols, @Nullable final ReadableMap headers, final int id) {
// ignoring protocols, since OKHttp overrides them.
OkHttpClient client = new OkHttpClient();
@@ -76,14 +78,25 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
.tag(id)
.url(url);
if (options != null && options.hasKey("origin")) {
if (ReadableType.String.equals(options.getType("origin"))) {
builder.addHeader("Origin", options.getString("origin"));
} else {
FLog.w(
ReactConstants.TAG,
"Ignoring: requested origin, value not a string");
if (headers != null) {
ReadableMapKeySetIterator iterator = headers.keySetIterator();
if (!headers.hasKey("origin")) {
builder.addHeader("origin", setDefaultOrigin(url));
}
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
if (ReadableType.String.equals(headers.getType(key))) {
builder.addHeader(key, headers.getString(key));
} else {
FLog.w(
ReactConstants.TAG,
"Ignoring: requested " + key + ", value not a string");
}
}
} else {
builder.addHeader("origin", setDefaultOrigin(url));
}
WebSocketCall.create(client, builder.build()).enqueue(new WebSocketListener() {
@@ -188,4 +201,37 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
params.putString("message", message);
sendEvent("websocketFailed", params);
}
/**
* Set a default origin
*
* @param Websocket connection endpoint
* @return A string of the endpoint converted to HTTP protocol
*/
private static String setDefaultOrigin(String uri) {
try {
String defaultOrigin;
String scheme = "";
URI requestURI = new URI(uri);
if (requestURI.getScheme().equals("wss")) {
scheme += "https";
} else if (requestURI.getScheme().equals("ws")) {
scheme += "http";
}
if (requestURI.getPort() != -1) {
defaultOrigin = String.format("%s://%s:%s", scheme, requestURI.getHost(), requestURI.getPort());
} else {
defaultOrigin = String.format("%s://%s/", scheme, requestURI.getHost());
}
return defaultOrigin;
} catch(URISyntaxException e) {
throw new IllegalArgumentException("Unable to set " + uri + " as default origin header.");
}
}
}