mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
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
2 changed files with 64 additions and 26 deletions
|
@ -27,21 +27,27 @@ package sun.net.www;
|
||||||
|
|
||||||
import jdk.internal.util.StaticProperty;
|
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.net.FileNameMap;
|
||||||
import java.util.Hashtable;
|
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import java.util.Hashtable;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
public class MimeTable implements FileNameMap {
|
public class MimeTable implements FileNameMap {
|
||||||
|
/** Hash mark introducing a URI fragment */
|
||||||
|
private static final int HASH_MARK = '#';
|
||||||
|
|
||||||
/** Keyed by content type, returns MimeEntries */
|
/** Keyed by content type, returns MimeEntries */
|
||||||
private Hashtable<String, MimeEntry> entries
|
private Hashtable<String, MimeEntry> entries = new Hashtable<>();
|
||||||
= new Hashtable<>();
|
|
||||||
|
|
||||||
/** Keyed by file extension (with the .), returns MimeEntries */
|
/** Keyed by file extension (with the .), returns MimeEntries */
|
||||||
private Hashtable<String, MimeEntry> extensionMap
|
private Hashtable<String, MimeEntry> extensionMap = new Hashtable<>();
|
||||||
= new Hashtable<>();
|
|
||||||
|
|
||||||
// Will be reset if in the platform-specific data file
|
// Will be reset if in the platform-specific data file
|
||||||
@SuppressWarnings("removal")
|
@SuppressWarnings("removal")
|
||||||
|
@ -84,9 +90,6 @@ public class MimeTable implements FileNameMap {
|
||||||
return DefaultInstanceHolder.defaultInstance;
|
return DefaultInstanceHolder.defaultInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public static FileNameMap loadTable() {
|
public static FileNameMap loadTable() {
|
||||||
MimeTable mt = getDefaultTable();
|
MimeTable mt = getDefaultTable();
|
||||||
return mt;
|
return mt;
|
||||||
|
@ -151,23 +154,15 @@ public class MimeTable implements FileNameMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Locate a MimeEntry by the file extension that has been associated
|
* Extracts the file extension and uses it to look up the entry.
|
||||||
* with it. Parses general file names, and URLs.
|
|
||||||
*/
|
*/
|
||||||
public MimeEntry findByFileName(String fname) {
|
private MimeEntry findViaFileExtension(String fname) {
|
||||||
String ext = "";
|
int i = fname.lastIndexOf('.');
|
||||||
|
|
||||||
int i = fname.lastIndexOf('#');
|
|
||||||
|
|
||||||
if (i > 0) {
|
|
||||||
fname = fname.substring(0, i - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
i = fname.lastIndexOf('.');
|
|
||||||
// REMIND: OS specific delimiters appear here
|
// REMIND: OS specific delimiters appear here
|
||||||
i = Math.max(i, fname.lastIndexOf('/'));
|
i = Math.max(i, fname.lastIndexOf('/'));
|
||||||
i = Math.max(i, fname.lastIndexOf('?'));
|
i = Math.max(i, fname.lastIndexOf('?'));
|
||||||
|
|
||||||
|
String ext = "";
|
||||||
if (i != -1 && fname.charAt(i) == '.') {
|
if (i != -1 && fname.charAt(i) == '.') {
|
||||||
ext = fname.substring(i).toLowerCase();
|
ext = fname.substring(i).toLowerCase();
|
||||||
}
|
}
|
||||||
|
@ -175,6 +170,38 @@ public class MimeTable implements FileNameMap {
|
||||||
return findByExt(ext);
|
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
|
* Locate a MimeEntry by the file extension that has been associated
|
||||||
* with it.
|
* with it.
|
||||||
|
|
|
@ -22,15 +22,17 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* @test
|
/* @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
|
* @summary Unit test for probeContentType method
|
||||||
* @library ../..
|
* @library ../..
|
||||||
* @build Basic SimpleFileTypeDetector
|
* @build Basic SimpleFileTypeDetector
|
||||||
* @run main/othervm Basic
|
* @run main/othervm Basic
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.nio.file.*;
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@ -79,10 +81,10 @@ public class Basic {
|
||||||
if (!expected.equals(actual)) {
|
if (!expected.equals(actual)) {
|
||||||
if (IS_UNIX) {
|
if (IS_UNIX) {
|
||||||
Path userMimeTypes =
|
Path userMimeTypes =
|
||||||
Paths.get(System.getProperty("user.home"), ".mime.types");
|
Path.of(System.getProperty("user.home"), ".mime.types");
|
||||||
checkMimeTypesFile(userMimeTypes);
|
checkMimeTypesFile(userMimeTypes);
|
||||||
|
|
||||||
Path etcMimeTypes = Paths.get("/etc/mime.types");
|
Path etcMimeTypes = Path.of("/etc/mime.types");
|
||||||
checkMimeTypesFile(etcMimeTypes);
|
checkMimeTypesFile(etcMimeTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,6 +190,15 @@ public class Basic {
|
||||||
};
|
};
|
||||||
failures += checkContentTypes(exTypes);
|
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) {
|
if (failures > 0) {
|
||||||
throw new RuntimeException("Test failed!");
|
throw new RuntimeException("Test failed!");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue