2003/09/25 10:56:08
[org.ibex.core.git] / src / org / xwt / HTTP.java
index 3dc73da..fed6e6e 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;
@@ -497,7 +510,7 @@ public class HTTP {
                 int i = super.read();
                 if (i == -1) throw new HTTPException("encountered end of stream while reading chunk length");
 
-                // FIXME: handle chunking extensions
+                // FEATURE: handle chunking extensions
                 if (i == '\r') {
                     super.read();    // LF
                     break;
@@ -742,8 +755,6 @@ public class HTTP {
                         try {
                             org.xwt.js.JS.Array arr = new org.xwt.js.JS.Array();
                             arr.addElement(((JS.Exn)e).getObject());
-                            // FIXME
-                            //XWT.recursivePrintObject.call();
                         } catch (Exception e2) {
                             Log.log(Platform.class, e);
                         }
@@ -771,10 +782,11 @@ public class HTTP {
 
                 if (authorization != oldAuth) return;
                 if (Log.on) Log.log(Authorization.class, "displaying proxy authorization dialog");
-                MessageQueue.add(new Message() {
+                Message.Q.add(new Message() {
                         public void perform() {
                             Box b = new Box();
-                            Template.getTemplate("org.xwt.builtin.proxy_authorization", null).apply(b, null, null, null, 0, 0);
+                            Template t = Template.getTemplate((Res)Main.builtin.get("org/xwt/builtin/proxy_authorization.xwt"));
+                            t.apply(b, null, null);
                             b.put("realm", realm);
                             b.put("proxyIP", proxyIP);
                         }
@@ -789,12 +801,10 @@ public class HTTP {
 
         // ProxyAutoConfigRootScope ////////////////////////////////////////////////////////////////////
 
-        public static class ProxyAutoConfigRootScope extends JS.Scope {
+        public static class ProxyAutoConfigRootScope extends JS.GlobalScope {
 
             public ProxyAutoConfigRootScope() { super(null); }
         
-            // FIXME: needs "standard objects"
-
             public Object get(Object name) {
                 if (name.equals("isPlainHostName")) return isPlainHostName;
                 else if (name.equals("dnsDomainIs")) return dnsDomainIs;