Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6744329.java
41154 views
/*1* Copyright (c) 2005, 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 674432926* @summary Exception in light weight Http server27* @library /test/lib28* @run main B674432929* @run main/othervm -Djava.net.preferIPv6Addresses=true B674432930*/3132import com.sun.net.httpserver.*;3334import java.util.*;35import java.util.concurrent.*;36import java.io.*;37import java.net.*;38import java.security.*;39import java.security.cert.*;40import javax.net.ssl.*;41import jdk.test.lib.net.URIBuilder;4243public class B6744329 {4445public static void main (String[] args) throws Exception {46Handler handler = new Handler();47InetAddress loopback = InetAddress.getLoopbackAddress();48InetSocketAddress addr = new InetSocketAddress (loopback, 0);49HttpServer server = HttpServer.create (addr, 0);50HttpContext ctx = server.createContext ("/test", handler);51ExecutorService executor = Executors.newCachedThreadPool();52server.setExecutor (executor);53server.start ();5455URL url = URIBuilder.newBuilder()56.scheme("http")57.loopback()58.port(server.getAddress().getPort())59.path("/test/foo.html")60.toURL();61HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);62try {63InputStream is = urlc.getInputStream();64int c = 0;65while (is.read()!= -1) {66c ++;67}68System.out.println ("OK");69} catch (IOException e) {70System.out.println ("exception");71e.printStackTrace();72error = true;73}74server.stop(2);75executor.shutdown();76if (error) {77throw new RuntimeException ("Test failed");78}79}8081public static boolean error = false;8283/* this must be the same size as in ChunkedOutputStream.java84*/85final static int CHUNK_SIZE = 4096;8687static class Handler implements HttpHandler {88int invocation = 1;89public void handle (HttpExchange t)90throws IOException91{92InputStream is = t.getRequestBody();93Headers map = t.getRequestHeaders();94Headers rmap = t.getResponseHeaders();95while (is.read () != -1) ;96is.close();97/* chunked response */98t.sendResponseHeaders (200, 0);99OutputStream os = t.getResponseBody();100byte[] first = new byte [CHUNK_SIZE * 2];101byte[] second = new byte [2];102os.write (first);103os.write ('x');104os.write ('x');105/* An index out of bounds exception will be thrown106* below, which is caught by server, and connection107* will be closed. resulting in IOException to client108* - if bug present109*/110os.write ('x');111os.write ('x');112os.write ('x');113t.close();114}115}116}117118119