Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/bugs/B6373555.java
38867 views
/*1* Copyright (c) 2006, 2011, 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 637355526* @summary HTTP Server failing to answer client requests27*/2829import java.net.*;30import java.io.*;31import javax.xml.soap.*;32import java.util.*;33import com.sun.net.httpserver.*;34import java.util.concurrent.*;3536public class B6373555 {3738private static int s_received = 0;39private static int sent = 0;4041private static int received = 0;42private static int port;4344private static volatile boolean error = false;45static HttpServer httpServer;46static ExecutorService pool, execs;47static int NUM = 1000;4849public static void main(String[] args) throws Exception {50try {51if (args.length > 0) {52NUM = Integer.parseInt (args[0]);53}54execs = Executors.newFixedThreadPool(5);55httpServer = createHttpServer(execs);56port = httpServer.getAddress().getPort();57pool = Executors.newFixedThreadPool(10);58httpServer.start();59for (int i=0; i < NUM; i++) {60pool.execute(new Client());61if (error) {62throw new Exception ("error in test");63}64}65System.out.println("Main thread waiting");66pool.shutdown();67long latest = System.currentTimeMillis() + 200 * 1000;68while (System.currentTimeMillis() < latest) {69if (pool.awaitTermination(2000L, TimeUnit.MILLISECONDS)) {70System.out.println("Main thread done!");71return;72}73if (error) {74throw new Exception ("error in test");75}76}77throw new Exception ("error in test: timed out");78} finally {79httpServer.stop(0);80pool.shutdownNow();81execs.shutdownNow();82}83}8485public static class Client implements Runnable {8687byte[] getBuf () {88byte[] buf = new byte [5200];89for (int i=0; i< 5200; i++) {90buf [i] = (byte)i;91}92return buf;93}9495public void run() {96try {97Thread.sleep(10);98byte[] buf = getBuf();99URL url = new URL("http://127.0.0.1:"+port+"/test");100HttpURLConnection con = (HttpURLConnection)url.openConnection();101con.setDoOutput(true);102con.setDoInput(true);103con.setRequestMethod("POST");104con.setRequestProperty(105"Content-Type",106"Multipart/Related; type=\"application/xop+xml\"; boundary=\"----=_Part_0_6251267.1128549570165\"; start-info=\"text/xml\"");107OutputStream out = con.getOutputStream();108out.write(buf);109out.close();110InputStream in = con.getInputStream();111byte[] newBuf = readFully(in);112in.close();113if (buf.length != newBuf.length) {114System.out.println("Doesn't match");115error = true;116}117}118catch(Exception e) {119e.printStackTrace();120System.out.print (".");121error = true;122}123}124}125126private static byte[] readFully(InputStream istream) throws IOException {127ByteArrayOutputStream bout = new ByteArrayOutputStream();128byte[] buf = new byte[1024];129int num = 0;130131if (istream != null) {132while ((num = istream.read(buf)) != -1) {133bout.write(buf, 0, num);134}135}136byte[] ret = bout.toByteArray();137return ret;138}139140141private static HttpServer createHttpServer(ExecutorService execs)142throws Exception {143InetSocketAddress inetAddress = new InetSocketAddress(0);144HttpServer testServer = HttpServer.create(inetAddress, 15);145testServer.setExecutor(execs);146HttpContext context = testServer.createContext("/test");147context.setHandler(new HttpHandler() {148public void handle(HttpExchange msg) {149try {150String method = msg.getRequestMethod();151if (method.equals("POST")) {152InputStream is = msg.getRequestBody();153byte[] buf = readFully(is);154is.close();155writePostReply(msg, buf);156} else {157System.out.println("****** METHOD not handled ***** "+method);158System.out.println("Received="+s_received);159}160}161catch(Exception e) {162e.printStackTrace();163}164finally {165msg.close();166}167}168}169);170return testServer;171}172173private static void writePostReply(HttpExchange msg, byte[] buf)174throws Exception {175msg.getResponseHeaders().add("Content-Type", "text/xml");176msg.sendResponseHeaders(200, buf.length);177OutputStream out = msg.getResponseBody();178out.write(buf);179out.close();180}181182}183184185