From 2b4144ceaaeeab894181d0fdaf8b6eb33ad4de2e Mon Sep 17 00:00:00 2001 From: megacz Date: Fri, 30 Jan 2004 06:49:48 +0000 Subject: [PATCH] 2002/08/07 04:44:53 darcs-hash:20040130064948-2ba56-1e8aab076dd0a686dab904bc88eda80e22f428dc.gz --- src/org/xwt/Proxy.java | 342 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 342 insertions(+) create mode 100644 src/org/xwt/Proxy.java diff --git a/src/org/xwt/Proxy.java b/src/org/xwt/Proxy.java new file mode 100644 index 0000000..15e6ef0 --- /dev/null +++ b/src/org/xwt/Proxy.java @@ -0,0 +1,342 @@ +// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] +package org.xwt; + +import java.net.*; +import java.io.*; +import java.util.*; +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 */ +public class Proxy { + + public Proxy() { } + + /** the HTTP Proxy host to use */ + public String httpProxyHost = null; + + /** the HTTP Proxy port to use */ + public int httpProxyPort = -1; + + /** if a seperate proxy should be used for HTTPS, this is the hostname; otherwise, httpProxyHost is used */ + public String httpsProxyHost = null; + + /** if a seperate proxy should be used for HTTPS, this is the port */ + public int httpsProxyPort = -1; + + /** the SOCKS Proxy Host to use */ + public String socksProxyHost = null; + + /** the SOCKS Proxy Port to use */ + public int socksProxyPort = -1; + + /** hosts to be excluded from proxy use; wildcards permitted */ + public String[] excluded = null; + + /** the PAC script */ + public Function proxyAutoConfigFunction = null; + + public static Proxy detectProxyViaManual() { + Proxy ret = new Proxy(); + + 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; + } + + public static Scriptable proxyAutoConfigRootScope = new ProxyAutoConfigRootScope(); + + public static Function 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 = ""; + while((s = br.readLine()) != null) script += s + "\n"; + if (Log.on) Log.log(Proxy.class, "successfully retrieved WPAD PAC:"); + if (Log.on) Log.log(Proxy.class, script); + + // MS CARP hack + Vector carpHosts = new Vector(); + for(int i=0; i= d1 && day <= d2) || + (d1 > d2 && (day >= d1 || day <= d2))) ? + Boolean.TRUE : Boolean.FALSE; + } + }; + + 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 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"); + } + }; + + } + +} -- 1.7.10.4