This is an automated email from the ASF dual-hosted git repository. svenmeier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/wicket.git
commit f1bd38690674be5578fc5c5e1c28cc81cdb7ae76 Author: Thorsten Schöning <[email protected]> AuthorDate: Sat Mar 14 17:54:55 2020 +0100 WICKET-6757 Avoid URL.getFile() for mime type detection Mime type detection is documented to work on paths or URLs, but URL.getFile provides neither of both and the default implementation is very limited regarding / vs. \ and such things. Forwarding an absolute URI always instead is compatible with the default implementation and makes absolutely clear what is available. URL.getFile for example might look like some path in the file system, but would work with paths containing spaces because of no decoding. --- .../wicket/core/util/resource/UrlResourceStream.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/UrlResourceStream.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/UrlResourceStream.java index 7f36cbe..bf451c9 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/UrlResourceStream.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/UrlResourceStream.java @@ -18,6 +18,7 @@ package org.apache.wicket.core.util.resource; import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.time.Instant; @@ -118,9 +119,16 @@ public class UrlResourceStream extends AbstractResourceStream streamData.connection = url.openConnection(); streamData.contentLength = streamData.connection.getContentLength(); + // Default implementation "sun.net.www.MimeTable" works on strings with "/" only and + // doesn't properly parse paths nor URLs. So providing an absolute URI is compatible + // with the default implementation, while the string can't be misinterpreted as path + // like has been the case with "URL.getFile" before. That doesn't decode to paths, + // results only look similar sometimes. + String uriStr = url.toURI().toString(); + if (Application.exists()) { - streamData.contentType = Application.get().getMimeType(url.getFile()); + streamData.contentType = Application.get().getMimeType(uriStr); } else { @@ -130,10 +138,10 @@ public class UrlResourceStream extends AbstractResourceStream if (streamData.contentType == null || streamData.contentType.contains("unknown")) { streamData.contentType = URLConnection.getFileNameMap().getContentTypeFor( - url.getFile()); + uriStr); } } - catch (IOException ex) + catch (IOException | URISyntaxException ex) { throw new IllegalArgumentException("Invalid URL parameter " + url, ex); }
