Skip to content

[WebSocket][Android] make protocols argument work fixes #5810 #6223

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

Closed
wants to merge 1 commit into from

Conversation

zxcpoiu
Copy link
Contributor

@zxcpoiu zxcpoiu commented Mar 1, 2016

Hey there and thanks for submitting a pull request! Please have a look at the following checklist so that others have enough information to review your pull request:

motivation

WebSocket spec supports Sec-WebSocket-Protocol as a standard way for negotiate a sub protocol between client and server.

  • ios WebSocket implementation supports it.
  • android WebSocket implementation ignores this header, leave a comment syas: "OkHttp will overrides it", so it did not implement.
  • after some test, OkHttp doesn't override the header we add.

Test plan (required)

  1. run and react-native app on android
  2. at the main page, invoke: var ws = new WebSocket('ws://example.ws-service.fakedomain.com', 'my-sub-protocol');
  3. see the header if it send the correct header, ex, use ngrep: sudo ngrep -t -Wbyline -deth0 host example.ws-service.fakedomain.com and port 80

you should see the WebSocket initial GET handshake includes header:
Sec-WebSocket-Protocol: my-sub-protocol
and the value should equals to the value you provided in js.

Closes #5810

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @aprock and @satya164 to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Mar 1, 2016
@zxcpoiu zxcpoiu changed the title [WebSocket][Android] Support WebSocket sub-protocols on Android fixes… [WebSocket][Android] Support WebSocket sub-protocols fixes #5810 Mar 1, 2016
@zxcpoiu
Copy link
Contributor Author

zxcpoiu commented Mar 5, 2016

NOTE: this PR does NOT change any api NOR add any new functionality.

It is just only un-comment the original implementation which considered okHttp will swallow headers we added thus commented out the protocols argument.

@facebook-github-bot
Copy link
Contributor

@zxcpoiu updated the pull request.

