1 /*
   2  * Copyright (c) 2005, 2010, 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 Test7a
  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 POST large file via chunked encoding (large chunks)
  40  */
  41 
  42 public class Test7a extends Test {
  43 
  44     public static void main (String[] args) throws Exception {
  45         //Logger log = Logger.getLogger ("com.sun.net.httpserver");
  46         //log.setLevel (Level.FINE);
  47         //ConsoleHandler h = new ConsoleHandler();
  48         //h.setLevel (Level.ALL);
  49         //log.addHandler (h);
  50         Handler handler = new Handler();
  51         InetSocketAddress addr = new InetSocketAddress (0);
  52         HttpsServer server = HttpsServer.create (addr, 0);
  53         HttpContext ctx = server.createContext ("/test", handler);
  54         ExecutorService executor = Executors.newCachedThreadPool();
  55         SSLContext ssl = new SimpleSSLContext(System.getProperty("test.src")).get();
  56         server.setHttpsConfigurator(new HttpsConfigurator (ssl));
  57         server.setExecutor (executor);
  58         server.start ();
  59 
  60         URL url = new URL ("https://localhost:"+server.getAddress().getPort()+"/test/foo.html");
  61         System.out.print ("Test7a: " );
  62         HttpsURLConnection urlc = (HttpsURLConnection)url.openConnection ();
  63         urlc.setDoOutput (true);
  64         urlc.setRequestMethod ("POST");
  65         urlc.setChunkedStreamingMode (16 * 1024); // big chunks
  66         urlc.setHostnameVerifier (new DummyVerifier());
  67         urlc.setSSLSocketFactory (ssl.getSocketFactory());
  68         OutputStream os = new BufferedOutputStream (urlc.getOutputStream(), 8000);
  69         for (int i=0; i<SIZE; i++) {
  70             os.write (i % 100);
  71         }
  72         os.close();
  73         int resp = urlc.getResponseCode();
  74         if (resp != 200) {
  75             throw new RuntimeException ("test failed response code");
  76         }
  77         if (error) {
  78             throw new RuntimeException ("test failed error");
  79         }
  80         delay();
  81         server.stop(2);
  82         executor.shutdown();
  83         System.out.println ("OK");
  84 
  85     }
  86 
  87     public static boolean error = false;
  88     final static int SIZE = 999999;
  89 
  90     static class Handler implements HttpHandler {
  91         int invocation = 1;
  92         public void handle (HttpExchange t)
  93             throws IOException
  94         {
  95             InputStream is = t.getRequestBody();
  96             Headers map = t.getRequestHeaders();
  97             Headers rmap = t.getResponseHeaders();
  98             int c, count=0;
  99             while ((c=is.read ()) != -1) {
 100                 if (c != (count % 100)) {
 101                     error = true;
 102                     break;
 103                 }
 104                 count ++;
 105             }
 106             if (count != SIZE) {
 107                 error = true;
 108             }
 109             is.close();
 110             t.sendResponseHeaders (200, -1);
 111             t.close();
 112         }
 113     }
 114 }