| Method from sun.net.www.http.HttpClient Detail: |
public static HttpClient New(URL url) throws IOException {
return HttpClient.New(url, Proxy.NO_PROXY, -1, true);
}
|
public static HttpClient New(URL url,
boolean useCache) throws IOException {
return HttpClient.New(url, Proxy.NO_PROXY, -1, useCache);
}
|
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,
boolean useCache) throws IOException {
if (p == null) {
p = Proxy.NO_PROXY;
}
HttpClient ret = null;
/* see if one's already around */
if (useCache) {
ret = kac.get(url, null);
if (ret != null) {
if ((ret.proxy != null && ret.proxy.equals(p)) ||
(ret.proxy == null && p == null)) {
synchronized (ret) {
ret.cachedHttpClient = true;
assert ret.inCache;
ret.inCache = false;
}
} else {
// We cannot return this connection to the cache as it's
// KeepAliveTimeout will get reset. We simply close the connection.
// This should be fine as it is very rare that a connection
// to the same host will not use the same proxy.
ret.inCache = false;
ret.closeServer();
ret = null;
}
}
}
if (ret == null) {
ret = new HttpClient(url, p, to);
} else {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkConnect(url.getHost(), url.getPort());
}
ret.url = url;
}
return ret;
}
|
public static HttpClient New(URL url,
String proxyHost,
int proxyPort,
boolean useCache) throws IOException {
return New(url, newHttpProxy(proxyHost, proxyPort, "http"), -1, useCache);
}
|
public static HttpClient New(URL url,
String proxyHost,
int proxyPort,
boolean useCache,
int to) throws IOException {
return New(url, newHttpProxy(proxyHost, proxyPort, "http"), to, useCache);
}
|
public void afterConnect() throws UnknownHostException, IOException {
// NO-OP. Needs to be overwritten by HttpsClient
}
|
public void closeIdleConnection() {
HttpClient http = kac.get(url, null);
if (http != null) {
http.closeServer();
}
}
|
public void closeServer() {
try {
keepingAlive = false;
serverSocket.close();
} catch (Exception e) {}
}
|
protected void finalize() throws Throwable {
// This should do nothing. The stream finalizer will
// close the fd.
}
|
public void finished() {
if (reuse) /* will be reused */
return;
keepAliveConnections--;
poster = null;
if (keepAliveConnections > 0 && isKeepingAlive() &&
!(serverOutput.checkError())) {
/* This connection is keepingAlive && still valid.
* Return it to the cache.
*/
putInKeepAliveCache();
} else {
closeServer();
}
}
|
CacheRequest getCacheRequest() {
return cacheRequest;
}
|
protected int getDefaultPort() {
return httpPortNumber;
}
return default port number (subclasses may override) |
public boolean getHttpKeepAliveSet() {
String keepAlive = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("http.keepAlive"));
String retryPost = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("sun.net.http.retryPost"));
if (keepAlive != null) {
keepAliveProp = Boolean.valueOf(keepAlive).booleanValue();
} else {
keepAliveProp = true;
}
if (retryPost != null) {
retryPostProp = Boolean.valueOf(retryPost).booleanValue();
} else
retryPostProp = true;
return keepAliveProp;
}
|
public synchronized InputStream getInputStream() {
return serverInput;
}
|
int getKeepAliveTimeout() {
return keepAliveTimeout;
}
|
public OutputStream getOutputStream() {
return serverOutput;
}
|
public String getProxyHostUsed() {
if (!usingProxy) {
return null;
} else {
return ((InetSocketAddress)proxy.address()).getHostString();
}
}
|
public int getProxyPortUsed() {
if (usingProxy)
return ((InetSocketAddress)proxy.address()).getPort();
return -1;
}
|
public String getURLFile() throws IOException {
String fileName = url.getFile();
if ((fileName == null) || (fileName.length() == 0))
fileName = "/";
/**
* proxyDisabled is set by subclass HttpsClient!
*/
if (usingProxy && !proxyDisabled) {
// Do not use URLStreamHandler.toExternalForm as the fragment
// should not be part of the RequestURI. It should be an
// absolute URI which does not have a fragment part.
StringBuffer result = new StringBuffer(128);
result.append(url.getProtocol());
result.append(":");
if (url.getAuthority() != null && url.getAuthority().length() > 0) {
result.append("//");
result.append(url.getAuthority());
}
if (url.getPath() != null) {
result.append(url.getPath());
}
if (url.getQuery() != null) {
result.append('?");
result.append(url.getQuery());
}
fileName = result.toString();
}
if (fileName.indexOf('\n") == -1)
return fileName;
else
throw new java.net.MalformedURLException("Illegal character in URL");
}
|
public boolean isCachedConnection() {
return cachedHttpClient;
}
|
protected boolean isInKeepAliveCache() {
return inCache;
}
|
public final boolean isKeepingAlive() {
return getHttpKeepAliveSet() && keepingAlive;
}
|
public boolean needsTunneling() {
return false;
}
|
protected static Proxy newHttpProxy(String proxyHost,
int proxyPort,
String proto) {
if (proxyHost == null || proto == null)
return Proxy.NO_PROXY;
int pport = proxyPort < 0 ? getDefaultPort(proto) : proxyPort;
InetSocketAddress saddr = InetSocketAddress.createUnresolved(proxyHost, pport);
return new Proxy(Proxy.Type.HTTP, saddr);
}
|
protected synchronized void openServer() throws IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkConnect(host, port);
}
if (keepingAlive) { // already opened
return;
}
String urlHost = url.getHost().toLowerCase();
if (url.getProtocol().equals("http") ||
url.getProtocol().equals("https") ) {
if ((proxy != null) && (proxy.type() == Proxy.Type.HTTP)) {
privilegedOpenServer((InetSocketAddress) proxy.address());
usingProxy = true;
return;
} else {
// make direct connection
openServer(host, port);
usingProxy = false;
return;
}
} else {
/* we're opening some other kind of url, most likely an
* ftp url.
*/
if ((proxy != null) && (proxy.type() == Proxy.Type.HTTP)) {
privilegedOpenServer((InetSocketAddress) proxy.address());
usingProxy = true;
return;
} else {
// make direct connection
super.openServer(host, port);
usingProxy = false;
return;
}
}
}
|
public void openServer(String server,
int port) throws IOException {
serverSocket = doConnect(server, port);
try {
serverOutput = new PrintStream(
new BufferedOutputStream(serverSocket.getOutputStream()),
false, encoding);
} catch (UnsupportedEncodingException e) {
throw new InternalError(encoding+" encoding not found");
}
serverSocket.setTcpNoDelay(true);
}
|
public boolean parseHTTP(MessageHeader responses,
ProgressSource pi,
HttpURLConnection httpuc) throws IOException {
/* If "HTTP/*" is found in the beginning, return true. Let
* HttpURLConnection parse the mime header itself.
*
* If this isn't valid HTTP, then we don't try to parse a header
* out of the beginning of the response into the responses,
* and instead just queue up the output stream to it's very beginning.
* This seems most reasonable, and is what the NN browser does.
*/
try {
serverInput = serverSocket.getInputStream();
serverInput = new BufferedInputStream(serverInput);
return (parseHTTPHeader(responses, pi, httpuc));
} catch (SocketTimeoutException stex) {
// We don't want to retry the request when the app. sets a timeout
closeServer();
throw stex;
} catch (IOException e) {
closeServer();
cachedHttpClient = false;
if (!failedOnce && requests != null) {
if (httpuc.getRequestMethod().equals("POST") && !retryPostProp) {
// do not retry the request
} else {
// try once more
failedOnce = true;
openServer();
if (needsTunneling()) {
httpuc.doTunneling();
}
afterConnect();
writeRequests(requests, poster);
return parseHTTP(responses, pi, httpuc);
}
}
throw e;
}
}
Parse the first line of the HTTP request. It usually looks
something like: "HTTP/1.0 comment\r\n". |
protected synchronized void putInKeepAliveCache() {
if (inCache) {
assert false : "Duplicate put to keep alive cache";
return;
}
inCache = true;
kac.put(url, null, this);
}
|
public static synchronized void resetProperties() {
} Deprecated! -- - system properties are no longer cached.
A NOP method kept for backwards binary compatibility |
public void setCacheRequest(CacheRequest cacheRequest) {
this.cacheRequest = cacheRequest;
}
|
public void setDoNotRetry(boolean value) {
// failedOnce is used to determine if a request should be retried.
failedOnce = value;
}
|
public int setTimeout(int timeout) throws SocketException {
int old = serverSocket.getSoTimeout ();
serverSocket.setSoTimeout (timeout);
return old;
}
|
public String toString() {
return getClass().getName()+"("+url+")";
}
|
public void writeRequests(MessageHeader head) {
requests = head;
requests.print(serverOutput);
serverOutput.flush();
} Deprecated!
|
public void writeRequests(MessageHeader head,
PosterOutputStream pos) throws IOException {
requests = head;
requests.print(serverOutput);
poster = pos;
if (poster != null)
poster.writeTo(serverOutput);
serverOutput.flush();
}
|