diff options
author | Andrew Azores <aazores@redhat.com> | 2013-06-18 15:57:01 -0400 |
---|---|---|
committer | Andrew Azores <aazores@redhat.com> | 2013-06-18 15:57:01 -0400 |
commit | 222acc6f3e99b2c473036ebc76f6370acf487e6d (patch) | |
tree | 3d09892e0ae31d1c73a6826324b57b13f288710a /tests/test-extensions/net | |
parent | faffea863331de3cb97e5654313922fafd61745c (diff) |
Extract URL to file logic in TinyHttpdImpl.java, with unit tests
Diffstat (limited to 'tests/test-extensions/net')
-rw-r--r-- | tests/test-extensions/net/sourceforge/jnlp/TinyHttpdImpl.java | 28 |
1 files changed, 24 insertions, 4 deletions
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 |