Kaynağa Gözat

YARN-1728. Workaround guice3x-undecoded pathInfo in YARN WebApp. (Yuanbo Liu via gera)

(cherry picked from commit df35ba81fe26f526a1534b72089fbb310efccdd9)
Gera Shegalov 8 yıl önce
ebeveyn
işleme
c8b1112ed9

+ 11 - 0
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/Dispatcher.java

@@ -21,6 +21,8 @@ package org.apache.hadoop.yarn.webapp;
 import static com.google.common.base.Preconditions.checkState;
 
 import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Timer;
 import java.util.TimerTask;
 
@@ -115,6 +117,15 @@ public class Dispatcher extends HttpServlet {
     if (pathInfo == null) {
       pathInfo = "/";
     }
+    // The implementation class of HttpServletRequest in
+    // Guice-3.0 does not decode paths that are encoded,
+    // decode path info here for further operation.
+    try {
+      pathInfo = new URI(pathInfo).getPath();
+    }  catch (URISyntaxException ex) {
+      // Just leave it alone for compatibility.
+      LOG.error(pathInfo + ": Failed to decode path.", ex);
+    }
     Controller.RequestContext rc =
         injector.getInstance(Controller.RequestContext.class);
     if (setCookieParams(rc, req) > 0) {

+ 30 - 0
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/webapp/TestWebApp.java

@@ -33,6 +33,7 @@ import static org.junit.Assert.assertTrue;
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.net.URLEncoder;
 
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.hadoop.yarn.MockApps;
@@ -260,6 +261,35 @@ public class TestWebApp {
     }
   }
 
+  @Test public void testEncodedUrl() throws Exception {
+    WebApp app =
+        WebApps.$for("test", TestWebApp.class, this, "ws").start(new WebApp() {
+          @Override
+          public void setup() {
+            bind(MyTestJAXBContextResolver.class);
+            bind(MyTestWebService.class);
+
+            route("/:foo", FooController.class);
+          }
+        });
+    String baseUrl = baseUrl(app);
+
+    try {
+      // Test encoded url
+      String rawPath = "localhost:8080";
+      String encodedUrl = baseUrl + "test/" +
+          URLEncoder.encode(rawPath, "UTF-8");
+      assertEquals("foo" + rawPath, getContent(encodedUrl).trim());
+
+      rawPath = "@;%$";
+      encodedUrl = baseUrl + "test/" +
+          URLEncoder.encode(rawPath, "UTF-8");
+      assertEquals("foo" + rawPath, getContent(encodedUrl).trim());
+    } finally {
+      app.stop();
+    }
+  }
+
   // This is to test the GuiceFilter should only be applied to webAppContext,
   // not to staticContext  and logContext;
   @Test public void testYARNWebAppContext() throws Exception {