Path: blob/master/test/jdk/java/net/URLClassLoader/HttpTest.java
41149 views
/*1* Copyright (c) 2002, 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 463633126* @library /test/lib27* @summary Check that URLClassLoader doesn't create excessive http28* connections29*/30import java.net.*;31import java.io.*;32import java.util.*;3334import jdk.test.lib.net.URIBuilder;3536public class HttpTest {3738/*39* Simple http server to service http requests. Auto shutdown40* if "idle" (no requests) for 10 seconds. Forks worker thread41* to service persistent connections. Work threads shutdown if42* "idle" for 5 seconds.43*/44static class HttpServer implements Runnable {4546private static HttpServer svr = null;47private static Counters cnts = null;48private static ServerSocket ss;4950private static Object counterLock = new Object();51private static int getCount = 0;52private static int headCount = 0;5354class Worker extends Thread {55Socket s;56Worker(Socket s) {57this.s = s;58}5960public void run() {61InputStream in = null;62try {63in = s.getInputStream();64for (;;) {6566// read entire request from client67byte b[] = new byte[1024];68int n, total=0;6970// max 5 seconds to wait for new request71s.setSoTimeout(5000);72try {73do {74n = in.read(b, total, b.length-total);75// max 0.5 seconds between each segment76// of request.77s.setSoTimeout(500);78if (n > 0) total += n;79} while (n > 0);80} catch (SocketTimeoutException e) { }8182if (total == 0) {83s.close();84return;85}8687boolean getRequest = false;88if (b[0] == 'G' && b[1] == 'E' && b[2] == 'T')89getRequest = true;9091synchronized (counterLock) {92if (getRequest)93getCount++;94else95headCount++;96}9798// response to client99PrintStream out = new PrintStream(100new BufferedOutputStream(101s.getOutputStream() ));102out.print("HTTP/1.1 200 OK\r\n");103104out.print("Content-Length: 75000\r\n");105out.print("\r\n");106if (getRequest) {107for (int i=0; i<75*1000; i++) {108out.write( (byte)'.' );109}110}111out.flush();112113} // for114115} catch (Exception e) {116unexpected(e);117} finally {118if (in != null) { try {in.close(); } catch(IOException e) {unexpected(e);} }119}120}121}122123HttpServer() throws Exception {124ss = new ServerSocket();125ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));126}127128public void run() {129try {130// shutdown if no request in 10 seconds.131ss.setSoTimeout(10000);132for (;;) {133Socket s = ss.accept();134(new Worker(s)).start();135}136} catch (Exception e) {137}138}139140void unexpected(Exception e) {141System.out.println(e);142e.printStackTrace();143}144145public static HttpServer create() throws Exception {146if (svr != null)147return svr;148cnts = new Counters();149svr = new HttpServer();150(new Thread(svr)).start();151return svr;152}153154public static void shutdown() throws Exception {155if (svr != null) {156ss.close();157svr = null;158}159}160161public int port() {162return ss.getLocalPort();163}164165public static class Counters {166public void reset() {167synchronized (counterLock) {168getCount = 0;169headCount = 0;170}171}172173public int getCount() {174synchronized (counterLock) {175return getCount;176}177}178179public int headCount() {180synchronized (counterLock) {181return headCount;182}183}184185public String toString() {186synchronized (counterLock) {187return "GET count: " + getCount + "; " +188"HEAD count: " + headCount;189}190}191}192193public Counters counters() {194return cnts;195}196197}198199public static void main(String args[]) throws Exception {200boolean failed = false;201202// create http server203HttpServer svr = HttpServer.create();204205// create class loader206URL urls[] = {207URIBuilder.newBuilder().scheme("http").loopback().port(svr.port())208.path("/dir1/").toURL(),209URIBuilder.newBuilder().scheme("http").loopback().port(svr.port())210.path("/dir2/").toURL(),211};212URLClassLoader cl = new URLClassLoader(urls);213214// Test 1 - check that getResource does single HEAD request215svr.counters().reset();216URL url = cl.getResource("foo.gif");217System.out.println(svr.counters());218219if (svr.counters().getCount() > 0 ||220svr.counters().headCount() > 1) {221failed = true;222}223224// Test 2 - check that getResourceAsStream does at most225// one GET request226svr.counters().reset();227InputStream in = cl.getResourceAsStream("foo2.gif");228in.close();229System.out.println(svr.counters());230if (svr.counters().getCount() > 1) {231failed = true;232}233234// Test 3 - check that getResources only does HEAD requests235svr.counters().reset();236Enumeration e = cl.getResources("foos.gif");237try {238for (;;) {239e.nextElement();240}241} catch (NoSuchElementException exc) { }242System.out.println(svr.counters());243if (svr.counters().getCount() > 1) {244failed = true;245}246247// shutdown http server248svr.shutdown();249250if (failed) {251throw new Exception("Excessive http connections established - Test failed");252}253}254255}256257258