Skip to content

Commit

Permalink
fixes #2233 rollback the method overwritten rule to pattern matching …
Browse files Browse the repository at this point in the history
…from path prefix
  • Loading branch information
stevehu committed Apr 29, 2024
1 parent 797cc72 commit 5c736b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,9 @@ private HttpString createProxyRequestMethod(String target) {
if (LOG.isTraceEnabled()) LOG.trace("method rewrite rules size {}", this.methodRewriteRules.size());
for (var rule : this.methodRewriteRules) {
if(LOG.isTraceEnabled()) LOG.trace("rule sourceMethod {} targetMethod {} requestPath {}", rule.getSourceMethod(), rule.getTargetMethod(), rule.getRequestPath());
if (target.startsWith(rule.getRequestPath()) && m.toString().equals(rule.getSourceMethod())) {
if (StringUtils.matchPathToPattern(target, rule.getRequestPath()) && m.toString().equals(rule.getSourceMethod())) {
if (LOG.isDebugEnabled())
LOG.debug("Rewrite HTTP method from {} to {} with path {} and pathPrefix {}", rule.getSourceMethod(), rule.getTargetMethod(), target, rule.getRequestPath());
LOG.debug("Rewrite HTTP method from {} to {} with path {} and pathPattern {}", rule.getSourceMethod(), rule.getTargetMethod(), target, rule.getRequestPath());
m = new HttpString(rule.getTargetMethod());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.networknt.handler.config;

import com.networknt.utility.StringUtils;
import org.junit.Assert;
import org.junit.Test;


public class MethodRewriteRuleTest {

@Test
public void testMethodRewriteRule() {
MethodRewriteRule methodRewriteRule = new MethodRewriteRule("/gateway/sit/service-request/{tracing-no}", "PUT", "PATCH");
String requestPath = "/gateway/sit/service-request/123?version=1.1";
Assert.assertTrue(StringUtils.matchPathToPattern(requestPath, methodRewriteRule.getRequestPath()));
requestPath = "/gateway/sit/service-request?version=2";
Assert.assertFalse(StringUtils.matchPathToPattern(requestPath, methodRewriteRule.getRequestPath()));
}
}
3 changes: 0 additions & 3 deletions utility/src/main/java/com/networknt/utility/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1498,9 +1498,6 @@ public static String maskHalfString(String str) {
public static boolean matchPathToPattern(String requestPath, String endpointPattern) {
String[] pathPatternParts = endpointPattern.split("/");
String[] pathParts = requestPath.split("/");
if (pathPatternParts.length != pathParts.length) {
return false;
}

boolean isMatch = true;
for (int i = 0; i < pathPatternParts.length; i++) {
Expand Down

0 comments on commit 5c736b5

Please sign in to comment.