0N/A/*
3261N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6270015
2612N/A * @run main/othervm Test9a
0N/A * @summary Light weight HTTP server
0N/A */
0N/A
0N/Aimport com.sun.net.httpserver.*;
0N/A
0N/Aimport java.util.concurrent.*;
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport javax.net.ssl.*;
0N/A
0N/A/* Same as Test1 but requests run in parallel.
0N/A */
0N/A
0N/Apublic class Test9a extends Test {
0N/A
0N/A static SSLContext serverCtx, clientCtx;
0N/A static boolean error = false;
0N/A
0N/A public static void main (String[] args) throws Exception {
0N/A HttpsServer server = null;
0N/A ExecutorService executor=null;
0N/A try {
0N/A String root = System.getProperty ("test.src")+ "/docs";
0N/A System.out.print ("Test9a: ");
0N/A InetSocketAddress addr = new InetSocketAddress (0);
0N/A server = HttpsServer.create (addr, 0);
0N/A HttpHandler h = new FileServerHandler (root);
0N/A HttpContext c1 = server.createContext ("/test1", h);
0N/A executor = Executors.newCachedThreadPool();
0N/A server.setExecutor (executor);
0N/A serverCtx = new SimpleSSLContext(System.getProperty("test.src")).get();
0N/A clientCtx = new SimpleSSLContext(System.getProperty("test.src")).get();
0N/A server.setHttpsConfigurator(new HttpsConfigurator (serverCtx));
0N/A server.start();
0N/A
0N/A int port = server.getAddress().getPort();
0N/A error = false;
0N/A Thread[] t = new Thread[100];
0N/A
0N/A t[0] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
0N/A t[1] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
0N/A t[2] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
0N/A t[3] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
0N/A t[4] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
0N/A t[5] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
0N/A t[6] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
0N/A t[7] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
0N/A t[8] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
0N/A t[9] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
0N/A t[10] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
0N/A t[11] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
0N/A t[12] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
0N/A t[13] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
0N/A t[14] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
0N/A t[15] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
0N/A for (int i=0; i<16; i++) {
0N/A t[i].join();
0N/A }
0N/A if (error) {
0N/A throw new RuntimeException ("error");
0N/A }
0N/A
0N/A System.out.println ("OK");
0N/A } finally {
0N/A delay();
1324N/A if (server != null)
1324N/A server.stop(2);
1324N/A if (executor != null)
1324N/A executor.shutdown();
0N/A }
0N/A }
0N/A
0N/A static int foo = 1;
0N/A
0N/A static ClientThread test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
0N/A ClientThread t = new ClientThread (fixedLen, protocol, root, port, f, size);
0N/A t.start();
0N/A return t;
0N/A }
0N/A
0N/A static Object fileLock = new Object();
0N/A
0N/A static class ClientThread extends Thread {
0N/A
0N/A boolean fixedLen;
0N/A String protocol;
0N/A String root;
0N/A int port;
0N/A String f;
0N/A int size;
0N/A
0N/A ClientThread (boolean fixedLen, String protocol, String root, int port, String f, int size) {
0N/A this.fixedLen = fixedLen;
0N/A this.protocol = protocol;
0N/A this.root = root;
0N/A this.port = port;
0N/A this.f = f;
0N/A this.size = size;
0N/A }
0N/A
0N/A public void run () {
0N/A try {
0N/A URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);
0N/A HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
0N/A if (urlc instanceof HttpsURLConnection) {
0N/A HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
0N/A urlcs.setHostnameVerifier (new HostnameVerifier () {
0N/A public boolean verify (String s, SSLSession s1) {
0N/A return true;
0N/A }
0N/A });
0N/A urlcs.setSSLSocketFactory (clientCtx.getSocketFactory());
0N/A }
0N/A byte [] buf = new byte [4096];
0N/A
0N/A String s = "chunk";
0N/A if (fixedLen) {
0N/A urlc.setRequestProperty ("XFixed", "yes");
0N/A s = "fixed";
0N/A }
0N/A InputStream is = urlc.getInputStream();
0N/A File temp;
0N/A synchronized (fileLock) {
0N/A temp = File.createTempFile (s, null);
0N/A temp.deleteOnExit();
0N/A }
0N/A OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
0N/A int c, count = 0;
0N/A while ((c=is.read(buf)) != -1) {
0N/A count += c;
0N/A fout.write (buf, 0, c);
0N/A }
0N/A is.close();
0N/A fout.close();
0N/A
0N/A if (count != size) {
0N/A System.out.println ("wrong amount of data returned");
0N/A System.out.println ("fixedLen = "+fixedLen);
0N/A System.out.println ("protocol = "+protocol);
0N/A System.out.println ("root = "+root);
0N/A System.out.println ("port = "+port);
0N/A System.out.println ("f = "+f);
0N/A System.out.println ("size = "+size);
0N/A System.out.println ("temp = "+temp);
0N/A System.out.println ("count = "+count);
0N/A error = true;
0N/A }
0N/A String orig = root + "/" + f;
0N/A compare (new File(orig), temp);
0N/A temp.delete();
0N/A } catch (IOException e) {
0N/A error = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /* compare the contents of the two files */
0N/A
0N/A static void compare (File f1, File f2) throws IOException {
0N/A InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
0N/A InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
0N/A
0N/A int c1,c2;
0N/A try {
0N/A while ((c1=i1.read()) != -1) {
0N/A c2 = i2.read();
0N/A if (c1 != c2) {
0N/A throw new RuntimeException ("file compare failed 1");
0N/A }
0N/A }
0N/A if (i2.read() != -1) {
0N/A throw new RuntimeException ("file compare failed 2");
0N/A }
0N/A } finally {
0N/A i1.close();
0N/A i2.close();
0N/A }
0N/A }
0N/A}