/hg/icedtea-web: Extract URL to file logic in TinyHttpdImpl.java...

adomurad at icedtea.classpath.org adomurad at icedtea.classpath.org
Tue Jun 18 12:57:26 PDT 2013


changeset fa6a80c73e0d in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=fa6a80c73e0d
author: Andrew Azores <aazores at redhat.com>
date: Tue Jun 18 15:57:01 2013 -0400

	Extract URL to file logic in TinyHttpdImpl.java, with unit tests


diffstat:

 ChangeLog                                                              |   7 +
 tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java |  38 ++++++++++
 tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java          |  28 ++++++-
 3 files changed, 69 insertions(+), 4 deletions(-)

diffs (121 lines):

diff -r 1a327a09262e -r fa6a80c73e0d ChangeLog
--- a/ChangeLog	Mon Jun 10 13:22:53 2013 +0200
+++ b/ChangeLog	Tue Jun 18 15:57:01 2013 -0400
@@ -1,3 +1,10 @@
+2013-06-18  Andrew Azores  <aazores at redhat.com>
+
+	* tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java:
+	extracted some lines out of run() into new method urlToFilePath()
+	* tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java:
+	unit tests added for new urlToFilePath()
+
 2013-06-06  Jiri Vanek  <jvanek at redhat.com>
         Andrew Azores  <aazores at redhat.com>
 
diff -r 1a327a09262e -r fa6a80c73e0d tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java
--- a/tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java	Mon Jun 10 13:22:53 2013 +0200
+++ b/tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java	Tue Jun 18 15:57:01 2013 -0400
@@ -39,6 +39,8 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.net.URL;
+import java.net.URLDecoder;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -217,6 +219,42 @@
         Assert.assertArrayEquals(b2, bb[1]);
         Assert.assertArrayEquals(b3, bb[2]);
     }
+
+    private static final String[] filePathTestUrls = {
+            "/foo.html",
+            "/foo/",
+            "/foo/bar.jar",
+            "/foo/bar.jar;path_param",
+            "/foo/bar.jar%3Bpath_param",
+            "/foo/bar?query=string&red=hat"
+    };
+
+    @Test
+    public void urlToFilePathTest() throws Exception {
+        for (String url : filePathTestUrls) {
+            String newUrl = TinyHttpdImpl.urlToFilePath(url);
+
+            Assert.assertFalse("File path should not contain query string: " + newUrl, newUrl.contains("?"));
+            Assert.assertTrue("File path should be relative: " + newUrl, newUrl.startsWith("./"));
+            Assert.assertFalse("File path should not contain \"/XslowX\":" + newUrl,
+                    newUrl.toLowerCase().contains("/XslowX".toLowerCase()));
+
+            if (url.endsWith("/")) {
+                Assert.assertTrue(newUrl.endsWith("/index.html"));
+            }
+        }
+    }
+
+    @Test
+    public void urlToFilePathUrlDecodeTest() throws Exception {
+        // This test may fail with strange original URLs, eg those containing the substring "%253B",
+        // which can be decoded into "%3B", then decoded again into ';'.
+
+        for (String url : filePathTestUrls) {
+            String newUrl = TinyHttpdImpl.urlToFilePath(url);
+            Assert.assertEquals(newUrl, URLDecoder.decode(newUrl, "UTF-8"));
+        }
+    }
     
     @Test
     public void stripHttpPathParamTest() {
diff -r 1a327a09262e -r fa6a80c73e0d tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java
--- a/tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java	Mon Jun 10 13:22:53 2013 +0200
+++ b/tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java	Tue Jun 18 15:57:01 2013 -0400
@@ -42,6 +42,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
 import java.net.Socket;
 import java.net.SocketException;
@@ -124,10 +125,7 @@
                             p = p.replace(XSX, "/");
                         }
                         ServerAccess.logNoReprint("Getting: " + p);
-                        p = URLDecoder.decode(p, "UTF-8");
-                        p = p.replaceAll("\\?.*", "");
-                        p = (".".concat((p.endsWith("/")) ? p.concat("index.html") : p)).replace('/', File.separatorChar);
-                        p = stripHttpPathParams(p);
+                        p = urlToFilePath(p);
                         ServerAccess.logNoReprint("Serving: " + p);
                         File pp = new File(dir, p);
                         int l = (int) pp.length();
@@ -206,6 +204,28 @@
     }
     
     /**
+    * This function transforms a request URL into a path to a file which the server
+    * will return to the requester.
+    * @param url - the request URL
+    * @return a String representation of the local path to the file
+    * @throws UnsupportedEncodingException
+    */
+    public static String urlToFilePath(String url) throws UnsupportedEncodingException {
+        url = URLDecoder.decode(url, "UTF-8"); // Decode URL encoded charaters, eg "%3B" b    ecomes ';'
+        if (url.startsWith(XSX)) {
+            url = url.replace(XSX, "/");
+        }
+        url = url.replaceAll("\\?.*", ""); // Remove query string from URL
+        url = ".".concat(url); // Change path into relative path
+        if (url.endsWith("/")) {
+            url += "index.html";
+        }
+        url = url.replace('/', File.separatorChar); // If running on Windows, replace '/'     in path with "\\"
+        url = stripHttpPathParams(url);
+        return url;
+    }
+
+    /**
      * This function removes the HTTP Path Parameter from a given JAR URL, assuming that the
      * path param delimiter is a semicolon
      * @param url - the URL from which to remove the path parameter



More information about the distro-pkg-dev mailing list