Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6401598.java
41154 views
/*1* Copyright (c) 2006, 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* @library /test/lib26* @bug 640159827* @summary new HttpServer cannot serve binary stream data28* @run main B640159829* @run main/othervm -Djava.net.preferIPv6Addresses=true B640159830*/3132import java.io.*;33import java.net.HttpURLConnection;34import java.net.MalformedURLException;35import java.net.Proxy;36import java.net.URL;37import java.net.InetAddress;38import java.net.InetSocketAddress;39import java.util.concurrent.*;4041import jdk.test.lib.net.URIBuilder;4243import com.sun.net.httpserver.HttpExchange;44import com.sun.net.httpserver.HttpHandler;45import com.sun.net.httpserver.HttpServer;4647public class B6401598 {4849static class MyHandler implements HttpHandler {5051public MyHandler() {5253}5455public void handle(HttpExchange arg0) throws IOException {56try {57InputStream is = arg0.getRequestBody();58OutputStream os = arg0.getResponseBody();5960DataInputStream dis = new DataInputStream(is);6162short input = dis.readShort();63while (dis.read() != -1) ;64dis.close();6566DataOutputStream dos = new DataOutputStream(os);6768arg0.sendResponseHeaders(200, 0);6970dos.writeShort(input);7172dos.flush();73dos.close();74} catch (IOException e) {75e.printStackTrace();76error = true;77}78}7980}8182static int port;83static boolean error = false;84static ExecutorService exec;85static HttpServer server;8687public static void main(String[] args) {88try {89InetAddress loopback = InetAddress.getLoopbackAddress();90server = HttpServer.create(new InetSocketAddress(loopback, 0), 400);91server.createContext("/server/", new MyHandler());92exec = Executors.newFixedThreadPool(3);93server.setExecutor(exec);94port = server.getAddress().getPort();95server.start();9697short counter;9899for (counter = 0; counter < 1000; counter++) {100URL url = URIBuilder.newBuilder()101.scheme("http")102.loopback()103.port(port)104.path("/server/")105.toURLUnchecked();106System.out.println("URL: " + url);107HttpURLConnection connection = getHttpURLConnection(url, 10000);108109OutputStream os = connection.getOutputStream();110111DataOutputStream dos = new DataOutputStream(os);112113dos.writeShort(counter);114115dos.flush();116dos.close();117118counter++;119120InputStream is = connection.getInputStream();121122DataInputStream dis = new DataInputStream(is);123124short ret = dis.readShort();125126dis.close();127}128System.out.println ("Stopping");129} catch (Exception e) {130throw new AssertionError("Unexpected exception: " + e, e);131} finally {132server.stop (1);133exec.shutdown();134}135}136137138139static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {140141HttpURLConnection httpURLConnection =142(HttpURLConnection) url.openConnection(Proxy.NO_PROXY);143144httpURLConnection.setConnectTimeout(40000);145httpURLConnection.setReadTimeout(timeout);146httpURLConnection.setDoOutput(true);147httpURLConnection.setDoInput(true);148httpURLConnection.setUseCaches(false);149httpURLConnection.setAllowUserInteraction(false);150httpURLConnection.setRequestMethod("POST");151152// HttpURLConnection httpURLConnection = new MyHttpURLConnection(url);153154return httpURLConnection;155}156}157158159