/hg/icedtea-web: 2 new changesets

adomurad at icedtea.classpath.org adomurad at icedtea.classpath.org
Tue Apr 23 10:58:22 PDT 2013


changeset 6b680fe9c390 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=6b680fe9c390
author: Adam Domurad <adomurad at redhat.com>
date: Tue Apr 23 13:55:23 2013 -0400

	Introduce more UrlUtils functions.


changeset b912e91204b1 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=b912e91204b1
author: Adam Domurad <adomurad at redhat.com>
date: Tue Apr 23 13:59:20 2013 -0400

	Add tests for newly added UrlUtils functions


diffstat:

 ChangeLog                                                                                      |  17 ++
 netx/net/sourceforge/jnlp/cache/CacheUtil.java                                                 |  29 ++-
 netx/net/sourceforge/jnlp/cache/ResourceTracker.java                                           |  38 +----
 netx/net/sourceforge/jnlp/security/appletextendedsecurity/UnsignedAppletTrustConfirmation.java |   8 +-
 netx/net/sourceforge/jnlp/util/UrlUtils.java                                                   |  75 +++++++++-
 tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java                            |   8 +-
 tests/netx/unit/net/sourceforge/jnlp/util/UrlUtilsTest.java                                    |  38 ++++-
 7 files changed, 156 insertions(+), 57 deletions(-)

diffs (358 lines):

diff -r 9d17ddb0003f -r b912e91204b1 ChangeLog
--- a/ChangeLog	Tue Apr 23 13:47:42 2013 -0400
+++ b/ChangeLog	Tue Apr 23 13:59:20 2013 -0400
@@ -1,3 +1,20 @@
+2013-04-23  Adam Domurad  <adomurad at redhat.com>
+
+	* tests/netx/unit/net/sourceforge/jnlp/util/UrlUtilsTest.java:
+	Added tests for decodeUrlQuietly, normalizeUrl, normalizeUrlQuietly.
+
+2013-04-23  Adam Domurad  <adomurad at redhat.com>
+
+	* netx/net/sourceforge/jnlp/cache/ResourceTracker.java: Remove no
+	longer used constants. Remove (normalizeUrl). Update calls.
+	* netx/net/sourceforge/jnlp/cache/CacheUtil.java: Expand imports.
+	Update calls.
+	* netx/net/sourceforge/jnlp/security/appletextendedsecurity/UnsignedAppletTrustConfirmation.java:
+	Ensure file://-protocol URLs are encoded.
+	* netx/net/sourceforge/jnlp/util/UrlUtils.java: Add (normalizeUrl),
+	and related utility methods. Allow for optionally encoding file://
+	URLs. 
+
 2013-04-23  Adam Domurad  <adomurad at redhat.com>
 
 	Ensure document-base is properly encoded.
diff -r 9d17ddb0003f -r b912e91204b1 netx/net/sourceforge/jnlp/cache/CacheUtil.java
--- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Tue Apr 23 13:47:42 2013 -0400
+++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Tue Apr 23 13:59:20 2013 -0400
@@ -18,25 +18,38 @@
 
 import static net.sourceforge.jnlp.runtime.Translator.R;
 
-import java.io.*;
-import java.net.*;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FilePermission;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 import java.nio.channels.OverlappingFileLockException;
+import java.security.Permission;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.Set;
-import java.security.*;
-import javax.jnlp.*;
 
-import net.sourceforge.jnlp.*;
+import javax.jnlp.DownloadServiceListener;
+
+import net.sourceforge.jnlp.Version;
 import net.sourceforge.jnlp.config.DeploymentConfiguration;
-import net.sourceforge.jnlp.runtime.*;
+import net.sourceforge.jnlp.runtime.ApplicationInstance;
+import net.sourceforge.jnlp.runtime.JNLPRuntime;
 import net.sourceforge.jnlp.util.FileUtils;
 import net.sourceforge.jnlp.util.PropertiesFile;
