Mercurial > hg > release > icedtea-web-1.4
changeset 687:6b680fe9c390
Introduce more UrlUtils functions.
author | Adam Domurad <adomurad@redhat.com> |
---|---|
date | Tue, 23 Apr 2013 13:55:23 -0400 |
parents | 9d17ddb0003f |
children | b912e91204b1 |
files | ChangeLog netx/net/sourceforge/jnlp/cache/CacheUtil.java netx/net/sourceforge/jnlp/cache/ResourceTracker.java netx/net/sourceforge/jnlp/security/appletextendedsecurity/UnsignedAppletTrustConfirmation.java netx/net/sourceforge/jnlp/util/UrlUtils.java tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java |
diffstat | 6 files changed, 114 insertions(+), 56 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Tue Apr 23 13:47:42 2013 -0400 +++ b/ChangeLog Tue Apr 23 13:55:23 2013 -0400 @@ -1,3 +1,15 @@ +2013-04-23 Adam Domurad <adomurad@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@redhat.com> Ensure document-base is properly encoded.
--- 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:55:23 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 javax.jnlp.DownloadServiceListener; -import net.sourceforge.jnlp.*; +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; }
--- 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:55:23 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; - } }
--- 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:55:23 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");
--- 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:55:23 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); + } + }
--- 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:55:23 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;