2003/09/30 21:30:49
[org.ibex.core.git] / src / org / xwt / HTTP.java
index 8f26528..cc476cb 100644 (file)
@@ -54,9 +54,6 @@ public class HTTP {
     /** cache for resolveAndCheckIfFirewalled() */
     static Hashtable resolvedHosts = new Hashtable();
 
-    /** if any request encounters an IOException, the entire HTTP connection is invalidated */
-    boolean invalid = false;
-
     /** true iff we are allowed to skip the resolve check (only allowed when we're downloading the PAC script) */
     boolean skipResolveCheck = false;
 
@@ -88,12 +85,12 @@ public class HTTP {
         Semaphore blockOn = null;
         Semaphore releaseMe = null;
         synchronized(this) {
-            if (invalid) throw new HTTPException("connection failed on a previous pipelined call");
             try {
                 connect();
                 sendRequest(contentType, content);
             } catch (IOException e) {
-                invalid = true;
+                sock = null;
+                in = null;
                 throw e;
             }
             blockOn = okToRecieve;
@@ -104,7 +101,12 @@ public class HTTP {
         boolean doRelease = true;
         try {
             if (blockOn != null) blockOn.block();
-            if (invalid) throw new HTTPException("connection failed on a previous pipelined call");
+            
+            // previous call wrecked the socket connection, but we already sent our request, so we can't just retry --
+            // this could cause the server to receive the request twice, which could be bad (think of the case where the
+            // server call causes Amazon.com to ship you an item with one-click purchasing).
+            if (sock == null)
+                throw new HTTPException("a previous pipelined call messed up the socket");
             
             Hashtable h = in == null ? null : parseHeaders(in);
             if (h == null) {
@@ -145,7 +147,7 @@ public class HTTP {
                 
             }
             
-        } catch (IOException e) { invalid = true; throw e;
+        } catch (IOException e) { sock = null; in = null; throw e;
         } finally { if (doRelease) releaseMe.release();
         }
     }
@@ -358,10 +360,15 @@ public class HTTP {
         if (Log.verbose) Log.log(this, "creating HTTP object for connection to " + host + ":" + port);
 
         Proxy pi = Platform.detectProxy();
-        if (sock == null && pi != null && pi.proxyAutoConfigFunction != null) sock = attemptPAC(pi.proxyAutoConfigFunction);
-        if (sock == null && pi != null && ssl && pi.httpsProxyHost != null) sock = attemptHttpProxy(pi.httpsProxyHost, pi.httpsProxyPort);
-        if (sock == null && pi != null && pi.httpProxyHost != null) sock = attemptHttpProxy(pi.httpProxyHost, pi.httpProxyPort);
-        if (sock == null && pi != null && pi.socksProxyHost != null) sock = attemptSocksProxy(pi.socksProxyHost, pi.socksProxyPort);
+        OUTER: do {
+            if (pi != null) {
+                for(int i=0; i<pi.excluded.length; i++) if (host.equals(pi.excluded[i])) break OUTER;
+                if (sock == null && pi.proxyAutoConfigFunction != null) sock = attemptPAC(pi.proxyAutoConfigFunction);
+                if (sock == null && ssl && pi.httpsProxyHost != null) sock = attemptHttpProxy(pi.httpsProxyHost, pi.httpsProxyPort);
+                if (sock == null && pi.httpProxyHost != null) sock = attemptHttpProxy(pi.httpProxyHost, pi.httpProxyPort);
+                if (sock == null && pi.socksProxyHost != null) sock = attemptSocksProxy(pi.socksProxyHost, pi.socksProxyPort);
+            }
+        } while (false);
         proxied = sock != null;
         if (sock == null) sock = attemptDirect();
         if (sock == null) throw new HTTPException("unable to contact host " + host);
@@ -543,7 +550,7 @@ public class HTTP {
                 }
                 return ret;
             } finally {
-                if (!good) invalid = true;
+                if (!good) { HTTP.this.sock = null; HTTP.this.in = null; }
             }
         }