Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Wilkins2014-03-20 00:32:01 +0000
committerGreg Wilkins2014-03-20 00:32:01 +0000
commit0d876de7595498a9b7da9da71e1d0dfbb41e0dd8 (patch)
treefc43c722e612686eefed60dbf68fe618fb262010 /jetty-rewrite
parentc251d5789d9969e5b49cd41351f0de7f432417be (diff)
downloadorg.eclipse.jetty.project-0d876de7595498a9b7da9da71e1d0dfbb41e0dd8.tar.gz
org.eclipse.jetty.project-0d876de7595498a9b7da9da71e1d0dfbb41e0dd8.tar.xz
org.eclipse.jetty.project-0d876de7595498a9b7da9da71e1d0dfbb41e0dd8.zip
414206 Rewrite rules re-encode requestURI
Diffstat (limited to 'jetty-rewrite')
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/CompactPathRule.java2
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewritePatternRule.java13
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.java8
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/Rule.java2
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RuleContainer.java7
-rw-r--r--jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteHandlerTest.java28
-rw-r--r--jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewritePatternRuleTest.java1
-rw-r--r--jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRuleTest.java6
8 files changed, 38 insertions, 29 deletions
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/CompactPathRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/CompactPathRule.java
index fbf86e61be..13f2f0925b 100644
--- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/CompactPathRule.java
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/CompactPathRule.java
@@ -38,7 +38,7 @@ public class CompactPathRule extends Rule implements Rule.ApplyURI
}
@Override
- public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
+ public void applyURI(Request request, String oldURI, String newURI) throws IOException
{
String uri = request.getRequestURI();
if (uri.startsWith("/"))
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewritePatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewritePatternRule.java
index a13c7e4772..eca9da6766 100644
--- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewritePatternRule.java
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewritePatternRule.java
@@ -71,7 +71,6 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
}
/* ------------------------------------------------------------ */
-
/**
* This method will add _query to the requests's queryString and also combine it with existing queryStrings in
* the request. However it won't take care for duplicate. E.g. if request.getQueryString contains a parameter
@@ -80,16 +79,16 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
* cases.
*
* @param request
- * @param oldTarget
- * @param newTarget
+ * @param oldURI
+ * @param newURI
* @throws IOException
*/
@Override
- public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
+ public void applyURI(Request request, String oldURI, String newURI) throws IOException
{
if (_query == null)
{
- request.setRequestURI(newTarget);
+ request.setRequestURI(newURI);
}
else
{
@@ -98,9 +97,9 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI
queryString = queryString + "&" + _query;
else
queryString = _query;
- HttpURI uri = new HttpURI(newTarget + "?" + queryString);
+ HttpURI uri = new HttpURI(newURI + "?" + queryString);
request.setUri(uri);
- request.setRequestURI(newTarget);
+ request.setRequestURI(newURI);
request.setQueryString(queryString);
}
}
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 ee83f1fad0..f9fd8a347d 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
@@ -96,11 +96,11 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
/* ------------------------------------------------------------ */
@Override
- public void applyURI(Request request, String oldTarget, String newTarget) throws IOException
+ public void applyURI(Request request, String oldURI, String newURI) throws IOException
{
if (_query==null)
{
- request.setRequestURI(newTarget);
+ request.setRequestURI(newURI);
}
else
{
@@ -108,9 +108,9 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI
if (!_queryGroup && request.getQueryString()!=null)
query=request.getQueryString()+"&"+query;
- HttpURI uri=new HttpURI(newTarget+"?"+query);
+ HttpURI uri=new HttpURI(newURI+"?"+query);
request.setUri(uri);
- request.setRequestURI(newTarget);
+ request.setRequestURI(newURI);
request.setQueryString(query);
}
}
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/Rule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/Rule.java
index 5f912b92ce..c0a55e32be 100644
--- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/Rule.java
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/Rule.java
@@ -35,7 +35,7 @@ public abstract class Rule
*/
public interface ApplyURI
{
- void applyURI(Request request, String oldTarget, String newTarget) throws IOException;
+ void applyURI(Request request, String oldURI, String newURI) throws IOException;
}
protected boolean _terminating;
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RuleContainer.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RuleContainer.java
index 88ec9dac10..eea013eee3 100644
--- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RuleContainer.java
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RuleContainer.java
@@ -26,6 +26,8 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.ArrayUtil;
+import org.eclipse.jetty.util.URIUtil;
+import org.eclipse.jetty.util.UrlEncoded;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@@ -211,10 +213,11 @@ public class RuleContainer extends Rule
if (_rewriteRequestURI)
{
+ String encoded=URIUtil.encodePath(applied);
if (rule instanceof Rule.ApplyURI)
- ((Rule.ApplyURI)rule).applyURI((Request)request, target, applied);
+ ((Rule.ApplyURI)rule).applyURI((Request)request,((Request)request).getRequestURI(), encoded);
else
- ((Request)request).setRequestURI(applied);
+ ((Request)request).setRequestURI(encoded);
}
if (_rewritePathInfo)
diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteHandlerTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteHandlerTest.java
index 9adfd15805..907df35143 100644
--- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteHandlerTest.java
+++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteHandlerTest.java
@@ -166,6 +166,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
}
+ @Test
public void testEncodedPattern() throws Exception
{
_response.setStatus(200);
@@ -173,15 +174,18 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
_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);
+ _request.setRequestURI("/ccc/x%20y");
+ _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"));
+ assertEquals("/ddd/x y",_request.getAttribute("target"));
+ assertEquals("/ddd/x%20y",_request.getAttribute("URI"));
+ assertEquals("/ccc/x y",_request.getAttribute("info"));
}
+
+
+ @Test
public void testEncodedRegex() throws Exception
{
_response.setStatus(200);
@@ -189,13 +193,13 @@ public class RewriteHandlerTest extends AbstractRuleTestCase
_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);
+ _request.setRequestURI("/xxx/x%20y");
+ _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"));
+ assertEquals("/x y/zzz",_request.getAttribute("target"));
+ assertEquals("/x%20y/zzz",_request.getAttribute("URI"));
+ assertEquals("/xxx/x y",_request.getAttribute("info"));
}
}
diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewritePatternRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewritePatternRuleTest.java
index 05509a16f1..b3bbcc98bd 100644
--- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewritePatternRuleTest.java
+++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewritePatternRuleTest.java
@@ -36,6 +36,7 @@ public class RewritePatternRuleTest extends AbstractRuleTestCase
{"/foo/bar", "/foo/*", "/replace/bar"},
{"/foo/bar", "/foo/bar", "/replace"},
{"/foo/bar.txt", "*.txt", "/replace"},
+ {"/foo/bar/%20x", "/foo/*", "/replace/bar/%20x"},
};
private RewritePatternRule _rule;
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 34c4c231b0..f2be9ed41c 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
@@ -25,6 +25,7 @@ import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.util.MultiMap;
+import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.UrlEncoded;
import org.junit.Before;
import org.junit.Test;
@@ -37,6 +38,7 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase
{"/foo1/bar","n=v",".*","/replace","/replace","n=v"},
{"/foo2/bar",null,"/xxx.*","/replace",null,null},
{"/foo3/bar",null,"/(.*)/(.*)","/$2/$1/xxx","/bar/foo3/xxx",null},
+ {"/f%20o3/bar",null,"/(.*)/(.*)","/$2/$1/xxx","/bar/f%20o3/xxx",null},
{"/foo4/bar",null,"/(.*)/(.*)","/test?p2=$2&p1=$1","/test","p2=bar&p1=foo4"},
{"/foo5/bar","n=v","/(.*)/(.*)","/test?p2=$2&p1=$1","/test","n=v&p2=bar&p1=foo5"},
{"/foo6/bar",null,"/(.*)/(.*)","/foo6/bar?p2=$2&p1=$1","/foo6/bar","p2=bar&p1=foo6"},
@@ -112,8 +114,8 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase
_request.setQueryString(test[1]);
_request.getAttributes().clearAttributes();
- String result = container.apply(test[0],_request,_response);
- assertEquals(t,test[4]==null?test[0]:test[4], result);
+ String result = container.apply(URIUtil.decodePath(test[0]),_request,_response);
+ assertEquals(t,URIUtil.decodePath(test[4]==null?test[0]:test[4]), result);
assertEquals(t,test[4]==null?test[0]:test[4], _request.getRequestURI());
assertEquals(t,test[5], _request.getQueryString());
}

Back to the top