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