/[classpath]/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java
ViewVC logotype

Diff of /classpath/gnu/java/net/protocol/http/HTTPURLConnection.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by dog, Tue Dec 7 11:45:56 2004 UTC revision 1.5 by dog, Wed Dec 8 14:26:55 2004 UTC
# Line 70  public class HTTPURLConnection Line 70  public class HTTPURLConnection
70    implements HandshakeCompletedListener    implements HandshakeCompletedListener
71  {  {
72    
73      /**
74       * Pool of reusable connections, used if keepAlive is true.
75       */
76      private static final Map connectionPool = new LinkedHashMap();
77    
78    /*    /*
79     * The underlying connection.     * The underlying connection.
80     */     */
# Line 77  public class HTTPURLConnection Line 82  public class HTTPURLConnection
82    
83    private String proxyHostname;    private String proxyHostname;
84    private int proxyPort;    private int proxyPort;
85      private String agent;
86      private boolean keepAlive;
87      private int maxConnections;
88    
89    private Request request;    private Request request;
90    private Headers requestHeaders;    private Headers requestHeaders;
# Line 97  public class HTTPURLConnection Line 105  public class HTTPURLConnection
105    {    {
106      super(url);      super(url);
107      requestHeaders = new Headers();      requestHeaders = new Headers();
108      AccessController.doPrivileged(this.new GetProxyAction());      AccessController.doPrivileged(this.new GetHTTPPropertiesAction());
109    }    }
110    
111    class GetProxyAction    class GetHTTPPropertiesAction
112      implements PrivilegedAction      implements PrivilegedAction
113    {    {
114    
# Line 112  public class HTTPURLConnection Line 120  public class HTTPURLConnection
120            String port = System.getProperty("http.proxyPort");            String port = System.getProperty("http.proxyPort");
121            if (port != null && port.length() > 0)            if (port != null && port.length() > 0)
122              {              {
123                proxyPort = Integer.parseInt (port);                proxyPort = Integer.parseInt(port);
124              }              }
125            else            else
126              {              {
# Line 120  public class HTTPURLConnection Line 128  public class HTTPURLConnection
128                proxyPort = -1;                proxyPort = -1;
129              }              }
130          }          }
131          agent = System.getProperty("http.agent");
132          String ka = System.getProperty("http.keepAlive");
133          keepAlive = !(ka != null && "false".equals(ka));
134          String mc = System.getProperty("http.maxConnections");
135          maxConnections = (mc != null && mc.length() > 0) ?
136            Math.max(Integer.parseInt(mc), 1) : 5;
137        return null;        return null;
138      }      }
139        
140    }    }
141    
142    public void connect()    public void connect()
# Line 162  public class HTTPURLConnection Line 176  public class HTTPURLConnection
176          retry = false;          retry = false;
177          if (connection == null)          if (connection == null)
178            {            {
179              connection = new HTTPConnection(host, port, secure);              connection = getConnection(host, port, secure);
180              if (secure)              if (secure)
181                {                {
182                  SSLSocketFactory factory = getSSLSocketFactory();                  SSLSocketFactory factory = getSSLSocketFactory();
# Line 185  public class HTTPURLConnection Line 199  public class HTTPURLConnection
199              connection.setProxy(proxyHostname, proxyPort);              connection.setProxy(proxyHostname, proxyPort);
200            }            }
201          request = connection.newRequest(method, file);          request = connection.newRequest(method, file);
202            if (!keepAlive)
203              {
204                request.setHeader("Connection", "close");
205              }
206            if (agent != null)
207              {
208                request.setHeader("User-Agent", agent);
209              }
210          request.getHeaders().putAll(requestHeaders);          request.getHeaders().putAll(requestHeaders);
211          if (requestSink != null)          if (requestSink != null)
212            {            {
# Line 269  public class HTTPURLConnection Line 291  public class HTTPURLConnection
291      connected = true;      connected = true;
292    }    }
293    
294      /**
295       * Returns a connection, from the pool if necessary.
296       */
297      HTTPConnection getConnection(String host, int port, boolean secure)
298        throws IOException
299      {
300        HTTPConnection connection;
301        if (keepAlive)
302          {
303            StringBuffer buf = new StringBuffer(secure ? "https://" : "http://");
304            buf.append(host);
305            buf.append(':');
306            buf.append(port);
307            String key = buf.toString();
308            synchronized (connectionPool)
309              {
310                connection = (HTTPConnection) connectionPool.get(key);
311                if (connection == null)
312                  {
313                    connection = new HTTPConnection(host, port, secure);
314                    // Good housekeeping
315                    if (connectionPool.size() == maxConnections)
316                      {
317                        // maxConnections must always be >= 1
318                        Object lru = connectionPool.keySet().iterator().next();
319                        connectionPool.remove(lru);
320                      }
321                    connectionPool.put(key, connection);
322                  }
323              }
324          }
325        else
326          {
327            connection = new HTTPConnection(host, port, secure);
328          }
329        return connection;
330      }
331    
332    public void disconnect()    public void disconnect()
333    {    {
334      if (connection != null)      if (connection != null)

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26