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/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new b910b8533 [ISSUE #4508]Tolerant Path Slash. fix #4508 (#4522)
b910b8533 is described below

commit b910b8533240805a3503c597318fd00191433dbc
Author: iwangjie <[email protected]>
AuthorDate: Thu Apr 6 12:20:26 2023 +0800

    [ISSUE #4508]Tolerant Path Slash. fix #4508 (#4522)
    
    * Tolerant Path Slash. fix #4508
    
    * add switch.
    
    * fix ci.
    
    ---------
    
    Co-authored-by: xiaoyu <[email protected]>
    Co-authored-by: likeguo <[email protected]>
---
 .../config/CollapseSlashesConfiguration.java       | 59 ++++++++++++++++++++++
 .../src/main/resources/application.yml             |  1 +
 .../apache/shenyu/common/config/ShenyuConfig.java  | 21 +++++++-
 3 files changed, 80 insertions(+), 1 deletion(-)

diff --git 
a/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/config/CollapseSlashesConfiguration.java
 
b/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/config/CollapseSlashesConfiguration.java
new file mode 100644
index 000000000..72607b2b8
--- /dev/null
+++ 
b/shenyu-bootstrap/src/main/java/org/apache/shenyu/bootstrap/config/CollapseSlashesConfiguration.java
@@ -0,0 +1,59 @@
+/*
+ * 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.bootstrap.config;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.server.reactive.ServerHttpRequest;
+import org.springframework.web.server.ServerWebExchange;
+import org.springframework.web.server.WebFilter;
+import org.springframework.web.server.WebFilterChain;
+
+import java.net.URI;
+
+/**
+ * Tolerant Path Slash.
+ */
+@Configuration
+public class CollapseSlashesConfiguration {
+
+    /**
+     * declaration collapseSlashesFilter.
+     * @return collapseSlashesFilter
+     */
+    @Bean
+    @ConditionalOnProperty(
+            value = "shenyu.switchConfig.collapseSlashes",
+            havingValue = "true"
+    )
+    public WebFilter collapseSlashesFilter() {
+        return (ServerWebExchange exchange, WebFilterChain chain) -> {
+            ServerHttpRequest request = exchange.getRequest();
+            String newPath = request.getURI().getRawPath().replaceAll("/{2,}", 
"/");
+            if (!request.getURI().getRawPath().equals(newPath)) {
+                URI newUri = request.getURI().resolve(newPath);
+                ServerHttpRequest newRequest = 
request.mutate().uri(newUri).build();
+                return 
chain.filter(exchange.mutate().request(newRequest).build());
+            }
+            return chain.filter(exchange);
+        };
+    }
+}
+
+
diff --git a/shenyu-bootstrap/src/main/resources/application.yml 
b/shenyu-bootstrap/src/main/resources/application.yml
index d667e4c10..176f0e408 100644
--- a/shenyu-bootstrap/src/main/resources/application.yml
+++ b/shenyu-bootstrap/src/main/resources/application.yml
@@ -186,6 +186,7 @@ shenyu:
 
   switchConfig:
     local: true
+    collapseSlashes: false
   file:
     enabled: true
     maxSize : 10
diff --git 
a/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java 
b/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
index 1bce57657..c9a9a4723 100644
--- 
a/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
+++ 
b/shenyu-common/src/main/java/org/apache/shenyu/common/config/ShenyuConfig.java
@@ -881,6 +881,8 @@ public class ShenyuConfig {
     public static class SwitchConfig {
         
         private boolean local = true;
+
+        private boolean collapseSlashes = true;
     
         /**
          * Gets local.
@@ -899,7 +901,24 @@ public class ShenyuConfig {
         public void setLocal(final boolean local) {
             this.local = local;
         }
-        
+
+        /**
+         * get collapseSlashes.
+         *
+         * @return collapseSlashes
+         */
+        public boolean getCollapseSlashes() {
+            return collapseSlashes;
+        }
+
+        /**
+         * set collapseSlashes.
+         *
+         * @param collapseSlashes collapseSlashes
+         */
+        public void setCollapseSlashes(final boolean collapseSlashes) {
+            this.collapseSlashes = collapseSlashes;
+        }
     }
     
     /**

Reply via email to