From cacc5aefa214fc3eae05eb93d964863bb71198a0 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 30 Jan 2012 09:25:42 +1100 Subject: [PATCH] 370081: Handle null capture groups and query strings --- .../rewrite/handler/RewriteRegexRule.java | 37 ++++++++++++------- .../rewrite/handler/RewriteRegexRuleTest.java | 19 +++++++--- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.java index ce5700155f0..6c57e29bdf6 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.java @@ -23,10 +23,13 @@ import org.eclipse.jetty.server.Request; /** * Rewrite the URI by matching with a regular expression. * The replacement string may use $n" to replace the nth capture group. + * If the replacement string contains ? character, then it is split into a path + * and query string component. The returned target contains only the path. */ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI { private String _replacement; + private String _query; /* ------------------------------------------------------------ */ public RewriteRegexRule() @@ -43,7 +46,9 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI */ public void setReplacement(String replacement) { - _replacement = replacement; + String[] split=replacement.split("\\?",2); + _replacement = split[0]; + _query=split.length==2?split[1]:null; } @@ -54,32 +59,36 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI public String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException { target=_replacement; + String query=_query; for (int g=1;g<=matcher.groupCount();g++) { - String group = Matcher.quoteReplacement(matcher.group(g)); + String group=matcher.group(g); + if (group==null) + group=""; + else + group = Matcher.quoteReplacement(group); target=target.replaceAll("\\$"+g,group); + if (query!=null) + query=query.replaceAll("\\$"+g,group); } + if (query!=null) + request.setAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q",query); return target; } /* ------------------------------------------------------------ */ public void applyURI(Request request, String oldTarget, String newTarget) throws IOException { - Matcher matcher=_regex.matcher(request.getRequestURI()); - boolean matches = matcher.matches(); - if (matches) + request.setRequestURI(newTarget); + if (_query!=null) { - String uri=_replacement; - for (int g=1;g<=matcher.groupCount();g++) - { - String group = Matcher.quoteReplacement(matcher.group(g)); - uri=uri.replaceAll("\\$"+g,group); - } - request.setRequestURI(uri); + String query=(String)request.getAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q"); + if (request.getQueryString()==null) + request.setQueryString(query); + else + request.setQueryString(request.getQueryString()+"&"+query); } - else - request.setRequestURI(newTarget); } /* ------------------------------------------------------------ */ diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRuleTest.java index 35bb5f4ba0a..1464f67ab9c 100644 --- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRuleTest.java +++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRuleTest.java @@ -23,11 +23,13 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase { private String[][] _tests= { - {"/foo/bar",".*","/replace","/replace"}, - {"/foo/bar","/xxx.*","/replace",null}, - {"/foo/bar","/(.*)/(.*)","/$2/$1/xxx","/bar/foo/xxx"}, - {"/foo/$bar",".*","/$replace","/$replace"}, - {"/foo/$bar","/foo/(.*)","/$1/replace","/$bar/replace"}, + {"/foo/bar",".*","/replace","/replace",null}, + {"/foo/bar","/xxx.*","/replace",null,null}, + {"/foo/bar","/(.*)/(.*)","/$2/$1/xxx","/bar/foo/xxx",null}, + {"/foo/bar","/(foo)/(.*)(bar)","/$3/$1/xxx$2","/bar/foo/xxx",null}, + {"/foo/$bar",".*","/$replace","/$replace",null}, + {"/foo/$bar","/foo/(.*)","/$1/replace","/$bar/replace",null}, + {"/foo/bar/info","/foo/(NotHere)?([^/]*)/(.*)","/$3/other?p1=$2","/info/other","p1=bar"}, }; private RewriteRegexRule _rule; @@ -47,6 +49,13 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase _rule.setReplacement(test[2]); String result = _rule.matchAndApply(test[0], _request, _response); assertEquals(test[1], test[3], result); + + _request.setRequestURI(test[0]); + _request.setQueryString(null); + _rule.applyURI(_request,test[0],result); + + assertEquals(test[3], _request.getRequestURI()); + assertEquals(test[4], _request.getQueryString()); } } }