This is an automated email from the ASF dual-hosted git repository.

hefengen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new a0be71d7da [type:fix] fix uri plugin path (#5121)
a0be71d7da is described below

commit a0be71d7da8bdc6e87d3f11d2e3ad8a09b947829
Author: moremind <[email protected]>
AuthorDate: Thu Sep 7 16:18:19 2023 +0800

    [type:fix] fix uri plugin path (#5121)
    
    * [type:fix] fix uri plugin path
    
    * [type:fix] fix uri plugin path
    
    * [type:fix] fix uri plugin path
    
    * [type:fix] fix uri path error
    
    * [type:fix] fix uri path error
    
    * [type:fix] fix uri path error
    
    * [type:fix] fix uri path error
    
    ---------
    
    Co-authored-by: xiaoyu <[email protected]>
---
 .../http/controller/HttpTestController.java        | 27 +++++++++++++++
 .../test/http/HttpTestControllerTest.java          | 12 +++++++
 .../plugin/api/utils/RequestQueryCodecUtil.java    |  3 --
 .../shenyu/plugin/api/utils/RequestUrlUtils.java   | 15 +++++++--
 .../plugin/global/DefaultShenyuContextBuilder.java |  5 +--
 .../shenyu/plugin/rewrite/RewritePlugin.java       |  2 +-
 .../apache/shenyu/plugin/uri/URIPluginTest.java    | 39 ++++++++++++++++++++++
 7 files changed, 94 insertions(+), 9 deletions(-)

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 f4044c5d39..40f6a0e74f 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
@@ -461,4 +461,31 @@ public class HttpTestController {
         bigObject.setObj(obj);
         return bigObject;
     }
+    
+    /**
+     * test blank query.
+     *
+     * @return response body
+     */
+    @PostMapping("/ /query")
+    public ResultBean blankQuery() {
+        ResultBean resultBean = new ResultBean();
+        resultBean.setCode(200);
+        resultBean.setData("test blank query");
+        return resultBean;
+    }
+    
+    /**
+     * test blank query with params.
+     *
+     * @param param params
+     * @return response body
+     */
+    @PostMapping("/query")
+    public ResultBean blankQueryWithParams(@RequestParam("param") final String 
param) {
+        ResultBean resultBean = new ResultBean();
+        resultBean.setCode(200);
+        resultBean.setData(param);
+        return resultBean;
+    }
 }
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 b5d53bc563..84d0c9a062 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
@@ -241,4 +241,16 @@ public final class HttpTestControllerTest extends 
AbstractTest {
         BigObject response = 
HttpHelper.INSTANCE.postGateway("/http/test/bigObject", BigObject.class);
         assertNotNull(response);
     }
+    
+    @Test
+    public void testBlankQuery() throws IOException {
+        ResultBean resultBean = HttpHelper.INSTANCE.postGateway("/http/test/ 
/query", ResultBean.class);
+        assertNotNull(resultBean);
+    }
+    
+    @Test
+    public void testBlankQueryWithParams() throws IOException {
+        ResultBean resultBean = 
HttpHelper.INSTANCE.postGateway("/http/test/query?param=a%2Bb=", 
ResultBean.class);
+        assertEquals("a+b=", resultBean.getData());
+    }
 }
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
index dc189ec57f..eb5b882858 100644
--- 
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
@@ -41,9 +41,6 @@ public final class RequestQueryCodecUtil {
      * @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()
diff --git 
a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java
 
b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java
index 662ddb7f58..6ffc1f9637 100644
--- 
a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java
+++ 
b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java
@@ -21,6 +21,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.shenyu.common.constant.Constants;
 import org.apache.shenyu.plugin.api.context.ShenyuContext;
 import org.springframework.web.server.ServerWebExchange;
+import org.springframework.web.util.UriComponentsBuilder;
 
 import java.net.URI;
 
@@ -52,9 +53,17 @@ public final class RequestUrlUtils {
                 path = path + realUrl;
             }
         }
-        if 
(StringUtils.isNoneBlank(exchange.getRequest().getURI().getQuery())) {
-            path = String.join("?", path, 
RequestQueryCodecUtil.getCodecQuery(exchange));
+        URI uri = exchange.getRequest().getURI();
+        if ((StringUtils.isNotEmpty(uri.getRawQuery()) && 
uri.getRawQuery().contains("%"))
+                || (StringUtils.isNotEmpty(uri.getRawPath()) && 
uri.getRawPath().contains("%"))) {
+            path = path + "?" + RequestQueryCodecUtil.getCodecQuery(exchange);
+            return UriComponentsBuilder.fromHttpUrl(path).build(true).toUri();
+        } else {
+            if (StringUtils.isNotEmpty(uri.getQuery())) {
+                path = path + "?" + uri.getQuery();
+            }
+            assert path != null;
+            return UriComponentsBuilder.fromHttpUrl(path).build(false).toUri();
         }
-        return URI.create(path);
     }
 }
