Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6373555.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* @bug 637355526* @library /test/lib27* @summary HTTP Server failing to answer client requests28* @run main B637355529* @run main/othervm -Djava.net.preferIPv6Addresses=true B637355530*/3132import java.net.*;33import java.io.*;34import java.util.*;35import com.sun.net.httpserver.*;36import java.util.concurrent.*;37import jdk.test.lib.net.URIBuilder;3839public class B6373555 {4041private static int s_received = 0;42private static int sent = 0;4344private static int received = 0;45private static int port;4647private static volatile boolean error = false;48static HttpServer httpServer;49static ExecutorService pool, execs;50static int NUM = 1000;5152public static void main(String[] args) throws Exception {53try {54if (args.length > 0) {55NUM = Integer.parseInt (args[0]);56}57execs = Executors.newFixedThreadPool(5);58httpServer = createHttpServer(execs);59port = httpServer.getAddress().getPort();60pool = Executors.newFixedThreadPool(10);61httpServer.start();62for (int i=0; i < NUM; i++) {63pool.execute(new Client());64if (error) {65throw new Exception ("error in test");66}67}68System.out.println("Main thread waiting");69pool.shutdown();70long latest = System.currentTimeMillis() + 200 * 1000;71while (System.currentTimeMillis() < latest) {72if (pool.awaitTermination(2000L, TimeUnit.MILLISECONDS)) {73System.out.println("Main thread done!");74return;75}76if (error) {77throw new Exception ("error in test");78}79}80throw new Exception ("error in test: timed out");81} finally {82httpServer.stop(0);83pool.shutdownNow();84execs.shutdownNow();85}86}8788public static class Client implements Runnable {8990byte[] getBuf () {91byte[] buf = new byte [5200];92for (int i=0; i< 5200; i++) {93buf [i] = (byte)i;94}95return buf;96}9798public void run() {99try {100Thread.sleep(10);101byte[] buf = getBuf();102URL url = URIBuilder.newBuilder()103.scheme("http")104.loopback()105.port(port)106.path("/test")107.toURLUnchecked();108System.out.println("URL: " + url);109HttpURLConnection con = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);110con.setDoOutput(true);111con.setDoInput(true);112con.setRequestMethod("POST");113con.setRequestProperty(114"Content-Type",115"Multipart/Related; type=\"application/xop+xml\"; boundary=\"----=_Part_0_6251267.1128549570165\"; start-info=\"text/xml\"");116OutputStream out = con.getOutputStream();117out.write(buf);118out.close();119InputStream in = con.getInputStream();120byte[] newBuf = readFully(in);121in.close();122if (buf.length != newBuf.length) {123System.out.println("Doesn't match");124error = true;125}126}127catch(Exception e) {128e.printStackTrace();129System.out.print (".");130error = true;131}132}133}134135private static byte[] readFully(InputStream istream) throws IOException {136ByteArrayOutputStream bout = new ByteArrayOutputStream();137byte[] buf = new byte[1024];138int num = 0;139140if (istream != null) {141while ((num = istream.read(buf)) != -1) {142bout.write(buf, 0, num);143}144}145byte[] ret = bout.toByteArray();146return ret;147}148149150private static HttpServer createHttpServer(ExecutorService execs)151throws Exception {152InetAddress loopback = InetAddress.getLoopbackAddress();153InetSocketAddress inetAddress = new InetSocketAddress(loopback, 0);154HttpServer testServer = HttpServer.create(inetAddress, 15);155testServer.setExecutor(execs);156HttpContext context = testServer.createContext("/test");157context.setHandler(new HttpHandler() {158public void handle(HttpExchange msg) {159try {160String method = msg.getRequestMethod();161if (method.equals("POST")) {162InputStream is = msg.getRequestBody();163byte[] buf = readFully(is);164is.close();165writePostReply(msg, buf);166} else {167System.out.println("****** METHOD not handled ***** "+method);168System.out.println("Received="+s_received);169}170}171catch(Exception e) {172e.printStackTrace();173}174finally {175msg.close();176}177}178}179);180return testServer;181}182183private static void writePostReply(HttpExchange msg, byte[] buf)184throws Exception {185msg.getResponseHeaders().add("Content-Type", "text/xml");186msg.sendResponseHeaders(200, buf.length);187OutputStream out = msg.getResponseBody();188out.write(buf);189out.close();190}191192}193194195