Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6526158.java
41154 views
/*1* Copyright (c) 2007, 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 652615826* @library /test/lib27* @run main B652615828* @run main/othervm -Djava.net.preferIPv6Addresses=true B652615829* @summary HttpExchange.getRequestBody().close() throws Exception30*/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 B6526158 {4445/* keep under 64 k */46final static int SIZE = 60 * 1024;4748public static void main (String[] args) throws Exception {49Handler handler = new Handler();50InetAddress loopback = InetAddress.getLoopbackAddress();51InetSocketAddress addr = new InetSocketAddress (loopback, 0);52HttpServer server = HttpServer.create (addr, 0);53HttpContext ctx = server.createContext ("/test", handler);5455ExecutorService executor = Executors.newCachedThreadPool();56server.setExecutor (executor);57server.start ();5859URL url = URIBuilder.newBuilder()60.scheme("http")61.loopback()62.port(server.getAddress().getPort())63.path("/test/foo.html")64.toURL();65HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);66urlc.setDoOutput (true);67try {68OutputStream os = new BufferedOutputStream (urlc.getOutputStream());69for (int i=0; i< SIZE; i++) {70os.write (i);71}72os.close();73InputStream is = urlc.getInputStream();74int c = 0;75while (is.read()!= -1) {76c ++;77}78is.close();79} finally {80server.stop(2);81executor.shutdown();82}83if (error) {84throw new RuntimeException ("Test failed");85}86}8788public static boolean error = false;8990static class Handler implements HttpHandler {91int invocation = 1;92public void handle (HttpExchange t)93throws IOException94{95InputStream is = t.getRequestBody();96try {97is.close();98} catch (IOException e) {99e.printStackTrace();100error = true;101}102t.sendResponseHeaders (200, -1);103t.close();104}105}106}107108109