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 * @summary Light weight HTTP server 28 */ 29 30import com.sun.net.httpserver.*; 31 32import java.util.*; 33import java.util.concurrent.*; 34import java.io.*; 35import java.net.*; 36import java.security.*; 37import javax.security.auth.callback.*; 38import javax.net.ssl.*; 39 40/** 41 * Test POST large file via chunked encoding (unusually small chunks) 42 */ 43 44public class Test6 extends Test { 45 46 public static void main (String[] args) throws Exception { 47 Handler handler = new Handler(); 48 InetSocketAddress addr = new InetSocketAddress (0); 49 HttpServer server = HttpServer.create (addr, 0); 50 HttpContext ctx = server.createContext ("/test", handler); 51 ExecutorService executor = Executors.newCachedThreadPool(); 52 server.setExecutor (executor); 53 server.start (); 54 55 URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html"); 56 System.out.print ("Test6: " ); 57 HttpURLConnection urlc = (HttpURLConnection)url.openConnection (); 58 urlc.setDoOutput (true); 59 urlc.setRequestMethod ("POST"); 60 urlc.setChunkedStreamingMode (32); // small chunks 61 OutputStream os = new BufferedOutputStream (urlc.getOutputStream()); 62 for (int i=0; i<SIZE; i++) { 63 os.write (i % 100); 64 } 65 os.close(); 66 int resp = urlc.getResponseCode(); 67 if (resp != 200) { 68 throw new RuntimeException ("test failed response code"); 69 } 70 if (error) { 71 throw new RuntimeException ("test failed error"); 72 } 73 delay(); 74 server.stop(2); 75 executor.shutdown(); 76 System.out.println ("OK"); 77 78 } 79 80 public static boolean error = false; 81 final static int SIZE = 999999; 82 83 static class Handler implements HttpHandler { 84 int invocation = 1; 85 public void handle (HttpExchange t) 86 throws IOException 87 { 88 InputStream is = t.getRequestBody(); 89 Headers map = t.getRequestHeaders(); 90 Headers rmap = t.getResponseHeaders(); 91 int c, count=0; 92 while ((c=is.read ()) != -1) { 93 if (c != (count % 100)) { 94 error = true; 95 break; 96 } 97 count ++; 98 } 99 if (count != SIZE) { 100 error = true; 101 } 102 is.close(); 103 t.sendResponseHeaders (200, -1); 104 t.close(); 105 } 106 } 107} 108