From ad24bc3f39293b5d0fa1f8050acbc32ea20ef022 Mon Sep 17 00:00:00 2001 From: robm Date: Thu, 27 Sep 2012 22:35:07 +0100 Subject: [PATCH] 7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases Reviewed-by: chegar --- .../classes/sun/net/www/http/HttpClient.java | 61 ++++++++++++++++--- .../www/protocol/http/HttpURLConnection.java | 13 ++-- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/share/classes/sun/net/www/http/HttpClient.java b/src/share/classes/sun/net/www/http/HttpClient.java index 140cc9191..ec08b0e6b 100644 --- a/src/share/classes/sun/net/www/http/HttpClient.java +++ b/src/share/classes/sun/net/www/http/HttpClient.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2012, 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 @@ -244,16 +244,17 @@ public class HttpClient extends NetworkClient { */ public static HttpClient New(URL url) throws IOException { - return HttpClient.New(url, Proxy.NO_PROXY, -1, true); + return HttpClient.New(url, Proxy.NO_PROXY, -1, true, null); } public static HttpClient New(URL url, boolean useCache) throws IOException { - return HttpClient.New(url, Proxy.NO_PROXY, -1, useCache); + return HttpClient.New(url, Proxy.NO_PROXY, -1, useCache, null); } - public static HttpClient New(URL url, Proxy p, int to, boolean useCache) - throws IOException { + public static HttpClient New(URL url, Proxy p, int to, boolean useCache, + HttpURLConnection httpuc) throws IOException + { if (p == null) { p = Proxy.NO_PROXY; } @@ -261,6 +262,13 @@ public class HttpClient extends NetworkClient { /* see if one's already around */ if (useCache) { ret = kac.get(url, null); + if (ret != null && httpuc != null && + httpuc.streaming() && + httpuc.getRequestMethod() == "POST") { + if (!ret.available()) + ret = null; + } + if (ret != null) { if ((ret.proxy != null && ret.proxy.equals(p)) || (ret.proxy == null && p == null)) { @@ -302,20 +310,25 @@ public class HttpClient extends NetworkClient { return ret; } - public static HttpClient New(URL url, Proxy p, int to) throws IOException { - return New(url, p, to, true); + public static HttpClient New(URL url, Proxy p, int to, + HttpURLConnection httpuc) throws IOException + { + return New(url, p, to, true, httpuc); } public static HttpClient New(URL url, String proxyHost, int proxyPort, boolean useCache) throws IOException { - return New(url, newHttpProxy(proxyHost, proxyPort, "http"), -1, useCache); + return New(url, newHttpProxy(proxyHost, proxyPort, "http"), + -1, useCache, null); } public static HttpClient New(URL url, String proxyHost, int proxyPort, - boolean useCache, int to) + boolean useCache, int to, + HttpURLConnection httpuc) throws IOException { - return New(url, newHttpProxy(proxyHost, proxyPort, "http"), to, useCache); + return New(url, newHttpProxy(proxyHost, proxyPort, "http"), + to, useCache, httpuc); } /* return it to the cache as still usable, if: @@ -344,6 +357,34 @@ public class HttpClient extends NetworkClient { } } + protected synchronized boolean available() throws IOException { + boolean available = true; + int old = serverSocket.getSoTimeout(); + serverSocket.setSoTimeout(1); + BufferedInputStream tmpbuf = + new BufferedInputStream(serverSocket.getInputStream()); + + PlatformLogger logger = HttpURLConnection.getHttpLogger(); + try { + int r = tmpbuf.read(); + if (r == -1) { + if (logger.isLoggable(PlatformLogger.FINEST)) { + logger.finest("HttpClient.available(): " + + "read returned -1: not available"); + } + available = false; + } + } catch (SocketTimeoutException e) { + if (logger.isLoggable(PlatformLogger.FINEST)) { + logger.finest("HttpClient.available(): " + + "SocketTimeout: its available"); + } + } finally { + serverSocket.setSoTimeout(old); + } + return available; + } + protected synchronized void putInKeepAliveCache() { if (inCache) { assert false : "Duplicate put to keep alive cache"; diff --git a/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java b/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java index 50cecb0e0..036f469a9 100644 --- a/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java +++ b/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 2012, 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 @@ -662,7 +662,7 @@ public class HttpURLConnection extends java.net.HttpURLConnection { */ protected void setNewClient (URL url, boolean useCache) throws IOException { - http = HttpClient.New(url, null, -1, useCache, connectTimeout); + http = HttpClient.New(url, null, -1, useCache, connectTimeout, this); http.setReadTimeout(readTimeout); } @@ -703,7 +703,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection { String proxyHost, int proxyPort, boolean useCache) throws IOException { - http = HttpClient.New (url, proxyHost, proxyPort, useCache, connectTimeout); + http = HttpClient.New (url, proxyHost, proxyPort, useCache, + connectTimeout, this); http.setReadTimeout(readTimeout); } @@ -994,14 +995,14 @@ public class HttpURLConnection extends java.net.HttpURLConnection { // subclass HttpsClient will overwrite & return an instance of HttpsClient protected HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout) throws IOException { - return HttpClient.New(url, p, connectTimeout); + return HttpClient.New(url, p, connectTimeout, this); } // subclass HttpsClient will overwrite & return an instance of HttpsClient protected HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout, boolean useCache) throws IOException { - return HttpClient.New(url, p, connectTimeout, useCache); + return HttpClient.New(url, p, connectTimeout, useCache, this); } private void expect100Continue() throws IOException { @@ -1144,7 +1145,7 @@ public class HttpURLConnection extends java.net.HttpURLConnection { } } - private boolean streaming () { + public boolean streaming () { return (fixedContentLength != -1) || (fixedContentLengthLong != -1) || (chunkLength != -1); } -- GitLab