提交 ad24bc3f 编写于 作者: R robm

7199862: Make sure that a connection is still alive when retrieved from...

7199862: Make sure that a connection is still alive when retrieved from KeepAliveCache in certain cases
Reviewed-by: chegar
上级 db4a6fb8
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -244,16 +244,17 @@ public class HttpClient extends NetworkClient { ...@@ -244,16 +244,17 @@ public class HttpClient extends NetworkClient {
*/ */
public static HttpClient New(URL url) public static HttpClient New(URL url)
throws IOException { 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) public static HttpClient New(URL url, boolean useCache)
throws IOException { 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) public static HttpClient New(URL url, Proxy p, int to, boolean useCache,
throws IOException { HttpURLConnection httpuc) throws IOException
{
if (p == null) { if (p == null) {
p = Proxy.NO_PROXY; p = Proxy.NO_PROXY;
} }
...@@ -261,6 +262,13 @@ public class HttpClient extends NetworkClient { ...@@ -261,6 +262,13 @@ public class HttpClient extends NetworkClient {
/* see if one's already around */ /* see if one's already around */
if (useCache) { if (useCache) {
ret = kac.get(url, null); 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 != null) {
if ((ret.proxy != null && ret.proxy.equals(p)) || if ((ret.proxy != null && ret.proxy.equals(p)) ||
(ret.proxy == null && p == null)) { (ret.proxy == null && p == null)) {
...@@ -302,20 +310,25 @@ public class HttpClient extends NetworkClient { ...@@ -302,20 +310,25 @@ public class HttpClient extends NetworkClient {
return ret; return ret;
} }
public static HttpClient New(URL url, Proxy p, int to) throws IOException { public static HttpClient New(URL url, Proxy p, int to,
return New(url, p, to, true); HttpURLConnection httpuc) throws IOException
{
return New(url, p, to, true, httpuc);
} }
public static HttpClient New(URL url, String proxyHost, int proxyPort, public static HttpClient New(URL url, String proxyHost, int proxyPort,
boolean useCache) boolean useCache)
throws IOException { 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, public static HttpClient New(URL url, String proxyHost, int proxyPort,
boolean useCache, int to) boolean useCache, int to,
HttpURLConnection httpuc)
throws IOException { 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: /* return it to the cache as still usable, if:
...@@ -344,6 +357,34 @@ public class HttpClient extends NetworkClient { ...@@ -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() { protected synchronized void putInKeepAliveCache() {
if (inCache) { if (inCache) {
assert false : "Duplicate put to keep alive cache"; assert false : "Duplicate put to keep alive cache";
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -662,7 +662,7 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -662,7 +662,7 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
*/ */
protected void setNewClient (URL url, boolean useCache) protected void setNewClient (URL url, boolean useCache)
throws IOException { throws IOException {
http = HttpClient.New(url, null, -1, useCache, connectTimeout); http = HttpClient.New(url, null, -1, useCache, connectTimeout, this);
http.setReadTimeout(readTimeout); http.setReadTimeout(readTimeout);
} }
...@@ -703,7 +703,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -703,7 +703,8 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
String proxyHost, int proxyPort, String proxyHost, int proxyPort,
boolean useCache) boolean useCache)
throws IOException { throws IOException {
http = HttpClient.New (url, proxyHost, proxyPort, useCache, connectTimeout); http = HttpClient.New (url, proxyHost, proxyPort, useCache,
connectTimeout, this);
http.setReadTimeout(readTimeout); http.setReadTimeout(readTimeout);
} }
...@@ -994,14 +995,14 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -994,14 +995,14 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
// subclass HttpsClient will overwrite & return an instance of HttpsClient // subclass HttpsClient will overwrite & return an instance of HttpsClient
protected HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout) protected HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout)
throws IOException { throws IOException {
return HttpClient.New(url, p, connectTimeout); return HttpClient.New(url, p, connectTimeout, this);
} }
// subclass HttpsClient will overwrite & return an instance of HttpsClient // subclass HttpsClient will overwrite & return an instance of HttpsClient
protected HttpClient getNewHttpClient(URL url, Proxy p, protected HttpClient getNewHttpClient(URL url, Proxy p,
int connectTimeout, boolean useCache) int connectTimeout, boolean useCache)
throws IOException { throws IOException {
return HttpClient.New(url, p, connectTimeout, useCache); return HttpClient.New(url, p, connectTimeout, useCache, this);
} }
private void expect100Continue() throws IOException { private void expect100Continue() throws IOException {
...@@ -1144,7 +1145,7 @@ public class HttpURLConnection extends java.net.HttpURLConnection { ...@@ -1144,7 +1145,7 @@ public class HttpURLConnection extends java.net.HttpURLConnection {
} }
} }
private boolean streaming () { public boolean streaming () {
return (fixedContentLength != -1) || (fixedContentLengthLong != -1) || return (fixedContentLength != -1) || (fixedContentLengthLong != -1) ||
(chunkLength != -1); (chunkLength != -1);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册