Path: blob/master/test/jdk/sun/net/www/http/KeepAliveCache/KeepAliveProperty.java
66646 views
/*1* Copyright (c) 2022, 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* @library /test/lib26* @bug 827806727* @run main/othervm -Dhttp.keepAlive.time.server=30 KeepAliveProperty long28* @run main/othervm -Dhttp.keepAlive.time.server=1 KeepAliveProperty short29* @run main/othervm -ea -Dhttp.keepAlive.time.server=0 KeepAliveProperty short30*/3132import java.net.*;33import java.io.*;34import java.nio.charset.*;35import java.util.logging.*;36import jdk.test.lib.net.URIBuilder;37import static java.net.Proxy.NO_PROXY;3839public class KeepAliveProperty {4041static volatile boolean pass = false;4243static class Server extends Thread {44final ServerSocket server;4546Server (ServerSocket server) {47super ();48this.server = server;49}5051void readAll (Socket s) throws IOException {52byte[] buf = new byte [128];53int c;54String request = "";55InputStream is = s.getInputStream ();56while ((c=is.read(buf)) > 0) {57request += new String(buf, 0, c, StandardCharsets.US_ASCII);58if (request.contains("\r\n\r\n")) {59return;60}61}62if (c == -1)63throw new IOException("Socket closed");64}6566Socket s = null;67String BODY;68String CLEN;69PrintStream out;7071public void run() {72try {73s = server.accept();74readAll(s);7576BODY = "Hello world";77CLEN = "Content-Length: " + BODY.length() + "\r\n";78out = new PrintStream(new BufferedOutputStream(s.getOutputStream() ));7980/* send the header */81out.print("HTTP/1.1 200 OK\r\n");82out.print("Content-Type: text/plain; charset=iso-8859-1\r\n");83out.print(CLEN);84out.print("\r\n");85out.print(BODY);86out.flush();87} catch (Exception e) {88pass = false;89try {90if (s != null)91s.close();92server.close();93} catch (IOException unused) {}94return;95}9697// second request may legitimately fail9899try (Socket s2 = s; ServerSocket server2 = server; PrintStream out2 = out) {100// wait for second request.101readAll(s2);102103BODY = "Goodbye world";104CLEN = "Content-Length: " + BODY.length() + "\r\n";105106/* send the header */107out2.print("HTTP/1.1 200 OK\r\n");108out2.print("Content-Type: text/plain; charset=iso-8859-1\r\n");109out2.print(CLEN);110out2.print("\r\n");111out2.print(BODY);112out2.flush();113pass = !expectClose;114if (!pass) System.out.println("Failed: expected close");115} catch (Exception e) {116pass = expectClose;117if (!pass) System.out.println("Failed: did not expect close");118}119}120}121122static String fetch(URL url) throws Exception {123InputStream in = url.openConnection(NO_PROXY).getInputStream();124String s = "";125byte b[] = new byte[128];126int n;127do {128n = in.read(b);129if (n > 0)130s += new String(b, 0, n, StandardCharsets.US_ASCII);131} while (n > 0);132in.close();133return s;134}135136static volatile boolean expectClose;137138public static void main(String args[]) throws Exception {139// exercise the logging code140Logger logger = Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");141logger.setLevel(Level.FINEST);142ConsoleHandler h = new ConsoleHandler();143h.setLevel(Level.FINEST);144logger.addHandler(h);145146expectClose = args[0].equals("short");147InetAddress loopback = InetAddress.getLoopbackAddress();148ServerSocket ss = new ServerSocket();149ss.bind(new InetSocketAddress(loopback, 0));150Server s = new Server(ss);151s.start();152153URL url = URIBuilder.newBuilder()154.scheme("http")155.loopback()156.port(ss.getLocalPort())157.toURL();158System.out.println("URL: " + url);159160if (!fetch(url).equals("Hello world"))161throw new RuntimeException("Failed on first request");162163// Wait a while to see if connection is closed164Thread.sleep(3 * 1000);165166try {167if (!fetch(url).equals("Goodbye world"))168throw new RuntimeException("Failed on second request");169} catch (Exception e) {170if (!expectClose)171throw e;172}173174if (!pass)175throw new RuntimeException("Failed in server");176}177}178179180