1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
Description: Properly decode URL paths
Author: Tamas Cservenak <tamas@cservenak.net>
Bug-Google: http://code.google.com/p/google-guice/issues/detail?id=745
Last-Update: 2014-04-15
diff --git a/extensions/servlet/src/com/google/inject/servlet/ServletDefinition.java b/extensions/servlet/src/com/google/inject/servlet/ServletDefinition.java
index 3b5aea8..11328ed 100644
--- a/extensions/servlet/src/com/google/inject/servlet/ServletDefinition.java
+++ b/extensions/servlet/src/com/google/inject/servlet/ServletDefinition.java
@@ -26,6 +26,8 @@ import com.google.inject.spi.ProviderInstanceBinding;
import com.google.inject.spi.ProviderWithExtensionVisitor;
import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
@@ -216,6 +218,12 @@ class ServletDefinition implements ProviderWithExtensionVisitor<ServletDefinitio
// then pathinfo is null.
if (pathInfo.isEmpty() && servletPathLength > 0) {
pathInfo = null;
+ } else {
+ try {
+ pathInfo = new URI(pathInfo).getPath();
+ } catch (URISyntaxException e) {
+ // ugh, just leave it alone then
+ }
}
} else {
pathInfo = null; // we know nothing additional about the URI.
diff --git a/extensions/servlet/test/com/google/inject/servlet/ServletDefinitionPathsTest.java b/extensions/servlet/test/com/google/inject/servlet/ServletDefinitionPathsTest.java
index 5509a6c..c89834f 100644
--- a/extensions/servlet/test/com/google/inject/servlet/ServletDefinitionPathsTest.java
+++ b/extensions/servlet/test/com/google/inject/servlet/ServletDefinitionPathsTest.java
@@ -133,6 +133,10 @@ public class ServletDefinitionPathsTest extends TestCase {
"/.../h.thing");
pathInfoWithServletStyleMatching("/path/my/h.thing", "/path", "*.thing", null, "/my/h.thing");
+ // Encoded URLs
+ pathInfoWithServletStyleMatching("/path/index%2B.html", "/path", "/*", "/index+.html", "");
+ pathInfoWithServletStyleMatching("/path/a%20file%20with%20spaces%20in%20name.html", "/path", "/*", "/a file with spaces in name.html", "");
+ pathInfoWithServletStyleMatching("/path/Tam%C3%A1s%20nem%20m%C3%A1s.html", "/path", "/*", "/Tamás nem más.html", "");
}
private void pathInfoWithServletStyleMatching(final String requestUri, final String contextPath,
@@ -228,6 +232,11 @@ public class ServletDefinitionPathsTest extends TestCase {
// path
pathInfoWithRegexMatching("/path/test.com/com.test.MyServletModule", "", "/path/[^/]+/(.*)",
"com.test.MyServletModule", "/path/test.com/com.test.MyServletModule");
+
+ // Encoded URLs
+ pathInfoWithRegexMatching("/path/index%2B.html", "/path", "/(.)*", "/index+.html", "");
+ pathInfoWithRegexMatching("/path/a%20file%20with%20spaces%20in%20name.html", "/path", "/(.)*", "/a file with spaces in name.html", "");
+ pathInfoWithRegexMatching("/path/Tam%C3%A1s%20nem%20m%C3%A1s.html", "/path", "/(.)*", "/Tamás nem más.html", "");
}
public final void pathInfoWithRegexMatching(final String requestUri, final String contextPath,
|