From 7e5b89f7d896f43a55c6bae0642c56b678eb9be6 Mon Sep 17 00:00:00 2001 From: Dhamoder Nalla Date: Wed, 21 Feb 2024 21:58:06 +0000 Subject: [PATCH] Backport 26ac8366360685ef0cf3447ee7db16ba7a7fa1ec --- .../classes/sun/net/www/HeaderParser.java | 16 ++++++++++++++- .../classes/sun/net/www/http/HttpClient.java | 20 ++++++++++++++++++- .../sun/net/www/http/KeepAliveCache.java | 2 ++ .../www/http/HttpClient/KeepAliveTest.java | 7 +------ 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/jdk/src/share/classes/sun/net/www/HeaderParser.java b/jdk/src/share/classes/sun/net/www/HeaderParser.java index 3f1629fa8c6..5e41605ea03 100644 --- a/jdk/src/share/classes/sun/net/www/HeaderParser.java +++ b/jdk/src/share/classes/sun/net/www/HeaderParser.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,6 +26,7 @@ package sun.net.www; import java.util.Iterator; +import java.util.OptionalInt; /* This is useful for the nightmare of parsing multi-part HTTP/RFC822 headers * sensibly: @@ -244,6 +245,19 @@ public int findInt(String k, int Default) { return Default; } } + + public OptionalInt findInt(String k) { + try { + String s = findValue(k); + if (s == null) { + return OptionalInt.empty(); + } + return OptionalInt.of(Integer.parseInt(s)); + } catch (Throwable t) { + return OptionalInt.empty(); + } + } + /* public static void main(String[] a) throws Exception { System.out.print("enter line to parse> "); diff --git a/jdk/src/share/classes/sun/net/www/http/HttpClient.java b/jdk/src/share/classes/sun/net/www/http/HttpClient.java index ffb26c07c13..e7440f0b655 100644 --- a/jdk/src/share/classes/sun/net/www/http/HttpClient.java +++ b/jdk/src/share/classes/sun/net/www/http/HttpClient.java @@ -28,6 +28,7 @@ import java.io.*; import java.net.*; import java.util.Locale; +import java.util.OptionalInt; import sun.net.NetworkClient; import sun.net.ProgressSource; import sun.net.www.MessageHeader; @@ -119,6 +120,7 @@ static private int getDefaultPort(String proto) { * 0: the server specified no keep alive headers * -1: the server provided "Connection: keep-alive" but did not specify a * a particular time in a "Keep-Alive:" headers + * -2: the server provided "Connection: keep-alive" and timeout=0 * Positive values are the number of seconds specified by the server * in a "Keep-Alive" header */ @@ -816,7 +818,23 @@ private boolean parseHTTPHeader(MessageHeader responses, ProgressSource pi, Http responses.findValue("Keep-Alive")); /* default should be larger in case of proxy */ keepAliveConnections = p.findInt("max", usingProxy?50:5); - keepAliveTimeout = p.findInt("timeout", -1); + if (keepAliveConnections < 0) { + keepAliveConnections = usingProxy?50:5; + } + OptionalInt timeout = p.findInt("timeout"); + if (!timeout.isPresent()) { + keepAliveTimeout = -1; + } else { + keepAliveTimeout = timeout.getAsInt(); + if (keepAliveTimeout < 0) { + // if the server specified a negative (invalid) value + // then we set to -1, which is equivalent to no value + keepAliveTimeout = -1; + } else if (keepAliveTimeout == 0) { + // handled specially to mean close connection immediately + keepAliveTimeout = -2; + } + } } } else if (b[7] != '0') { /* diff --git a/jdk/src/share/classes/sun/net/www/http/KeepAliveCache.java b/jdk/src/share/classes/sun/net/www/http/KeepAliveCache.java index bde51c3fdf9..44f9e1e7ce7 100644 --- a/jdk/src/share/classes/sun/net/www/http/KeepAliveCache.java +++ b/jdk/src/share/classes/sun/net/www/http/KeepAliveCache.java @@ -174,6 +174,8 @@ public Void run() { // different default for server and proxy keepAliveTimeout = http.getUsingProxy() ? 60 : 5; } + } else if (keepAliveTimeout == -2) { + keepAliveTimeout = 0; } // at this point keepAliveTimeout is the number of seconds to keep // alive, which could be 0, if the user specified 0 for the property diff --git a/jdk/test/sun/net/www/http/HttpClient/KeepAliveTest.java b/jdk/test/sun/net/www/http/HttpClient/KeepAliveTest.java index 7ec12a279b4..15983f3196a 100644 --- a/jdk/test/sun/net/www/http/HttpClient/KeepAliveTest.java +++ b/jdk/test/sun/net/www/http/HttpClient/KeepAliveTest.java @@ -24,6 +24,7 @@ /* * @test * @library /test/lib + * @bug 8291226 8291638 * @modules java.base/sun.net:+open * java.base/sun.net.www.http:+open * java.base/sun.net.www:+open @@ -1015,12 +1016,6 @@ else if (scenarioNumber >= 144 && scenarioNumber <= 159){ } private void startScenario(int scenarioNumber) throws Exception { - //test scenarios are skipped because of JDK-8291638 - if((scenarioNumber >= 112 && scenarioNumber <= 127) || (scenarioNumber >= 144 && scenarioNumber <= 159)) { - System.out.println("Scenario Skipped:"+scenarioNumber); - this.countDownLatch.countDown(); - return; - } System.out.println("serverScenarios[" + scenarioNumber + "]=" + getServerScenario(scenarioNumber)); System.out.println("clientScenarios[" + scenarioNumber + "]=" + clientScenarios[getClientScenarioNumber(scenarioNumber)]); if(expectedValues[scenarioNumber] == 0) {