2002/07/15 23:11:20
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 06:48:49 +0000 (06:48 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 06:48:49 +0000 (06:48 +0000)
darcs-hash:20040130064849-2ba56-be18ce7ec9e9478acb99b5f747d3602364a2024f.gz

CHANGES
src/org/xwt/HTTP.java

diff --git a/CHANGES b/CHANGES
index ca14c3b..e118bdd 100644 (file)
--- a/CHANGES
+++ b/CHANGES
 02-Jul david  colorpicker.xwt, slider.xwt, slider_base.xwt: Implemented
               slider.xwt (limits, value display, quantization) and made
               major improvements/fixed to colorpicker.
+
+15-Jul megacz HTTP.java: implemented isInNet(), weekdayRange(), now
+              checks environment vars for proxies
index 3ac94fe..03fc997 100644 (file)
@@ -365,36 +365,53 @@ public class HTTP {
         // this method has been disabled because it was causing problems -- some domains are set up so that *.foo.com resolves
         // to a single IP, for any value of *. If the client's home domain is foo.com, then xwt-proxy-httpHost will resolve erroneously.
         public static ProxyInfo detectProxyViaManual() {
-            return null;
-            /*
-            try {
-                // continue iff one of the two resolves
-                try { InetAddress.getByName("xwt-proxy-httpHost"); }
-                catch (UnknownHostException e) { InetAddress.getByName("xwt-proxy-socksHost"); }
-
-                if (Log.on) Log.log(Platform.class, "using xwt-proxy-* configuration");
-                ProxyInfo ret = new ProxyInfo();
-                try {
-                    ret.httpProxyHost = InetAddress.getByName("xwt-proxy-httpHost").getHostAddress();
-                    byte[] quadbyte = InetAddress.getByName("xwt-proxy-httpPort").getAddress();
-                    ret.httpProxyPort = ((quadbyte[1] & 0xff) * 10000) + ((quadbyte[2] & 0xff) * 100) + (quadbyte[3] & 0xff);
-                } catch (UnknownHostException e) { }
-                try {
-                    ret.httpsProxyHost = InetAddress.getByName("xwt-proxy-httpsHost").getHostAddress();
-                    byte[] quadbyte = InetAddress.getByName("xwt-proxy-httpsPort").getAddress();
-                    ret.httpsProxyPort = ((quadbyte[1] & 0xff) * 10000) + ((quadbyte[2] & 0xff) * 100) + (quadbyte[3] & 0xff);
-                } catch (UnknownHostException e) { }
-                try {
-                    ret.socksProxyHost = InetAddress.getByName("xwt-proxy-socksHost").getHostAddress();
-                    byte[] quadbyte = InetAddress.getByName("xwt-proxy-socksPort").getAddress();
-                    ret.socksProxyPort = ((quadbyte[1] & 0xff) * 10000) + ((quadbyte[2] & 0xff) * 100) + (quadbyte[3] & 0xff);
-                } catch (UnknownHostException e) { }
-                return ret;
-            } catch (UnknownHostException e) {
-                if (Log.on) Log.log(Platform.class, "xwt-proxy-* detection failed due to: " + e);
-                return null;
+            ProxyInfo ret = new ProxyInfo();
+            
+            ret.httpProxyHost = Platform.getEnv("http_proxy");
+            if (ret.httpProxyHost != null) {
+                if (ret.httpProxyHost.startsWith("http://")) ret.httpProxyHost = ret.httpProxyHost.substring(7);
+                if (ret.httpProxyHost.endsWith("/")) ret.httpProxyHost = ret.httpProxyHost.substring(0, ret.httpProxyHost.length() - 1);
+                if (ret.httpProxyHost.indexOf(':') != -1) {
+                    ret.httpProxyPort = Integer.parseInt(ret.httpProxyHost.substring(ret.httpProxyHost.indexOf(':') + 1));
+                    ret.httpProxyHost = ret.httpProxyHost.substring(0, ret.httpProxyHost.indexOf(':'));
+                } else {
+                    ret.httpProxyPort = 80;
+                }
+            }
+            
+            ret.httpsProxyHost = Platform.getEnv("https_proxy");
+            if (ret.httpsProxyHost != null) {
+                if (ret.httpsProxyHost.startsWith("https://")) ret.httpsProxyHost = ret.httpsProxyHost.substring(7);
+                if (ret.httpsProxyHost.endsWith("/")) ret.httpsProxyHost = ret.httpsProxyHost.substring(0, ret.httpsProxyHost.length() - 1);
+                if (ret.httpsProxyHost.indexOf(':') != -1) {
+                    ret.httpsProxyPort = Integer.parseInt(ret.httpsProxyHost.substring(ret.httpsProxyHost.indexOf(':') + 1));
+                    ret.httpsProxyHost = ret.httpsProxyHost.substring(0, ret.httpsProxyHost.indexOf(':'));
+                } else {
+                    ret.httpsProxyPort = 80;
+                }
+            }
+            
+            ret.socksProxyHost = Platform.getEnv("socks_proxy");
+            if (ret.socksProxyHost != null) {
+                if (ret.socksProxyHost.startsWith("socks://")) ret.socksProxyHost = ret.socksProxyHost.substring(7);
+                if (ret.socksProxyHost.endsWith("/")) ret.socksProxyHost = ret.socksProxyHost.substring(0, ret.socksProxyHost.length() - 1);
+                if (ret.socksProxyHost.indexOf(':') != -1) {
+                    ret.socksProxyPort = Integer.parseInt(ret.socksProxyHost.substring(ret.socksProxyHost.indexOf(':') + 1));
+                    ret.socksProxyHost = ret.socksProxyHost.substring(0, ret.socksProxyHost.indexOf(':'));
+                } else {
+                    ret.socksProxyPort = 80;
+                }
+            }
+            
+            String noproxy = Platform.getEnv("no_proxy");
+            if (noproxy != null) {
+                StringTokenizer st = new StringTokenizer(noproxy, ",");
+                ret.excluded = new String[st.countTokens()];
+                for(int i=0; st.hasMoreTokens(); i++) ret.excluded[i] = st.nextToken();
             }
-            */
+            
+            if (ret.httpProxyHost == null && ret.socksProxyHost == null) return null;
+            return ret;
         }
 
         // FIXME: search up from default domain
@@ -421,15 +438,15 @@ public class HTTP {
                 String s = null;
                 String script = "";
                 while((s = br.readLine()) != null) script += s + "\n";
-                if (Log.on) Log.log(HTTP.ProxyInfo.class, "successfully retrieved WPAD PAC:");
-                if (Log.on) Log.log(HTTP.ProxyInfo.class, script);
+                if (Log.on) Log.log(ProxyInfo.class, "successfully retrieved WPAD PAC:");
+                if (Log.on) Log.log(ProxyInfo.class, script);
                 
                 // MS CARP hack
                 Vector carpHosts = new Vector();
                 for(int i=0; i<script.length(); i++)
                     if (script.regionMatches(i, "new Node(", 0, 9)) {
                         String host = script.substring(i + 10, script.indexOf('\"', i + 11));
-                        if (Log.on) Log.log(HTTP.ProxyInfo.class, "Detected MS Proxy Server CARP Script, Host=" + host);
+                        if (Log.on) Log.log(ProxyInfo.class, "Detected MS Proxy Server CARP Script, Host=" + host);
                         carpHosts.addElement(host);
                     }
                 if (carpHosts.size() > 0) {
@@ -437,8 +454,8 @@ public class HTTP {
                     for(int i=0; i<carpHosts.size(); i++)
                         script += "PROXY " + carpHosts.elementAt(i) + "; ";
                     script += "\";\n}";
-                    if (Log.on) Log.log(HTTP.ProxyInfo.class, "DeCARPed PAC script:");
-                    if (Log.on) Log.log(HTTP.ProxyInfo.class, script);
+                    if (Log.on) Log.log(ProxyInfo.class, "DeCARPed PAC script:");
+                    if (Log.on) Log.log(ProxyInfo.class, script);
                 }
 
                 Script scr = cx.compileReader(proxyAutoConfigRootScope, new StringReader(script), "PAC script at " + url, 0, null);
@@ -521,8 +538,19 @@ public class HTTP {
 
             private static final JSFunction isInNet = new JSFunction() {
                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
-                        // FIXME
-                        return null;
+                        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();
+                            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);
+                        }
                     }
                 };
 
@@ -576,17 +604,31 @@ public class HTTP {
                     }
                 };
 
+            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 {
-                        throw new JavaScriptException("XWT does not support weekdayRange() in PAC scripts");
-                        /*
                         TimeZone tz = (args.length < 3 || args[2] == null || !args[2].equals("GMT")) ? TimeZone.getTimeZone("UTC") : TimeZone.getDefault();
-                        Calendar c = new Calendar();
+                        Calendar c = new GregorianCalendar();
                         c.setTimeZone(tz);
                         c.setTime(new Date());
                         Date d = c.getTime();
-                        if (args.length == 1) return 
-                        */
+                        int day = d.getDay();
+
+                        String d1s = args[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)
+                            return d1 == day ? Boolean.TRUE : Boolean.FALSE;
+
+                        String d2s = args[1].toString().toUpperCase();
+                        for(int i=0; i<days.length; i++) if (days[i].equals(d2s)) d2 = i;
+
+                        return
+                            ((d1 <= d2 && day >= d1 && day <= d2) ||
+                             (d1 > d2 && (day >= d1 || day <= d2))) ?
+                            Boolean.TRUE : Boolean.FALSE;
                     }
                 };