8287237: (fs) Files.probeContentType returns null if filename contains hash mark on Linux
Reviewed-by: rriggs, jpai, vtewari
This commit is contained in:
parent
774928f944
commit
8071b2311c
src/java.base/share/classes/sun/net/www
test/jdk/java/nio/file/Files/probeContentType
@ -27,21 +27,27 @@ package sun.net.www;
|
||||
|
||||
import jdk.internal.util.StaticProperty;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.FileNameMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Properties;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class MimeTable implements FileNameMap {
|
||||
/** Hash mark introducing a URI fragment */
|
||||
private static final int HASH_MARK = '#';
|
||||
|
||||
/** Keyed by content type, returns MimeEntries */
|
||||
private Hashtable<String, MimeEntry> entries
|
||||
= new Hashtable<>();
|
||||
private Hashtable<String, MimeEntry> entries = new Hashtable<>();
|
||||
|
||||
/** Keyed by file extension (with the .), returns MimeEntries */
|
||||
private Hashtable<String, MimeEntry> extensionMap
|
||||
= new Hashtable<>();
|
||||
private Hashtable<String, MimeEntry> extensionMap = new Hashtable<>();
|
||||
|
||||
// Will be reset if in the platform-specific data file
|
||||
@SuppressWarnings("removal")
|
||||
@ -84,9 +90,6 @@ public class MimeTable implements FileNameMap {
|
||||
return DefaultInstanceHolder.defaultInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static FileNameMap loadTable() {
|
||||
MimeTable mt = getDefaultTable();
|
||||
return mt;
|
||||
@ -151,23 +154,15 @@ public class MimeTable implements FileNameMap {
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate a MimeEntry by the file extension that has been associated
|
||||
* with it. Parses general file names, and URLs.
|
||||
* Extracts the file extension and uses it to look up the entry.
|
||||
*/
|
||||
public MimeEntry findByFileName(String fname) {
|
||||
String ext = "";
|
||||
|
||||
int i = fname.lastIndexOf('#');
|
||||
|
||||
if (i > 0) {
|
||||
fname = fname.substring(0, i - 1);
|
||||
}
|
||||
|
||||
i = fname.lastIndexOf('.');
|
||||
private MimeEntry findViaFileExtension(String fname) {
|
||||
int i = fname.lastIndexOf('.');
|
||||
// REMIND: OS specific delimiters appear here
|
||||
i = Math.max(i, fname.lastIndexOf('/'));
|
||||
i = Math.max(i, fname.lastIndexOf('?'));
|
||||
|
||||
String ext = "";
|
||||
if (i != -1 && fname.charAt(i) == '.') {
|
||||
ext = fname.substring(i).toLowerCase();
|
||||
}
|
||||
@ -175,6 +170,38 @@ public class MimeTable implements FileNameMap {
|
||||
return findByExt(ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate a MimeEntry by its associated file extension.
|
||||
* Parses general file names, and URLs.
|
||||
*
|
||||
* @param fname the file name
|
||||
*
|
||||
* @return the MIME entry associated with the file name or {@code null}
|
||||
*/
|
||||
public MimeEntry findByFileName(String fname) {
|
||||
MimeEntry entry = null;
|
||||
|
||||
// If an optional fragment introduced by a hash mark is
|
||||
// present, then strip it and use the prefix
|
||||
int hashIndex = fname.lastIndexOf(HASH_MARK);
|
||||
if (hashIndex > 0) {
|
||||
entry = findViaFileExtension(fname.substring(0, hashIndex));
|
||||
if (entry != null) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
assert entry == null;
|
||||
|
||||
// If either no optional fragment was present, or the entry was not
|
||||
// found with the fragment stripped, then try again with the full name
|
||||
if (entry == null) {
|
||||
entry = findViaFileExtension(fname);
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate a MimeEntry by the file extension that has been associated
|
||||
* with it.
|
||||
|
@ -22,15 +22,17 @@
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4313887 8129632 8129633 8162624 8146215 8162745 8273655 8274171
|
||||
* @bug 4313887 8129632 8129633 8162624 8146215 8162745 8273655 8274171 8287237
|
||||
* @summary Unit test for probeContentType method
|
||||
* @library ../..
|
||||
* @build Basic SimpleFileTypeDetector
|
||||
* @run main/othervm Basic
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ -79,10 +81,10 @@ public class Basic {
|
||||
if (!expected.equals(actual)) {
|
||||
if (IS_UNIX) {
|
||||
Path userMimeTypes =
|
||||
Paths.get(System.getProperty("user.home"), ".mime.types");
|
||||
Path.of(System.getProperty("user.home"), ".mime.types");
|
||||
checkMimeTypesFile(userMimeTypes);
|
||||
|
||||
Path etcMimeTypes = Paths.get("/etc/mime.types");
|
||||
Path etcMimeTypes = Path.of("/etc/mime.types");
|
||||
checkMimeTypesFile(etcMimeTypes);
|
||||
}
|
||||
|
||||
@ -188,6 +190,15 @@ public class Basic {
|
||||
};
|
||||
failures += checkContentTypes(exTypes);
|
||||
|
||||
// Verify type is found when the extension is in a fragment component
|
||||
Path pathWithFragement = Path.of("SomePathWith#aFragement.png");
|
||||
String contentType = Files.probeContentType(pathWithFragement);
|
||||
if (contentType == null || !contentType.equals("image/png")) {
|
||||
System.err.printf("For %s expected \"png\" but got %s%n",
|
||||
pathWithFragement, contentType);
|
||||
failures++;
|
||||
}
|
||||
|
||||
if (failures > 0) {
|
||||
throw new RuntimeException("Test failed!");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user