1/* 2 * Copyright (c) 2006, 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 6431193 27 * @summary The new HTTP server exits immediately 28 */ 29 30import java.io.IOException; 31import java.io.InputStream; 32import java.io.OutputStream; 33import java.net.*; 34 35import com.sun.net.httpserver.*; 36 37public class B6431193 { 38 39 static boolean error = false; 40 41 public static void read (InputStream i) throws IOException { 42 while (i.read() != -1); 43 i.close(); 44 } 45 46 /** 47 * @param args 48 */ 49 public static void main(String[] args) { 50 class MyHandler implements HttpHandler { 51 public void handle(HttpExchange t) throws IOException { 52 InputStream is = t.getRequestBody(); 53 read(is); 54 // .. read the request body 55 String response = "This is the response"; 56 t.sendResponseHeaders(200, response.length()); 57 OutputStream os = t.getResponseBody(); 58 os.write(response.getBytes()); 59 os.close(); 60 error = Thread.currentThread().isDaemon(); 61 } 62 } 63 64 65 HttpServer server; 66 try { 67 server = HttpServer.create(new InetSocketAddress(0), 10); 68 69 server.createContext("/apps", new MyHandler()); 70 server.setExecutor(null); 71 // creates a default executor 72 server.start(); 73 int port = server.getAddress().getPort(); 74 String s = "http://localhost:"+port+"/apps/foo"; 75 URL url = new URL (s); 76 InputStream is = url.openStream(); 77 read (is); 78 server.stop (1); 79 if (error) { 80 throw new RuntimeException ("error in test"); 81 } 82 83 } 84 catch (IOException e) { 85 // TODO Auto-generated catch block 86 e.printStackTrace(); 87 } 88 } 89} 90