Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/B6431193.java
38867 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 643119326* @summary The new HTTP server exits immediately27*/2829import java.io.IOException;30import java.io.InputStream;31import java.io.OutputStream;32import java.net.*;3334import com.sun.net.httpserver.*;3536public class B6431193 {3738static boolean error = false;3940public static void read (InputStream i) throws IOException {41while (i.read() != -1);42i.close();43}4445/**46* @param args47*/48public static void main(String[] args) {49class MyHandler implements HttpHandler {50public void handle(HttpExchange t) throws IOException {51InputStream is = t.getRequestBody();52read(is);53// .. read the request body54String response = "This is the response";55t.sendResponseHeaders(200, response.length());56OutputStream os = t.getResponseBody();57os.write(response.getBytes());58os.close();59error = Thread.currentThread().isDaemon();60}61}626364HttpServer server;65try {66server = HttpServer.create(new InetSocketAddress(0), 10);6768server.createContext("/apps", new MyHandler());69server.setExecutor(null);70// creates a default executor71server.start();72int port = server.getAddress().getPort();73String s = "http://localhost:"+port+"/apps/foo";74URL url = new URL (s);75InputStream is = url.openStream();76read (is);77server.stop (1);78if (error) {79throw new RuntimeException ("error in test");80}8182}83catch (IOException e) {84// TODO Auto-generated catch block85e.printStackTrace();86}87}88}899091