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

HttpURLConnection's getHeaderFields method returns field values in reverse order

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 8u45
    • core-libs
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      The HttpURLConnection's getHeaderFields method returns combined values in lists in a headers map. The values in the list of a particular header field are always in reversed order to the original http response. As the order of the header values are important in some scenario (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2), JRE should keep the original order of the values.

      For example, the server returns the following response:
      HTTP/1.1 200 OK
      X-Powered-By: Servlet/3.0
      Content-Type: text/plain; charset=UTF-8
      Content-Language: en-US
      MyHeader: value1
      MyHeader: value2
      MyHeader: value3
      Transfer-Encoding: chunked
      Date: Sat, 09 May 2015 00:27:44 GMT
      Expires: Thu, 01 Dec 1994 16:00:00 GMT

      When I use getHeaderFields and fetch the value of field "MyHeader", the expected value list would be
      ["value1", "value2", "value3"]

      However what I get is always like the following, which does not honor the original reponse.
      ["value3", "value2", "value1"]


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      You need to create an http server that returns headers with same name, such as a server that return the following response:
      HTTP/1.1 200 OK
      X-Powered-By: Servlet/3.0
      Content-Type: text/plain; charset=UTF-8
      Content-Language: en-US
      MyHeader: value1
      MyHeader: value2
      MyHeader: value3
      Transfer-Encoding: chunked
      Date: Sat, 09 May 2015 00:27:44 GMT
      Expires: Thu, 01 Dec 1994 16:00:00 GMT

      Then write a java program to connect to the server using HttpURLConnection, and use getHeaderFields the retrieve and print out all the headers and the combined values.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The of the the values in the list of a particular field should honor the original response, such as
      ["value1", "value2", "value3"]
      ACTUAL -
      The values are always in reversed order, like
      ["value3", "value2", "value1"]

      REPRODUCIBILITY :
      This bug can be reproduced always.

            webbuggrp Webbug Group
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: