Skip to content

Commit 26ac836

Browse files
8291638: Keep-Alive timeout of 0 should close connection immediately
Reviewed-by: dfuchs, jpai
1 parent 6ae7e4d commit 26ac836

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

src/java.base/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:
@@ -246,6 +247,19 @@ public int findInt(String k, int Default) {
246247
return Default;
247248
}
248249
}
250+
251+
public OptionalInt findInt(String k) {
252+
try {
253+
String s = findValue(k);
254+
if (s == null) {
255+
return OptionalInt.empty();
256+
}
257+
return OptionalInt.of(Integer.parseInt(s));
258+
} catch (Throwable t) {
259+
return OptionalInt.empty();
260+
}
261+
}
262+
249263
/*
250264
public static void main(String[] a) throws Exception {
251265
System.out.print("enter line to parse> ");

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.net.*;
3030
import java.util.Locale;
3131
import java.util.Objects;
32+
import java.util.OptionalInt;
3233
import java.util.Properties;
3334
import java.util.concurrent.locks.ReentrantLock;
3435

@@ -127,6 +128,7 @@ private static int getDefaultPort(String proto) {
127128
* 0: the server specified no keep alive headers
128129
* -1: the server provided "Connection: keep-alive" but did not specify a
129130
* a particular time in a "Keep-Alive:" headers
131+
* -2: the server provided "Connection: keep-alive" and timeout=0
130132
* Positive values are the number of seconds specified by the server
131133
* in a "Keep-Alive" header
132134
*/
@@ -903,11 +905,19 @@ private boolean parseHTTPHeader(MessageHeader responses, ProgressSource pi, Http
903905
if (keepAliveConnections < 0) {
904906
keepAliveConnections = usingProxy?50:5;
905907
}
906-
keepAliveTimeout = p.findInt("timeout", -1);
907-
if (keepAliveTimeout < -1) {
908-
// if the server specified a negative (invalid) value
909-
// then we set to -1, which is equivalent to no value
908+
OptionalInt timeout = p.findInt("timeout");
909+
if (timeout.isEmpty()) {
910910
keepAliveTimeout = -1;
911+
} else {
912+
keepAliveTimeout = timeout.getAsInt();
913+
if (keepAliveTimeout < 0) {
914+
// if the server specified a negative (invalid) value
915+
// then we set to -1, which is equivalent to no value
916+
keepAliveTimeout = -1;
917+
} else if (keepAliveTimeout == 0) {
918+
// handled specially to mean close connection immediately
919+
keepAliveTimeout = -2;
920+
}
911921
}
912922
}
913923
} else if (b[7] != '0') {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ public Void run() {
173173
// different default for server and proxy
174174
keepAliveTimeout = http.getUsingProxy() ? 60 : 5;
175175
}
176+
} else if (keepAliveTimeout == -2) {
177+
keepAliveTimeout = 0;
176178
}
177179
// at this point keepAliveTimeout is the number of seconds to keep
178180
// alive, which could be 0, if the user specified 0 for the property

test/jdk/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 /test/lib
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)