update core for recent js changes
[org.ibex.core.git] / src / org / ibex / net / HTTP.java
index 10da62b..da94713 100644 (file)
@@ -1,13 +1,14 @@
 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.ibex;
+package org.ibex.net;
 
 import java.net.*;
 import java.io.*;
 import java.util.*;
 import org.ibex.js.*;
 import org.ibex.util.*;
-import org.bouncycastle.util.encoders.Base64;
-import org.bouncycastle.crypto.digests.*;
+import org.ibex.plat.*;
+import org.ibex.core.*;
+import org.ibex.crypto.*;
 
 /**
  *  This object encapsulates a *single* HTTP connection. Multiple requests may be pipelined over a connection (thread-safe),
@@ -179,8 +180,8 @@ public class HTTP {
     /** Attempts a direct connection */
     private Socket attemptDirect() {
         try {
-            if (Log.verbose) Log.info(this, "attempting to create unproxied socket to " +
-                                     host + ":" + port + (ssl ? " [ssl]" : ""));
+            Log.info(this, "attempting to create unproxied socket to " +
+                     host + ":" + port + (ssl ? " [ssl]" : ""));
             return getSocket(host, port, ssl, true);
         } catch (IOException e) {
             if (Log.on) Log.info(this, "exception in attemptDirect(): " + e);
@@ -270,9 +271,9 @@ public class HTTP {
         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();
+            JS obj = pacFunc.call(JS.S(url), JS.S(host), null, null, 2);
+            if (Log.verbose) Log.info(this, "  PAC script returned \"" + JS.debugToString(obj) + "\"");
+            pac = JS.toString(obj);
         } catch (Throwable e) {
             if (Log.on) Log.info(this, "PAC script threw exception " + e);
             return null;
@@ -327,7 +328,7 @@ public class HTTP {
             throw new IOException("HTTP only supports http/https urls");
         }
         if (url.indexOf("://") == -1) throw new IOException("URLs must contain a ://");
-        String temphost = url.substring(url.indexOf("://") + 1);
+        String temphost = url.substring(url.indexOf("://") + 3);
         path = temphost.substring(temphost.indexOf('/'));
         temphost = temphost.substring(0, temphost.indexOf('/'));
         if (temphost.indexOf(':') != -1) {
@@ -379,7 +380,6 @@ public class HTTP {
         if (authCache.get(originalUrl) != null) pw.print("Authorization: " + authCache.get(originalUrl) + "\r\n");
 
         pw.print(content == null ? "\r\n" : content);
-        pw.print("\r\n");
         pw.flush();
     }
 
@@ -638,7 +638,7 @@ public class HTTP {
 
     private String H(String s) throws IOException {
         byte[] b = s.getBytes("UTF8");
-        MD5Digest md5 = new MD5Digest();
+        MD5 md5 = new MD5();
         md5.update(b, 0, b.length);
         byte[] out = new byte[md5.getDigestSize()];
         md5.doFinal(out, 0);
@@ -718,7 +718,7 @@ public class HTTP {
             return ret;
         }
     
-        public static JSScope proxyAutoConfigRootScope = new ProxyAutoConfigRootScope();
+        public static JS proxyAutoConfigRootScope = /*new ProxyAutoConfigRootScope();*/ null; // JS:FIXME: New api
         public static JS getProxyAutoConfigFunction(String url) {
             try { 
                 BufferedReader br = new BufferedReader(new InputStreamReader(new HTTP(url, true).GET()));
@@ -746,20 +746,21 @@ public class HTTP {
                 }
 
                 JS scr = JS.fromReader("PAC script at " + url, 0, new StringReader(script));
-                JS.cloneWithNewParentScope(scr, proxyAutoConfigRootScope).call(null, null, null, null, 0);
-                return (JS)proxyAutoConfigRootScope.get("FindProxyForURL");
+                JS.cloneWithNewGlobalScope(scr, proxyAutoConfigRootScope).call(null, null, null, null, 0);
+                return (JS)proxyAutoConfigRootScope.get(JS.S("FindProxyForURL"));
             } catch (Exception e) {
                 if (Log.on) {
                     Log.info(Platform.class, "WPAD detection failed due to:");
-                    if (e instanceof JSExn) {
+                    // I have no idea what this was supposed to do
+                    /*if (e instanceof JSExn) {
                         try {
                             org.ibex.js.JSArray arr = new org.ibex.js.JSArray();
                             arr.addElement(((JSExn)e).getObject());
                         } catch (Exception e2) {
                             Log.info(Platform.class, e);
                         }
-                    }
-                    else Log.info(Platform.class, e);
+                    }*/
+                    Log.info(Platform.class, e);
                 }
                 return null;
             }
@@ -783,15 +784,15 @@ public class HTTP {
 
                 if (authorization != oldAuth) return;
                 if (Log.on) Log.info(Authorization.class, "displaying proxy authorization dialog");
-                Scheduler.add(new Scheduler.Task() {
+                Scheduler.add(new Task() {
                         public void perform() throws IOException, JSExn {
                             Box b = new Box();
                             Template t = null;
                             // FIXME
                             //Template.buildTemplate("org/ibex/builtin/proxy_authorization.ibex", Stream.getInputStream((JS)Main.builtin.get("org/ibex/builtin/proxy_authorization.ibex")), new Ibex(null));
                             t.apply(b);
-                            b.put("realm", realm);
-                            b.put("proxyIP", proxyIP);
+                            b.put(JS.S("realm"), JS.S(realm));
+                            b.put(JS.S("proxyIP"), JS.S(proxyIP));
                         }
                     });
 
@@ -803,7 +804,8 @@ public class HTTP {
 
         // ProxyAutoConfigRootJSScope ////////////////////////////////////////////////////////////////////
 
-        public static class ProxyAutoConfigRootScope extends JSScope.Global {
+        // FIXME: JS Update for new API
+        /*public static class ProxyAutoConfigRootScope extends JSScope.Global {
 
             public ProxyAutoConfigRootScope() { super(); }
         
@@ -826,7 +828,7 @@ public class HTTP {
                 return super.get(name);
             }
         
-            private static final JS proxyConfigBindings = new JS();
+            private static final JS proxyConfigBindings = new JS.O();
             private static final JS ProxyConfig = new JS() {
                     public Object get(Object name) {
                         if (name.equals("bindings")) return proxyConfigBindings;
@@ -915,7 +917,7 @@ public class HTTP {
                 return false;
             }
             public static String[] days = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
-        }
+        }*/
 
 
         /**
@@ -1021,7 +1023,7 @@ public class HTTP {
             public static byte[] getNTLM2SessionResponse(String password,
                                                          byte[] challenge, byte[] clientChallenge) throws UnsupportedEncodingException {
                 byte[] ntlmHash = ntlmHash(password);
-                MD5Digest md5 = new MD5Digest();
+                MD5 md5 = new MD5();
                 md5.update(challenge, 0, challenge.length);
                 md5.update(clientChallenge, 0, clientChallenge.length);
                 byte[] sessionHash = new byte[8];
@@ -1070,11 +1072,15 @@ public class HTTP {
              * of the NTLM Response and the NTLMv2 and LMv2 Hashes.
              */
             private static byte[] ntlmHash(String password) throws UnsupportedEncodingException {
+                // FIXME
+                /*
                 byte[] unicodePassword = password.getBytes("UnicodeLittleUnmarked");
-                MD4Digest md4 = new MD4Digest();
+                MD4 md4 = new MD4();
                 md4.update(unicodePassword, 0, unicodePassword.length);
                 byte[] ret = new byte[md4.getDigestSize()];
                 return ret;
+                */
+                return null;
             }
 
             /**
@@ -1231,14 +1237,14 @@ public class HTTP {
                 byte[] content = new byte[data.length + 64];
                 System.arraycopy(ipad, 0, content, 0, 64);
                 System.arraycopy(data, 0, content, 64, data.length);
-                MD5Digest md5 = new MD5Digest();
+                MD5 md5 = new MD5();
                 md5.update(content, 0, content.length);
                 data = new byte[md5.getDigestSize()];
                 md5.doFinal(data, 0);
                 content = new byte[data.length + 64];
                 System.arraycopy(opad, 0, content, 0, 64);
                 System.arraycopy(data, 0, content, 64, data.length);
-                md5 = new MD5Digest();
+                md5 = new MD5();
                 md5.update(content, 0, content.length);
                 byte[] ret = new byte[md5.getDigestSize()];
                 md5.doFinal(ret, 0);