JETTY-1390 RewriteHandler handles encoded URIs
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@3435 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
parent
95ff9d73f0
commit
97291cc28a
|
@ -19,6 +19,7 @@ jetty-7.4.3-SNAPSHOT
|
|||
+ 350533 Add "Origin" to the list of allowed headers in CrossOriginFilter
|
||||
+ 350634 Cleanup FileResource construction
|
||||
+ JETTY-1342 Recreate selector in change task
|
||||
+ JETTY-1390 RewriteHandler handles encoded URIs
|
||||
|
||||
jetty-7.4.2.v20110526
|
||||
+ 334443 Improve the ability to specify extra class paths using the Jetty Maven Plugin
|
||||
|
|
|
@ -18,12 +18,13 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.http.PathMap;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.util.URIUtil;
|
||||
|
||||
/**
|
||||
* Rewrite the URI by replacing the matched {@link PathMap} path with a fixed string.
|
||||
*/
|
||||
public class RewritePatternRule extends PatternRule
|
||||
public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
|
||||
{
|
||||
private String _replacement;
|
||||
|
||||
|
@ -56,6 +57,13 @@ public class RewritePatternRule extends PatternRule
|
|||
return target;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
|
||||
{
|
||||
String uri = URIUtil.addPaths(_replacement, PathMap.pathInfo(_pattern,request.getRequestURI()));
|
||||
request.setRequestURI(uri);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Returns the replacement string.
|
||||
|
|
|
@ -18,11 +18,13 @@ import java.util.regex.Matcher;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
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.
|
||||
*/
|
||||
public class RewriteRegexRule extends RegexRule
|
||||
public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
|
||||
{
|
||||
private String _replacement;
|
||||
|
||||
|
@ -61,6 +63,25 @@ public class RewriteRegexRule extends RegexRule
|
|||
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)
|
||||
{
|
||||
String uri=_replacement;
|
||||
for (int g=1;g<=matcher.groupCount();g++)
|
||||
{
|
||||
String group = matcher.group(g);
|
||||
uri=uri.replaceAll("\\$"+g,group);
|
||||
}
|
||||
request.setRequestURI(uri);
|
||||
}
|
||||
else
|
||||
request.setRequestURI(newTarget);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Returns the replacement string.
|
||||
|
|
|
@ -17,11 +17,21 @@ import java.io.IOException;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.server.Request;
|
||||
|
||||
/**
|
||||
* An abstract rule for creating rewrite rules.
|
||||
*/
|
||||
public abstract class Rule
|
||||
{
|
||||
/**
|
||||
* Interface used to apply a changed target if {@link RuleContainer#setRewriteRequestURI(boolean)} is true.
|
||||
*/
|
||||
public interface ApplyURI
|
||||
{
|
||||
void applyURI(Request request, String oldTarget, String newTarget) throws IOException;
|
||||
}
|
||||
|
||||
protected boolean _terminating;
|
||||
protected boolean _handling;
|
||||
|
||||
|
|
|
@ -203,9 +203,14 @@ public class RuleContainer extends Rule
|
|||
original_set=true;
|
||||
request.setAttribute(_originalPathAttribute, target);
|
||||
}
|
||||
|
||||
|
||||
if (_rewriteRequestURI)
|
||||
((Request)request).setRequestURI(applied);
|
||||
{
|
||||
if (rule instanceof Rule.ApplyURI && !target.equals(request.getRequestURI()))
|
||||
((Rule.ApplyURI)rule).applyURI((Request)request, target, applied);
|
||||
else
|
||||
((Request)request).setRequestURI(applied);
|
||||
}
|
||||
|
||||
if (_rewritePathInfo)
|
||||
((Request)request).setPathInfo(applied);
|
||||
|
|
|
@ -31,6 +31,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
|
|||
private RewritePatternRule _rule1;
|
||||
private RewritePatternRule _rule2;
|
||||
private RewritePatternRule _rule3;
|
||||
private RewriteRegexRule _rule4;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception
|
||||
|
@ -58,8 +59,11 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
|
|||
_rule3 = new RewritePatternRule();
|
||||
_rule3.setPattern("/ccc/*");
|
||||
_rule3.setReplacement("/ddd");
|
||||
_rule4 = new RewriteRegexRule();
|
||||
_rule4.setRegex("/xxx/(.*)");
|
||||
_rule4.setReplacement("/$1/zzz");
|
||||
|
||||
_handler.setRules(new Rule[]{_rule1,_rule2,_rule3});
|
||||
_handler.setRules(new Rule[]{_rule1,_rule2,_rule3,_rule4});
|
||||
|
||||
start(false);
|
||||
}
|
||||
|
@ -136,4 +140,38 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
|
|||
assertEquals("/aaa/bar",_request.getAttribute("before"));
|
||||
assertTrue(_request.isHandled());
|
||||
}
|
||||
|
||||
|
||||
public void testEncodedPattern() throws Exception
|
||||
{
|
||||
_response.setStatus(200);
|
||||
_request.setHandled(false);
|
||||
_handler.setOriginalPathAttribute("/before");
|
||||
_handler.setRewriteRequestURI(true);
|
||||
_handler.setRewritePathInfo(false);
|
||||
_request.setRequestURI("/ccc/x%2Fy");
|
||||
_request.setPathInfo("/ccc/x/y");
|
||||
_handler.handle("/ccc/x/y",_request,_request, _response);
|
||||
assertEquals(201,_response.getStatus());
|
||||
assertEquals("/ddd/x/y",_request.getAttribute("target"));
|
||||
assertEquals("/ddd/x%2Fy",_request.getAttribute("URI"));
|
||||
assertEquals("/ccc/x/y",_request.getAttribute("info"));
|
||||
|
||||
}
|
||||
public void testEncodedRegex() throws Exception
|
||||
{
|
||||
_response.setStatus(200);
|
||||
_request.setHandled(false);
|
||||
_handler.setOriginalPathAttribute("/before");
|
||||
_handler.setRewriteRequestURI(true);
|
||||
_handler.setRewritePathInfo(false);
|
||||
_request.setRequestURI("/xxx/x%2Fy");
|
||||
_request.setPathInfo("/xxx/x/y");
|
||||
_handler.handle("/xxx/x/y",_request,_request, _response);
|
||||
assertEquals(201,_response.getStatus());
|
||||
assertEquals("/x/y/zzz",_request.getAttribute("target"));
|
||||
assertEquals("/x%2Fy/zzz",_request.getAttribute("URI"));
|
||||
assertEquals("/xxx/x/y",_request.getAttribute("info"));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue