2003/09/24 06:10:54
[org.ibex.core.git] / src / org / xwt / HTTP.java
index ce94de2..19fc41c 100644 (file)
@@ -73,16 +73,16 @@ public class HTTP {
     }
 
     /** Performs an HTTP GET request */
-    public HTTPInputStream GET() throws IOException { return makeRequest(null, null); }
+    public InputStream GET() throws IOException { return makeRequest(null, null); }
 
     /** Performs an HTTP POST request; content is appended to the headers (so it should include a blank line to delimit the beginning of the body) */
-    public HTTPInputStream POST(String contentType, String content) throws IOException { return makeRequest(contentType, content); }
+    public InputStream POST(String contentType, String content) throws IOException { return makeRequest(contentType, content); }
 
     /**
      *  This method isn't synchronized; however, only one thread can be in the inner synchronized block at a time, and the rest of
      *  the method is protected by in-order one-at-a-time semaphore lock-steps
      */
-    private HTTPInputStream makeRequest(String contentType, String content) throws IOException {
+    private InputStream makeRequest(String contentType, String content) throws IOException {
 
         // Step 1: send the request and establish a semaphore to stop any requests that pipeline after us
         Semaphore blockOn = null;
@@ -135,7 +135,8 @@ public class HTTP {
                 if (h.get("HTTP").equals("1.0") && h.get("content-length") == null)
                     throw new HTTPException("XWT does not support HTTP/1.0 servers which fail to return the Content-Length header");
                 int cl = h.get("content-length") == null ? -1 : Integer.parseInt(h.get("content-length").toString());
-                HTTPInputStream ret = new HTTPInputStream(in, cl, releaseMe);
+                InputStream ret = new HTTPInputStream(in, cl, releaseMe);
+                if ("gzip".equals(h.get("content-encoding"))) ret = new java.util.zip.GZIPInputStream(ret);
                 doRelease = false;
                 return ret;
                 
@@ -176,6 +177,17 @@ public class HTTP {
         } catch (UnknownHostException uhe) { }
 
         if (Platform.detectProxy() == null) throw new HTTPException("could not resolve hostname \"" + host + "\" and no proxy configured");
+        if (Log.on) Log.log(this, "  could not resolve host " + host + "; using xmlrpc.xwt.org to ensure security");
+        try {
+            JS.Array args = new JS.Array();
+            args.addElement(host);
+            Object ret = new XMLRPC("http://xmlrpc.xwt.org/RPC2/", "dns.resolve").call(args);
+            if (ret == null || !(ret instanceof String)) throw new Exception("    xmlrpc.xwt.org returned non-String: " + ret);
+            resolvedHosts.put(host, ret);
+            return;
+        } catch (Throwable e) {
+            throw new HTTPException("exception while attempting to use xmlrpc.xwt.org to resolve " + host + ": " + e);
+        }
     }
 
 
@@ -367,6 +379,7 @@ public class HTTP {
         }
         
         pw.print("User-Agent: XWT\r\n");
+        pw.print("Accept-encoding: gzip\r\n");
         pw.print("Host: " + (host + (port == 80 ? "" : (":" + port))) + "\r\n");
         if (proxied) pw.print("X-RequestOrigin: " + Main.originHost + "\r\n");
 
@@ -467,7 +480,7 @@ public class HTTP {
         private int contentLength = 0;
         public int getContentLength() { return contentLength; }
 
-        HTTPInputStream(InputStream in, int length, Semaphore releaseMe) {
+        HTTPInputStream(InputStream in, int length, Semaphore releaseMe) throws IOException {
             super(in);
             this.releaseMe = releaseMe;
             this.contentLength = length;