Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/net/httpserver/Test9.java
38855 views
/*1* Copyright (c) 2005, 2010, 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 Test927* @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 Test9 extends Test {4142static SSLContext ctx;43static boolean error = false;4445public static void main (String[] args) throws Exception {46HttpServer s1 = null;47HttpsServer s2 = null;48ExecutorService executor=null;49try {50String root = System.getProperty ("test.src")+ "/docs";51System.out.print ("Test9: ");52InetSocketAddress addr = new InetSocketAddress (0);53s1 = HttpServer.create (addr, 0);54s2 = HttpsServer.create (addr, 0);55HttpHandler h = new FileServerHandler (root);56HttpContext c1 = s1.createContext ("/test1", h);57HttpContext c2 = s2.createContext ("/test1", h);58executor = Executors.newCachedThreadPool();59s1.setExecutor (executor);60s2.setExecutor (executor);61ctx = new SimpleSSLContext(System.getProperty("test.src")).get();62s2.setHttpsConfigurator(new HttpsConfigurator (ctx));63s1.start();64s2.start();6566int p1 = s1.getAddress().getPort();67int p2 = s2.getAddress().getPort();68error = false;69Thread[] t = new Thread[100];7071t[0] = test (true, "http", root+"/test1", p1, "smallfile.txt", 23);72t[1] = test (true, "http", root+"/test1", p1, "largefile.txt", 2730088);73t[2] = test (true, "https", root+"/test1", p2, "smallfile.txt", 23);74t[3] = test (true, "https", root+"/test1", p2, "largefile.txt", 2730088);75t[4] = test (false, "http", root+"/test1", p1, "smallfile.txt", 23);76t[5] = test (false, "http", root+"/test1", p1, "largefile.txt", 2730088);77t[6] = test (false, "https", root+"/test1", p2, "smallfile.txt", 23);78t[7] = test (false, "https", root+"/test1", p2, "largefile.txt", 2730088);79t[8] = test (true, "http", root+"/test1", p1, "smallfile.txt", 23);80t[9] = test (true, "http", root+"/test1", p1, "largefile.txt", 2730088);81t[10] = test (true, "https", root+"/test1", p2, "smallfile.txt", 23);82t[11] = test (true, "https", root+"/test1", p2, "largefile.txt", 2730088);83t[12] = test (false, "http", root+"/test1", p1, "smallfile.txt", 23);84t[13] = test (false, "http", root+"/test1", p1, "largefile.txt", 2730088);85t[14] = test (false, "https", root+"/test1", p2, "smallfile.txt", 23);86t[15] = test (false, "https", root+"/test1", p2, "largefile.txt", 2730088);87for (int i=0; i<16; i++) {88t[i].join();89}90if (error) {91throw new RuntimeException ("error");92}9394System.out.println ("OK");95} finally {96delay();97if (s1 != null)98s1.stop(2);99if (s2 != null)100s2.stop(2);101if (executor != null)102executor.shutdown ();103}104}105106static int foo = 1;107108static ClientThread test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {109ClientThread t = new ClientThread (fixedLen, protocol, root, port, f, size);110t.start();111return t;112}113114static Object fileLock = new Object();115116static class ClientThread extends Thread {117118boolean fixedLen;119String protocol;120String root;121int port;122String f;123int size;124125ClientThread (boolean fixedLen, String protocol, String root, int port, String f, int size) {126this.fixedLen = fixedLen;127this.protocol = protocol;128this.root = root;129this.port = port;130this.f = f;131this.size = size;132}133134public void run () {135try {136URL url = new URL (protocol+"://localhost:"+port+"/test1/"+f);137HttpURLConnection urlc = (HttpURLConnection) url.openConnection();138if (urlc instanceof HttpsURLConnection) {139HttpsURLConnection urlcs = (HttpsURLConnection) urlc;140urlcs.setHostnameVerifier (new HostnameVerifier () {141public boolean verify (String s, SSLSession s1) {142return true;143}144});145urlcs.setSSLSocketFactory (ctx.getSocketFactory());146}147byte [] buf = new byte [4096];148149String s = "chunk";150if (fixedLen) {151urlc.setRequestProperty ("XFixed", "yes");152s = "fixed";153}154InputStream is = urlc.getInputStream();155File temp;156synchronized (fileLock) {157temp = File.createTempFile (s, null);158temp.deleteOnExit();159}160OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));161int c, count = 0;162while ((c=is.read(buf)) != -1) {163count += c;164fout.write (buf, 0, c);165}166is.close();167fout.close();168169if (count != size) {170System.out.println ("wrong amount of data returned");171System.out.println ("fixedLen = "+fixedLen);172System.out.println ("protocol = "+protocol);173System.out.println ("root = "+root);174System.out.println ("port = "+port);175System.out.println ("f = "+f);176System.out.println ("size = "+size);177System.out.println ("temp = "+temp);178System.out.println ("count = "+count);179error = true;180}181String orig = root + "/" + f;182compare (new File(orig), temp);183temp.delete();184} catch (IOException e) {185error = true;186}187}188}189190/* compare the contents of the two files */191192static void compare (File f1, File f2) throws IOException {193InputStream i1 = new BufferedInputStream (new FileInputStream(f1));194InputStream i2 = new BufferedInputStream (new FileInputStream(f2));195196int c1,c2;197try {198while ((c1=i1.read()) != -1) {199c2 = i2.read();200if (c1 != c2) {201throw new RuntimeException ("file compare failed 1");202}203}204if (i2.read() != -1) {205throw new RuntimeException ("file compare failed 2");206}207} finally {208i1.close();209i2.close();210}211}212}213214215