< prev index next >

src/java.base/share/classes/sun/net/www/MimeTable.java

Print this page
8217944: bugs in java.net.URLConnection.getFileNameMap()
Reviewed-by: duke
Contributed-by: Tobias Thierer <tobiast@google.com>, Martin Buchholz <martinrb@google.com>

@@ -28,10 +28,11 @@
 
 import java.io.*;
 import java.net.FileNameMap;
 import java.util.Hashtable;
 import java.util.Enumeration;
+import java.util.Locale;
 import java.util.Properties;
 import java.util.StringTokenizer;
 
 public class MimeTable implements FileNameMap {
     /** Keyed by content type, returns MimeEntries */

@@ -165,28 +166,39 @@
     /**
      * Locate a MimeEntry by the file extension that has been associated
      * with it. Parses general file names, and URLs.
      */
     public MimeEntry findByFileName(String fname) {
-        String ext = "";
-
-        int i = fname.lastIndexOf('#');
+        String ext = getExtension(fname);
+        return findByExt(ext);
+    }
 
-        if (i > 0) {
-            fname = fname.substring(0, i - 1);
+    private static String getExtension(String fname) {
+        int fragmentIndex = fname.lastIndexOf('#');
+        if (fragmentIndex < 0) {
+            fragmentIndex = fname.length();
         }
 
-        i = fname.lastIndexOf('.');
-        // REMIND: OS specific delimters appear here
-        i = Math.max(i, fname.lastIndexOf('/'));
-        i = Math.max(i, fname.lastIndexOf('?'));
+        int queryIndex = fname.lastIndexOf('?', fragmentIndex);
+        if (queryIndex < 0) {
+            queryIndex = fragmentIndex;
+        }
 
-        if (i != -1 && fname.charAt(i) == '.') {
-            ext = fname.substring(i).toLowerCase();
+        int slashIndex = fname.lastIndexOf('/', queryIndex);
+        if (slashIndex < 0) {
+            slashIndex = 0;
         }
+        fname = fname.substring(slashIndex, queryIndex);
 
-        return findByExt(ext);
+        int dotIndex = fname.lastIndexOf('.', queryIndex);
+        String result;
+        if (dotIndex >= 0) {
+            result = fname.substring(dotIndex); // include the '.'
+        } else {
+            result = ""; // or: result = "." + fname
+        }
+        return result.toLowerCase(Locale.ROOT);
     }
 
     /**
      * Locate a MimeEntry by the file extension that has been associated
      * with it.
< prev index next >