/[classpath]/inetlib/source/gnu/inet/http/HTTPURLConnection.java
ViewVC logotype

Diff of /inetlib/source/gnu/inet/http/HTTPURLConnection.java

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

revision 1.11 by dog, Tue Dec 7 11:15:35 2004 UTC revision 1.12 by dog, Wed Dec 8 14:53:42 2004 UTC
# Line 62  import java.util.Map; Line 62  import java.util.Map;
62  public class HTTPURLConnection  public class HTTPURLConnection
63    extends HttpURLConnection    extends HttpURLConnection
64  {  {
65      
66      /**
67       * Pool of reusable connections, used if keepAlive is true.
68       */
69      private static final Map connectionPool = new LinkedHashMap();
70    
71    /*    /*
72     * The underlying connection.     * The underlying connection.
# Line 70  public class HTTPURLConnection Line 75  public class HTTPURLConnection
75    
76    private String proxyHostname;    private String proxyHostname;
77    private int proxyPort;    private int proxyPort;
78      private String agent;
79      private boolean keepAlive;
80      private int maxConnections;
81    
82    private Request request;    private Request request;
83    private Headers requestHeaders;    private Headers requestHeaders;
# Line 87  public class HTTPURLConnection Line 95  public class HTTPURLConnection
95    {    {
96      super(url);      super(url);
97      requestHeaders = new Headers();      requestHeaders = new Headers();
98      AccessController.doPrivileged(this.new GetProxyAction());      AccessController.doPrivileged(this.new GetHTTPPropertiesAction());
99    }    }
100    
101    class GetProxyAction    class GetHTTPPropertiesAction
102      implements PrivilegedAction      implements PrivilegedAction
103    {    {
104    
105      public Object run()      public Object run()
106      {      {
107        proxyHostname = System.getProperty("http.proxyHost");        proxyHostname = System.getProperty("http.proxyHost");
108        if (proxyHostname != null)        if (proxyHostname != null && proxyHostname.length() > 0)
109          {          {
110            String port = System.getProperty("http.proxyPort");            String port = System.getProperty("http.proxyPort");
111            proxyPort = (port != null) ? Integer.parseInt (port) : -1;            if (port != null && port.length() > 0)
112                {
113                  proxyPort = Integer.parseInt(port);
114                }
115              else
116                {
117                  proxyHostname = null;
118                  proxyPort = -1;
119                }
120          }          }
121          agent = System.getProperty("http.agent");
122          String ka = System.getProperty("http.keepAlive");
123          keepAlive = !(ka != null && "false".equals(ka));
124          String mc = System.getProperty("http.maxConnections");
125          maxConnections = (mc != null && mc.length() > 0) ?
126            Math.max(Integer.parseInt(mc), 1) : 5;
127        return null;        return null;
128      }      }
129            
# Line 144  public class HTTPURLConnection Line 166  public class HTTPURLConnection
166          retry = false;          retry = false;
167          if (connection == null)          if (connection == null)
168            {            {
169              connection = new HTTPConnection(host, port, secure);              connection = getConnection(host, port, secure);
170            }            }
171          if (proxyHostname != null)          if (proxyHostname != null)
172            {            {
# Line 156  public class HTTPURLConnection Line 178  public class HTTPURLConnection
178              connection.setProxy(proxyHostname, proxyPort);              connection.setProxy(proxyHostname, proxyPort);
179            }            }
180          request = connection.newRequest(method, file);          request = connection.newRequest(method, file);
181            if (!keepAlive)
182              {
183                request.setHeader("Connection", "close");
184              }
185            if (agent != null)
186              {
187                request.setHeader("User-Agent", agent);
188              }
189          request.getHeaders().putAll(requestHeaders);          request.getHeaders().putAll(requestHeaders);
190          if (requestSink != null)          if (requestSink != null)
191            {            {
# Line 240  public class HTTPURLConnection Line 270  public class HTTPURLConnection
270      connected = true;      connected = true;
271    }    }
272    
273      /**
274       * Returns a connection, from the pool if necessary.
275       */
276      HTTPConnection getConnection(String host, int port, boolean secure)
277        throws IOException
278      {
279        HTTPConnection connection;
280        if (keepAlive)
281          {
282            StringBuffer buf = new StringBuffer(secure ? "https://" : "http://");
283            buf.append(host);
284            buf.append(':');
285            buf.append(port);
286            String key = buf.toString();
287            synchronized (connectionPool)
288              {
289                connection = (HTTPConnection) connectionPool.get(key);
290                if (connection == null)
291                  {
292                    connection = new HTTPConnection(host, port, secure);
293                    // Good housekeeping
294                    if (connectionPool.size() == maxConnections)
295                      {
296                        // maxConnections must always be >= 1
297                        Object lru = connectionPool.keySet().iterator().next();
298                        connectionPool.remove(lru);
299                      }
300                    connectionPool.put(key, connection);
301                  }
302              }
303          }
304        else
305          {
306            connection = new HTTPConnection(host, port, secure);
307          }
308        return connection;
309      }
310    
311    public void disconnect()    public void disconnect()
312    {    {
313      if (connection != null)      if (connection != null)

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12

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