From 7b53a244cd85b894497939e1a5776812a510763c Mon Sep 17 00:00:00 2001 From: Kudo Chien Date: Thu, 22 Sep 2022 04:20:44 -0700 Subject: [PATCH] Fix port as -1 if dev server without specifying port on Android (#34705) Summary: when specifying dev server without port, e.g. http://www.example.com/, there are some issues. 1. redbox error 2. showing -1 in loading view the root cause is coming from [`java.net.URL.getPort()` will return -1 when the url doesn't have a port](https://developer.android.com/reference/java/net/URL#getPort()). this pr replaces the parser to [`okhttp3.HttpUrl`](https://square.github.io/okhttp/4.x/okhttp/okhttp3/-http-url/#port) that it will have default port 80 for http or port 443 for https. the two call paths should only serve http/https address, not file:// address. it should be safe to change from java.net.URL to okhttp3.HttpUrl. not fully related, in the case above, android will connect to `ws://www.example.com/:8097` for react-devtools we should strip the trailing slash in *setUpReactDevTools.js* ## Changelog [Android] [Fixed] - Fix port as -1 if dev server without specifying port on Android Pull Request resolved: https://github.com/facebook/react-native/pull/34705 Test Plan: test on rn-tester with the following steps 1. `yarn start` 2. open another terminal and run `ngrok http 8081` and it will return a tunnel url, e.g. `71a1-114-36-194-97.jp.ngrok.io` 3. open dev setting in app and change the dev server to `71a1-114-36-194-97.jp.ngrok.io` 5. reload the app Reviewed By: cipolleschi Differential Revision: D39573988 Pulled By: cortinico fbshipit-source-id: 397df90ab30533207bd87a3f069132d97c22c7fd --- Libraries/Core/setUpReactDevTools.js | 5 ++++- .../facebook/react/devsupport/DevLoadingViewController.java | 4 ++-- .../com/facebook/react/devsupport/DevSupportManagerBase.java | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Libraries/Core/setUpReactDevTools.js b/Libraries/Core/setUpReactDevTools.js index 46955da62035f5..be647e33b4d2ca 100644 --- a/Libraries/Core/setUpReactDevTools.js +++ b/Libraries/Core/setUpReactDevTools.js @@ -39,7 +39,10 @@ if (__DEV__) { // Get hostname from development server (packager) const devServer = getDevServer(); const host = devServer.bundleLoadedFromServer - ? devServer.url.replace(/https?:\/\//, '').split(':')[0] + ? devServer.url + .replace(/https?:\/\//, '') + .replace(/\/$/, '') + .split(':')[0] : 'localhost'; // Read the optional global variable for backward compatibility. diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java index db8ef299fc67e7..fa1554bda002f4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevLoadingViewController.java @@ -69,9 +69,9 @@ public void showForUrl(String url) { return; } + int port = parsedURL.getPort() != -1 ? parsedURL.getPort() : parsedURL.getDefaultPort(); showMessage( - context.getString( - R.string.catalyst_loading_from_url, parsedURL.getHost() + ":" + parsedURL.getPort())); + context.getString(R.string.catalyst_loading_from_url, parsedURL.getHost() + ":" + port)); } public void showForRemoteJSEnabled() { diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java index 76977bfb462639..0141e70c3eb69b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java @@ -703,7 +703,7 @@ private void resetCurrentContext(@Nullable ReactContext reactContext) { URL sourceUrl = new URL(getSourceUrl()); String path = sourceUrl.getPath().substring(1); // strip initial slash in path String host = sourceUrl.getHost(); - int port = sourceUrl.getPort(); + int port = sourceUrl.getPort() != -1 ? sourceUrl.getPort() : sourceUrl.getDefaultPort(); mCurrentContext .getJSModule(HMRClient.class) .setup("android", path, host, port, mDevSettings.isHotModuleReplacementEnabled());