Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test9a.java
38855 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 627001526* @run main/othervm Test9a27* @summary Light weight HTTP server28*/2930import com.sun.net.httpserver.*;3132import java.util.concurrent.*;33import java.io.*;34import java.net.*;35import javax.net.ssl.*;3637/* Same as Test1 but requests run in parallel.38*/3940public class Test9a extends Test {4142static SSLContext serverCtx;43static volatile SSLContext clientCtx = null;44static volatile boolean error = false;4546public static void main (String[] args) throws Exception {47HttpsServer server = null;48ExecutorService executor=null;49try {50String root = System.getProperty ("test.src")+ "/docs";51System.out.print ("Test9a: ");52InetSocketAddress addr = new InetSocketAddress (0);53server = HttpsServer.create (addr, 0);54HttpHandler h = new FileServerHandler (root);55HttpContext c1 = server.createContext ("/test1", h);56executor = Executors.newCachedThreadPool();57server.setExecutor (executor);58serverCtx = new SimpleSSLContext(System.getProperty("test.src")).get();59clientCtx = new SimpleSSLContext(System.getProperty("test.src")).get();60server.setHttpsConfigurator(new HttpsConfigurator (serverCtx));61server.start();6263int port = server.getAddress().getPort();64error = false;65Thread[] t = new Thread[100];6667t[0] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);68t[1] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);69t[2] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);70t[3] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);71t[4] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);72t[5] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);73t[6] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);74t[7] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);75t[8] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);76t[9] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);77t[10] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);78t[11] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);79t[12] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);80t[13] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);81t[14] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);82t[15] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);83for (int i=0; i<16; i++) {84t[i].join();85}86if (error) {87throw new RuntimeException ("error");88}8990System.out.println ("OK");91} finally {92delay();93if (server != null)94server.stop(2);95if (executor != null)96executor.shutdown();97}98}99100static int foo = 1;101102static ClientThread test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {103ClientThread t = new ClientThread (fixedLen, protocol, root, port, f, size);104t.start();105return t;106}107108static Object fileLock = new Object();109110static class ClientThread extends Thread {111112boolean fixedLen;113String protocol;114String root;115int port;116String f;117int size;118119ClientThread (boolean fixedLen, String protocol, String root, int port, String f, int size) {120this.fixedLen = fixedLen;121this.protocol = protocol;122this.root = root;123this.port = port;124this.f = f;125this.size = size;126}127128public void run () {129try {130URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);131HttpURLConnection urlc = (HttpURLConnection) url.openConnection();132if (urlc instanceof HttpsURLConnection) {133HttpsURLConnection urlcs = (HttpsURLConnection) urlc;134urlcs.setHostnameVerifier (new HostnameVerifier () {135public boolean verify (String s, SSLSession s1) {136return true;137}138});139urlcs.setSSLSocketFactory (clientCtx.getSocketFactory());140}141byte [] buf = new byte [4096];142143String s = "chunk";144if (fixedLen) {145urlc.setRequestProperty ("XFixed", "yes");146s = "fixed";147}148InputStream is = urlc.getInputStream();149File temp;150synchronized (fileLock) {151temp = File.createTempFile (s, null);152temp.deleteOnExit();153}154OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));155int c, count = 0;156while ((c=is.read(buf)) != -1) {157count += c;158fout.write (buf, 0, c);159}160is.close();161fout.close();162163if (count != size) {164System.out.println ("wrong amount of data returned");165System.out.println ("fixedLen = "+fixedLen);166System.out.println ("protocol = "+protocol);167System.out.println ("root = "+root);168System.out.println ("port = "+port);169System.out.println ("f = "+f);170System.out.println ("size = "+size);171System.out.println ("temp = "+temp);172System.out.println ("count = "+count);173error = true;174}175String orig = root + "/" + f;176compare (new File(orig), temp);177temp.delete();178} catch (IOException e) {179e.printStackTrace();180error = true;181}182}183}184185/* compare the contents of the two files */186187static void compare (File f1, File f2) throws IOException {188InputStream i1 = new BufferedInputStream (new FileInputStream(f1));189InputStream i2 = new BufferedInputStream (new FileInputStream(f2));190191int c1,c2;192try {193while ((c1=i1.read()) != -1) {194c2 = i2.read();195if (c1 != c2) {196throw new RuntimeException ("file compare failed 1");197}198}199if (i2.read() != -1) {200throw new RuntimeException ("file compare failed 2");201}202} finally {203i1.close();204i2.close();205}206}207}208209210