+import net.sourceforge.jnlp.util.UrlUtils;
 
 /**
  * Provides static methods to interact with the cache, download
@@ -72,8 +85,8 @@
             return true;
         }
         try {
-            URL nu1 = ResourceTracker.normalizeUrl(u1, false);
-            URL nu2 = ResourceTracker.normalizeUrl(u2, false);
+            URL nu1 = UrlUtils.normalizeUrl(u1);
+            URL nu2 = UrlUtils.normalizeUrl(u2);
             if (notNullUrlEquals(nu1, nu2)) {
                 return true;
             }
diff -r 9d17ddb0003f -r b912e91204b1 netx/net/sourceforge/jnlp/cache/ResourceTracker.java
--- a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Tue Apr 23 13:47:42 2013 -0400
+++ b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Tue Apr 23 13:59:20 2013 -0400
@@ -50,6 +50,7 @@
 import net.sourceforge.jnlp.event.DownloadListener;
 import net.sourceforge.jnlp.runtime.JNLPRuntime;
 import net.sourceforge.jnlp.util.StreamUtils;
+import net.sourceforge.jnlp.util.UrlUtils;
 import net.sourceforge.jnlp.util.WeakList;
 
 /**
@@ -115,15 +116,6 @@
     private static final int ERROR = Resource.ERROR;
     private static final int STARTED = Resource.STARTED;
 
-    // normalization of url
-    private static final char PATH_DELIMITER_MARK = '/';
-    private static final String PATH_DELIMITER = "" + PATH_DELIMITER_MARK;
-    private static final char QUERY_DELIMITER_MARK = '&';
-    private static final String QUERY_DELIMITER = "" + QUERY_DELIMITER_MARK;
-    private static final char QUERY_MARK = '?';
-    private static final char HREF_MARK = '#';
-    private static final String UTF8 = "utf-8";
-
     /** max threads */
     private static final int maxThreads = 5;
 
@@ -190,7 +182,7 @@
         if (location == null)
             throw new IllegalResourceDescriptorException("location==null");
         try {
-            location = normalizeUrl(location, JNLPRuntime.isDebug());
+            location = UrlUtils.normalizeUrl(location);
         } catch (Exception ex) {
             System.err.println("Normalization of " + location.toString() + " have failed");
             ex.printStackTrace();
@@ -1195,30 +1187,4 @@
             // selectNextResource();
         }
     };
-
-    public static URL normalizeUrl(URL u, boolean debug) throws MalformedURLException, UnsupportedEncodingException, URISyntaxException {
-        if (u == null) {
-            return null;
-        }
-        String protocol = u.getProtocol();
-
-        if (protocol == null || "file".equals(protocol)) {
-            return u;
-        }
-
-        if (u.getPath() == null) {
-            return u;
-        }
-
-        //Decode the URL before encoding
-        URL decodedURL = new URL(URLDecoder.decode(u.toString(), UTF8));
-
-        //Create URI with the decoded URL
-        URI uri = new URI(decodedURL.getProtocol(), null, decodedURL.getHost(), decodedURL.getPort(), decodedURL.getPath(), decodedURL.getQuery(), null);
-
-        //Returns the encoded URL
-        URL encodedURL = new URL(uri.toASCIIString());
-
-        return encodedURL;
-    }
 }
diff -r 9d17ddb0003f -r b912e91204b1 netx/net/sourceforge/jnlp/security/appletextendedsecurity/UnsignedAppletTrustConfirmation.java
--- a/netx/net/sourceforge/jnlp/security/appletextendedsecurity/UnsignedAppletTrustConfirmation.java	Tue Apr 23 13:47:42 2013 -0400
+++ b/netx/net/sourceforge/jnlp/security/appletextendedsecurity/UnsignedAppletTrustConfirmation.java	Tue Apr 23 13:59:20 2013 -0400
@@ -99,8 +99,8 @@
 
     private static UnsignedAppletActionEntry getMatchingItem(UnsignedAppletActionStorage actionStorage, PluginBridge file) {
         return actionStorage.getMatchingItem(
-                UrlUtils.normalizeUrlAndStripParams(file.getSourceLocation()).toString(), 
-                UrlUtils.normalizeUrlAndStripParams(file.getCodeBase()).toString(), 
+                UrlUtils.normalizeUrlAndStripParams(file.getSourceLocation(), true /* encode local files */).toString(), 
+                UrlUtils.normalizeUrlAndStripParams(file.getCodeBase(), true /* encode local files */).toString(), 
                 toRelativePaths(file.getArchiveJars(), file.getCodeBase().toString()));
     }
 
