Empty InputStream on getRequestBody() used in HttpsServer

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P3
    • None
    • Affects Version/s: 6
    • Component/s: core-libs
    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.6.0-beta2"
      Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
      Java HotSpot(TM) Client VM (build 1.6.0-beta2-b86, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Linux ramses 2.6.11.4-20a-default #1 Wed Mar 23 21:52:37 UTC 2005 i686 athlon i386 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      When experimenting with with HttpServer and HttpsServer I run in to following problem:

      I managed to setup both Http and Https Server with a simple handler returning results to my browser which works fine for both.

      When using the getRequestBody() method of the HttpExchange object to access form data sent with a POST request, I get an Empty InputStream from within my HttpsServer.

      With HttpServer the InputStream correctly contains my fom data.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The testcase assumes, that java and a browser are available on the host 'localhost'
      1. Build a keystore with
      keytool -genkey -keystore keystore
      use "passphrase" as keystore-passoword and "localhost" as name
      2. Create the files given in Source Code below
      3. javac HttpsServerBug.java
      4. Run it with
      java HttpsServerBug 9000
      5. From a Browser load the following html code and submit
      <HTML><BODY><FORM name="form" action="https://localhost:9000/test" method="post">
      <INPUT type="SUBMIT" name="SUBMIT" value="GO">
      </FORM></BODY></HTML>
      You have to accept the ssl certificate
      The Browser should show something like:
      Thu Sep 28 11:46:35 CEST 2006 for /test
      If above output does not show, something is wrong with your test setup.
      6. Watch the output of HttpsServerBug


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The HttpsServerBug Program should output:

      Press return to quit
      request, method = POST, available = 9
      SUBMIT=GO

      ACTUAL -
      The HttpsServerBug Program outputs:

      Press return to quit
      request, method = POST, available = 0

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      no error message

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      FILE: HttpsServerBug.java

      import java.lang.*;
      import java.util.*;
      import java.io.*;
      import java.net.*;
      import com.sun.net.httpserver.*;
      import javax.net.ssl.*;
      import java.security.*;
      import java.security.cert.*;

      public class HttpsServerBug
      {
      public static void main( String[] args )
        throws
          IOException,
          NoSuchAlgorithmException,
          KeyManagementException,
          KeyStoreException,
          CertificateException,
          UnrecoverableKeyException
      {
        int port = new Integer(args[0]).intValue();
        HttpsServer server = HttpsServer.create( new InetSocketAddress(port), 10 );
        char[] passphrase = "passphrase".toCharArray();
        KeyStore ksKeys = KeyStore.getInstance("JKS");
        ksKeys.load(new FileInputStream("keystore"), passphrase);
        KeyStore ksTrust = KeyStore.getInstance("JKS");
        ksTrust.load(new FileInputStream("keystore"), passphrase);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ksKeys, passphrase);
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(ksTrust);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
        server.setHttpsConfigurator (new HttpsConfigurator(sslContext));
        server.createContext( "/", new DateHandlerBug() );
        server.start();
        System.out.println("Press return to quit");
        System.in.read();
      }
      }

      FILE: DateHandlerBug.java

      import java.lang.*;
      import java.util.*;
      import java.io.*;
      import java.net.*;

      import com.sun.net.httpserver.*;

      class DateHandlerBug implements HttpHandler
      {
      public void handle( HttpExchange httpExchange ) throws IOException
      {

        httpExchange.getResponseHeaders().add( "Content-type", "text/html" );
        String response = "<html><body><b>" + new Date() + "</b> for " + httpExchange.getRequestURI() + "<br>";
        response = response + "</body></html>";
        httpExchange.sendResponseHeaders( 200, response.length() );

        InputStream is = httpExchange.getRequestBody();
        int available = is.available();
        System.out.println("request, method = " + httpExchange.getRequestMethod() + ", available = " + String.valueOf(available) + ":");
        byte[] buffer = new byte[available];
        is.read(buffer,0,available);
        System.out.print(new String(buffer));
        System.out.println("");

        OutputStream os = httpExchange.getResponseBody();
        byte[] bytes= response.getBytes() ;
        os.write( bytes );
        os.close();
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      no workaround found

            Assignee:
            Michael McMahon
            Reporter:
            Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: