Handler client request with / path

This commit is contained in:
hongqiaowei
2023-02-10 18:09:13 +08:00
parent 34a49767d1
commit f409c0db44
2 changed files with 117 additions and 43 deletions

View File

@@ -224,6 +224,18 @@ public abstract class WebUtils {
if (svc == null) { if (svc == null) {
String p = exchange.getRequest().getPath().value(); String p = exchange.getRequest().getPath().value();
int secFS = p.indexOf(Consts.S.FORWARD_SLASH, 1); int secFS = p.indexOf(Consts.S.FORWARD_SLASH, 1);
if (secFS == -1) {
if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) {
svc = p.substring(1);
} else {
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); String prefix = p.substring(0, secFS);
if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) {
if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) {
@@ -243,6 +255,7 @@ public abstract class WebUtils {
throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix); throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix);
} }
} }
}
exchange.getAttributes().put(clientService, svc); exchange.getAttributes().put(clientService, svc);
} }
return svc; return svc;
@@ -356,6 +369,18 @@ public abstract class WebUtils {
// p = exchange.getRequest().getPath().value(); // p = exchange.getRequest().getPath().value();
p = exchange.getRequest().getURI().getPath(); p = exchange.getRequest().getURI().getPath();
int secFS = p.indexOf(Consts.S.FORWARD_SLASH, 1); int secFS = p.indexOf(Consts.S.FORWARD_SLASH, 1);
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); String prefix = p.substring(0, secFS);
if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) {
if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) {
@@ -365,7 +390,6 @@ public abstract class WebUtils {
p = p.substring(secFS); p = p.substring(secFS);
} }
} else { } else {
if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { if (gatewayPrefix.equals(prefix) || SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) {
int trdFS = p.indexOf(Consts.S.FORWARD_SLASH, secFS + 1); int trdFS = p.indexOf(Consts.S.FORWARD_SLASH, secFS + 1);
if (trdFS == -1) { if (trdFS == -1) {
@@ -377,6 +401,7 @@ public abstract class WebUtils {
throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix); throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix);
} }
} }
}
exchange.getAttributes().put(clientRequestPath, p); exchange.getAttributes().put(clientRequestPath, p);
} }
return p; return p;
@@ -395,6 +420,18 @@ public abstract class WebUtils {
if (prefix == null) { if (prefix == null) {
String path = exchange.getRequest().getPath().value(); String path = exchange.getRequest().getPath().value();
int secFS = path.indexOf(Consts.S.FORWARD_SLASH, 1); 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 {
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); prefix = path.substring(0, secFS);
if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) { if (StringUtils.isBlank(gatewayPrefix) || Consts.S.FORWARD_SLASH_STR.equals(gatewayPrefix)) {
if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) { if (SystemConfig.DEFAULT_GATEWAY_TEST_PREFIX.equals(prefix)) {
@@ -409,6 +446,7 @@ public abstract class WebUtils {
throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix); throw Utils.runtimeExceptionWithoutStack("wrong prefix " + prefix);
} }
} }
}
exchange.getAttributes().put(clientRequestPathPrefix, prefix); exchange.getAttributes().put(clientRequestPathPrefix, prefix);
} }
return prefix; return prefix;

View File

@@ -72,6 +72,24 @@ public class WebUtilsTests {
clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange);
assertEquals("/", clientReqPathPrefix); 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(); mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/_proxytest/xservice/ybiz?a=b").build();
mockExchange = MockServerWebExchange.from(mockRequest); mockExchange = MockServerWebExchange.from(mockRequest);
clientService = WebUtils.getClientService(mockExchange); clientService = WebUtils.getClientService(mockExchange);
@@ -91,6 +109,24 @@ public class WebUtilsTests {
clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange); clientReqPathPrefix = WebUtils.getClientReqPathPrefix(mockExchange);
assertEquals("/", clientReqPathPrefix); 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(); mockRequest = MockServerHttpRequest.get("http://127.0.0.1:8600/_proxytest/xservice/ybiz?a=b").build();
mockExchange = MockServerWebExchange.from(mockRequest); mockExchange = MockServerWebExchange.from(mockRequest);
clientService = WebUtils.getClientService(mockExchange); clientService = WebUtils.getClientService(mockExchange);