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