2003/10/01 03:08:31
[org.ibex.core.git] / src / org / xwt / HTTP.java
index 50c71c9..ec46105 100644 (file)
@@ -51,12 +51,12 @@ public class HTTP {
      */
     Semaphore okToRecieve = null;
 
+    /** true iff this is the first request to be made on this socket */
+    boolean firstRequest = true;
+
     /** 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 +88,11 @@ 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;
+                reset();
                 throw e;
             }
             blockOn = okToRecieve;
@@ -104,12 +103,18 @@ 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) {
+                if (firstRequest) throw new HTTPException("server closed the socket with no response");
                 // sometimes the server chooses to close the stream between requests
-                in = null; sock = null;
+                reset();
                 releaseMe.release();
                 return makeRequest(contentType, content);
             }
@@ -123,7 +128,7 @@ public class HTTP {
                 
                 if (h.get("HTTP").equals("1.0") && h.get("content-length") == null) {
                     if (Log.on) Log.log(this, "proxy returned an HTTP/1.0 reply with no content-length...");
-                    in = null; sock = null;
+                    reset();
                 } else {
                     int cl = h.get("content-length") == null ? -1 : Integer.parseInt(h.get("content-length").toString());
                     new HTTPInputStream(in, cl, releaseMe).close();
@@ -145,7 +150,7 @@ public class HTTP {
                 
             }
             
-        } catch (IOException e) { invalid = true; throw e;
+        } catch (IOException e) { reset(); throw e;
         } finally { if (doRelease) releaseMe.release();
         }
     }
@@ -323,6 +328,10 @@ public class HTTP {
     // Everything Else ////////////////////////////////////////////////////////////////////////////
 
     private synchronized void connect() throws IOException {
+        if (originalUrl.equals("stdio:")) {
+            in = new BufferedInputStream(System.in);
+            return;
+        }
         if (sock != null) {
             if (in == null) in = new BufferedInputStream(sock.getInputStream());
             return;
@@ -354,10 +363,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);
@@ -366,7 +380,7 @@ public class HTTP {
 
     public void sendRequest(String contentType, String content) throws IOException {
 
-        PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
+        PrintWriter pw = new PrintWriter(new OutputStreamWriter(originalUrl.equals("stdio:") ? System.out : sock.getOutputStream()));
         if (content != null) {
             pw.print("POST " + path + " HTTP/1.1\r\n");
             int contentLength = content.substring(0, 2).equals("\r\n") ?
@@ -539,7 +553,7 @@ public class HTTP {
                 }
                 return ret;
             } finally {
-                if (!good) invalid = true;
+                if (!good) reset();
             }
         }
 
@@ -557,6 +571,12 @@ public class HTTP {
         }
     }
 
+    void reset() {
+        firstRequest = true;
+        in = null;
+        sock = null;
+    }
+
 
     // Misc Helpers ///////////////////////////////////////////////////////////////////////////////////
 
@@ -786,7 +806,7 @@ public class HTTP {
                         public void perform() {
                             Box b = new Box();
                             Template t = Template.getTemplate((Res)Main.builtin.get("org/xwt/builtin/proxy_authorization.xwt"));
-                            t.apply(b, null, 0, 0, null);
+                            t.apply(b, null, null);
                             b.put("realm", realm);
                             b.put("proxyIP", proxyIP);
                         }