Path: blob/master/test/jdk/com/sun/net/httpserver/Test13.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.SimpleSSLContext28* @run main/othervm Test1329* @run main/othervm -Djava.net.preferIPv6Addresses=true Test1330* @summary Light weight HTTP server31*/3233import com.sun.net.httpserver.*;3435import java.util.concurrent.*;36import java.util.logging.*;37import java.io.*;38import java.net.*;39import javax.net.ssl.*;40import jdk.test.lib.net.SimpleSSLContext;41import jdk.test.lib.net.URIBuilder;4243/* basic http/s connectivity test44* Tests:45* - same as Test12, but with 64 threads46*/4748public class Test13 extends Test {4950static SSLContext ctx;5152final static int NUM = 32; // was 325354static boolean fail = false;5556public static void main (String[] args) throws Exception {57HttpServer s1 = null;58HttpsServer s2 = null;59ExecutorService executor=null;60Logger l = Logger.getLogger ("com.sun.net.httpserver");61Handler ha = new ConsoleHandler();62ha.setLevel(Level.ALL);63l.setLevel(Level.ALL);64l.addHandler(ha);65InetAddress loopback = InetAddress.getLoopbackAddress();6667try {68String root = System.getProperty ("test.src")+ "/docs";69System.out.print ("Test13: ");70InetSocketAddress addr = new InetSocketAddress(loopback, 0);71s1 = HttpServer.create (addr, 0);72s2 = HttpsServer.create (addr, 0);73HttpHandler h = new FileServerHandler (root);74HttpContext c1 = s1.createContext ("/test1", h);75HttpContext c2 = s2.createContext ("/test1", h);76executor = Executors.newCachedThreadPool();77s1.setExecutor (executor);78s2.setExecutor (executor);79ctx = new SimpleSSLContext().get();80s2.setHttpsConfigurator(new HttpsConfigurator (ctx));81s1.start();82s2.start();8384int port = s1.getAddress().getPort();85int httpsport = s2.getAddress().getPort();86Runner r[] = new Runner[NUM*2];87for (int i=0; i<NUM; i++) {88r[i] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23);89r[i+NUM] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);90}91start (r);92join (r);93System.out.println ("OK");94} finally {95delay();96if (s1 != null)97s1.stop(2);98if (s2 != null)99s2.stop(2);100if (executor != null)101executor.shutdown ();102}103}104105static void start (Runner[] x) {106for (int i=0; i<x.length; i++) {107if (x[i] != null)108x[i].start();109}110}111112static void join (Runner[] x) {113for (int i=0; i<x.length; i++) {114try {115if (x[i] != null)116x[i].join();117} catch (InterruptedException e) {}118}119}120121122static class Runner extends Thread {123124boolean fixedLen;125String protocol;126String root;127int port;128String f;129int size;130131Runner (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();148HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);149if (urlc instanceof HttpsURLConnection) {150HttpsURLConnection urlcs = (HttpsURLConnection) urlc;151urlcs.setHostnameVerifier (new HostnameVerifier () {152public boolean verify (String s, SSLSession s1) {153return true;154}155});156urlcs.setSSLSocketFactory (ctx.getSocketFactory());157}158byte [] buf = new byte [4096];159160if (fixedLen) {161urlc.setRequestProperty ("XFixed", "yes");162}163InputStream is = urlc.getInputStream();164File temp = File.createTempFile ("Test1", null);165temp.deleteOnExit();166OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));167int c, count = 0;168while ((c=is.read(buf)) != -1) {169count += c;170fout.write (buf, 0, c);171}172is.close();173fout.close();174175if (count != size) {176throw new RuntimeException ("wrong amount of data returned");177}178String orig = root + "/" + f;179compare (new File(orig), temp);180temp.delete();181} catch (Exception e) {182e.printStackTrace();183fail = true;184}185}186}187188/* compare the contents of the two files */189190static void compare (File f1, File f2) throws IOException {191InputStream i1 = new BufferedInputStream (new FileInputStream(f1));192InputStream i2 = new BufferedInputStream (new FileInputStream(f2));193194int c1,c2;195try {196while ((c1=i1.read()) != -1) {197c2 = i2.read();198if (c1 != c2) {199throw new RuntimeException ("file compare failed 1");200}201}202if (i2.read() != -1) {203throw new RuntimeException ("file compare failed 2");204}205} finally {206i1.close();207i2.close();208}209}210}211212213