Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8217944

bugs in java.net.URLConnection.getFileNameMap()

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: tbd
    • Component/s: core-libs
    • Labels:
      None

      Description

      Colleague Tobias Thierer reports:

      ----
      java.net.URLConnection.getFileNameMap() is implemented on top of sun.net.www.MimeTable.findByFileName(String), which has multiple bugs.
      The most egregious bug is that the logic for stripping off fragments ('#') chops off one character too many at the end. (It also fails when fname starts with a '#').
      There's also logic that looks like it tries to deal with path separators '/' and query parameters '?', but it doesn't work right.
      I've searched bugs.openjdk.java.net for bugs mentioning "MimeTable" or "URLConnection.getFileNameMap" but have not found them reported before.

      Some of the bugs are demonstrated by the following test, which passes but should not pass:

      public void testActualButNonsensicalBehavior() {
        FileNameMap map = URLConnection.getFileNameMap();

        // These ones makes sense (apart from the extra character that gets cut off before '#')
        assertEquals(null, map.getContentTypeFor(""));
        assertEquals(null, map.getContentTypeFor("example"));
        assertEquals("text/plain", map.getContentTypeFor("test.txtX#fragment"));
        assertEquals("text/plain", map.getContentTypeFor("example.com/test.txtX#fragment"));

        // But these ones are inconsistent and make no sense
        assertEquals("text/plain", map.getContentTypeFor("#example.txt"));
        assertEquals("text/plain", map.getContentTypeFor("test.txtX#fragment"));
        assertEquals("text/plain", map.getContentTypeFor("example.com/test.txt"));
        assertEquals("text/plain", map.getContentTypeFor("test.html?key=value.txt"));
        assertEquals(null, map.getContentTypeFor("test.txt?key=value"));
        assertEquals("text/plain", map.getContentTypeFor("test.txtX#fra?gm=ent"));
      }
      I'm attaching a .patch against OpenJDK head that fixes all of these bugs and which correctly strips off fragments, query parameters, and path separators.

      I have not tested URLConnection.getFileNameMap() after applying the test, but I have tested that the helper method getExtension() passes the following test; the helper method returns extensions including the ".", to be consistent with what the old code did (not sure whether deliberately):

      public void testGetExtension() {
          assertEquals(".txt", getExtension("example.txt"));
          assertEquals(".txt", getExtension("example.TxT"));
          assertEquals(".txt", getExtension("example.txt#fragment"));
          assertEquals(".txt", getExtension("example.txt?key=value#fragment"));

          // everything from # onwards is part of the fragment here
          assertEquals(".txt", getExtension("example.txt#fragment?key=value"));
          assertEquals(".txt", getExtension("example.txt#fragment/foo/bar.html"));

          assertEquals(".txt", getExtension("example.com/readme.txt"));
          assertEquals(".txt", getExtension("example.come/readme.txt?key=value#fragment"));

          assertEquals("", getExtension("example"));
      }

        Attachments

          Activity

            People

            Assignee:
            chegar Chris Hegarty
            Reporter:
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: