370081: Handle null capture groups and query strings

This commit is contained in:
Greg Wilkins 2012-01-30 09:25:42 +11:00
parent a27a518bf6
commit cacc5aefa2
2 changed files with 37 additions and 19 deletions

View File

@ -23,10 +23,13 @@ import org.eclipse.jetty.server.Request;
/** /**
* Rewrite the URI by matching with a regular expression. * Rewrite the URI by matching with a regular expression.
* The replacement string may use $n" to replace the nth capture group. * 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 public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
{ {
private String _replacement; private String _replacement;
private String _query;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public RewriteRegexRule() public RewriteRegexRule()
@ -43,7 +46,9 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
*/ */
public void setReplacement(String replacement) 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 public String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException
{ {
target=_replacement; target=_replacement;
String query=_query;
for (int g=1;g<=matcher.groupCount();g++) 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); 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; return target;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public void applyURI(Request request, String oldTarget, String newTarget) throws IOException public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
{ {
Matcher matcher=_regex.matcher(request.getRequestURI()); request.setRequestURI(newTarget);
boolean matches = matcher.matches(); if (_query!=null)
if (matches)
{ {
String uri=_replacement; String query=(String)request.getAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q");
for (int g=1;g<=matcher.groupCount();g++) if (request.getQueryString()==null)
{ request.setQueryString(query);
String group = Matcher.quoteReplacement(matcher.group(g)); else
uri=uri.replaceAll("\\$"+g,group); request.setQueryString(request.getQueryString()+"&"+query);
}
request.setRequestURI(uri);
} }
else
request.setRequestURI(newTarget);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */

View File

@ -23,11 +23,13 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase
{ {
private String[][] _tests= private String[][] _tests=
{ {
{"/foo/bar",".*","/replace","/replace"}, {"/foo/bar",".*","/replace","/replace",null},
{"/foo/bar","/xxx.*","/replace",null}, {"/foo/bar","/xxx.*","/replace",null,null},
{"/foo/bar","/(.*)/(.*)","/$2/$1/xxx","/bar/foo/xxx"}, {"/foo/bar","/(.*)/(.*)","/$2/$1/xxx","/bar/foo/xxx",null},
{"/foo/$bar",".*","/$replace","/$replace"}, {"/foo/bar","/(foo)/(.*)(bar)","/$3/$1/xxx$2","/bar/foo/xxx",null},
{"/foo/$bar","/foo/(.*)","/$1/replace","/$bar/replace"}, {"/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; private RewriteRegexRule _rule;
@ -47,6 +49,13 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase
_rule.setReplacement(test[2]); _rule.setReplacement(test[2]);
String result = _rule.matchAndApply(test[0], _request, _response); String result = _rule.matchAndApply(test[0], _request, _response);
assertEquals(test[1], test[3], result); 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());
} }
} }
} }