Path: blob/master/test/jdk/com/sun/net/httpserver/Test9.java
41152 views
/*1* Copyright (c) 2005, 2019, 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* @library /test/lib27* @build jdk.test.lib.net.SimpleSSLContext jdk.test.lib.net.URIBuilder28* @run main/othervm Test929* @run main/othervm -Djava.net.preferIPv6Addresses=true Test930* @summary Light weight HTTP server31*/3233import com.sun.net.httpserver.*;3435import java.util.concurrent.*;36import java.io.*;37import java.net.*;38import javax.net.ssl.*;39import jdk.test.lib.net.SimpleSSLContext;40import jdk.test.lib.net.URIBuilder;4142/* Same as Test1 but requests run in parallel.43*/4445public class Test9 extends Test {4647static SSLContext ctx;48static boolean error = false;4950public static void main (String[] args) throws Exception {51HttpServer s1 = null;52HttpsServer s2 = null;53ExecutorService executor=null;54try {55String root = System.getProperty ("test.src")+ "/docs";56System.out.print ("Test9: ");57InetAddress loopback = InetAddress.getLoopbackAddress();58InetSocketAddress addr = new InetSocketAddress(loopback, 0);59s1 = HttpServer.create (addr, 0);60s2 = HttpsServer.create (addr, 0);61HttpHandler h = new FileServerHandler (root);62HttpContext c1 = s1.createContext ("/test1", h);63HttpContext c2 = s2.createContext ("/test1", h);64executor = Executors.newCachedThreadPool();65s1.setExecutor (executor);66s2.setExecutor (executor);67ctx = new SimpleSSLContext().get();68s2.setHttpsConfigurator(new HttpsConfigurator (ctx));69s1.start();70s2.start();7172int p1 = s1.getAddress().getPort();73int p2 = s2.getAddress().getPort();74error = false;75Thread[] t = new Thread[100];7677t[0] = test (true, "http", root+"/test1", p1, "smallfile.txt", 23);78t[1] = test (true, "http", root+"/test1", p1, "largefile.txt", 2730088);79t[2] = test (true, "https", root+"/test1", p2, "smallfile.txt", 23);80t[3] = test (true, "https", root+"/test1", p2, "largefile.txt", 2730088);81t[4] = test (false, "http", root+"/test1", p1, "smallfile.txt", 23);82t[5] = test (false, "http", root+"/test1", p1, "largefile.txt", 2730088);83t[6] = test (false, "https", root+"/test1", p2, "smallfile.txt", 23);84t[7] = test (false, "https", root+"/test1", p2, "largefile.txt", 2730088);85t[8] = test (true, "http", root+"/test1", p1, "smallfile.txt", 23);86t[9] = test (true, "http", root+"/test1", p1, "largefile.txt", 2730088);87t[10] = test (true, "https", root+"/test1", p2, "smallfile.txt", 23);88t[11] = test (true, "https", root+"/test1", p2, "largefile.txt", 2730088);89t[12] = test (false, "http", root+"/test1", p1, "smallfile.txt", 23);90t[13] = test (false, "http", root+"/test1", p1, "largefile.txt", 2730088);91t[14] = test (false, "https", root+"/test1", p2, "smallfile.txt", 23);92t[15] = test (false, "https", root+"/test1", p2, "largefile.txt", 2730088);93for (int i=0; i<16; i++) {94t[i].join();95}96if (error) {97throw new RuntimeException ("error");98}99100System.out.println ("OK");101} finally {102delay();103if (s1 != null)104s1.stop(2);105if (s2 != null)106s2.stop(2);107if (executor != null)108executor.shutdown ();109}110}111112static int foo = 1;113114static ClientThread test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {115ClientThread t = new ClientThread (fixedLen, protocol, root, port, f, size);116t.start();117return t;118}119120static Object fileLock = new Object();121122static class ClientThread extends Thread {123124boolean fixedLen;125String protocol;126String root;127int port;128String f;129int size;130131ClientThread (boolean fixedLen, String protocol, String root, int port, String f, int size) {132this.fixedLen = fixedLen;133this.protocol = protocol;134this.root = root;135this.port = port;136this.f = f;137this.size = size;138}139140public void run () {141try {142URL url = URIBuilder.newBuilder()143.scheme(protocol)144.loopback()145.port(port)146.path("/test1/" + f)147.toURL();148149HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);150if (urlc instanceof HttpsURLConnection) {151HttpsURLConnection urlcs = (HttpsURLConnection) urlc;152urlcs.setHostnameVerifier (new HostnameVerifier () {153public boolean verify (String s, SSLSession s1) {154return true;155}156});157urlcs.setSSLSocketFactory (ctx.getSocketFactory());158}159byte [] buf = new byte [4096];160161String s = "chunk";162if (fixedLen) {163urlc.setRequestProperty ("XFixed", "yes");164s = "fixed";165}166InputStream is = urlc.getInputStream();167File temp;168synchronized (fileLock) {169temp = File.createTempFile (s, null);170temp.deleteOnExit();171}172OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));173int c, count = 0;174while ((c=is.read(buf)) != -1) {175count += c;176fout.write (buf, 0, c);177}178is.close();179fout.close();180181if (count != size) {182System.out.println ("wrong amount of data returned");183System.out.println ("fixedLen = "+fixedLen);184System.out.println ("protocol = "+protocol);185System.out.println ("root = "+root);186System.out.println ("port = "+port);187System.out.println ("f = "+f);188System.out.println ("size = "+size);189System.out.println ("temp = "+temp);190System.out.println ("count = "+count);191error = true;192}193String orig = root + "/" + f;194compare (new File(orig), temp);195temp.delete();196} catch (Exception e) {197e.printStackTrace();198error = true;199}200}201}202203/* compare the contents of the two files */204205static void compare (File f1, File f2) throws IOException {206InputStream i1 = new BufferedInputStream (new FileInputStream(f1));207InputStream i2 = new BufferedInputStream (new FileInputStream(f2));208209int c1,c2;210try {211while ((c1=i1.read()) != -1) {212c2 = i2.read();213if (c1 != c2) {214throw new RuntimeException ("file compare failed 1");215}216}217if (i2.read() != -1) {218throw new RuntimeException ("file compare failed 2");219}220} finally {221i1.close();222i2.close();223}224}225}226227228