Skip to content

Commit 1253eb4

Browse files
committed
8291638: Keep-Alive timeout of 0 should close connection immediately
Backport-of: 26ac8366360685ef0cf3447ee7db16ba7a7fa1ec
1 parent f53661b commit 1253eb4

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
*/
@@ -900,11 +902,19 @@ private boolean parseHTTPHeader(MessageHeader responses, ProgressSource pi, Http
900902
if (keepAliveConnections < 0) {
901903
keepAliveConnections = usingProxy?50:5;
902904
}
903-
keepAliveTimeout = p.findInt("timeout", -1);
904-
if (keepAliveTimeout < -1) {
905-
// if the server specified a negative (invalid) value
906-
// then we set to -1, which is equivalent to no value
905+
OptionalInt timeout = p.findInt("timeout");
906+
if (timeout.isEmpty()) {
907907
keepAliveTimeout = -1;
908+
} else {
909+
keepAliveTimeout = timeout.getAsInt();
910+
if (keepAliveTimeout < 0) {
911+
// if the server specified a negative (invalid) value
912+
// then we set to -1, which is equivalent to no value
913+
keepAliveTimeout = -1;
914+
} else if (keepAliveTimeout == 0) {
915+
// handled specially to mean close connection immediately
916+
keepAliveTimeout = -2;
917+
}
908918
}
909919
}
910920
} 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
@@ -172,6 +172,8 @@ public Void run() {
172172
// different default for server and proxy
173173
keepAliveTimeout = http.getUsingProxy() ? 60 : 5;
174174
}
175+
} else if (keepAliveTimeout == -2) {
176+
keepAliveTimeout = 0;
175177
}
176178
// at this point keepAliveTimeout is the number of seconds to keep
177179
// 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)