From 255ea578771edb8981ee82fcc27fb5cef80c8c42 Mon Sep 17 00:00:00 2001 From: Francis Dong Date: Mon, 31 Oct 2022 14:56:42 +0800 Subject: [PATCH] Fix blank gateway context problem #458 --- fizz-core/src/main/java/we/util/WebUtils.java | 31 +++++++++--- .../src/test/java/we/util/WebUtilsTests.java | 50 +++++++++++++++++++ 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/fizz-core/src/main/java/we/util/WebUtils.java b/fizz-core/src/main/java/we/util/WebUtils.java index 72a6631..0674251 100644 --- a/fizz-core/src/main/java/we/util/WebUtils.java +++ b/fizz-core/src/main/java/we/util/WebUtils.java @@ -216,10 +216,15 @@ public abstract class WebUtils { if (svc == null) { String p = exchange.getRequest().getPath().value(); int secFS = p.indexOf(Consts.S.FORWARD_SLASH, 1); + String prefix = p.substring(0, secFS); if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { - svc = p.substring(1, secFS); + if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { + int trdFS = p.indexOf(Consts.S.FORWARD_SLASH, secFS + 1); + svc = p.substring(secFS + 1, trdFS); + } else { + svc = p.substring(1, secFS); + } } else { - String prefix = p.substring(0, secFS); if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { int trdFS = p.indexOf(Consts.S.FORWARD_SLASH, secFS + 1); svc = p.substring(secFS + 1, trdFS); @@ -340,10 +345,16 @@ public abstract class WebUtils { // p = exchange.getRequest().getPath().value(); p = exchange.getRequest().getURI().getPath(); int secFS = p.indexOf(Consts.S.FORWARD_SLASH, 1); + String prefix = p.substring(0, secFS); if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { - p = p.substring(secFS); + if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { + int trdFS = p.indexOf(Consts.S.FORWARD_SLASH, secFS + 1); + p = p.substring(trdFS); + } else { + p = p.substring(secFS); + } } else { - String prefix = p.substring(0, secFS); + if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { int trdFS = p.indexOf(Consts.S.FORWARD_SLASH, secFS + 1); p = p.substring(trdFS); @@ -367,12 +378,16 @@ public abstract class WebUtils { public static String getClientReqPathPrefix(ServerWebExchange exchange) { String prefix = exchange.getAttribute(clientRequestPathPrefix); if (prefix == null) { + String path = exchange.getRequest().getPath().value(); + int secFS = path.indexOf(Consts.S.FORWARD_SLASH, 1); + prefix = path.substring(0, secFS); if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { - prefix = Consts.S.FORWARD_SLASH_STR; + if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { + prefix = prefix + Consts.S.FORWARD_SLASH; + } else { + prefix = Consts.S.FORWARD_SLASH_STR; + } } else { - String path = exchange.getRequest().getPath().value(); - int secFS = path.indexOf(Consts.S.FORWARD_SLASH, 1); - prefix = path.substring(0, secFS); if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { prefix = prefix + Consts.S.FORWARD_SLASH; } else { diff --git a/fizz-core/src/test/java/we/util/WebUtilsTests.java b/fizz-core/src/test/java/we/util/WebUtilsTests.java index edbe0c7..9e2dc47 100644 --- a/fizz-core/src/test/java/we/util/WebUtilsTests.java +++ b/fizz-core/src/test/java/we/util/WebUtilsTests.java @@ -31,6 +31,15 @@ public class WebUtilsTests { assertEquals("/ybiz", clientReqPath); String clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); assertEquals("/_proxytest/", clientReqPathPrefix); + + mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/_proxytestx/test/ybiz").build(); + mockExchange = MockServerWebExchange.from(mockRequest); + clientService = WebUtils.getClientService(mockExchange); + assertEquals("test", clientService); + clientReqPath = WebUtils.getClientReqPath(mockExchange); + assertEquals("/ybiz", clientReqPath); + clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); + assertEquals("/_proxytestx/", clientReqPathPrefix); WebUtils.setGatewayPrefix("/prox"); mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/prox/test/ybiz").build(); @@ -39,6 +48,17 @@ public class WebUtilsTests { assertEquals("test", clientService); clientReqPath = WebUtils.getClientReqPath(mockExchange); assertEquals("/ybiz", clientReqPath); + clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); + assertEquals("/prox/", clientReqPathPrefix); + + mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/_proxytest/xservice/ybiz?a=b").build(); + mockExchange = MockServerWebExchange.from(mockRequest); + clientService = WebUtils.getClientService(mockExchange); + assertEquals("xservice", clientService); + clientReqPath = WebUtils.getClientReqPath(mockExchange); + assertEquals("/ybiz", clientReqPath); + clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); + assertEquals("/_proxytest/", clientReqPathPrefix); WebUtils.setGatewayPrefix(""); mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/aservice/ybiz1").build(); @@ -47,6 +67,36 @@ public class WebUtilsTests { assertEquals("aservice", clientService); clientReqPath = WebUtils.getClientReqPath(mockExchange); assertEquals("/ybiz1", clientReqPath); + clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); + assertEquals("/", clientReqPathPrefix); + + mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/_proxytest/xservice/ybiz?a=b").build(); + mockExchange = MockServerWebExchange.from(mockRequest); + clientService = WebUtils.getClientService(mockExchange); + assertEquals("xservice", clientService); + clientReqPath = WebUtils.getClientReqPath(mockExchange); + assertEquals("/ybiz", clientReqPath); + clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); + assertEquals("/_proxytest/", clientReqPathPrefix); + + WebUtils.setGatewayPrefix("/"); + mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/aservice/ybiz1").build(); + mockExchange = MockServerWebExchange.from(mockRequest); + clientService = WebUtils.getClientService(mockExchange); + assertEquals("aservice", clientService); + clientReqPath = WebUtils.getClientReqPath(mockExchange); + assertEquals("/ybiz1", clientReqPath); + clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); + assertEquals("/", clientReqPathPrefix); + + mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/_proxytest/xservice/ybiz?a=b").build(); + mockExchange = MockServerWebExchange.from(mockRequest); + clientService = WebUtils.getClientService(mockExchange); + assertEquals("xservice", clientService); + clientReqPath = WebUtils.getClientReqPath(mockExchange); + assertEquals("/ybiz", clientReqPath); + clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); + assertEquals("/_proxytest/", clientReqPathPrefix); } @Test