@@ -132,8 +132,8 @@
                 return;
             }
 
-            URL codebase = UrlUtils.normalizeUrlAndStripParams(file.getCodeBase());
-            URL documentbase = UrlUtils.normalizeUrlAndStripParams(file.getSourceLocation());
+            URL codebase = UrlUtils.normalizeUrlAndStripParams(file.getCodeBase(), true /* encode local files */);
+            URL documentbase = UrlUtils.normalizeUrlAndStripParams(file.getSourceLocation(), true /* encode local files */);
 
             /* Else, create a new entry */
             UrlRegEx codebaseRegex = new UrlRegEx("\\Q" + codebase + "\\E");
diff -r 9d17ddb0003f -r b912e91204b1 netx/net/sourceforge/jnlp/util/UrlUtils.java
--- a/netx/net/sourceforge/jnlp/util/UrlUtils.java	Tue Apr 23 13:47:42 2013 -0400
+++ b/netx/net/sourceforge/jnlp/util/UrlUtils.java	Tue Apr 23 13:59:20 2013 -0400
@@ -38,18 +38,21 @@
 package net.sourceforge.jnlp.util;
 
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
-
-import net.sourceforge.jnlp.cache.ResourceTracker;
+import java.net.URLDecoder;
 
 public class UrlUtils {
+    private static final String UTF8 = "utf-8";
 
-    public static URL normalizeUrlAndStripParams(URL url) {
+    public static URL normalizeUrlAndStripParams(URL url, boolean encodeFileUrls) {
         try {
             String[] urlParts = url.toString().split("\\?");
             URL strippedUrl = new URL(urlParts[0]); 
-            return ResourceTracker.normalizeUrl(strippedUrl, false);
+            return normalizeUrl(strippedUrl, encodeFileUrls);
         } catch (IOException e) {
             e.printStackTrace();
         } catch (URISyntaxException e) {
@@ -58,6 +61,10 @@
         return url;
     }
 
+    public static URL normalizeUrlAndStripParams(URL url) {
+        return normalizeUrlAndStripParams(url, false);
+    }
+
     public static boolean isLocalFile(URL url) {
 
         if (url.getProtocol().equals("file") &&
@@ -67,4 +74,64 @@
         }
         return false;
     }
+
+    /* Decode a percent-encoded URL. Catch checked exceptions and log. */
+    public static URL decodeUrlQuietly(URL url) {
+        try {
+            return new URL(URLDecoder.decode(url.toString(), UTF8));
+        } catch (IOException e) {
+            e.printStackTrace();
+            return url;
+        }
+    }
+
+    /* Ensure a URL is properly percent-encoded.
+     * Certain usages require local-file URLs to be encoded, eg for code-base & document-base. */
+    public static URL normalizeUrl(URL url, boolean encodeFileUrls) throws MalformedURLException, UnsupportedEncodingException, URISyntaxException {
+        if (url == null) {
+            return null;
+        }
+        String protocol = url.getProtocol();
+        boolean shouldEncode = (encodeFileUrls || !"file".equals(protocol));
+
+        if (protocol == null || !shouldEncode || url.getPath() == null) {
+            return url;
+        }
+
+        //Decode the URL before encoding
+        URL decodedURL = new URL(URLDecoder.decode(url.toString(), UTF8));
+
+        //Create URI with the decoded URL
+        URI uri = new URI(decodedURL.getProtocol(), null, decodedURL.getHost(), decodedURL.getPort(), decodedURL.getPath(), decodedURL.getQuery(), null);
+
+        //Returns the encoded URL
+        URL encodedURL = new URL(uri.toASCIIString());
+
+        return encodedURL;
+    }
+
+    /* Ensure a URL is properly percent-encoded. Does not encode local-file URLs. */
+    public static URL normalizeUrl(URL url) throws MalformedURLException, UnsupportedEncodingException, URISyntaxException {
+        return normalizeUrl(url, false);
+    }
+
+    /* Ensure a URL is properly percent-encoded. Catch checked exceptions and log. */
+    public static URL normalizeUrlQuietly(URL url, boolean encodeFileUrls) {
+        try {
+            return normalizeUrl(url, encodeFileUrls);
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        } catch (URISyntaxException e) {
+            e.printStackTrace();
+        }
+        return url;
+    }
+
+    /* Ensure a URL is properly percent-encoded. Catch checked exceptions and log. */
+    public static URL normalizeUrlQuietly(URL url) {
+        return normalizeUrlQuietly(url, false);
+    }
+
 }
diff -r 9d17ddb0003f -r b912e91204b1 tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java
--- a/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java	Tue Apr 23 13:47:42 2013 -0400
+++ b/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java	Tue Apr 23 13:59:20 2013 -0400
@@ -40,6 +40,9 @@
 import java.net.MalformedURLException;
 import java.net.URISyntaxException;
 import java.net.URL;
+
+import net.sourceforge.jnlp.util.UrlUtils;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -62,9 +65,6 @@
         }
     }
 