StringBuilder protocolsValue = new StringBuilder("");
for (int i = 0; i < protocols.size(); i++) {
String v = protocols.getString(i).trim();
if (!TextUtils.isEmpty(v) && !v.contains(",")) {
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 use String.isEmpty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, it can be avoided to import additional TextUtils.

Just a side note:

String.isEmpty()
only checks length of string and introduced since API level 9 (Android 2.3)
so it maybe caused some crashes on earlier versions of Android.

TextUtils.isEmpty()
checks both length of string and null.
is available since API level 1

Copy link
Contributor

Choose a reason for hiding this comment

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

@zxcpoiu It's okay, because we only support Android 4.1 and above.

@satya164
Copy link
Contributor

Related #6016

@christopherdro
Copy link
Contributor

@zxcpoiu I'm wondering if my PR thats linked here will work since it allows you to specify custom headers.

@zxcpoiu
Copy link
Contributor Author

zxcpoiu commented Mar 11, 2016

Hi @christopherdro

thanks for the great PR, it really helps!
And, yes, of course your PR can solve any additional headers issues.

But I think the reason that original WebSocket implementation left an argument protocols is to compatible with the web spec which requires:
var ws = new WebSocket(url: string, protocols: ?Array<string>)

So I guess it is related PR but not overlaps.

Original implementation protocols argument
WebSocketModule.java#L66-L67

Libraries/WebSocket/WebSocket.js#L36-L39

@facebook-github-bot
Copy link
Contributor

@zxcpoiu updated the pull request.

@zxcpoiu
Copy link
Contributor Author

zxcpoiu commented Mar 11, 2016

@satya164

I've refined the PR by:

  • remove import TextUtils
  • use String.isEmpty() and StringBuilder.length() rather than TextUtils.isEmpty()

diff about this change

diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java
index ea2329a..af4f092 100644
--- a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java
+++ b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java
@@ -38,8 +38,6 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;

-import android.text.TextUtils;
-
 import okio.Buffer;
 import okio.BufferedSource;

@@ -91,12 +89,12 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
       StringBuilder protocolsValue = new StringBuilder("");
       for (int i = 0; i < protocols.size(); i++) {
         String v = protocols.getString(i).trim();
-        if (!TextUtils.isEmpty(v) && !v.contains(",")) {
+        if (!v.isEmpty() && !v.contains(",")) {
           protocolsValue.append(v);
           protocolsValue.append(",");
         }
       }
-      if (!TextUtils.isEmpty(protocolsValue)) {
+      if (protocolsValue.length() > 0) {
         protocolsValue.replace(protocolsValue.length() - 1, protocolsValue.length(), "");
         builder.addHeader("Sec-WebSocket-Protocol", protocolsValue.toString());
       }

@christopherdro
Copy link
Contributor

@zxcpoiu Just a heads up that my related PR just got shipped.

@zxcpoiu
Copy link
Contributor Author

zxcpoiu commented Mar 11, 2016

@christopherdro
hurrah!!
great thanks!!

@zxcpoiu zxcpoiu force-pushed the master branch 2 times, most recently from fb80688 to d687c4a Compare March 16, 2016 05:47
@facebook-github-bot
Copy link
Contributor

@zxcpoiu updated the pull request.

@zxcpoiu
Copy link
Contributor Author

zxcpoiu commented Mar 16, 2016

fixed conflicts follow up #6016

@zxcpoiu
Copy link
Contributor Author

zxcpoiu commented Apr 5, 2016

Hi @satya164
Is there anything I need to revise?

@satya164
Copy link
Contributor

satya164 commented Apr 6, 2016

@christopherdro Do you think this is good to go?

@christopherdro
Copy link
Contributor

satya This looks good to me. Just needs to be rebased to master.

@zxcpoiu
Copy link
Contributor Author

zxcpoiu commented Apr 6, 2016

thanks @satya164 @christopherdro
rebased!

@facebook-github-bot
Copy link
Contributor

@zxcpoiu updated the pull request.

@zxcpoiu zxcpoiu changed the title [WebSocket][Android] Support WebSocket sub-protocols fixes #5810 [WebSocket][Android] make protocols argument work fixes #5810 fix #6137 Apr 11, 2016
@christopherdro
Copy link
Contributor

@facebook-github-bot shipit

@facebook-github-bot
Copy link
Contributor

Thanks for importing. If you are an FB employee go to Phabricator to review.

@ghost ghost closed this in 914f33c Apr 11, 2016
@zxcpoiu zxcpoiu changed the title [WebSocket][Android] make protocols argument work fixes #5810 fix #6137 [WebSocket][Android] make protocols argument work fixes #5810 Apr 11, 2016
zebulgar pushed a commit to nightingale/react-native that referenced this pull request Jun 18, 2016
Summary:Hey there and thanks for submitting a pull request! Please have a look at the following checklist so that others have enough information to review your pull request:

**motivation**

WebSocket spec supports [Sec-WebSocket-Protocol](https://tools.ietf.org/html/rfc6455#section-11.3.4) as a standard way for negotiate a sub protocol between client and server.

* ios WebSocket implementation supports it.
* android WebSocket implementation ignores this header, leave a comment syas: "OkHttp will overrides it", so it did not implement.
* after some test, OkHttp doesn't override the header we add.

**Test plan (required)**

1. run and react-native app on android
2. at the main page, invoke: `var ws = new WebSocket('ws://example.ws-service.fakedomain.com', 'my-sub-protocol');`
3. see the header if it send the correct header, ex, use ngrep: `sudo ngrep -t -Wbyline -deth0 host example.ws-service.fakedomain.com and port 80`

you should see the WebSocket initial GET handshake includes header:
`Sec-WebSocke
Closes facebook#6223

Differential Revision: D3162822

fb-gh-sync-id: a00f1c0f3e1c24ad6aa234329cbb2abad7664264
fbshipit-source-id: a00f1c0f3e1c24ad6aa234329cbb2abad7664264
drewtunes pushed a commit to Tredsite/react-native that referenced this pull request Aug 11, 2016
Summary:Hey there and thanks for submitting a pull request! Please have a look at the following checklist so that others have enough information to review your pull request:

**motivation**

WebSocket spec supports [Sec-WebSocket-Protocol](https://tools.ietf.org/html/rfc6455#section-11.3.4) as a standard way for negotiate a sub protocol between client and server.

* ios WebSocket implementation supports it.
* android WebSocket implementation ignores this header, leave a comment syas: "OkHttp will overrides it", so it did not implement.
* after some test, OkHttp doesn't override the header we add.

**Test plan (required)**

1. run and react-native app on android
2. at the main page, invoke: `var ws = new WebSocket('ws://example.ws-service.fakedomain.com', 'my-sub-protocol');`
3. see the header if it send the correct header, ex, use ngrep: `sudo ngrep -t -Wbyline -deth0 host example.ws-service.fakedomain.com and port 80`

you should see the WebSocket initial GET handshake includes header:
`Sec-WebSocke
Closes facebook#6223

Differential Revision: D3162822

fb-gh-sync-id: a00f1c0f3e1c24ad6aa234329cbb2abad7664264
fbshipit-source-id: a00f1c0f3e1c24ad6aa234329cbb2abad7664264
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants