diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java | 38 | ||||
-rw-r--r-- | tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java | 28 |
3 files changed, 69 insertions, 4 deletions
@@ -1,3 +1,10 @@ +2013-06-18 Andrew Azores <[email protected]> + + * 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 <[email protected]> Andrew Azores <[email protected]> diff --git a/tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java b/tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java index 3ddd09e..1b93e3e 100644 --- a/tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java +++ b/tests/test-extensions-tests/net/sourceforge/jnlp/ServerAccessTest.java @@ -39,6 +39,8 @@ package net.sourceforge.jnlp; 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 @@ public class ServerAccessTest { 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 --git a/tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java b/tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java index 4ef6450..c0a00d0 100644 --- a/tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java +++ b/tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java @@ -42,6 +42,7 @@ import java.io.DataOutputStream; 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 @@ public class TinyHttpdImpl extends Thread { 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 @@ public class TinyHttpdImpl extends Thread { } /** + * 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 |