Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLClassLoader/HttpTest.java
38811 views
/*1* Copyright (c) 2002, 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 463633126* @summary Check that URLClassLoader doesn't create excessive http27* connections28*/29import java.net.*;30import java.io.*;31import java.util.*;3233public class HttpTest {3435/*36* Simple http server to service http requests. Auto shutdown37* if "idle" (no requests) for 10 seconds. Forks worker thread38* to service persistent connections. Work threads shutdown if39* "idle" for 5 seconds.40*/41static class HttpServer implements Runnable {4243private static HttpServer svr = null;44private static Counters cnts = null;45private static ServerSocket ss;4647private static Object counterLock = new Object();48private static int getCount = 0;49private static int headCount = 0;5051class Worker extends Thread {52Socket s;53Worker(Socket s) {54this.s = s;55}5657public void run() {58InputStream in = null;59try {60in = s.getInputStream();61for (;;) {6263// read entire request from client64byte b[] = new byte[1024];65int n, total=0;6667// max 5 seconds to wait for new request68s.setSoTimeout(5000);69try {70do {71n = in.read(b, total, b.length-total);72// max 0.5 seconds between each segment73// of request.74s.setSoTimeout(500);75if (n > 0) total += n;76} while (n > 0);77} catch (SocketTimeoutException e) { }7879if (total == 0) {80s.close();81return;82}8384boolean getRequest = false;85if (b[0] == 'G' && b[1] == 'E' && b[2] == 'T')86getRequest = true;8788synchronized (counterLock) {89if (getRequest)90getCount++;91else92headCount++;93}9495// response to client96PrintStream out = new PrintStream(97new BufferedOutputStream(98s.getOutputStream() ));99out.print("HTTP/1.1 200 OK\r\n");100101out.print("Content-Length: 75000\r\n");102out.print("\r\n");103if (getRequest) {104for (int i=0; i<75*1000; i++) {105out.write( (byte)'.' );106}107}108out.flush();109110} // for111112} catch (Exception e) {113unexpected(e);114} finally {115if (in != null) { try {in.close(); } catch(IOException e) {unexpected(e);} }116}117}118}119120HttpServer() throws Exception {121ss = new ServerSocket(0);122}123124public void run() {125try {126// shutdown if no request in 10 seconds.127ss.setSoTimeout(10000);128for (;;) {129Socket s = ss.accept();130(new Worker(s)).start();131}132} catch (Exception e) {133}134}135136void unexpected(Exception e) {137System.out.println(e);138e.printStackTrace();139}140141public static HttpServer create() throws Exception {142if (svr != null)143return svr;144cnts = new Counters();145svr = new HttpServer();146(new Thread(svr)).start();147return svr;148}149150public static void shutdown() throws Exception {151if (svr != null) {152ss.close();153svr = null;154}155}156157public int port() {158return ss.getLocalPort();159}160161public static class Counters {162public void reset() {163synchronized (counterLock) {164getCount = 0;165headCount = 0;166}167}168169public int getCount() {170synchronized (counterLock) {171return getCount;172}173}174175public int headCount() {176synchronized (counterLock) {177return headCount;178}179}180181public String toString() {182synchronized (counterLock) {183return "GET count: " + getCount + "; " +184"HEAD count: " + headCount;185}186}187}188189public Counters counters() {190return cnts;191}192193}194195public static void main(String args[]) throws Exception {196boolean failed = false;197198// create http server199HttpServer svr = HttpServer.create();200201// create class loader202URL urls[] =203{ new URL("http://localhost:" + svr.port() + "/dir1/"),204new URL("http://localhost:" + svr.port() + "/dir2/") };205URLClassLoader cl = new URLClassLoader(urls);206207// Test 1 - check that getResource does single HEAD request208svr.counters().reset();209URL url = cl.getResource("foo.gif");210System.out.println(svr.counters());211212if (svr.counters().getCount() > 0 ||213svr.counters().headCount() > 1) {214failed = true;215}216217// Test 2 - check that getResourceAsStream does at most218// one GET request219svr.counters().reset();220InputStream in = cl.getResourceAsStream("foo2.gif");221in.close();222System.out.println(svr.counters());223if (svr.counters().getCount() > 1) {224failed = true;225}226227// Test 3 - check that getResources only does HEAD requests228svr.counters().reset();229Enumeration e = cl.getResources("foos.gif");230try {231for (;;) {232e.nextElement();233}234} catch (NoSuchElementException exc) { }235System.out.println(svr.counters());236if (svr.counters().getCount() > 1) {237failed = true;238}239240// shutdown http server241svr.shutdown();242243if (failed) {244throw new Exception("Excessive http connections established - Test failed");245}246}247248}249250251