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

HttpURLConnection strips Content-Length header on Post

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 6u22
    • core-libs
    • Cause Known
    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.6.0_22"


      ADDITIONAL OS VERSION INFORMATION :
      This appears on:
      Ubuntu Linux
      Windows 2003 32bit and 64bit
      Windows 2008 64bit
      Mac

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Configuration is not relavant it appears when upgrading any of OS to build 22

      A DESCRIPTION OF THE PROBLEM :
      When doing a HTTP Post via the HttpURLConnection

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
                          URL url = new URL("http://httpserver/uploadhandler");

                          HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();

                          urlConn.setDoInput(true);

                          urlConn.setDoOutput(true);

                          urlConn.setUseCaches(false);

                          // Setup the Headers
                          urlConn.setRequestMethod("POST");
                          urlConn.setRequestProperty("Connection", "Keep-Alive");
                          urlConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary + "; charset=UTF-8");
                          urlConn.setRequestProperty("Transfer-Encoding", "chunked" );
                          urlConn.setRequestProperty("User-Agent", "Test Agent" );
                          urlConn.setRequestProperty("Content-Length", Long.toString(contentLength));
                          urlConn.setChunkedStreamingMode(0);
                          urlConn.setReadTimeout(READ_TIMEOUT);
                          urlConn.setConnectTimeout(READ_TIMEOUT);

                          urlConn.connect();

                          DataOutputStream printout = new DataOutputStream (urlConn.getOutputStream());

                          // Write boundary and header
                          printout.writeBytes(contentBoundary);
                          printout.write(headerStr.getBytes("UTF-8"));

                          // write upload buffer
                          if (bytesRead != -1) {
                              printout.write(buffer, 0, bytesRead);
                          }

                          // Send the footer
                          printout.writeBytes(lineEnd);
                          printout.writeBytes(endingBoundary);

      Using the above code in any version previous to build 22 the result shows a Content-Length header in the HTTP Request when inspected through wireshark.

      Wireshark output with missing header:


      POST /uploadhandler HTTP/1.1
      Content-Type: multipart/form-data; boundary=***********vdvuiBIOG&#(RO#$Bkjdbvsdbebr
      User-Agent: Test Agent
      Cache-Control: no-cache
      Pragma: no-cache
      Host: httpserver
      Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
      Connection: keep-alive
      Transfer-Encoding: chunked


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The Content-Length as provided by the HttpURLConnection.setRequestProperty should be evident in the request to the HTTP server when doing a post.
      ACTUAL -
      All headers provided by the setRequestProperty are sent correctly except the Content-Length which appears to be stripped out.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No Error except from the server and wireshark capture not showing the content-length header.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
                          URL url = new URL("http://httpserver/uploadhandler");

                          HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();

                          urlConn.setDoInput(true);

                          urlConn.setDoOutput(true);

                          urlConn.setUseCaches(false);

                          // Setup the Headers
                          urlConn.setRequestMethod("POST");
                          urlConn.setRequestProperty("Connection", "Keep-Alive");
                          urlConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary + "; charset=UTF-8");
                          urlConn.setRequestProperty("Transfer-Encoding", "chunked" );
                          urlConn.setRequestProperty("User-Agent", "Test Agent" );
                          urlConn.setRequestProperty("Content-Length", Long.toString(contentLength));
                          urlConn.setChunkedStreamingMode(0);
                          urlConn.setReadTimeout(READ_TIMEOUT);
                          urlConn.setConnectTimeout(READ_TIMEOUT);

                          urlConn.connect();

                          DataOutputStream printout = new DataOutputStream (urlConn.getOutputStream());

                          // Write boundary and header
                          printout.writeBytes(contentBoundary);
                          printout.write(headerStr.getBytes("UTF-8"));

                          // write upload buffer
                          if (bytesRead != -1) {
                              printout.write(buffer, 0, bytesRead);
                          }

                          // Send the footer
                          printout.writeBytes(lineEnd);
                          printout.writeBytes(endingBoundary);

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Unknown

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: