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 Test13
0N/A * @summary Light weight HTTP server
0N/A */
0N/A
0N/Aimport com.sun.net.httpserver.*;
0N/A
0N/Aimport java.util.concurrent.*;
3105N/Aimport java.util.logging.*;
0N/Aimport java.io.*;
0N/Aimport java.net.*;
2612N/A
0N/Aimport javax.net.ssl.*;
0N/A
0N/A/* basic http/s connectivity test
0N/A * Tests:
0N/A * - same as Test12, but with 64 threads
0N/A */
0N/A
0N/Apublic class Test13 extends Test {
0N/A
0N/A static SSLContext ctx;
0N/A
3105N/A final static int NUM = 32; // was 32
3105N/A
0N/A static boolean fail = false;
0N/A
0N/A public static void main (String[] args) throws Exception {
0N/A HttpServer s1 = null;
0N/A HttpsServer s2 = null;
0N/A ExecutorService executor=null;
3105N/A Logger l = Logger.getLogger ("com.sun.net.httpserver");
3105N/A Handler ha = new ConsoleHandler();
3105N/A ha.setLevel(Level.ALL);
3105N/A l.setLevel(Level.ALL);
3105N/A l.addHandler(ha);
0N/A try {
0N/A String root = System.getProperty ("test.src")+ "/docs";
0N/A System.out.print ("Test13: ");
0N/A InetSocketAddress addr = new InetSocketAddress (0);
0N/A s1 = HttpServer.create (addr, 0);
0N/A s2 = HttpsServer.create (addr, 0);
0N/A HttpHandler h = new FileServerHandler (root);
0N/A HttpContext c1 = s1.createContext ("/test1", h);
0N/A HttpContext c2 = s2.createContext ("/test1", h);
0N/A executor = Executors.newCachedThreadPool();
0N/A s1.setExecutor (executor);
0N/A s2.setExecutor (executor);
0N/A ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
0N/A s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
0N/A s1.start();
0N/A s2.start();
0N/A
0N/A int port = s1.getAddress().getPort();
0N/A int httpsport = s2.getAddress().getPort();
3105N/A Runner r[] = new Runner[NUM*2];
3105N/A for (int i=0; i<NUM; i++) {
0N/A r[i] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23);
3105N/A r[i+NUM] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
0N/A }
0N/A start (r);
0N/A join (r);
0N/A System.out.println ("OK");
0N/A } finally {
0N/A delay();
1324N/A if (s1 != null)
1324N/A s1.stop(2);
1324N/A if (s2 != null)
1324N/A s2.stop(2);
1324N/A if (executor != null)
1324N/A executor.shutdown ();
0N/A }
0N/A }
0N/A
0N/A static void start (Runner[] x) {
0N/A for (int i=0; i<x.length; i++) {
3105N/A if (x[i] != null)
0N/A x[i].start();
0N/A }
0N/A }
0N/A
0N/A static void join (Runner[] x) {
0N/A for (int i=0; i<x.length; i++) {
0N/A try {
3105N/A if (x[i] != null)
0N/A x[i].join();
0N/A } catch (InterruptedException e) {}
0N/A }
0N/A }
0N/A
0N/A
0N/A static class Runner 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 Runner (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 (ctx.getSocketFactory());
0N/A }
0N/A byte [] buf = new byte [4096];
0N/A
0N/A if (fixedLen) {
0N/A urlc.setRequestProperty ("XFixed", "yes");
0N/A }
0N/A InputStream is = urlc.getInputStream();
0N/A File temp = File.createTempFile ("Test1", null);
0N/A temp.deleteOnExit();
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 throw new RuntimeException ("wrong amount of data returned");
0N/A }
0N/A String orig = root + "/" + f;
0N/A compare (new File(orig), temp);
0N/A temp.delete();
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A fail = 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}