From: adam Date: Fri, 23 Dec 2005 00:23:29 +0000 (+0000) Subject: migrated JS PAC stuff out of HTTP and into org.ibex.js X-Git-Url: http://git.megacz.com/?p=org.ibex.net.git;a=commitdiff_plain;h=038a2be3688a5df60474b866a4a5cb3796198974 migrated JS PAC stuff out of HTTP and into org.ibex.js darcs-hash:20051223002329-5007d-a1e5a91db09731c64bf15e3a3648817fe1d62238.gz --- diff --git a/src/org/ibex/net/HTTP.java b/src/org/ibex/net/HTTP.java index 894a236..5cab19d 100644 --- a/src/org/ibex/net/HTTP.java +++ b/src/org/ibex/net/HTTP.java @@ -93,7 +93,7 @@ public class HTTP { case "domain": domain = end; case "path": path = end; case "expires": expires = new Date(end); - //#end + //#end } if (h.get(name) == null) h.put(name, new Vec()); ((Vec)h.get(name)).addElement(new Cookie(name, value, domain, path, expires, secure)); @@ -104,7 +104,9 @@ public class HTTP { // Public Methods //////////////////////////////////////////////////////////////////////////////////////// public HTTP(String url) { this(url, false); } - public HTTP(String url, boolean skipResolveCheck) { originalUrl = url; this.skipResolveCheck = skipResolveCheck; } + public HTTP(String url, boolean skipResolveCheck) { this(url, skipResolveCheck, null); } + public HTTP(String url, Proxy p) { this(url, false, p); } + public HTTP(String url, boolean skipResolveCheck, Proxy p) { originalUrl = url; this.skipResolveCheck = skipResolveCheck; proxy = p; } /** Performs an HTTP GET request */ public InputStream GET(String referer, Cookie.Jar cookies) throws IOException { @@ -139,6 +141,7 @@ public class HTTP { boolean firstRequest = true; ///< true iff this is the first request to be made on this socket boolean skipResolveCheck = false; ///< allowed to skip the resolve check when downloading PAC script boolean proxied = false; ///< true iff we're using a proxy + Proxy proxy; /** this is null if the current request is the first request on * this HTTP connection; otherwise it is a Semaphore which will be @@ -260,8 +263,8 @@ public class HTTP { } catch (UnknownHostException uhe) { } /* - if (Platform.detectProxy() == null) - throw new HTTPException("could not resolve hostname \"" + host + "\" and no proxy configured"); + if (Platform.detectProxy() == null) + throw new HTTPException("could not resolve hostname \"" + host + "\" and no proxy configured"); */ } @@ -275,7 +278,7 @@ public class HTTP { } /** Attempts a direct connection */ - private Socket attemptDirect() { + Socket attemptDirect() { try { Log.info(this, "attempting to create unproxied socket to " + host + ":" + port + (ssl ? " [ssl]" : "")); @@ -286,120 +289,7 @@ public class HTTP { } } - /** Attempts to use an HTTP proxy, employing the CONNECT method if HTTPS is requested */ - private Socket attemptHttpProxy(String proxyHost, int proxyPort) { - try { - if (Log.verbose) Log.info(this, "attempting to create HTTP proxied socket using proxy " + proxyHost + ":" + proxyPort); - Socket sock = getSocket(proxyHost, proxyPort, ssl, false); - - if (!ssl) { - if (!path.startsWith("http://")) path = "http://" + host + ":" + port + path; - return sock; - } - - PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream())); - BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream())); - pw.print("CONNECT " + host + ":" + port + " HTTP/1.1\r\n\r\n"); - pw.flush(); - String s = br.readLine(); - if (s.charAt(9) != '2') throw new HTTPException("proxy refused CONNECT method: \"" + s + "\""); - while (br.readLine().length() > 0) { }; - ((SSL)sock).negotiate(); - return sock; - - } catch (IOException e) { - if (Log.on) Log.info(this, "exception in attemptHttpProxy(): " + e); - return null; - } - } - - /** - * Implements SOCKSv4 with v4a DNS extension - * @see http://www.socks.nec.com/protocol/socks4.protocol - * @see http://www.socks.nec.com/protocol/socks4a.protocol - */ - private Socket attemptSocksProxy(String proxyHost, int proxyPort) { - - // even if host is already a "x.y.z.w" string, we use this to parse it into bytes - InetAddress addr = null; - try { addr = InetAddress.getByName(host); } catch (Exception e) { } - - if (Log.verbose) Log.info(this, "attempting to create SOCKSv4" + (addr == null ? "" : "a") + - " proxied socket using proxy " + proxyHost + ":" + proxyPort); - - try { - Socket sock = getSocket(proxyHost, proxyPort, ssl, false); - - DataOutputStream dos = new DataOutputStream(sock.getOutputStream()); - dos.writeByte(0x04); // SOCKSv4(a) - dos.writeByte(0x01); // CONNECT - dos.writeShort(port & 0xffff); // port - if (addr == null) dos.writeInt(0x00000001); // bogus IP - else dos.write(addr.getAddress()); // actual IP - dos.writeByte(0x00); // no userid - if (addr == null) { - PrintWriter pw = new PrintWriter(new OutputStreamWriter(dos)); - pw.print(host); - pw.flush(); - dos.writeByte(0x00); // hostname null terminator - } - dos.flush(); - - DataInputStream dis = new DataInputStream(sock.getInputStream()); - dis.readByte(); // reply version - byte success = dis.readByte(); // success/fail - dis.skip(6); // ip/port - - if ((int)(success & 0xff) == 90) { - if (ssl) ((SSL)sock).negotiate(); - return sock; - } - if (Log.on) Log.info(this, "SOCKS server denied access, code " + (success & 0xff)); - return null; - - } catch (IOException e) { - if (Log.on) Log.info(this, "exception in attemptSocksProxy(): " + e); - return null; - } - } - - /** executes the PAC script and dispatches a call to one of the other attempt methods based on the result */ - /* - private Socket attemptPAC(org.ibex.js.JS pacFunc) { - if (Log.verbose) Log.info(this, "evaluating PAC script"); - String pac = null; - try { - Object obj = pacFunc.call(url, host, null, null, 2); - if (Log.verbose) Log.info(this, " PAC script returned \"" + obj + "\""); - pac = obj.toString(); - } catch (Throwable e) { - if (Log.on) Log.info(this, "PAC script threw exception " + e); - return null; - } - - StringTokenizer st = new StringTokenizer(pac, ";", false); - while (st.hasMoreTokens()) { - String token = st.nextToken().trim(); - if (Log.verbose) Log.info(this, " trying \"" + token + "\"..."); - try { - Socket ret = null; - if (token.startsWith("DIRECT")) - ret = attemptDirect(); - else if (token.startsWith("PROXY")) - ret = attemptHttpProxy(token.substring(token.indexOf(' ') + 1, token.indexOf(':')), - Integer.parseInt(token.substring(token.indexOf(':') + 1))); - else if (token.startsWith("SOCKS")) - ret = attemptSocksProxy(token.substring(token.indexOf(' ') + 1, token.indexOf(':')), - Integer.parseInt(token.substring(token.indexOf(':') + 1))); - if (ret != null) return ret; - } catch (Throwable e) { - if (Log.on) Log.info(this, "attempt at \"" + token + "\" failed due to " + e + "; trying next token"); - } - } - if (Log.on) Log.info(this, "all PAC results exhausted"); - return null; - } - */ + // Everything Else //////////////////////////////////////////////////////////////////////////// @@ -439,20 +329,12 @@ public class HTTP { host = temphost; if (Log.verbose) Log.info(this, "creating HTTP object for connection to " + host + ":" + port); - /* - Proxy pi = Platform.detectProxy(); - OUTER: do { - if (pi != null) { - for(int i=0; i 0) { }; + ((SSL)sock).negotiate(); + return sock; + + } catch (IOException e) { + if (Log.on) Log.info(this, "exception in attemptHttpProxy(): " + e); + return null; + } + } + + /** + * Implements SOCKSv4 with v4a DNS extension + * @see http://www.socks.nec.com/protocol/socks4.protocol + * @see http://www.socks.nec.com/protocol/socks4a.protocol + */ + protected Socket attemptSocksProxy(HTTP http, String proxyHost, int proxyPort) { + + // even if host is already a "x.y.z.w" string, we use this to parse it into bytes + InetAddress addr = null; + try { addr = InetAddress.getByName(http.host); } catch (Exception e) { } + + if (Log.verbose) Log.info(this, "attempting to create SOCKSv4" + (addr == null ? "" : "a") + + " proxied socket using proxy " + proxyHost + ":" + proxyPort); + + try { + Socket sock = http.getSocket(proxyHost, proxyPort, http.ssl, false); - // MS CARP hack - Vector carpHosts = new Vector(); - for(int i=0; i= d1 && day <= d2) || (d1 > d2 && (day >= d1 || day <= d2))) ? T : F; - - case "dateRange": throw new JSExn("Ibex does not support dateRange() in PAC scripts"); - case "timeRange": throw new JSExn("Ibex does not support timeRange() in PAC scripts"); - // #end - return super.callMethod(method, a0, a1, a2, rest, nargs); - } - private static boolean match(String[] arr, String s, int index) { - if (index >= arr.length) return true; - for(int i=0; i>> 1); - material[2] = (byte) (keyBytes[1] << 6 | (keyBytes[2] & 0xff) >>> 2); - material[3] = (byte) (keyBytes[2] << 5 | (keyBytes[3] & 0xff) >>> 3); - material[4] = (byte) (keyBytes[3] << 4 | (keyBytes[4] & 0xff) >>> 4); - material[5] = (byte) (keyBytes[4] << 3 | (keyBytes[5] & 0xff) >>> 5); - material[6] = (byte) (keyBytes[5] << 2 | (keyBytes[6] & 0xff) >>> 6); - material[7] = (byte) (keyBytes[6] << 1); - oddParity(material); - return new SecretKeySpec(material, "DES"); - } - */ + /* + private static Key createDESKey(byte[] bytes, int offset) { + byte[] keyBytes = new byte[7]; + System.arraycopy(bytes, offset, keyBytes, 0, 7); + byte[] material = new byte[8]; + material[0] = keyBytes[0]; + material[1] = (byte) (keyBytes[0] << 7 | (keyBytes[1] & 0xff) >>> 1); + material[2] = (byte) (keyBytes[1] << 6 | (keyBytes[2] & 0xff) >>> 2); + material[3] = (byte) (keyBytes[2] << 5 | (keyBytes[3] & 0xff) >>> 3); + material[4] = (byte) (keyBytes[3] << 4 | (keyBytes[4] & 0xff) >>> 4); + material[5] = (byte) (keyBytes[4] << 3 | (keyBytes[5] & 0xff) >>> 5); + material[6] = (byte) (keyBytes[5] << 2 | (keyBytes[6] & 0xff) >>> 6); + material[7] = (byte) (keyBytes[6] << 1); + oddParity(material); + return new SecretKeySpec(material, "DES"); + } + */ /** * Applies odd parity to the given byte array.