added $Q handling to RewriteReqexRule
This commit is contained in:
parent
d3b2a3ee0c
commit
67ce45928c
|
@ -24,12 +24,15 @@ 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.
|
||||
* and query string component. The replacement query string may also contain $Q, which
|
||||
* is replaced with the original query string.
|
||||
* The returned target contains only the path.
|
||||
*/
|
||||
public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
|
||||
{
|
||||
private String _replacement;
|
||||
private String _query;
|
||||
private boolean _queryGroup;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public RewriteRegexRule()
|
||||
|
@ -49,6 +52,7 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
|
|||
String[] split=replacement.split("\\?",2);
|
||||
_replacement = split[0];
|
||||
_query=split.length==2?split[1]:null;
|
||||
_queryGroup=_query!=null && _query.contains("$Q");
|
||||
}
|
||||
|
||||
|
||||
|
@ -73,7 +77,12 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
|
|||
}
|
||||
|
||||
if (query!=null)
|
||||
{
|
||||
if (_queryGroup)
|
||||
query=query.replace("$Q",request.getQueryString()==null?"":request.getQueryString());
|
||||
request.setAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q",query);
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
|
@ -84,7 +93,7 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
|
|||
if (_query!=null)
|
||||
{
|
||||
String query=(String)request.getAttribute("org.eclipse.jetty.rewrite.handler.RewriteRegexRule.Q");
|
||||
if (request.getQueryString()==null)
|
||||
if (_queryGroup||request.getQueryString()==null)
|
||||
request.setQueryString(query);
|
||||
else
|
||||
request.setQueryString(request.getQueryString()+"&"+query);
|
||||
|
|
|
@ -23,13 +23,19 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase
|
|||
{
|
||||
private String[][] _tests=
|
||||
{
|
||||
{"/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"},
|
||||
{"/foo/bar",null,".*","/replace","/replace",null},
|
||||
{"/foo/bar","n=v",".*","/replace","/replace","n=v"},
|
||||
{"/foo/bar",null,"/xxx.*","/replace",null,null},
|
||||
{"/foo/bar",null,"/(.*)/(.*)","/$2/$1/xxx","/bar/foo/xxx",null},
|
||||
{"/foo/bar",null,"/(.*)/(.*)","/test?p2=$2&p1=$1","/test","p2=bar&p1=foo"},
|
||||
{"/foo/bar","n=v","/(.*)/(.*)","/test?p2=$2&p1=$1","/test","n=v&p2=bar&p1=foo"},
|
||||
{"/foo/bar",null,"/(foo)/(.*)(bar)","/$3/$1/xxx$2","/bar/foo/xxx",null},
|
||||
{"/foo/$bar",null,".*","/$replace","/$replace",null},
|
||||
{"/foo/$bar",null,"/foo/(.*)","/$1/replace","/$bar/replace",null},
|
||||
{"/foo/bar/info",null,"/foo/(NotHere)?([^/]*)/(.*)","/$3/other?p1=$2","/info/other","p1=bar"},
|
||||
{"/foo/bar/info",null,"/foo/(NotHere)?([^/]*)/(.*)","/$3/other?p1=$2&$Q","/info/other","p1=bar&"},
|
||||
{"/foo/bar/info","n=v","/foo/(NotHere)?([^/]*)/(.*)","/$3/other?p1=$2&$Q","/info/other","p1=bar&n=v"},
|
||||
{"/foo/bar/info","n=v","/foo/(NotHere)?([^/]*)/(.*)","/$3/other?p1=$2","/info/other","n=v&p1=bar"},
|
||||
};
|
||||
private RewriteRegexRule _rule;
|
||||
|
||||
|
@ -45,17 +51,20 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase
|
|||
{
|
||||
for (String[] test : _tests)
|
||||
{
|
||||
_rule.setRegex(test[1]);
|
||||
_rule.setReplacement(test[2]);
|
||||
String result = _rule.matchAndApply(test[0], _request, _response);
|
||||
assertEquals(test[1], test[3], result);
|
||||
|
||||
String t=test[0]+"?"+test[1]+">"+test[2]+"|"+test[3];
|
||||
_rule.setRegex(test[2]);
|
||||
_rule.setReplacement(test[3]);
|
||||
|
||||
_request.setRequestURI(test[0]);
|
||||
_request.setQueryString(null);
|
||||
_request.setQueryString(test[1]);
|
||||
_request.getAttributes().clearAttributes();
|
||||
|
||||
String result = _rule.matchAndApply(test[0], _request, _response);
|
||||
assertEquals(t, test[4], result);
|
||||
_rule.applyURI(_request,test[0],result);
|
||||
|
||||
assertEquals(test[3], _request.getRequestURI());
|
||||
assertEquals(test[4], _request.getQueryString());
|
||||
assertEquals(t,test[4], _request.getRequestURI());
|
||||
assertEquals(t,test[5], _request.getQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue