1 /*
   2  * Copyright (c) 2005, 2006, 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 6270015
  27  * @run main/othervm Test9
  28  * @summary  Light weight HTTP server
  29  */
  30 
  31 import com.sun.net.httpserver.*;
  32 
  33 import java.util.concurrent.*;
  34 import java.io.*;
  35 import java.net.*;
  36 import javax.net.ssl.*;
  37 
  38 /* Same as Test1 but requests run in parallel.
  39  */
  40 
  41 public class Test9 extends Test {
  42 
  43     static SSLContext ctx;
  44     static boolean error = false;
  45 
  46     public static void main (String[] args) throws Exception {
  47         HttpServer s1 = null;
  48         HttpsServer s2 = null;
  49         ExecutorService executor=null;
  50         try {
  51             String root = System.getProperty ("test.src")+ "/docs";
  52             System.out.print ("Test9: ");
  53             InetSocketAddress addr = new InetSocketAddress (0);
  54             s1 = HttpServer.create (addr, 0);
  55             s2 = HttpsServer.create (addr, 0);
  56             HttpHandler h = new FileServerHandler (root);
  57             HttpContext c1 = s1.createContext ("/test1", h);
  58             HttpContext c2 = s2.createContext ("/test1", h);
  59             executor = Executors.newCachedThreadPool();
  60             s1.setExecutor (executor);
  61             s2.setExecutor (executor);
  62             ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
  63             s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
  64             s1.start();
  65             s2.start();
  66 
  67             int p1 = s1.getAddress().getPort();
  68             int p2 = s2.getAddress().getPort();
  69             error = false;
  70             Thread[] t = new Thread[100];
  71 
  72             t[0] = test (true, "http", root+"/test1", p1, "smallfile.txt", 23);
  73             t[1] = test (true, "http", root+"/test1", p1, "largefile.txt", 2730088);
  74             t[2] = test (true, "https", root+"/test1", p2, "smallfile.txt", 23);
  75             t[3] = test (true, "https", root+"/test1", p2, "largefile.txt", 2730088);
  76             t[4] = test (false, "http", root+"/test1", p1, "smallfile.txt", 23);
  77             t[5] = test (false, "http", root+"/test1", p1, "largefile.txt", 2730088);
  78             t[6] = test (false, "https", root+"/test1", p2, "smallfile.txt", 23);
  79             t[7] = test (false, "https", root+"/test1", p2, "largefile.txt", 2730088);
  80             t[8] = test (true, "http", root+"/test1", p1, "smallfile.txt", 23);
  81             t[9] = test (true, "http", root+"/test1", p1, "largefile.txt", 2730088);
  82             t[10] = test (true, "https", root+"/test1", p2, "smallfile.txt", 23);
  83             t[11] = test (true, "https", root+"/test1", p2, "largefile.txt", 2730088);
  84             t[12] = test (false, "http", root+"/test1", p1, "smallfile.txt", 23);
  85             t[13] = test (false, "http", root+"/test1", p1, "largefile.txt", 2730088);
  86             t[14] = test (false, "https", root+"/test1", p2, "smallfile.txt", 23);
  87             t[15] = test (false, "https", root+"/test1", p2, "largefile.txt", 2730088);
  88             for (int i=0; i<16; i++) {
  89                 t[i].join();
  90             }
  91             if (error) {
  92                 throw new RuntimeException ("error");
  93             }
  94 
  95             System.out.println ("OK");
  96         } finally {
  97             delay();
  98             if (s1 != null)
  99                 s1.stop(2);
 100             if (s2 != null)
 101                 s2.stop(2);
 102             if (executor != null)
 103                 executor.shutdown ();
 104         }
 105     }
 106 
 107     static int foo = 1;
 108 
 109     static ClientThread test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
 110         ClientThread t = new ClientThread (fixedLen, protocol, root, port, f, size);
 111         t.start();
 112         return t;
 113     }
 114 
 115     static Object fileLock = new Object();
 116 
 117     static class ClientThread extends Thread {
 118 
 119         boolean fixedLen;
 120         String protocol;
 121         String root;
 122         int port;
 123         String f;
 124         int size;
 125 
 126         ClientThread (boolean fixedLen, String protocol, String root, int port, String f, int size) {
 127             this.fixedLen = fixedLen;
 128             this.protocol = protocol;
 129             this.root = root;
 130             this.port = port;
 131             this.f =  f;
 132             this.size = size;
 133         }
 134 
 135         public void run () {
 136             try {
 137                 URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);
 138                 HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
 139                 if (urlc instanceof HttpsURLConnection) {
 140                     HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
 141                     urlcs.setHostnameVerifier (new HostnameVerifier () {
 142                         public boolean verify (String s, SSLSession s1) {
 143                             return true;
 144                         }
 145                     });
 146                     urlcs.setSSLSocketFactory (ctx.getSocketFactory());
 147                 }
 148                 byte [] buf = new byte [4096];
 149 
 150                 String s = "chunk";
 151                 if (fixedLen) {
 152                     urlc.setRequestProperty ("XFixed", "yes");
 153                     s = "fixed";
 154                 }
 155                 InputStream is = urlc.getInputStream();
 156                 File temp;
 157                 synchronized (fileLock) {
 158                     temp = File.createTempFile (s, null);
 159                     temp.deleteOnExit();
 160                 }
 161                 OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
 162                 int c, count = 0;
 163                 while ((c=is.read(buf)) != -1) {
 164                     count += c;
 165                     fout.write (buf, 0, c);
 166                 }
 167                 is.close();
 168                 fout.close();
 169 
 170                 if (count != size) {
 171                     System.out.println ("wrong amount of data returned");
 172                     System.out.println ("fixedLen = "+fixedLen);
 173                     System.out.println ("protocol = "+protocol);
 174                     System.out.println ("root = "+root);
 175                     System.out.println ("port = "+port);
 176                     System.out.println ("f = "+f);
 177                     System.out.println ("size = "+size);
 178                     System.out.println ("temp = "+temp);
 179                     System.out.println ("count = "+count);
 180                     error = true;
 181                 }
 182                 String orig = root + "/" + f;
 183                 compare (new File(orig), temp);
 184                 temp.delete();
 185             } catch (IOException e) {
 186                 error = true;
 187             }
 188         }
 189     }
 190 
 191     /* compare the contents of the two files */
 192 
 193     static void compare (File f1, File f2) throws IOException {
 194         InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
 195         InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
 196 
 197         int c1,c2;
 198         try {
 199             while ((c1=i1.read()) != -1) {
 200                 c2 = i2.read();
 201                 if (c1 != c2) {
 202                     throw new RuntimeException ("file compare failed 1");
 203                 }
 204             }
 205             if (i2.read() != -1) {
 206                 throw new RuntimeException ("file compare failed 2");
 207             }
 208         } finally {
 209             i1.close();
 210             i2.close();
 211         }
 212     }
 213 }