Path: blob/master/test/jdk/com/sun/net/httpserver/Test10.java
41152 views
/*1* Copyright (c) 2010, 2019, 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 700501626* @summary pit jdk7 b121 sqe test jhttp/HttpServer150013 failing27* @run main/othervm -Dsun.net.httpserver.clockTick=1000 -Dsun.net.httpserver.idleInterval=3 Test1028* @run main/othervm -Dsun.net.httpserver.clockTick=1000 -Dsun.net.httpserver.idleInterval=329* -Djava.net.preferIPv6Addresses Test1030*/3132import com.sun.net.httpserver.*;3334import java.io.*;35import java.net.*;36import java.util.concurrent.*;3738/*39* Test handling of empty Http headers40*/4142public class Test10 extends Test {43public static void main (String[] args) throws Exception {44System.out.print ("Test10: ");45Handler handler = new Handler();46InetAddress loopback = InetAddress.getLoopbackAddress();47InetSocketAddress addr = new InetSocketAddress(loopback, 0);48HttpServer server = HttpServer.create (addr, 0);49int port = server.getAddress().getPort();50HttpContext c2 = server.createContext ("/test", handler);5152ExecutorService exec = Executors.newCachedThreadPool();53server.setExecutor (exec);54try {55server.start ();56doClient(port);57System.out.println ("OK");58} finally {59delay();60if (server != null)61server.stop(2);62if (exec != null)63exec.shutdown();64}65}6667static class Handler implements HttpHandler {68volatile int invocation = 0;69public void handle (HttpExchange t)70throws IOException71{72InputStream is = t.getRequestBody();73while (is.read() != -1);74Headers map = t.getRequestHeaders();75t.sendResponseHeaders (200, -1);76t.close();77}78}7980public static void doClient (int port) throws Exception {81String s = "GET /test/1.html HTTP/1.1\r\n\r\n";8283Socket socket = new Socket (InetAddress.getLoopbackAddress(), port);84OutputStream os = socket.getOutputStream();85os.write (s.getBytes());86socket.setSoTimeout (10 * 1000);87InputStream is = socket.getInputStream();88int c;89byte[] b = new byte [1024];90while ((c=is.read(b)) != -1) ;91is.close();92socket.close();93}94}959697