2003/08/10 06:03:02
[org.ibex.core.git] / src / org / xwt / Proxy.java
index 15e6ef0..def26ca 100644 (file)
@@ -4,8 +4,8 @@ package org.xwt;
 import java.net.*;
 import java.io.*;
 import java.util.*;
+import org.xwt.js.*;
 import org.xwt.util.*;
-import org.mozilla.javascript.*;
 import org.bouncycastle.util.encoders.Base64;
 
 /** encapsulates most of the proxy logic; some is shared in HTTP.java */
@@ -35,7 +35,7 @@ public class Proxy {
     public String[] excluded = null;
     
     /** the PAC script */
-    public Function proxyAutoConfigFunction = null;
+    public JS.Callable proxyAutoConfigFunction = null;
     
     public static Proxy detectProxyViaManual() {
         Proxy ret = new Proxy();
@@ -87,12 +87,9 @@ public class Proxy {
         return ret;
     }
     
-    public static Scriptable proxyAutoConfigRootScope = new ProxyAutoConfigRootScope();
-    
-    public static Function getProxyAutoConfigFunction(String url) {
+    public static JS.Scope proxyAutoConfigRootScope = new ProxyAutoConfigRootScope();
+    public static JS.Callable getProxyAutoConfigFunction(String url) {
         try { 
-            Context cx = Context.enter();
-            cx.setOptimizationLevel(-1);
             BufferedReader br = new BufferedReader(new InputStreamReader(new HTTP(url, true).GET()));
             String s = null;
             String script = "";
@@ -116,15 +113,23 @@ public class Proxy {
                 if (Log.on) Log.log(Proxy.class, "DeCARPed PAC script:");
                 if (Log.on) Log.log(Proxy.class, script);
             }
-            
-            Script scr = cx.compileReader(proxyAutoConfigRootScope, new StringReader(script), "PAC script at " + url, 0, null);
-            scr.exec(cx, proxyAutoConfigRootScope);
-            return (Function)proxyAutoConfigRootScope.get("FindProxyForURL", null);
+
+            JS.CompiledFunction scr = JS.parse("PAC script at " + url, 0, new StringReader(script));
+            scr.call(new JS.Array(), proxyAutoConfigRootScope);
+            return (JS.Callable)proxyAutoConfigRootScope.get("FindProxyForURL");
         } catch (Exception e) {
             if (Log.on) {
                 Log.log(Platform.class, "WPAD detection failed due to:");
-                if (e instanceof EcmaError) Log.log(HTTP.class, ((EcmaError)e).getMessage() + " at " +
-                                                    ((EcmaError)e).getSourceName() + ":" + ((EcmaError)e).getLineNumber());
+                if (e instanceof JS.Exn) {
+                    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);
+                    }
+                }
                 else Log.log(Platform.class, e);
             }
             return null;
@@ -150,7 +155,8 @@ public class Proxy {
             if (Log.on) Log.log(Authorization.class, "displaying proxy authorization dialog");
             MessageQueue.add(new Message() {
                     public void perform() {
-                        Box b = new Box("org.xwt.builtin.proxy_authorization", null);
+                        Box b = new Box();
+                        Template.getTemplate("org.xwt.builtin.proxy_authorization", null).apply(b, null, null, null, 0, 0);
                         b.put("realm", realm);
                         b.put("proxyIP", proxyIP);
                     }
@@ -164,13 +170,14 @@ public class Proxy {
 
 
     // ProxyAutoConfigRootScope ////////////////////////////////////////////////////////////////////
-    
-    public static class ProxyAutoConfigRootScope extends ScriptableObject {
-        
-        public String getClassName() { return "ProxyAutoConfigRootScope"; }
-        ProxyAutoConfigRootScope() { Context.enter().initStandardObjects(this); }
+
+    public static class ProxyAutoConfigRootScope extends JS.Scope {
+
+        public ProxyAutoConfigRootScope() { super(null); }
         
-        public Object get(String name, Scriptable start) {
+        // FIXME: needs "standard objects"
+
+        public Object get(Object name) {
             if (name.equals("isPlainHostName")) return isPlainHostName;
             else if (name.equals("dnsDomainIs")) return dnsDomainIs;
             else if (name.equals("localHostOrDomainIs")) return localHostOrDomainIs;
@@ -184,82 +191,77 @@ public class Proxy {
             else if (name.equals("dateRange")) return dateRange;
             else if (name.equals("timeRange")) return timeRange;
             else if (name.equals("ProxyConfig")) return ProxyConfig;
-            else return super.get(name, start);
+            else return super.get(name);
         }
         
-        private static final JSObject proxyConfigBindings = new JSObject();
-        private static final JSObject ProxyConfig = new JSObject() {
-                public Object get(String name, Scriptable start) {
+        private static final JS.Obj proxyConfigBindings = new JS.Obj();
+        private static final JS.Obj ProxyConfig = new JS.Obj() {
+                public Object get(Object name) {
                     if (name.equals("bindings")) return proxyConfigBindings;
                     return null;
                 }
             };
         
-        private static abstract class JSFunction extends JSObject implements Function {
-            JSFunction() { setSeal(true); }
-            public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
-        }
-        
-        private static final JSFunction isPlainHostName = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                    return (args[0].toString().indexOf('.') == -1) ? Boolean.TRUE : Boolean.FALSE;
+        private static final JS.Callable isPlainHostName = new JS.Callable() {
+                public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+                    return (args.elementAt(0).toString().indexOf('.') == -1) ? Boolean.TRUE : Boolean.FALSE;
                 }
             };
         
-        private static final JSFunction dnsDomainIs = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                    return (args[0].toString().endsWith(args[1].toString())) ? 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 {
+                    return (args.elementAt(0).toString().endsWith(args.elementAt(1).toString())) ? Boolean.TRUE : Boolean.FALSE;
                 }
             };
         
-        private static final JSFunction localHostOrDomainIs = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                    return (args[0].toString().equals(args[1].toString()) || 
-                            (args[0].toString().indexOf('.') == -1 && args[1].toString().startsWith(args[0].toString()))) ?
+        private static final JS.Callable localHostOrDomainIs = new JS.Callable() {
+                public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+                    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 JSFunction isResolvable = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+        private static final JS.Callable isResolvable = new JS.Callable() {
+                public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
                     try {
-                        return (InetAddress.getByName(args[0].toString()) != null) ? Boolean.TRUE : Boolean.FALSE;
+                        return (InetAddress.getByName(args.elementAt(0).toString()) != null) ? Boolean.TRUE : Boolean.FALSE;
                     } catch (UnknownHostException e) {
                         return Boolean.FALSE;
                     }
                 }
             };
         
-        private static final JSFunction isInNet = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                    if (args.length != 3) return Boolean.FALSE;
+        private static final JS.Callable isInNet = new JS.Callable() {
+                public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+                    if (args.length() != 3) return Boolean.FALSE;
                     try {
-                        byte[] host = InetAddress.getByName(args[0].toString()).getAddress();
-                        byte[] net = InetAddress.getByName(args[1].toString()).getAddress();
-                        byte[] mask = InetAddress.getByName(args[2].toString()).getAddress();
+                        byte[] host = InetAddress.getByName(args.elementAt(0).toString()).getAddress();
+                        byte[] net = InetAddress.getByName(args.elementAt(1).toString()).getAddress();
+                        byte[] mask = InetAddress.getByName(args.elementAt(2).toString()).getAddress();
                         return ((host[0] & mask[0]) == net[0] &&
                                 (host[1] & mask[1]) == net[1] &&
                                 (host[2] & mask[2]) == net[2] &&
                                 (host[3] & mask[3]) == net[3]) ?
                             Boolean.TRUE : Boolean.FALSE;
                     } catch (Exception e) {
-                        throw new JavaScriptException("exception in isInNet(): " + e);
+                        throw new JS.Exn("exception in isInNet(): " + e);
                     }
                 }
             };
         
-        private static final JSFunction dnsResolve = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+        private static final JS.Callable dnsResolve = new JS.Callable() {
+                public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
                     try {
-                        return InetAddress.getByName(args[0].toString()).getHostAddress();
+                        return InetAddress.getByName(args.elementAt(0).toString()).getHostAddress();
                     } catch (UnknownHostException e) {
                         return null;
                     }
                 }
             };
         
-        private static final JSFunction myIpAddress = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
+        private static final JS.Callable myIpAddress = new JS.Callable() {
+                public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
                     try {
                         return InetAddress.getLocalHost().getHostAddress();
                     } catch (UnknownHostException e) {
@@ -269,9 +271,9 @@ public class Proxy {
                 }
             };
         
-        private static final JSFunction dnsDomainLevels = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                    String s = args[0].toString();
+        private static final JS.Callable dnsDomainLevels = new JS.Callable() {
+                public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+                    String s = args.elementAt(0).toString();
                     int i = 0;
                     while((i = s.indexOf('.', i)) != -1) i++;
                     return new Integer(i);
@@ -279,19 +281,19 @@ public class Proxy {
             };
         
         private static boolean match(String[] arr, String s, int index) {
-            if (index == arr.length) return true;
+            if (index >= arr.length) return true;
             for(int i=0; i<s.length(); i++) {
                 String s2 = s.substring(i);
-                if (s2.startsWith(arr[index]) && match(arr, s.substring(arr[index].length()), index + 1)) return true;
+                if (s2.startsWith(arr[index]) && match(arr, s2.substring(arr[index].length()), index + 1)) return true;
             }
             return false;
         }
         
-        private static final JSFunction shExpMatch = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                    StringTokenizer st = new StringTokenizer(args[1].toString(), "*", false);
+        private static final JS.Callable shExpMatch = new JS.Callable() {
+                public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+                    StringTokenizer st = new StringTokenizer(args.elementAt(1).toString(), "*", false);
                     String[] arr = new String[st.countTokens()];
-                    String s = args[0].toString();
+                    String s = args.elementAt(0).toString();
                     for (int i=0; st.hasMoreTokens(); i++) arr[i] = st.nextToken();
                     return match(arr, s, 0) ? Boolean.TRUE : Boolean.FALSE;
                 }
@@ -299,23 +301,23 @@ public class Proxy {
         
         public static String[] days = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
         
-        private static final JSFunction weekdayRange = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                    TimeZone tz = (args.length < 3 || args[2] == null || !args[2].equals("GMT")) ? TimeZone.getTimeZone("UTC") : TimeZone.getDefault();
+        private static final JS.Callable weekdayRange = new JS.Callable() {
+                public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
+                    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);
-                    c.setTime(new Date());
-                    Date d = c.getTime();
+                    c.setTime(new java.util.Date());
+                    java.util.Date d = c.getTime();
                     int day = d.getDay();
                     
-                    String d1s = args[0].toString().toUpperCase();
+                    String d1s = args.elementAt(0).toString().toUpperCase();
                     int d1 = 0, d2 = 0;
                     for(int i=0; i<days.length; i++) if (days[i].equals(d1s)) d1 = i;
                     
-                    if (args.length == 1)
+                    if (args.length() == 1)
                         return d1 == day ? Boolean.TRUE : Boolean.FALSE;
                     
-                    String d2s = args[1].toString().toUpperCase();
+                    String d2s = args.elementAt(1).toString().toUpperCase();
                     for(int i=0; i<days.length; i++) if (days[i].equals(d2s)) d2 = i;
                     
                     return
@@ -325,18 +327,18 @@ public class Proxy {
                 }
             };
         
-        private static final JSFunction dateRange = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                    throw new JavaScriptException("XWT does not support dateRange() in PAC scripts");
+        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 JSFunction timeRange = new JSFunction() {
-                public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                    throw new JavaScriptException("XWT does not support timeRange() 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");
                 }
             };
         
     }
-    
+
 }