Skip to content

Commit aeaf286

Browse files
brunobar79facebook-github-bot
authored andcommitted
- Origin header check shouldn't be case sensitive (#27827)
Summary: Based on [this](https://stackoverflow.com/a/5259004), header names are not case sensitive. That means that it's valid to pass a header `Origin` or `origin`. With the current implementation. on Android only, if you pass `Origin`, it will get overwritten by the default origin. This made me waste a lot of time debugging a problem while trying to connect to a websockets server that required an `origin` header and my implementation was working fine in iOS but not on Android, so I'm suggest changing the logic a little bit to take that into account. ## Changelog [Android] [Fixed] - Support for case insensitive "Origin" headers for Websockets Pull Request resolved: #27827 Test Plan: Here's a screenshot of that shows the issue before the fix (`Origin` header set, but getting overridden) ![Screen Shot 2020-01-21 at 11 41 33 AM](https://user-images.githubusercontent.com/1247834/72824606-86302900-3c43-11ea-92c2-3d39881495f0.png) The fix is not that easy to test since it requires a public websocket server that checks for a custom Origin header, but I think the code changes are very small and clear. Differential Revision: D19578860 Pulled By: cpojer fbshipit-source-id: d854e887d1b9e8e54da662b2da2ebe08ce65fdbc
1 parent 7a13a1a commit aeaf286

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,29 @@ public void connect(
102102
builder.addHeader("Cookie", cookie);
103103
}
104104

105+
boolean hasOriginHeader = false;
106+
105107
if (options != null
106108
&& options.hasKey("headers")
107109
&& options.getType("headers").equals(ReadableType.Map)) {
108110

109111
ReadableMap headers = options.getMap("headers");
110112
ReadableMapKeySetIterator iterator = headers.keySetIterator();
111113

112-
if (!headers.hasKey("origin")) {
113-
builder.addHeader("origin", getDefaultOrigin(url));
114-
}
115-
116114
while (iterator.hasNextKey()) {
117115
String key = iterator.nextKey();
118116
if (ReadableType.String.equals(headers.getType(key))) {
117+
if (key.equalsIgnoreCase("origin")) {
118+
hasOriginHeader = true;
119+
}
119120
builder.addHeader(key, headers.getString(key));
120121
} else {
121122
FLog.w(ReactConstants.TAG, "Ignoring: requested " + key + ", value not a string");
122123
}
123124
}
124-
} else {
125+
}
126+
127+
if (!hasOriginHeader) {
125128
builder.addHeader("origin", getDefaultOrigin(url));
126129
}
127130

0 commit comments

Comments
 (0)