1 /*
   2  * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 6270015
  27  * @run main/othervm Test6a
  28  * @summary  Light weight HTTP server
  29  */
  30 
  31 import com.sun.net.httpserver.*;
  32 
  33 import java.util.concurrent.*;
  34 import java.io.*;
  35 import java.net.*;
  36 import javax.net.ssl.*;
  37 
  38 /**
  39  * Test https POST large file via chunked encoding (unusually small chunks)
  40  */
  41 
  42 public class Test6a extends Test {
  43 
  44     public static void main (String[] args) throws Exception {
  45         Handler handler = new Handler();
  46         InetSocketAddress addr = new InetSocketAddress (0);
  47         HttpsServer server = HttpsServer.create (addr, 0);
  48         HttpContext ctx = server.createContext ("/test", handler);
  49         ExecutorService executor = Executors.newCachedThreadPool();
  50         SSLContext ssl = new SimpleSSLContext(System.getProperty("test.src")).get();
  51         server.setExecutor (executor);
  52         server.setHttpsConfigurator(new HttpsConfigurator (ssl));
  53         server.start ();
  54 
  55         URL url = new URL ("https://localhost:"+server.getAddress().getPort()+"/test/foo.html");
  56         System.out.print ("Test6a: " );
  57         HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection ();
  58         urlc.setDoOutput (true);
  59         urlc.setRequestMethod ("POST");
  60         urlc.setChunkedStreamingMode (32); // small chunks
  61         urlc.setSSLSocketFactory (ssl.getSocketFactory());
  62         urlc.setHostnameVerifier (new DummyVerifier());
  63         OutputStream os = new BufferedOutputStream (urlc.getOutputStream());
  64         for (int i=0; i<SIZE; i++) {
  65             os.write (i % 100);
  66         }
  67         os.close();
  68         int resp = urlc.getResponseCode();
  69         if (resp != 200) {
  70             throw new RuntimeException ("test failed response code");
  71         }
  72         if (error) {
  73             throw new RuntimeException ("test failed error");
  74         }
  75         delay();
  76         server.stop(2);
  77         executor.shutdown();
  78         System.out.println ("OK");
  79 
  80     }
  81 
  82     public static boolean error = false;
  83     final static int SIZE = 999999;
  84 
  85     static class Handler implements HttpHandler {
  86         int invocation = 1;
  87         public void handle (HttpExchange t)
  88             throws IOException
  89         {
  90             InputStream is = t.getRequestBody();
  91             Headers map = t.getRequestHeaders();
  92             Headers rmap = t.getResponseHeaders();
  93             int c, count=0;
  94             while ((c=is.read ()) != -1) {
  95                 if (c != (count % 100)) {
  96                     error = true;
  97                     break;
  98                 }
  99                 count ++;
 100             }
 101             if (count != SIZE) {
 102                 error = true;
 103             }
 104             is.close();
 105             t.sendResponseHeaders (200, -1);
 106             t.close();
 107         }
 108     }
 109 }