Author: markt
Date: Thu Dec 11 23:18:47 2014
New Revision: 1644790
URL: http://svn.apache.org/r1644790
Log:
Ensure that the return value for request.getRequestURI() is in the expected
non-URL-encoded form when using the RewriteValve
Modified:
tomcat/trunk/java/org/apache/catalina/connector/CoyoteAdapter.java
tomcat/trunk/java/org/apache/catalina/valves/rewrite/RewriteValve.java
tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
Modified: tomcat/trunk/java/org/apache/catalina/connector/CoyoteAdapter.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/connector/CoyoteAdapter.java?rev=1644790&r1=1644789&r2=1644790&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/connector/CoyoteAdapter.java
(original)
+++ tomcat/trunk/java/org/apache/catalina/connector/CoyoteAdapter.java Thu Dec
11 23:18:47 2014
@@ -605,11 +605,12 @@ public class CoyoteAdapter implements Ad
return false;
}
- // Copy the raw URI to the decodedURI
MessageBytes decodedURI = req.decodedURI();
- decodedURI.duplicate(undecodedURI);
if (undecodedURI.getType() == MessageBytes.T_BYTES) {
+ // Copy the raw URI to the decodedURI
+ decodedURI.duplicate(undecodedURI);
+
// Parse the path parameters. This will:
// - strip out the path parameters
// - convert the decodedURI to bytes
@@ -645,9 +646,13 @@ public class CoyoteAdapter implements Ad
return false;
}
} else {
- // The URL is chars or String, and has been sent using an in-memory
- // protocol handler, we have to assume the URL has been properly
- // decoded already
+ /* The URI is chars or String, and has been sent using an in-memory
+ * protocol handler. The following assumptions are made:
+ * - req.requestURI() has been set to the 'original' non-decoded,
+ * non-normalized URI
+ * - req.decodedURI() has been set to the decoded, normalized form
+ * of req.requestURI()
+ */
decodedURI.toChars();
// Remove all path parameters; any needed path parameter should be
set
// using the request object rather than passing it in the URL
Modified: tomcat/trunk/java/org/apache/catalina/valves/rewrite/RewriteValve.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/rewrite/RewriteValve.java?rev=1644790&r1=1644789&r2=1644790&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/valves/rewrite/RewriteValve.java
(original)
+++ tomcat/trunk/java/org/apache/catalina/valves/rewrite/RewriteValve.java Thu
Dec 11 23:18:47 2014
@@ -44,6 +44,7 @@ import org.apache.catalina.LifecycleList
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.util.LifecycleSupport;
+import org.apache.catalina.util.URLEncoder;
import org.apache.catalina.valves.ValveBase;
import org.apache.tomcat.util.buf.CharChunk;
import org.apache.tomcat.util.buf.MessageBytes;
@@ -443,7 +444,7 @@ public class RewriteValve extends ValveB
queryString = urlString.substring(queryIndex+1);
urlString = urlString.substring(0, queryIndex);
}
- // Set the new URL
+ // Set the new 'original' URI
String contextPath = null;
if (context) {
contextPath = request.getContextPath();
@@ -454,8 +455,17 @@ public class RewriteValve extends ValveB
if (context) {
chunk.append(contextPath);
}
- chunk.append(urlString);
+ chunk.append(URLEncoder.DEFAULT.encode(urlString));
request.getCoyoteRequest().requestURI().toChars();
+ // Decoded URI
+ request.getCoyoteRequest().decodedURI().setString(null);
+ chunk =
request.getCoyoteRequest().decodedURI().getCharChunk();
+ chunk.recycle();
+ if (context) {
+ chunk.append(contextPath);
+ }
+ chunk.append(urlString);
+ request.getCoyoteRequest().decodedURI().toChars();
// Set the new Query if there is one
if (queryString != null) {
request.getCoyoteRequest().queryString().setString(null);
Modified:
tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java?rev=1644790&r1=1644789&r2=1644790&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
(original)
+++ tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
Thu Dec 11 23:18:47 2014
@@ -33,13 +33,11 @@ public class TestRewriteValve extends To
}
@Test
- @Ignore // getRequestURI is not encoded
public void testNoopRewrite() throws Exception {
doTestRewrite("RewriteRule ^(.*) $1", "/a/%255A", "/a/%255A");
}
@Test
- @Ignore // getRequestURI is not encoded
public void testPathRewrite() throws Exception {
doTestRewrite("RewriteRule ^/b(.*) /a$1", "/b/%255A", "/a/%255A");
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]