Skip to content

Commit 6dedd8a

Browse files
committed
8291638: Keep-Alive timeout of 0 should close connection immediately
Backport-of: 26ac8366360685ef0cf3447ee7db16ba7a7fa1ec
1 parent 00b7647 commit 6dedd8a

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 sun.net.NetworkClient;
3435
import sun.net.ProgressSource;
@@ -123,6 +124,7 @@ private static int getDefaultPort(String proto) {
123124
* 0: the server specified no keep alive headers
124125
* -1: the server provided "Connection: keep-alive" but did not specify a
125126
* a particular time in a "Keep-Alive:" headers
127+
* -2: the server provided "Connection: keep-alive" and timeout=0
126128
* Positive values are the number of seconds specified by the server
127129
* in a "Keep-Alive" header
128130
*/
@@ -863,11 +865,19 @@ private boolean parseHTTPHeader(MessageHeader responses, ProgressSource pi, Http
863865
if (keepAliveConnections < 0) {
864866
keepAliveConnections = usingProxy?50:5;
865867
}
866-
keepAliveTimeout = p.findInt("timeout", -1);
867-
if (keepAliveTimeout < -1) {
868-
// if the server specified a negative (invalid) value
869-
// then we set to -1, which is equivalent to no value
868+
OptionalInt timeout = p.findInt("timeout");
869+
if (timeout.isEmpty()) {
870870
keepAliveTimeout = -1;
871+
} else {
872+
keepAliveTimeout = timeout.getAsInt();
873+
if (keepAliveTimeout < 0) {
874+
// if the server specified a negative (invalid) value
875+
// then we set to -1, which is equivalent to no value
876+
keepAliveTimeout = -1;
877+
} else if (keepAliveTimeout == 0) {
878+
// handled specially to mean close connection immediately
879+
keepAliveTimeout = -2;
880+
}
871881
}
872882
}
873883
} 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
@@ -157,6 +157,8 @@ public Void run() {
157157
// different default for server and proxy
158158
keepAliveTimeout = http.getUsingProxy() ? 60 : 5;
159159
}
160+
} else if (keepAliveTimeout == -2) {
161+
keepAliveTimeout = 0;
160162
}
161163
// at this point keepAliveTimeout is the number of seconds to keep
162164
// 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)