2003/11/27 05:05:09
[org.ibex.core.git] / src / org / xwt / HTTP.java
index eca310f..98f9397 100644 (file)
@@ -188,11 +188,18 @@ public class HTTP {
 
     // Methods to attempt socket creation /////////////////////////////////////////////////////////////////
 
+    private Socket getSocket(String host, int port, boolean ssl, boolean negotiate) throws IOException {
+        Socket ret = ssl ? new TinySSL(host, port, negotiate) : new Socket(java.net.InetAddress.getByName(host), port);
+        ret.setTcpNoDelay(true);
+        return ret;
+    }
+
+
     /** Attempts a direct connection */
     public Socket attemptDirect() {
         try {
             if (Log.verbose) Log.log(this, "attempting to create unproxied socket to " + host + ":" + port + (ssl ? " [ssl]" : ""));
-            return Platform.getSocket(host, port, ssl, true);
+            return getSocket(host, port, ssl, true);
         } catch (IOException e) {
             if (Log.on) Log.log(this, "exception in attemptDirect(): " + e);
             return null;
@@ -204,7 +211,7 @@ public class HTTP {
         try {
             if (Log.verbose) Log.log(this, "attempting to create HTTP proxied socket using proxy " + proxyHost + ":" + proxyPort);
 
-            Socket sock = Platform.getSocket(proxyHost, proxyPort, ssl, false);
+            Socket sock = getSocket(proxyHost, proxyPort, ssl, false);
             if (!ssl) {
                 if (!path.startsWith("http://")) path = "http://" + host + ":" + port + path;
             } else {
@@ -240,7 +247,7 @@ public class HTTP {
                                  " proxied socket using proxy " + proxyHost + ":" + proxyPort);
 
         try {
-            Socket sock = Platform.getSocket(proxyHost, proxyPort, ssl, false);
+            Socket sock = getSocket(proxyHost, proxyPort, ssl, false);
             
             DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
             dos.writeByte(0x04);                         // SOCKSv4(a)
@@ -276,18 +283,14 @@ public class HTTP {
     }
 
     /** executes the PAC script and dispatches a call to one of the other attempt methods based on the result */
-    public Socket attemptPAC(org.xwt.js.JS.Callable pacFunc) {
+    public Socket attemptPAC(org.xwt.js.JS pacFunc) {
         if (Log.verbose) Log.log(this, "evaluating PAC script");
         String pac = null;
         try {
-            org.xwt.js.JS.Array args = new org.xwt.js.JS.Array();
-            args.addElement(url.toString());
-            args.addElement(url.getHost());
-            /* FIXME
-            Object obj = pacFunc.call(args);
+            org.xwt.js.JSArray args = new org.xwt.js.JSArray();
+            Object obj = pacFunc.call(url.toString(), url.getHost(), null, null, 2);
             if (Log.verbose) Log.log(this, "  PAC script returned \"" + obj + "\"");
             pac = obj.toString();
-            */
         } catch (Throwable e) {
             if (Log.on) Log.log(this, "PAC script threw exception " + e);
             return null;
@@ -358,7 +361,7 @@ public class HTTP {
         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 && pi.proxyAutoConfigJSFunction != null) sock = attemptPAC(pi.proxyAutoConfigJSFunction);
                 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);
@@ -474,8 +477,8 @@ public class HTTP {
                 if (i+3<type2.length) log += Integer.toString(type2[i+3] & 0xff, 16) + " ";
                 Log.log(this, log);
             }
-            // FIXME: need to keep the connection open between type1 and type3
-            // FIXME: finish this
+            // FEATURE: need to keep the connection open between type1 and type3
+            // FEATURE: finish this
             //byte[] type3 = Proxy.NTLM.getResponse(
             //Proxy.Authorization.authorization2 = "NTLM " + Base64.encode(type3));
         }            
@@ -701,7 +704,7 @@ public class HTTP {
         public String[] excluded = null;
     
         /** the PAC script */
-        public JS.Callable proxyAutoConfigFunction = null;
+        public JS proxyAutoConfigJSFunction = null;
     
         public static Proxy detectProxyViaManual() {
             Proxy ret = new Proxy();
@@ -753,8 +756,8 @@ public class HTTP {
             return ret;
         }
     
-        public static JS.Scope proxyAutoConfigRootScope = new ProxyAutoConfigRootScope();
-        public static JS.Callable getProxyAutoConfigFunction(String url) {
+        public static JSScope proxyAutoConfigRootJSScope = new ProxyAutoConfigRootJSScope();
+        public static JS getProxyAutoConfigJSFunction(String url) {
             try { 
                 BufferedReader br = new BufferedReader(new InputStreamReader(new HTTP(url, true).GET()));
                 String s = null;
@@ -780,19 +783,16 @@ public class HTTP {
                     if (Log.on) Log.log(Proxy.class, script);
                 }
 
-                Function scr = JS.parse("PAC script at " + url, 0, new StringReader(script));
-                // FIXME
-                /*
-                scr.call(new JS.Array(), proxyAutoConfigRootScope);
-                */
-                return (JS.Callable)proxyAutoConfigRootScope.get("FindProxyForURL");
+                JSFunction scr = JSFunction.fromReader("PAC script at " + url, 0, new StringReader(script));
+                scr.cloneWithNewParentScope(proxyAutoConfigRootJSScope).call(null, null, null, null, 0);
+                return (JS)proxyAutoConfigRootJSScope.get("FindProxyForURL");
             } catch (Exception e) {
                 if (Log.on) {
                     Log.log(Platform.class, "WPAD detection failed due to:");
-                    if (e instanceof JS.Exn) {
+                    if (e instanceof JSExn) {
                         try {
-                            org.xwt.js.JS.Array arr = new org.xwt.js.JS.Array();
-                            arr.addElement(((JS.Exn)e).getObject());
+                            org.xwt.js.JSArray arr = new org.xwt.js.JSArray();
+                            arr.addElement(((JSExn)e).getObject());
                         } catch (Exception e2) {
                             Log.log(Platform.class, e);
                         }
@@ -821,7 +821,6 @@ public class HTTP {
                 if (authorization != oldAuth) return;
                 if (Log.on) Log.log(Authorization.class, "displaying proxy authorization dialog");
                 /*
-                  FIXME
                 Message.Q.add(new Message() {
                         public void perform() {
                             Box b = new Box();
@@ -839,11 +838,11 @@ public class HTTP {
         }
 
 
-        // ProxyAutoConfigRootScope ////////////////////////////////////////////////////////////////////
+        // ProxyAutoConfigRootJSScope ////////////////////////////////////////////////////////////////////
 
-        public static class ProxyAutoConfigRootScope extends JS.GlobalScope {
+        public static class ProxyAutoConfigRootJSScope extends JSScope.Global {
 
-            public ProxyAutoConfigRootScope() { super(null); }
+            public ProxyAutoConfigRootJSScope() { super(); }
         
             public Object get(Object name) {
                 if (name.equals("isPlainHostName")) return isPlainHostName;
@@ -862,36 +861,36 @@ public class HTTP {
                 else return super.get(name);
             }
         
-            private static final JS.Obj proxyConfigBindings = new JS.Obj();
-            private static final JS.Obj ProxyConfig = new JS.Obj() {
+            private static final JS proxyConfigBindings = new JS();
+            private static final JS ProxyConfig = new JS() {
                     public Object get(Object name) {
                         if (name.equals("bindings")) return proxyConfigBindings;
                         return null;
                     }
                 };
         
-            private static final JS.Callable isPlainHostName = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+            private static final JS isPlainHostName = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
                         return (args.elementAt(0).toString().indexOf('.') == -1) ? Boolean.TRUE : Boolean.FALSE;
                     }
                 };
         
-            private static final JS.Callable dnsDomainIs = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+            private static final JS dnsDomainIs = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
                         return (args.elementAt(0).toString().endsWith(args.elementAt(1).toString())) ? Boolean.TRUE : Boolean.FALSE;
                     }
                 };
         
-            private static final JS.Callable localHostOrDomainIs = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+            private static final JS localHostOrDomainIs = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
                         return (args.elementAt(0).toString().equals(args.elementAt(1).toString()) || 
                                 (args.elementAt(0).toString().indexOf('.') == -1 && args.elementAt(1).toString().startsWith(args.elementAt(0).toString()))) ?
                             Boolean.TRUE : Boolean.FALSE;
                     }
                 };
         
-            private static final JS.Callable isResolvable = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+            private static final JS isResolvable = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
                         try {
                             return (InetAddress.getByName(args.elementAt(0).toString()) != null) ? Boolean.TRUE : Boolean.FALSE;
                         } catch (UnknownHostException e) {
@@ -900,8 +899,8 @@ public class HTTP {
                     }
                 };
         
-            private static final JS.Callable isInNet = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+            private static final JS isInNet = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
                         if (args.length() != 3) return Boolean.FALSE;
                         try {
                             byte[] host = InetAddress.getByName(args.elementAt(0).toString()).getAddress();
@@ -913,13 +912,13 @@ public class HTTP {
                                     (host[3] & mask[3]) == net[3]) ?
                                 Boolean.TRUE : Boolean.FALSE;
                         } catch (Exception e) {
-                            throw new JS.Exn("exception in isInNet(): " + e);
+                            throw new JSExn("exception in isInNet(): " + e);
                         }
                     }
                 };
         
-            private static final JS.Callable dnsResolve = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+            private static final JS dnsResolve = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
                         try {
                             return InetAddress.getByName(args.elementAt(0).toString()).getHostAddress();
                         } catch (UnknownHostException e) {
@@ -928,8 +927,8 @@ public class HTTP {
                     }
                 };
         
-            private static final JS.Callable myIpAddress = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+            private static final JS myIpAddress = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
                         try {
                             return InetAddress.getLocalHost().getHostAddress();
                         } catch (UnknownHostException e) {
@@ -939,8 +938,8 @@ public class HTTP {
                     }
                 };
         
-            private static final JS.Callable dnsDomainLevels = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+            private static final JS dnsDomainLevels = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
                         String s = args.elementAt(0).toString();
                         int i = 0;
                         while((i = s.indexOf('.', i)) != -1) i++;
@@ -957,8 +956,8 @@ public class HTTP {
                 return false;
             }
         
-            private static final JS.Callable shExpMatch = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+            private static final JS shExpMatch = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
                         StringTokenizer st = new StringTokenizer(args.elementAt(1).toString(), "*", false);
                         String[] arr = new String[st.countTokens()];
                         String s = args.elementAt(0).toString();
@@ -969,8 +968,8 @@ public class HTTP {
         
             public static String[] days = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
         
-            private static final JS.Callable weekdayRange = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+            private static final JS weekdayRange = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
                         TimeZone tz = (args.length() < 3 || args.elementAt(2) == null || !args.elementAt(2).equals("GMT")) ? TimeZone.getTimeZone("UTC") : TimeZone.getDefault();
                         Calendar c = new GregorianCalendar();
                         c.setTimeZone(tz);
@@ -995,15 +994,15 @@ public class HTTP {
                     }
                 };
         
-            private static final JS.Callable dateRange = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
-                        throw new JS.Exn("XWT does not support dateRange() in PAC scripts");
+            private static final JS dateRange = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
+                        throw new JSExn("XWT does not support dateRange() in PAC scripts");
                     }
                 };
         
-            private static final JS.Callable timeRange = new JS.Callable() {
-                    public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
-                        throw new JS.Exn("XWT does not support timeRange() in PAC scripts");
+            private static final JS timeRange = new JS() {
+                    public Object call(org.xwt.js.JSArray args) throws JSExn {
+                        throw new JSExn("XWT does not support timeRange() in PAC scripts");
                     }
                 };
         
@@ -1149,7 +1148,7 @@ public class HTTP {
                 System.arraycopy(highHash, 0, lmHash, 8, 8);
                 return lmHash;
                 */
-                return null; // FIXME
+                return null;
             }
 
             /**
@@ -1215,7 +1214,7 @@ public class HTTP {
                 System.arraycopy(highResponse, 0, lmResponse, 16, 8);
                 return lmResponse;
                 */
-                return null; // FIXME
+                return null;
             }
 
             /**