diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java index 5e559ebddd71..177af22e4cdc 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java @@ -251,7 +251,7 @@ public boolean checkAndSubmit(final String selectorId, final CommonUpstream comm if (!REGISTER_TYPE_HTTP.equalsIgnoreCase(registerType) || !checked) { return false; } - final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getUpstreamUrl()); + final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getProtocol(), commonUpstream.getUpstreamUrl()); if (pass) { this.submit(selectorId, commonUpstream); return false; @@ -310,7 +310,7 @@ private void checkZombie0(final ZombieUpstream zombieUpstream) { ZOMBIE_SET.remove(zombieUpstream); String selectorId = zombieUpstream.getSelectorId(); CommonUpstream commonUpstream = zombieUpstream.getCommonUpstream(); - final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getUpstreamUrl()); + final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getProtocol(), commonUpstream.getUpstreamUrl()); if (pass) { commonUpstream.setTimestamp(System.currentTimeMillis()); commonUpstream.setStatus(true); @@ -332,7 +332,7 @@ private void check(final String selectorId, final List upstreamL final List> checkFutures = new ArrayList<>(upstreamList.size()); for (CommonUpstream commonUpstream : upstreamList) { checkFutures.add(CompletableFuture.supplyAsync(() -> { - final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getUpstreamUrl()); + final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getProtocol(), commonUpstream.getUpstreamUrl()); if (pass) { if (!commonUpstream.isStatus()) { commonUpstream.setTimestamp(System.currentTimeMillis()); diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java index 04de27492300..3059e0fa767b 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java @@ -160,9 +160,9 @@ public void testScheduled() { .handle("[{\"upstreamHost\":\"localhost\",\"protocol\":\"http://\",\"localhost\":\"divide-upstream-60\",\"weight\":60}]") .build(); try (MockedStatic mocked = mockStatic(UpstreamCheckUtils.class)) { - mocked.when(() -> UpstreamCheckUtils.checkUrl("ReachableUrl")) + mocked.when(() -> UpstreamCheckUtils.checkUrl("https://", "ReachableUrl")) .thenReturn(true); - mocked.when(() -> UpstreamCheckUtils.checkUrl("ErrorUrl")) + mocked.when(() -> UpstreamCheckUtils.checkUrl("https://", "ErrorUrl")) .thenReturn(false); zombieSet.clear(); diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/UpstreamCheckUtils.java b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/UpstreamCheckUtils.java index bb16da853c92..dd0f61ef2ce2 100644 --- a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/UpstreamCheckUtils.java +++ b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/UpstreamCheckUtils.java @@ -44,22 +44,24 @@ public class UpstreamCheckUtils { /** * Check url boolean. * - * @param url the url + * @param protocol the protocol + * @param url the url * @return the boolean */ - public static boolean checkUrl(final String url) { - return checkUrl(url, DEFAULT_TIMEOUT); + public static boolean checkUrl(final String protocol, final String url) { + return checkUrl(protocol, url, DEFAULT_TIMEOUT); } /** * Check url boolean. * - * @param url the url - * @param timeout timeout + * @param protocol the protocol + * @param url the url + * @param timeout timeout * @return the boolean */ - public static boolean checkUrl(final String url, final int timeout) { - if (StringUtils.isBlank(url)) { + public static boolean checkUrl(final String protocol, final String url, final int timeout) { + if (StringUtils.isBlank(protocol) || StringUtils.isBlank(url)) { return false; } String[] hostPort; @@ -69,7 +71,7 @@ public static boolean checkUrl(final String url, final int timeout) { } else { hostPort = StringUtils.split(url, Constants.COLONS); } - final boolean isHttps = url.startsWith(HTTPS); + final boolean isHttps = protocol.startsWith(HTTPS); final int port = hostPort.length > 1 ? Integer.parseInt(hostPort[1].trim()) : isHttps ? 443 : 80; return isHostConnector(hostPort[0].trim(), port, timeout); } diff --git a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/UpstreamCheckUtilsTest.java b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/UpstreamCheckUtilsTest.java index 47d86dbc74e0..5a0cac2c57e8 100644 --- a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/UpstreamCheckUtilsTest.java +++ b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/UpstreamCheckUtilsTest.java @@ -40,7 +40,7 @@ public final class UpstreamCheckUtilsTest { @Test public void testBlank() { - assertFalse(UpstreamCheckUtils.checkUrl("")); + assertFalse(UpstreamCheckUtils.checkUrl("https://", "")); } @Test @@ -63,9 +63,9 @@ public void testSocketConnect() { Thread.yield(); } - assertTrue(UpstreamCheckUtils.checkUrl("127.0.0.1:" + port)); - assertFalse(UpstreamCheckUtils.checkUrl("http://127.0.0.1:" + (port == 0 ? port + 1 : port - 1))); - assertTrue(UpstreamCheckUtils.checkUrl("http://127.0.0.1:" + port)); - assertTrue(UpstreamCheckUtils.checkUrl("https://shenyu.apache.org")); + assertTrue(UpstreamCheckUtils.checkUrl("https://", "127.0.0.1:" + port)); + assertFalse(UpstreamCheckUtils.checkUrl("https://", "127.0.0.1:" + (port == 0 ? port + 1 : port - 1))); + assertTrue(UpstreamCheckUtils.checkUrl("http://", "127.0.0.1:" + port)); + assertTrue(UpstreamCheckUtils.checkUrl("https://", "shenyu.apache.org")); } } diff --git a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTask.java b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTask.java index 359aa06253a4..18d68adb0893 100644 --- a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTask.java +++ b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTask.java @@ -190,7 +190,7 @@ private void check(final Map> map) { } private UpstreamWithSelectorId check(final String selectorId, final Upstream upstream) { - boolean pass = UpstreamCheckUtils.checkUrl(upstream.getUrl(), checkTimeout); + boolean pass = UpstreamCheckUtils.checkUrl(upstream.getProtocol(), upstream.getUrl(), checkTimeout); if (pass) { if (upstream.isHealthy()) { upstream.setLastHealthTimestamp(System.currentTimeMillis()); diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/DividePluginTest.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/DividePluginTest.java index 70109d02dcf3..5c58e5e356e9 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/DividePluginTest.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/DividePluginTest.java @@ -126,7 +126,7 @@ public void setup() { // mock static mockCheckUtils = mockStatic(UpstreamCheckUtils.class); - mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyInt())).thenReturn(true); + mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyString(), anyInt())).thenReturn(true); initMockInfo(); } diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DividePluginDataHandlerTest.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DividePluginDataHandlerTest.java index 70e496b9f493..c923491970ad 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DividePluginDataHandlerTest.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DividePluginDataHandlerTest.java @@ -77,7 +77,7 @@ public void setUp() { // mock static mockCheckUtils = mockStatic(UpstreamCheckUtils.class); - mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyInt())).thenReturn(true); + mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyString(), anyInt())).thenReturn(true); } @AfterEach diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandlerTest.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandlerTest.java index 184ae75b6185..cdcf51fd3c5e 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandlerTest.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandlerTest.java @@ -69,7 +69,7 @@ public void setUp() { // mock static mockCheckUtils = mockStatic(UpstreamCheckUtils.class); - mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyInt())).thenReturn(true); + mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyString(), anyInt())).thenReturn(true); } @AfterEach diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/WebSocketPluginTest.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/WebSocketPluginTest.java index c0841d389b43..89fc8d283d98 100644 --- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/WebSocketPluginTest.java +++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-websocket/src/test/java/org/apache/shenyu/plugin/websocket/WebSocketPluginTest.java @@ -110,7 +110,7 @@ public void setup() { // mock static mockCheckUtils = mockStatic(UpstreamCheckUtils.class); - mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyInt())).thenReturn(true); + mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyString(), anyInt())).thenReturn(true); } @AfterEach