2003/02/22 03:55:06
[org.ibex.core.git] / src / org / xwt / HTTP.java
index b39240d..acdfcc3 100644 (file)
@@ -39,6 +39,9 @@ public class HTTP {
     /** the socket's inputstream */
     InputStream in = null;
 
+    /** the socket's outputstream */
+    PrintWriter out = null;
+
     /** the username and password portions of the URL */
     String userInfo = null;
 
@@ -161,8 +164,6 @@ public class HTTP {
         // cached
         if (resolvedHosts.get(host) != null) return;
 
-        if (Log.on) Log.log(this, "  resolveAndCheckIfFirewalled: resolving " + host);
-
         // if all scripts are trustworthy (local FS), continue
         if (Main.originAddr == null) return;
 
@@ -320,12 +321,24 @@ public class HTTP {
     // Everything Else ////////////////////////////////////////////////////////////////////////////
 
     private synchronized void connect() throws IOException {
-        if (sock != null) {
+        if (sock != null && !"".equals(originalUrl)) {
             if (in == null) in = new BufferedInputStream(sock.getInputStream());
             return;
         }
         // grab the userinfo; gcj doesn't have java.net.URL.getUserInfo()
         String url = originalUrl;
+
+        if ("".equals(url)) {
+            userInfo = "";
+            this.url = null;
+            port = 80;
+            path = "/RPC2";
+            host = "";
+            in = System.in;
+            out = new PrintWriter(System.out);
+            return;
+        }
+
         userInfo = url.substring(url.indexOf("://") + 3);
         userInfo = userInfo.indexOf('/') == -1 ? userInfo : userInfo.substring(0, userInfo.indexOf('/'));
         if (userInfo.indexOf('@') != -1) {
@@ -334,7 +347,7 @@ public class HTTP {
         } else {
             userInfo = null;
         }
-
+        
         if (url.startsWith("https:")) {
             this.url = new URL("http" + url.substring(5));
             ssl = true;
@@ -343,6 +356,7 @@ public class HTTP {
         } else {
             this.url = new URL(url);
         }
+
         if (!skipResolveCheck) resolveAndCheckIfFirewalled(this.url.getHost());
         port = this.url.getPort();
         path = this.url.getFile();
@@ -358,14 +372,18 @@ public class HTTP {
         if (sock == null) sock = attemptDirect();
         if (sock == null) throw new HTTPException("unable to contact host " + host);
         if (in == null) in = new BufferedInputStream(sock.getInputStream());
+        if (out == null) out = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
     }
 
     public void sendRequest(String contentType, String content) throws IOException {
 
-        PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
+        PrintWriter pw = out;
         if (content != null) {
             pw.print("POST " + path + " HTTP/1.1\r\n");
-            pw.print("Content-Length: " + content.length() + "\r\n");
+        int contentLength = content.substring(0, 2).equals("\r\n") ?
+        content.length() - 2 :
+        (content.length() - content.indexOf("\r\n\r\n") - 4);
+            pw.print("Content-Length: " + contentLength + "\r\n");
             if (contentType != null) pw.print("Content-Type: " + contentType + "\r\n");
         } else {
             pw.print("GET " + path + " HTTP/1.1\r\n");
@@ -416,6 +434,7 @@ public class HTTP {
     }
 
     private void doProxyAuth(Hashtable h0, String method) throws IOException {
+        if (Log.on) Log.log(this, "Proxy AuthChallenge: " + h0.get("proxy-authenticate"));
         Hashtable h = parseAuthenticationChallenge(h0.get("proxy-authenticate").toString());
         String style = h.get("AUTHTYPE").toString();
         String realm = h.get("realm").toString();
@@ -522,7 +541,7 @@ public class HTTP {
                 }
                 skip(2);
             } else {
-                skip(length);
+                if (length != 0) skip(length);
             }
             if (releaseMe != null) releaseMe.release();
         }