From f409c0db44407e5389da24abf5eb2e9b83d6420d Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Fri, 10 Feb 2023 18:09:13 +0800 Subject: [PATCH] Handler client request with / path --- .../main/java/com/fizzgate/util/WebUtils.java | 124 ++++++++++++------ .../java/com/fizzgate/util/WebUtilsTests.java | 36 +++++ 2 files changed, 117 insertions(+), 43 deletions(-) diff --git a/fizz-core/src/main/java/com/fizzgate/util/WebUtils.java b/fizz-core/src/main/java/com/fizzgate/util/WebUtils.java index cb703bd..79e2b32 100644 --- a/fizz-core/src/main/java/com/fizzgate/util/WebUtils.java +++ b/fizz-core/src/main/java/com/fizzgate/util/WebUtils.java @@ -224,23 +224,36 @@ 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)) { - 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 { - if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { - int trdFS = p.indexOf(Consts.S.FORWARD_SLASH, secFS + 1); - if (trdFS == -1) { - trdFS = p.length(); - } - svc = p.substring(secFS + 1, trdFS); + if (secFS == -1) { + if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { + svc = p.substring(1); } else { - throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix); + String prefix = p.substring(1); + if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { + throw Utils.runtimeExceptionWithoutStack("no service in request path"); + } else { + throw Utils.runtimeExceptionWithoutStack("request prefix is wrong and no service in request path"); + } + } + } else { + String prefix = p.substring(0, secFS); + if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { + 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 { + if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { + int trdFS = p.indexOf(Consts.S.FORWARD_SLASH, secFS + 1); + if (trdFS == -1) { + trdFS = p.length(); + } + svc = p.substring(secFS + 1, trdFS); + } else { + throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix); + } } } exchange.getAttributes().put(clientService, svc); @@ -356,25 +369,37 @@ 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)) { - 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 { - - if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { - int trdFS = p.indexOf(Consts.S.FORWARD_SLASH, secFS + 1); - if (trdFS == -1) { - p = Consts.S.FORWARD_SLASH_STR; + if (secFS == -1) { + if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { + p = Consts.S.FORWARD_SLASH_STR; + } else { + String prefix = p.substring(1); + if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { + throw Utils.runtimeExceptionWithoutStack("no service and request path"); } else { + throw Utils.runtimeExceptionWithoutStack("request prefix is wrong, no service and request path"); + } + } + } else { + String prefix = p.substring(0, secFS); + if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { + 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 { - throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix); + if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { + int trdFS = p.indexOf(Consts.S.FORWARD_SLASH, secFS + 1); + if (trdFS == -1) { + p = Consts.S.FORWARD_SLASH_STR; + } else { + p = p.substring(trdFS); + } + } else { + throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix); + } } } exchange.getAttributes().put(clientRequestPath, p); @@ -394,19 +419,32 @@ public abstract class WebUtils { 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)) { - if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { - prefix = prefix + Consts.S.FORWARD_SLASH; - } else { - prefix = Consts.S.FORWARD_SLASH_STR; - } - } else { - if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { - prefix = prefix + Consts.S.FORWARD_SLASH; + int secFS = path.indexOf(Consts.S.FORWARD_SLASH, 1); + if (secFS == -1) { + prefix = path.substring(1); + if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { + prefix = Consts.S.FORWARD_SLASH_STR; } else { - throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix); + if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { + prefix = prefix + Consts.S.FORWARD_SLASH; + } else { + throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix); + } + } + } else { + prefix = path.substring(0, secFS); + if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { + if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { + prefix = prefix + Consts.S.FORWARD_SLASH; + } else { + prefix = Consts.S.FORWARD_SLASH_STR; + } + } else { + if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { + prefix = prefix + Consts.S.FORWARD_SLASH; + } else { + throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix); + } } } exchange.getAttributes().put(clientRequestPathPrefix, prefix); diff --git a/fizz-core/src/test/java/com/fizzgate/util/WebUtilsTests.java b/fizz-core/src/test/java/com/fizzgate/util/WebUtilsTests.java index 84aa970..438ce0f 100644 --- a/fizz-core/src/test/java/com/fizzgate/util/WebUtilsTests.java +++ b/fizz-core/src/test/java/com/fizzgate/util/WebUtilsTests.java @@ -71,6 +71,24 @@ public class WebUtilsTests { assertEquals("/ybiz1", clientReqPath); clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); assertEquals("/", clientReqPathPrefix); + + mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/aservice/").build(); + mockExchange = MockServerWebExchange.from(mockRequest); + clientService = WebUtils.getClientService(mockExchange); + assertEquals("aservice", clientService); + clientReqPath = WebUtils.getClientReqPath(mockExchange); + assertEquals("/", clientReqPath); + clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); + assertEquals("/", clientReqPathPrefix); + + mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/aservice").build(); + mockExchange = MockServerWebExchange.from(mockRequest); + clientService = WebUtils.getClientService(mockExchange); + assertEquals("aservice", clientService); + clientReqPath = WebUtils.getClientReqPath(mockExchange); + assertEquals("/", 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); @@ -90,6 +108,24 @@ public class WebUtilsTests { assertEquals("/ybiz1", clientReqPath); clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); assertEquals("/", clientReqPathPrefix); + + mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/aservice/").build(); + mockExchange = MockServerWebExchange.from(mockRequest); + clientService = WebUtils.getClientService(mockExchange); + assertEquals("aservice", clientService); + clientReqPath = WebUtils.getClientReqPath(mockExchange); + assertEquals("/", clientReqPath); + clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); + assertEquals("/", clientReqPathPrefix); + + mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/aservice").build(); + mockExchange = MockServerWebExchange.from(mockRequest); + clientService = WebUtils.getClientService(mockExchange); + assertEquals("aservice", clientService); + clientReqPath = WebUtils.getClientReqPath(mockExchange); + assertEquals("/", 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);