1 /* 2 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 6401598 27 * @summary new HttpServer cannot serve binary stream data 28 */ 29 30 import java.io.*; 31 import java.net.HttpURLConnection; 32 import java.net.MalformedURLException; 33 import java.net.URL; 34 import java.net.InetSocketAddress; 35 import java.util.concurrent.*; 36 37 import com.sun.net.httpserver.HttpExchange; 38 import com.sun.net.httpserver.HttpHandler; 39 import com.sun.net.httpserver.HttpServer; 40 41 public class B6401598 { 42 43 static class MyHandler implements HttpHandler { 44 MyHandler()45 public MyHandler() { 46 47 } 48 handle(HttpExchange arg0)49 public void handle(HttpExchange arg0) throws IOException { 50 try { 51 InputStream is = arg0.getRequestBody(); 52 OutputStream os = arg0.getResponseBody(); 53 54 DataInputStream dis = new DataInputStream(is); 55 56 short input = dis.readShort(); 57 while (dis.read() != -1) ; 58 dis.close(); 59 60 DataOutputStream dos = new DataOutputStream(os); 61 62 arg0.sendResponseHeaders(200, 0); 63 64 dos.writeShort(input); 65 66 dos.flush(); 67 dos.close(); 68 } catch (IOException e) { 69 e.printStackTrace(); 70 error = true; 71 } 72 } 73 74 } 75 76 static int port; 77 static boolean error = false; 78 static ExecutorService exec; 79 static HttpServer server; 80 main(String[] args)81 public static void main(String[] args) { 82 try { 83 server = HttpServer.create(new InetSocketAddress(0), 400); 84 server.createContext("/server/", new MyHandler()); 85 exec = Executors.newFixedThreadPool(3); 86 server.setExecutor(exec); 87 port = server.getAddress().getPort(); 88 server.start(); 89 90 short counter; 91 92 for (counter = 0; counter < 1000; counter++) { 93 HttpURLConnection connection = getHttpURLConnection(new URL("http://127.0.0.1:"+port+"/server/"), 10000); 94 95 OutputStream os = connection.getOutputStream(); 96 97 DataOutputStream dos = new DataOutputStream(os); 98 99 dos.writeShort(counter); 100 101 dos.flush(); 102 dos.close(); 103 104 counter++; 105 106 InputStream is = connection.getInputStream(); 107 108 DataInputStream dis = new DataInputStream(is); 109 110 short ret = dis.readShort(); 111 112 dis.close(); 113 } 114 System.out.println ("Stopping"); 115 server.stop (1); 116 exec.shutdown(); 117 } catch (IOException e) { 118 // TODO Auto-generated catch block 119 e.printStackTrace(); 120 server.stop (1); 121 exec.shutdown(); 122 } 123 } 124 125 126 getHttpURLConnection(URL url, int timeout)127 static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException { 128 129 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 130 131 httpURLConnection.setConnectTimeout(40000); 132 httpURLConnection.setReadTimeout(timeout); 133 httpURLConnection.setDoOutput(true); 134 httpURLConnection.setDoInput(true); 135 httpURLConnection.setUseCaches(false); 136 httpURLConnection.setAllowUserInteraction(false); 137 httpURLConnection.setRequestMethod("POST"); 138 139 // HttpURLConnection httpURLConnection = new MyHttpURLConnection(url); 140 141 return httpURLConnection; 142 } 143 } 144