Fix blank gateway context problem #458

This commit is contained in:
Francis Dong
2022-10-31 14:56:42 +08:00
committed by dxfeng10
parent fb3e5d0431
commit 255ea57877
2 changed files with 73 additions and 8 deletions

View File

@@ -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 {

View File

@@ -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