-    private static URL normalizeUrl(URL uRL) throws MalformedURLException, UnsupportedEncodingException, URISyntaxException {
-        return ResourceTracker.normalizeUrl(uRL, false);
-    }
     public static final int CHANGE_BORDER = 6;
 
     public static URL[] getUrls() throws MalformedURLException {
@@ -92,7 +92,7 @@
 
         URL[] n = new URL[u.length];
         for (int i = 0; i < n.length; i++) {
-            n[i] = normalizeUrl(u[i]);
+            n[i] = UrlUtils.normalizeUrl(u[i]);
         }
         return n;
 
diff -r 9d17ddb0003f -r b912e91204b1 tests/netx/unit/net/sourceforge/jnlp/util/UrlUtilsTest.java
--- a/tests/netx/unit/net/sourceforge/jnlp/util/UrlUtilsTest.java	Tue Apr 23 13:47:42 2013 -0400
+++ b/tests/netx/unit/net/sourceforge/jnlp/util/UrlUtilsTest.java	Tue Apr 23 13:59:20 2013 -0400
@@ -27,4 +27,40 @@
         assertEquals("http://example.com/%20test%20test",
                 UrlUtils.normalizeUrlAndStripParams(new URL("http://example.com/ test%20test  ?test=test")).toString());
     }
-}
+
+    @Test
+    public void testDecodeUrlQuietly() throws Exception {
+        // This is a wrapper over URLDecoder.decode, simple test suffices
+        assertEquals("http://example.com/ test test",
+                UrlUtils.decodeUrlQuietly(new URL("http://example.com/%20test%20test")).toString());
+    }
+
+    @Test
+    public void testNormalizeUrl() throws Exception {
+        boolean[] encodeFileUrlPossiblities = {false, true};
+
+        // encodeFileUrl flag should have no effect on non-file URLs, but let's be sure.
+        for (boolean encodeFileUrl : encodeFileUrlPossiblities ) {
+            // Test URL with no previous encoding
+            assertEquals("http://example.com/%20test",
+                    UrlUtils.normalizeUrl(new URL("http://example.com/ test"), encodeFileUrl).toString());
+            // Test partially encoded URL with trailing spaces
+            assertEquals("http://example.com/%20test%20test",
+                    UrlUtils.normalizeUrl(new URL("http://example.com/ test%20test  "), encodeFileUrl).toString());
+        }
+
+        // Test file URL with file URL encoding turned off
+        assertFalse("file://example/%20test".equals(
+                  UrlUtils.normalizeUrl(new URL("file://example/ test"), false).toString()));
+
+        // Test file URL with file URL encoding turned on
+        assertEquals("file://example/%20test",
+                  UrlUtils.normalizeUrl(new URL("file://example/ test"), true).toString());
+    }
+    @Test
+    public void testNormalizeUrlQuietly() throws Exception {
+        // This is a wrapper over UrlUtils.normalizeUrl(), simple test suffices
+        assertEquals("http://example.com/%20test%20test",
+                UrlUtils.normalizeUrl(new URL("http://example.com/ test%20test  ")).toString());
+    }
+}
\ No newline at end of file



More information about the distro-pkg-dev mailing list