Skip to content

Commit d0d9a15

Browse files
Dhamoder Nallajerboaa
authored andcommitted
8291638: Keep-Alive timeout of 0 should close connection immediately
Reviewed-by: sgehwolf Backport-of: 26ac8366360685ef0cf3447ee7db16ba7a7fa1ec
1 parent 1db6a76 commit d0d9a15

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

jdk/src/share/classes/sun/net/www/HeaderParser.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,7 @@
2626
package sun.net.www;
2727

2828
import java.util.Iterator;
29+
import java.util.OptionalInt;
2930

3031
/* This is useful for the nightmare of parsing multi-part HTTP/RFC822 headers
3132
* sensibly:
@@ -244,6 +245,19 @@ public int findInt(String k, int Default) {
244245
return Default;
245246
}
246247
}
248+
249+
public OptionalInt findInt(String k) {
250+
try {
251+
String s = findValue(k);
252+
if (s == null) {
253+
return OptionalInt.empty();
254+
}
255+
return OptionalInt.of(Integer.parseInt(s));
256+
} catch (Throwable t) {
257+
return OptionalInt.empty();
258+
}
259+
}
260+
247261
/*
248262
public static void main(String[] a) throws Exception {
249263
System.out.print("enter line to parse> ");

jdk/src/share/classes/sun/net/www/http/HttpClient.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.*;
2929
import java.net.*;
3030
import java.util.Locale;
31+
import java.util.OptionalInt;
3132
import sun.net.NetworkClient;
3233
import sun.net.ProgressSource;
3334
import sun.net.www.MessageHeader;
@@ -119,6 +120,7 @@ static private int getDefaultPort(String proto) {
119120
* 0: the server specified no keep alive headers
120121
* -1: the server provided "Connection: keep-alive" but did not specify a
121122
* a particular time in a "Keep-Alive:" headers
123+
* -2: the server provided "Connection: keep-alive" and timeout=0
122124
* Positive values are the number of seconds specified by the server
123125
* in a "Keep-Alive" header
124126
*/
@@ -819,11 +821,19 @@ private boolean parseHTTPHeader(MessageHeader responses, ProgressSource pi, Http
819821
if (keepAliveConnections < 0) {
820822
keepAliveConnections = usingProxy?50:5;
821823
}
822-
keepAliveTimeout = p.findInt("timeout", -1);
823-
if (keepAliveTimeout < -1) {
824-
// if the server specified a negative (invalid) value
825-
// then we set to -1, which is equivalent to no value
824+
OptionalInt timeout = p.findInt("timeout");
825+
if (!timeout.isPresent()) {
826826
keepAliveTimeout = -1;
827+
} else {
828+
keepAliveTimeout = timeout.getAsInt();
829+
if (keepAliveTimeout < 0) {
830+
// if the server specified a negative (invalid) value
831+
// then we set to -1, which is equivalent to no value
832+
keepAliveTimeout = -1;
833+
} else if (keepAliveTimeout == 0) {
834+
// handled specially to mean close connection immediately
835+
keepAliveTimeout = -2;
836+
}
827837
}
828838
}
829839
} else if (b[7] != '0') {

jdk/src/share/classes/sun/net/www/http/KeepAliveCache.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public Void run() {
167167
// different default for server and proxy
168168
keepAliveTimeout = http.getUsingProxy() ? 60 : 5;
169169
}
170+
} else if (keepAliveTimeout == -2) {
171+
keepAliveTimeout = 0;
170172
}
171173
// at this point keepAliveTimeout is the number of seconds to keep
172174
// alive, which could be 0, if the user specified 0 for the property

jdk/test/sun/net/www/http/HttpClient/KeepAliveTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/*
2525
* @test
2626
* @library /lib/testlibrary
27+
* @bug 8291226 8291638
2728
* @modules java.base/sun.net:+open
2829
* java.base/sun.net.www.http:+open
2930
* java.base/sun.net.www:+open
@@ -1015,12 +1016,6 @@ else if (scenarioNumber >= 144 && scenarioNumber <= 159){
10151016
}
10161017

10171018
private void startScenario(int scenarioNumber) throws Exception {
1018-
//test scenarios are skipped because of JDK-8291638
1019-
if((scenarioNumber >= 112 && scenarioNumber <= 127) || (scenarioNumber >= 144 && scenarioNumber <= 159)) {
1020-
System.out.println("Scenario Skipped:"+scenarioNumber);
1021-
this.countDownLatch.countDown();
1022-
return;
1023-
}
10241019
System.out.println("serverScenarios[" + scenarioNumber + "]=" + getServerScenario(scenarioNumber));
10251020
System.out.println("clientScenarios[" + scenarioNumber + "]=" + clientScenarios[getClientScenarioNumber(scenarioNumber)]);
10261021
if(expectedValues[scenarioNumber] == 0) {

0 commit comments

Comments
 (0)