Harmony
  1. Harmony
  2. HARMONY-6504

[classlib][luni]Harmony returns null for header related function when file URL is handled

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 5.0M14
    • Fix Version/s: 5.0M13
    • Component/s: Classlib
    • Labels:
      None
    • Environment:
      All platforms
    • Patch Info:
      Patch Available

      Description

      Header related functions [getHeaderField(int)/getHeaderFieldKey(int)/getHeaderField(String)/getLastModified()] returns null when file URL is handled.

      Consider the following test function, which fails with Harmony

      public void test() throws Exception

      { String resourceName = "org/apache/harmony/luni/tests/"; //folder name URL url = ClassLoader.getSystemClassLoader().getResource(resourceName); FileURLConnection conn = new FileURLConnection(url); assertNotNull(conn.getInputStream()); assertEquals(conn.getContentType(), conn.getHeaderField("content-type")) ; resourceName = "org/apache/harmony/luni/tests/" "test.rtf";; //folder name url = ClassLoader.getSystemClassLoader().getResource(resourceName); conn = new FileURLConnection(url); assertNotNull(conn.getInputStream()); assertEquals(conn.getContentType(), conn.getHeaderField("content-type")) ; assertEquals(Integer.toString(conn.getContentLength()), conn.getHeaderField("content-length")) ; assertEquals(conn.getHeaderField(0), conn.getHeaderField("content-type")); assertEquals(conn.getHeaderField(1), conn.getHeaderField("content-length")); assertEquals(conn.getHeaderField(2), conn.getHeaderField("last-modified")); assertEquals("last-modified", conn.getHeaderFieldKey(2)); assertEquals("content-length", conn.getHeaderFieldKey(1)); assertEquals("content-type", conn.getHeaderFieldKey(0)); }

      Thanks and Regards,
      Mohan

      1. URLConnection.HARMONY-6504.diff
        1 kB
        Kevin Zhou
      2. HARMONY-6504.diff
        7 kB
        Mohanraj Loganathan

        Activity

        Hide
        Mohanraj Loganathan added a comment -

        Attaching patch for this issue. Patch also contains the testcases.

        I am Granting license to ASF for inclusion in ASF works

        Show
        Mohanraj Loganathan added a comment - Attaching patch for this issue. Patch also contains the testcases. I am Granting license to ASF for inclusion in ASF works
        Hide
        Kevin Zhou added a comment -

        Hi Mohan,

        Thank you very much for your patch. It has been applied at r935873, please help to verify.

        Show
        Kevin Zhou added a comment - Hi Mohan, Thank you very much for your patch. It has been applied at r935873, please help to verify.
        Hide
        Mohanraj Loganathan added a comment -

        Thanks Kevin for committing this patch.
        I Verified @ r935874.

        Thanks and Regards,
        Mohan

        Show
        Mohanraj Loganathan added a comment - Thanks Kevin for committing this patch. I Verified @ r935874. Thanks and Regards, Mohan
        Hide
        Kevin Zhou added a comment -

        Patch applied with a minor modification by adding final modifier to the header field r935873:
        Originally, its like:
        + private LinkedHashMap<String, String> header;
        I modify it to:
        + private final LinkedHashMap<String, String> header;

        Thanks

        Show
        Kevin Zhou added a comment - Patch applied with a minor modification by adding final modifier to the header field r935873: Originally, its like: + private LinkedHashMap<String, String> header; I modify it to: + private final LinkedHashMap<String, String> header; Thanks
        Hide
        Mark Hindess added a comment -

        Kevin, Mohanraj,

        This change causes two test failures:

        org.apache.harmony.luni.tests.java.lang.ClassLoaderTest.test_getResourceAsStream_withSharpChar
        org.apache.harmony.luni.tests.java.lang.ClassTest.test_getResourceAsStream_withSharpChar

        see:

        http://hudson.zones.apache.org/hudson/view/Harmony/job/Harmony-1.5-head-linux-x86_64-full-tests/165/testReport/

        These tests also fail for me on windows. Do these tests pass for you? (If not, you shouldn't have attached the patch/committed this change.)

        Show
        Mark Hindess added a comment - Kevin, Mohanraj, This change causes two test failures: org.apache.harmony.luni.tests.java.lang.ClassLoaderTest.test_getResourceAsStream_withSharpChar org.apache.harmony.luni.tests.java.lang.ClassTest.test_getResourceAsStream_withSharpChar see: http://hudson.zones.apache.org/hudson/view/Harmony/job/Harmony-1.5-head-linux-x86_64-full-tests/165/testReport/ These tests also fail for me on windows. Do these tests pass for you? (If not, you shouldn't have attached the patch/committed this change.)
        Hide
        Kevin Zhou added a comment -

        Hi Mark,

        I think this just reveals another problem in java.net.URLConnection.guessContentTypeFromStream(InputStream) method. If there is not data in the given input stream, it can not determine the content type of this stream, so that it should directly return null as content/unknown.
        But previously, in the method, no matter what the length is, it will finally invoke String header = new String(bytes, start, length - start, encoding); to create a string of the header, which causes 2 failures in ClassLoaderTest and ClassTest of ava.lang.StringIndexOutOfBoundsException.

        Do you agree?

        Show
        Kevin Zhou added a comment - Hi Mark, I think this just reveals another problem in java.net.URLConnection.guessContentTypeFromStream(InputStream) method. If there is not data in the given input stream, it can not determine the content type of this stream, so that it should directly return null as content/unknown. But previously, in the method, no matter what the length is, it will finally invoke String header = new String(bytes, start, length - start, encoding); to create a string of the header, which causes 2 failures in ClassLoaderTest and ClassTest of ava.lang.StringIndexOutOfBoundsException. Do you agree?
        Hide
        Kevin Zhou added a comment -

        For instance, given a test case [1], RI passes while HARMONY throws a StringIndexOutOfBoundsException as follows:

        java.lang.StringIndexOutOfBoundsException
        at java.lang.String.<init>(String.java)
        at java.net.URLConnection.guessContentTypeFromStream(URLConnection.java:723)

        [1] Test Case
        public void test_EmptyInputStream() throws IOException

        { // try to guess from an empty string ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); assertNull(URLConnection.guessContentTypeFromStream(bais)); }
        Show
        Kevin Zhou added a comment - For instance, given a test case [1] , RI passes while HARMONY throws a StringIndexOutOfBoundsException as follows: java.lang.StringIndexOutOfBoundsException at java.lang.String.<init>(String.java) at java.net.URLConnection.guessContentTypeFromStream(URLConnection.java:723) [1] Test Case public void test_EmptyInputStream() throws IOException { // try to guess from an empty string ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); assertNull(URLConnection.guessContentTypeFromStream(bais)); }
        Hide
        Kevin Zhou added a comment -

        Would you please help to review this patch and testify it?

        Show
        Kevin Zhou added a comment - Would you please help to review this patch and testify it?
        Hide
        Mark Hindess added a comment -

        Looks good to me.

        Show
        Mark Hindess added a comment - Looks good to me.
        Hide
        Kevin Zhou added a comment -

        already resolved

        Show
        Kevin Zhou added a comment - already resolved
        Hide
        Kevin Zhou added a comment -

        patch applied to r935873

        Show
        Kevin Zhou added a comment - patch applied to r935873
        Hide
        Mohanraj Loganathan added a comment -

        Thanks Kevin for committing changes. I verified the fix @ r965571. I verified the tests, Its working as expected.

        Show
        Mohanraj Loganathan added a comment - Thanks Kevin for committing changes. I verified the fix @ r965571. I verified the tests, Its working as expected.

          People

          • Assignee:
            Kevin Zhou
            Reporter:
            Mohanraj Loganathan
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development