diff options
author | mark <mark@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-03-10 21:46:48 +0000 |
---|---|---|
committer | mark <mark@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-03-10 21:46:48 +0000 |
commit | ce57ab760f69de6db452def7ffbf5b114a2d8694 (patch) | |
tree | ea38c56431c5d4528fb54254c3f8e50f517bede3 /libjava/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java | |
parent | 50996fe55769882de3f410896032c887f0ff0d04 (diff) | |
download | ppe42-gcc-ce57ab760f69de6db452def7ffbf5b114a2d8694.tar.gz ppe42-gcc-ce57ab760f69de6db452def7ffbf5b114a2d8694.zip |
Imported GNU Classpath 0.90
* scripts/makemake.tcl: Set gnu/java/awt/peer/swing to ignore.
* gnu/classpath/jdwp/VMFrame.java (SIZE): New constant.
* java/lang/VMCompiler.java: Use gnu.java.security.hash.MD5.
* java/lang/Math.java: New override file.
* java/lang/Character.java: Merged from Classpath.
(start, end): Now 'int's.
(canonicalName): New field.
(CANONICAL_NAME, NO_SPACES_NAME, CONSTANT_NAME): New constants.
(UnicodeBlock): Added argument.
(of): New overload.
(forName): New method.
Updated unicode blocks.
(sets): Updated.
* sources.am: Regenerated.
* Makefile.in: Likewise.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@111942 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java')
-rw-r--r-- | libjava/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java | 186 |
1 files changed, 73 insertions, 113 deletions
diff --git a/libjava/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java b/libjava/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java index d5da7d61ae4..5c2af9eb7f6 100644 --- a/libjava/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java +++ b/libjava/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java @@ -1,5 +1,5 @@ /* HTTPURLConnection.java -- - Copyright (C) 2004, 2005 Free Software Foundation, Inc. + Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -38,6 +38,8 @@ exception statement from your version. */ package gnu.java.net.protocol.http; +import gnu.classpath.SystemProperties; + import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -45,11 +47,11 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.ProtocolException; import java.net.URL; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.security.cert.Certificate; +import java.util.ArrayList; import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; @@ -70,13 +72,6 @@ public class HTTPURLConnection extends HttpsURLConnection implements HandshakeCompletedListener { - - /** - * Pool of reusable connections, used if keepAlive is true. - */ - private static final LinkedHashMap connectionPool = new LinkedHashMap(); - static int maxConnections; - /* * The underlying connection. */ @@ -108,38 +103,23 @@ public class HTTPURLConnection { super(url); requestHeaders = new Headers(); - AccessController.doPrivileged(this.new GetHTTPPropertiesAction()); - } - - class GetHTTPPropertiesAction - implements PrivilegedAction - { - - public Object run() - { - proxyHostname = System.getProperty("http.proxyHost"); - if (proxyHostname != null && proxyHostname.length() > 0) - { - String port = System.getProperty("http.proxyPort"); - if (port != null && port.length() > 0) - { - proxyPort = Integer.parseInt(port); - } - else - { - proxyHostname = null; - proxyPort = -1; - } - } - agent = System.getProperty("http.agent"); - String ka = System.getProperty("http.keepAlive"); - keepAlive = !(ka != null && "false".equals(ka)); - String mc = System.getProperty("http.maxConnections"); - maxConnections = (mc != null && mc.length() > 0) ? - Math.max(Integer.parseInt(mc), 1) : 5; - return null; - } - + proxyHostname = SystemProperties.getProperty("http.proxyHost"); + if (proxyHostname != null && proxyHostname.length() > 0) + { + String port = SystemProperties.getProperty("http.proxyPort"); + if (port != null && port.length() > 0) + { + proxyPort = Integer.parseInt(port); + } + else + { + proxyHostname = null; + proxyPort = -1; + } + } + agent = SystemProperties.getProperty("http.agent"); + String ka = SystemProperties.getProperty("http.keepAlive"); + keepAlive = !(ka != null && "false".equals(ka)); } public void connect() @@ -254,8 +234,24 @@ public class HTTPURLConnection } } - if (response.getCodeClass() == 3 && getInstanceFollowRedirects()) + if (response.isRedirect() && getInstanceFollowRedirects()) { + // Read the response body, if there is one. If the + // redirect points us back at the same server, we will use + // the cached connection, so we must make sure there is no + // pending data in it. + InputStream body = response.getBody(); + if (body != null) + { + byte[] ignore = new byte[1024]; + while (true) + { + int n = body.read(ignore, 0, ignore.length); + if (n == -1) + break; + } + } + // Follow redirect String location = response.getHeader("Location"); if (location != null) @@ -333,16 +329,13 @@ public class HTTPURLConnection { responseSink = response.getBody(); - if (response.getCode() == 404) - { - errorSink = responseSink; - throw new FileNotFoundException(url.toString()); - } + if (response.isError()) + errorSink = responseSink; } } while (retry); connected = true; - } + } /** * Returns a connection, from the pool if necessary. @@ -353,16 +346,7 @@ public class HTTPURLConnection HTTPConnection connection; if (keepAlive) { - Object key = HTTPConnection.getPoolKey(host, port, secure); - synchronized (connectionPool) - { - connection = (HTTPConnection) connectionPool.remove(key); - if (connection == null) - { - connection = new HTTPConnection(host, port, secure); - connection.setPool(connectionPool); - } - } + connection = HTTPConnection.Pool.instance.get(host, port, secure); } else { @@ -427,30 +411,32 @@ public class HTTPURLConnection public String getRequestProperty(String key) { + if (key == null) + return null; + return requestHeaders.getValue(key); } public Map getRequestProperties() { - return requestHeaders; + if (connected) + throw new IllegalStateException("Already connected"); + + Map m = requestHeaders.getAsMap(); + return Collections.unmodifiableMap(m); } public void setRequestProperty(String key, String value) { + super.setRequestProperty(key, value); + requestHeaders.put(key, value); } public void addRequestProperty(String key, String value) { - String old = requestHeaders.getValue(key); - if (old == null) - { - requestHeaders.put(key, value); - } - else - { - requestHeaders.put(key, old + "," + value); - } + super.addRequestProperty(key, value); + requestHeaders.addValue(key, value); } public OutputStream getOutputStream() @@ -493,6 +479,17 @@ public class HTTPURLConnection { throw new ProtocolException("doInput is false"); } + + if (response.isError()) + { + int code = response.getCode(); + if (code == 404 || code == 410) + throw new FileNotFoundException(url.toString()); + + throw new IOException("Server returned HTTP response code " + code + + " for URL " + url.toString()); + } + return responseSink; } @@ -514,17 +511,9 @@ public class HTTPURLConnection return null; } } - Headers headers = response.getHeaders(); - LinkedHashMap ret = new LinkedHashMap(); - ret.put(null, Collections.singletonList(getStatusLine(response))); - for (Iterator i = headers.entrySet().iterator(); i.hasNext(); ) - { - Map.Entry entry = (Map.Entry) i.next(); - String key = (String) entry.getKey(); - String value = (String) entry.getValue(); - ret.put(key, Collections.singletonList(value)); - } - return Collections.unmodifiableMap(ret); + Map m = response.getHeaders().getAsMap(); + m.put(null, Collections.singletonList(getStatusLine(response))); + return Collections.unmodifiableMap(m); } String getStatusLine(Response response) @@ -552,20 +541,7 @@ public class HTTPURLConnection { return getStatusLine(response); } - Iterator i = response.getHeaders().entrySet().iterator(); - Map.Entry entry; - int count = 1; - do - { - if (!i.hasNext()) - { - return null; - } - entry = (Map.Entry) i.next(); - count++; - } - while (count <= index); - return (String) entry.getValue(); + return response.getHeaders().getHeaderValue(index - 1); } public String getHeaderFieldKey(int index) @@ -581,24 +557,8 @@ public class HTTPURLConnection return null; } } - if (index == 0) - { - return null; - } - Iterator i = response.getHeaders().entrySet().iterator(); - Map.Entry entry; - int count = 1; - do - { - if (!i.hasNext()) - { - return null; - } - entry = (Map.Entry) i.next(); - count++; - } - while (count <= index); - return (String) entry.getKey(); + // index of zero is the status line. + return response.getHeaders().getHeaderName(index - 1); } public String getHeaderField(String name) @@ -614,7 +574,7 @@ public class HTTPURLConnection return null; } } - return (String) response.getHeader(name); + return response.getHeader(name); } public long getHeaderFieldDate(String name, long def) |