You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/01/10 06:27:02 UTC
[incubator-shenyu] branch master updated: [type: refactor] optimize & fix uri with invalid char. (#2738)
This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 9727565 [type: refactor] optimize & fix uri with invalid char. (#2738)
9727565 is described below
commit 97275658349bce4ff13119edd70ddafdab2908a4
Author: Qicz <qi...@gmail.com>
AuthorDate: Mon Jan 10 14:26:43 2022 +0800
[type: refactor] optimize & fix uri with invalid char. (#2738)
* [type: refactor] optimize & fix uri with invalid char.
* add validate
* add integrate test
* fix test
---
.../src/main/http/http-test-api-local.http | 17 +++++++
.../src/main/http/http-test-api.http | 57 ++++++++++++++++++++++
.../http/controller/HttpTestController.java | 11 +++++
.../test/http/HttpTestControllerTest.java | 41 ++++++++++++++--
.../plugin/api/utils/RequestQueryCodecUtil.java | 53 ++++++++++++++++++++
.../org/apache/shenyu/plugin/uri/URIPlugin.java | 22 +++------
.../apache/shenyu/plugin/uri/URIPluginTest.java | 33 ++++++++++++-
7 files changed, 215 insertions(+), 19 deletions(-)
diff --git a/shenyu-examples/shenyu-examples-http/src/main/http/http-test-api-local.http b/shenyu-examples/shenyu-examples-http/src/main/http/http-test-api-local.http
index 1aeec63..6acec07 100644
--- a/shenyu-examples/shenyu-examples-http/src/main/http/http-test-api-local.http
+++ b/shenyu-examples/shenyu-examples-http/src/main/http/http-test-api-local.http
@@ -93,6 +93,21 @@ GET http://localhost:8189/test/findByUserId?userId=123
Accept: application/json
Content-Type: application/json
+### example local findByUserId
+GET http://localhost:8189/test/findByUserId?userId=123%5D%2D
+Accept: application/json
+Content-Type: application/json
+
+### example local findByUserId
+GET http://localhost:8189/test/findByUserId?userId=123%5D%2D%2Casd
+Accept: application/json
+Content-Type: application/json
+
+### example local findByUserId
+GET http://localhost:8189/test/findByUserId?userId=123%5D%2D,asd
+Accept: application/json
+Content-Type: application/json
+
### example local path
GET http://localhost:8189/test/path/123?name=haha
Accept: application/json
@@ -117,3 +132,5 @@ Content-Type: application/json
GET http://localhost:8189/test/request/parameter/pass?requestParameter=test
Accept: application/json
Content-Type: application/json
+
+###
diff --git a/shenyu-examples/shenyu-examples-http/src/main/http/http-test-api.http b/shenyu-examples/shenyu-examples-http/src/main/http/http-test-api.http
index 842bbf4..e52961b 100644
--- a/shenyu-examples/shenyu-examples-http/src/main/http/http-test-api.http
+++ b/shenyu-examples/shenyu-examples-http/src/main/http/http-test-api.http
@@ -92,6 +92,61 @@ GET http://localhost:9195/http/test/findByUserId?userId=123
Accept: application/json
Content-Type: application/json
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserId?userId=123%5D%2D
+Accept: application/json
+Content-Type: application/json
+
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserId?userId=1#
+Accept: application/json
+Content-Type: application/json
+
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserId?userId=1#1
+Accept: application/json
+Content-Type: application/json
+
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserId?userId=1%25
+Accept: application/json
+Content-Type: application/json
+
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserId?userId=123%20zcq
+Accept: application/json
+Content-Type: application/json
+
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserId?userId=#123
+Accept: application/json
+Content-Type: application/json
+
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserId?userId=%25
+Accept: application/json
+Content-Type: application/json
+
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserIdName?userId=#&name=shenyu
+Accept: application/json
+Content-Type: application/json
+
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserId?userId=123%5D%2D%2Casd
+Accept: application/json
+Content-Type: application/json
+
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserId?userId=%5B%27z%27%2C%27zc%27%2C%27zz%27%5D
+Accept: application/json
+Content-Type: application/json
+
+### shengyu getway proxy findByUserId
+GET http://localhost:9195/http/test/findByUserId?userId=['z','zc','zz']
+Accept: application/json
+Content-Type: application/json
+
### shengyu getway proxy path
GET http://localhost:9195/http/test/path/123?name=haha
Accept: application/json
@@ -116,3 +171,5 @@ Content-Type: application/json
GET http://localhost:9195/http/test/request/parameter/pass?requestParameter=test
Accept: application/json
Content-Type: application/json
+
+###
diff --git a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
index f2ca799..181bcda 100644
--- a/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
+++ b/shenyu-examples/shenyu-examples-http/src/main/java/org/apache/shenyu/examples/http/controller/HttpTestController.java
@@ -69,6 +69,17 @@ public class HttpTestController {
}
/**
+ * Find by user id string.
+ *
+ * @param userId the user id
+ * @return the string
+ */
+ @GetMapping("/findByUserIdName")
+ public UserDTO findByUserId(@RequestParam("userId") final String userId, @RequestParam("name") final String name) {
+ return buildUser(userId, name);
+ }
+
+ /**
* Find by page user dto.
*
* @param keyword the keyword
diff --git a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/HttpTestControllerTest.java b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/HttpTestControllerTest.java
index cf18ec2..0d1cbaa 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/HttpTestControllerTest.java
+++ b/shenyu-integrated-test/shenyu-integrated-test-http/src/test/java/org/apache/shenyu/integrated/test/http/HttpTestControllerTest.java
@@ -34,7 +34,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public final class HttpTestControllerTest extends AbstractTest {
-
+
@Test
public void testPayment() throws IOException {
UserDTO user = new UserDTO("1", "http-test");
@@ -42,14 +42,49 @@ public final class HttpTestControllerTest extends AbstractTest {
assertEquals("1", user.getUserId());
assertEquals("http-test", user.getUserName());
}
-
+
@Test
public void testFindByUserId() throws IOException {
UserDTO user = HttpHelper.INSTANCE.getFromGateway("/http/test/findByUserId?userId=1", UserDTO.class);
assertEquals("1", user.getUserId());
assertEquals("hello world", user.getUserName());
}
-
+
+ @Test
+ public void testFindByUserIdSepcChar0() throws IOException {
+ UserDTO user = HttpHelper.INSTANCE.getFromGateway("/http/test/findByUserId?userId=1%25", UserDTO.class);
+ assertEquals("1%", user.getUserId());
+ assertEquals("hello world", user.getUserName());
+ }
+
+ @Test
+ public void testFindByUserIdSepcChar1() throws IOException {
+ UserDTO user = HttpHelper.INSTANCE.getFromGateway("/http/test/findByUserId?userId=1%201", UserDTO.class);
+ assertEquals("1 1", user.getUserId());
+ assertEquals("hello world", user.getUserName());
+ }
+
+ @Test
+ public void testFindByUserIdSepcChar3() throws IOException {
+ UserDTO user = HttpHelper.INSTANCE.getFromGateway("/http/test/findByUserId?userId=1%231", UserDTO.class);
+ assertEquals("1#1", user.getUserId());
+ assertEquals("hello world", user.getUserName());
+ }
+
+ @Test
+ public void testFindByUserIdName() throws IOException {
+ UserDTO user = HttpHelper.INSTANCE.getFromGateway("/http/test/findByUserIdName?userId=1%231&name=shenyu%20666", UserDTO.class);
+ assertEquals("1#1", user.getUserId());
+ assertEquals("shenyu 666", user.getUserName());
+ }
+
+ @Test
+ public void testFindByUserIdName1() throws IOException {
+ UserDTO user = HttpHelper.INSTANCE.getFromGateway("/http/test/findByUserIdName?userId=1%231&name=[shenyu%20666]", UserDTO.class);
+ assertEquals("1#1", user.getUserId());
+ assertEquals("[shenyu 666]", user.getUserName());
+ }
+
@Test
public void testFindByPage() throws IOException {
UserDTO user = HttpHelper.INSTANCE.getFromGateway("/http/test/findByPage?keyword=http-test&page=1&pageSize=10", UserDTO.class);
diff --git a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestQueryCodecUtil.java b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestQueryCodecUtil.java
new file mode 100644
index 0000000..382adfe
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestQueryCodecUtil.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.api.utils;
+
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.server.ServerWebExchange;
+import org.springframework.web.util.UriUtils;
+
+import java.nio.charset.StandardCharsets;
+import java.util.stream.Collectors;
+
+/**
+ * RequestQueryCodecUtil.
+ */
+public final class RequestQueryCodecUtil {
+
+ private RequestQueryCodecUtil() {
+ }
+
+ /**
+ * Gets codec query string.
+ *
+ * @param exchange the exchange
+ * @return codec query string
+ */
+ public static String getCodecQuery(final ServerWebExchange exchange) {
+ if (!exchange.getRequest().getURI().getRawQuery().contains("%")) {
+ return exchange.getRequest().getURI().getQuery();
+ }
+ MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
+ return queryParams.keySet().stream()
+ .map(key -> queryParams.get(key).stream()
+ .map(item -> String.join("=", key,
+ UriUtils.encode(item, StandardCharsets.UTF_8)))
+ .collect(Collectors.joining("&")))
+ .collect(Collectors.joining("&")).trim();
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java b/shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java
index a06087f..4248e6b 100644
--- a/shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java
+++ b/shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java
@@ -23,8 +23,8 @@ import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
import org.apache.shenyu.plugin.api.ShenyuPluginChain;
import org.apache.shenyu.plugin.api.context.ShenyuContext;
+import org.apache.shenyu.plugin.api.utils.RequestQueryCodecUtil;
import org.springframework.web.server.ServerWebExchange;
-import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import java.net.URI;
@@ -42,27 +42,19 @@ public class URIPlugin implements ShenyuPlugin {
if (StringUtils.isBlank(path)) {
return chain.execute(exchange);
}
- String rewriteURI = (String) exchange.getAttributes().get(Constants.REWRITE_URI);
- URI uri = exchange.getRequest().getURI();
- if (StringUtils.isNoneBlank(rewriteURI)) {
- path = path + rewriteURI;
+ String rewriteUri = (String) exchange.getAttributes().get(Constants.REWRITE_URI);
+ if (StringUtils.isNoneBlank(rewriteUri)) {
+ path = path + rewriteUri;
} else {
String realUrl = shenyuContext.getRealUrl();
if (StringUtils.isNoneBlank(realUrl)) {
path = path + realUrl;
}
}
- URI realURI;
- if (StringUtils.isNotEmpty(uri.getRawQuery()) && uri.getRawQuery().contains("%")) {
- path = String.join("?", path, uri.getRawQuery());
- realURI = UriComponentsBuilder.fromHttpUrl(path).build(true).toUri();
- } else {
- if (StringUtils.isNotEmpty(uri.getQuery())) {
- path = String.join("?", path, uri.getQuery());
- }
- realURI = UriComponentsBuilder.fromHttpUrl(path).build(false).toUri();
+ if (StringUtils.isNoneBlank(exchange.getRequest().getURI().getQuery())) {
+ path = String.join("?", path, RequestQueryCodecUtil.getCodecQuery(exchange));
}
- exchange.getAttributes().put(Constants.HTTP_URI, realURI);
+ exchange.getAttributes().put(Constants.HTTP_URI, URI.create(path));
return chain.execute(exchange);
}
diff --git a/shenyu-plugin/shenyu-plugin-uri/src/test/java/org/apache/shenyu/plugin/uri/URIPluginTest.java b/shenyu-plugin/shenyu-plugin-uri/src/test/java/org/apache/shenyu/plugin/uri/URIPluginTest.java
index 290effb..5a220ba 100644
--- a/shenyu-plugin/shenyu-plugin-uri/src/test/java/org/apache/shenyu/plugin/uri/URIPluginTest.java
+++ b/shenyu-plugin/shenyu-plugin-uri/src/test/java/org/apache/shenyu/plugin/uri/URIPluginTest.java
@@ -29,11 +29,14 @@ import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.net.InetSocketAddress;
+import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
@@ -107,7 +110,7 @@ public class URIPluginTest {
when(exchange.getAttribute(Constants.HTTP_DOMAIN)).thenReturn("http://localhost:8090/query");
when(chain.execute(exchange)).thenReturn(Mono.empty());
StepVerifier.create(uriPlugin.execute(exchange, chain)).expectSubscription().verifyComplete();
- assertEquals("http://localhost:8090/query?queryParam=Hello,%20World", exchange.getAttributes().get(Constants.HTTP_URI).toString());
+ assertEquals("http://localhost:8090/query?queryParam=Hello%2C%20World", exchange.getAttributes().get(Constants.HTTP_URI).toString());
}
@Test
@@ -129,4 +132,32 @@ public class URIPluginTest {
when(shenyuContext.getRpcType()).thenReturn(RpcTypeEnum.DUBBO.getName());
Assert.assertTrue(uriPlugin.skip(exchange));
}
+
+ @Test
+ public void testRequestQueryCodec() {
+ MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
+ queryParams.add("zcq", "01");
+ queryParams.add("zcq", "03");
+ queryParams.add("add", "bj");
+ queryParams.add("name", "b j");
+ queryParams.add("age", "['z','zc','zz']");
+ Assert.assertEquals("zcq=01&zcq=03&add=bj&name=b%20j&age=['z','zc','zz']", getCodecQuery(queryParams));
+ }
+
+ /**
+ * Gets codec query string.
+ *
+ * @param queryParams the queryParams
+ * @return codec query string
+ */
+ private static String getCodecQuery(final MultiValueMap<String, String> queryParams) {
+ return queryParams.keySet().stream()
+ .map(key -> queryParams.get(key).stream()
+ .map(item -> String.join("=", key,
+ // https://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
+ // https://www.ietf.org/rfc/rfc2396.txt
+ item.replaceAll(" ", "%20")))
+ .collect(Collectors.joining("&")))
+ .collect(Collectors.joining("&")).trim();
+ }
}