diff --git 
a/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
 
b/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
index 81df8007c8..5b2ba8b48e 100644
--- 
a/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
+++ 
b/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
@@ -30,6 +30,7 @@ import org.springframework.http.HttpHeaders;
 import org.springframework.http.server.reactive.ServerHttpRequest;
 import org.springframework.web.server.ServerWebExchange;
 
+import java.net.URI;
 import java.time.LocalDateTime;
 import java.util.Map;
 import java.util.Objects;
@@ -83,8 +84,8 @@ public class DefaultShenyuContextBuilder implements 
ShenyuContextBuilder {
 
     private ShenyuContext buildDefaultContext(final ServerHttpRequest request) 
{
         ShenyuContext shenyuContext = new ShenyuContext();
-        String path = request.getURI().getPath();
-        shenyuContext.setPath(path);
+        URI requestURI = request.getURI();
+        shenyuContext.setPath(requestURI.getRawPath());
         shenyuContext.setStartDateTime(LocalDateTime.now());
         Optional.ofNullable(request.getMethod()).ifPresent(httpMethod -> 
shenyuContext.setHttpMethod(httpMethod.name()));
         return shenyuContext;
diff --git 
a/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
 
b/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
index 845516ff9b..1711f15c00 100644
--- 
a/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
+++ 
b/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
@@ -51,7 +51,7 @@ public class RewritePlugin extends AbstractShenyuPlugin {
             LOG.error("uri rewrite rule can not configuration:{}", handle);
             return chain.execute(exchange);
         }
-        String rewriteUri = exchange.getRequest().getURI().getPath();
+        String rewriteUri = exchange.getRequest().getURI().getRawPath();
         if (StringUtils.isNoneBlank(rewriteHandle.getRegex(), 
rewriteHandle.getReplace())) {
             rewriteUri = rewriteHandle.getReplace().contains("{")
                     ? PathMatchUtils.replaceAll(rewriteHandle.getReplace(), 
rewriteHandle.getRegex().substring(rewriteHandle.getRegex().indexOf("{")),
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 e2cfac2602..4f41d6b140 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
@@ -116,6 +116,45 @@ public class URIPluginTest {
         when(chain.execute(exchange)).thenReturn(Mono.empty());
         StepVerifier.create(uriPlugin.execute(exchange, 
chain)).expectSubscription().verifyComplete();
         
assertEquals("http://localhost:8090/query?queryParam=Hello%2C%20World";, 
exchange.getAttributes().get(Constants.HTTP_URI).toString());
+        
+        request = MockServerHttpRequest
+                .get("localhost")
+                .remoteAddress(new InetSocketAddress(8090))
+                .queryParam("queryParam", "Hello, World")
+                .build();
+        this.exchange = spy(MockServerWebExchange.from(request));
+        shenyuContext = mock(ShenyuContext.class);
+        exchange.getAttributes().put(Constants.CONTEXT, shenyuContext);
+        
when(exchange.getAttribute(Constants.HTTP_DOMAIN)).thenReturn("http://localhost:8090/%20/query";);
+        when(chain.execute(exchange)).thenReturn(Mono.empty());
+        StepVerifier.create(uriPlugin.execute(exchange, 
chain)).expectSubscription().verifyComplete();
+        
assertEquals("http://localhost:8090/%20/query?queryParam=Hello%2C%20World";, 
exchange.getAttributes().get(Constants.HTTP_URI).toString());
+        
+        request = MockServerHttpRequest
+                .get("localhost")
+                .remoteAddress(new InetSocketAddress(8090))
+                .queryParam("queryParam", "Hello,  World")
+                .build();
+        this.exchange = spy(MockServerWebExchange.from(request));
+        shenyuContext = mock(ShenyuContext.class);
+        exchange.getAttributes().put(Constants.CONTEXT, shenyuContext);
+        
when(exchange.getAttribute(Constants.HTTP_DOMAIN)).thenReturn("http://localhost:8090/%20/query";);
+        when(chain.execute(exchange)).thenReturn(Mono.empty());
+        StepVerifier.create(uriPlugin.execute(exchange, 
chain)).expectSubscription().verifyComplete();
+        
assertEquals("http://localhost:8090/%20/query?queryParam=Hello%2C%20%20World";, 
exchange.getAttributes().get(Constants.HTTP_URI).toString());
+        
+        request = MockServerHttpRequest
+                .get("localhost")
+                .remoteAddress(new InetSocketAddress(8090))
+                .queryParam("p", "a%2Bb=")
+                .build();
+        this.exchange = spy(MockServerWebExchange.from(request));
+        shenyuContext = mock(ShenyuContext.class);
+        exchange.getAttributes().put(Constants.CONTEXT, shenyuContext);
+        
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?p=a%252Bb%3D";, 
exchange.getAttributes().get(Constants.HTTP_URI).toString());
     }
 
     @Test

Reply via email to