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