Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/B6401598.java
38867 views
/*1* Copyright (c) 2006, 2010, 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 640159826* @summary new HttpServer cannot serve binary stream data27*/2829import java.io.*;30import java.net.HttpURLConnection;31import java.net.MalformedURLException;32import java.net.URL;33import java.net.InetSocketAddress;34import java.util.concurrent.*;3536import com.sun.net.httpserver.HttpExchange;37import com.sun.net.httpserver.HttpHandler;38import com.sun.net.httpserver.HttpServer;3940public class B6401598 {4142static class MyHandler implements HttpHandler {4344public MyHandler() {4546}4748public void handle(HttpExchange arg0) throws IOException {49try {50InputStream is = arg0.getRequestBody();51OutputStream os = arg0.getResponseBody();5253DataInputStream dis = new DataInputStream(is);5455short input = dis.readShort();56while (dis.read() != -1) ;57dis.close();5859DataOutputStream dos = new DataOutputStream(os);6061arg0.sendResponseHeaders(200, 0);6263dos.writeShort(input);6465dos.flush();66dos.close();67} catch (IOException e) {68e.printStackTrace();69error = true;70}71}7273}7475static int port;76static boolean error = false;77static ExecutorService exec;78static HttpServer server;7980public static void main(String[] args) {81try {82server = HttpServer.create(new InetSocketAddress(0), 400);83server.createContext("/server/", new MyHandler());84exec = Executors.newFixedThreadPool(3);85server.setExecutor(exec);86port = server.getAddress().getPort();87server.start();8889short counter;9091for (counter = 0; counter < 1000; counter++) {92HttpURLConnection connection = getHttpURLConnection(new URL("http://127.0.0.1:"+port+"/server/"), 10000);9394OutputStream os = connection.getOutputStream();9596DataOutputStream dos = new DataOutputStream(os);9798dos.writeShort(counter);99100dos.flush();101dos.close();102103counter++;104105InputStream is = connection.getInputStream();106107DataInputStream dis = new DataInputStream(is);108109short ret = dis.readShort();110111dis.close();112}113System.out.println ("Stopping");114server.stop (1);115exec.shutdown();116} catch (IOException e) {117// TODO Auto-generated catch block118e.printStackTrace();119server.stop (1);120exec.shutdown();121}122}123124125126static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {127128HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();129130httpURLConnection.setConnectTimeout(40000);131httpURLConnection.setReadTimeout(timeout);132httpURLConnection.setDoOutput(true);133httpURLConnection.setDoInput(true);134httpURLConnection.setUseCaches(false);135httpURLConnection.setAllowUserInteraction(false);136httpURLConnection.setRequestMethod("POST");137138// HttpURLConnection httpURLConnection = new MyHttpURLConnection(url);139140return httpURLConnection;141}142}143144145