X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2FXMLRPC.java;h=e2ea79bb22a4a9e98c05047d9bcd89426b35577a;hb=1296555e827bcd7606b7d82b6c1f2a875ff22e2c;hp=ca5b54f88ed38b08e02b2ac2d8dc93a11918e5e1;hpb=6242c991f365dbd67eba62ecfa5df769a83fcbc6;p=org.ibex.core.git diff --git a/src/org/xwt/XMLRPC.java b/src/org/xwt/XMLRPC.java index ca5b54f..e2ea79b 100644 --- a/src/org/xwt/XMLRPC.java +++ b/src/org/xwt/XMLRPC.java @@ -6,6 +6,7 @@ import java.net.*; import java.util.*; import org.mozilla.javascript.*; import org.xwt.util.*; +import org.bouncycastle.util.encoders.Base64; /** * An XML-RPC client implemented as a Rhino JavaScript Host @@ -31,24 +32,12 @@ import org.xwt.util.*; */ class XMLRPC extends XML implements Function { - /** should we use SSL? */ - protected boolean ssl = false; - /** the url to connect to */ - protected URL url = null; + protected String url = null; /** the method name to invoke on the remove server */ protected String methodname = null; - /** the host portion of the url; not calculated until first call() */ - protected String host = null; - - /** the filename portion of the url; not calculated until first call() */ - protected String filename = null; - - /** the port to connect to; not calculated until the first call() */ - protected int port = -1; - /** this holds character content as we read it in -- since there is only one per instance, we don't support mixed content */ protected AccessibleCharArrayWriter content = new AccessibleCharArrayWriter(100); @@ -85,6 +74,7 @@ class XMLRPC extends XML implements Function { // Methods to Recieve and parse XML-RPC Response //////////////////////////////////////////////////// public void startElement(String name, String[] keys, Object[] vals, int line, int col) { + content.reset(); if (name.equals("fault")) fault = true; else if (name.equals("struct")) objects.setElementAt(new JSObject(false), objects.size() - 1); else if (name.equals("array")) objects.setElementAt(null, objects.size() - 1); @@ -106,7 +96,7 @@ class XMLRPC extends XML implements Function { objects.setElementAt(new Double(new String(content.getBuf(), 0, content.size())), objects.size() - 1); else if (name.equals("base64")) - objects.setElementAt(new String(content.getBuf(), 0, content.size()), objects.size() - 1); + objects.setElementAt(new ByteStream(Base64.decode(new String(content.getBuf(), 0, content.size()))), objects.size() - 1); else if (name.equals("name")) objects.addElement(new String(content.getBuf(), 0, content.size())); @@ -168,6 +158,7 @@ class XMLRPC extends XML implements Function { } } + // Methods to make outbound XML-RPC request /////////////////////////////////////////////////// /** Appends the XML-RPC representation of o to sb */ @@ -192,6 +183,30 @@ class XMLRPC extends XML implements Function { sb.append(((Boolean)o).booleanValue() ? "1" : "0"); sb.append("\n"); + } else if (o instanceof ByteStream) { + try { + sb.append(" \n"); + InputStream is = ((ByteStream)o).getInputStream(); + byte[] buf = new byte[54]; + while(true) { + int numread = is.read(buf, 0, 54); + if (numread == -1) break; + byte[] writebuf = buf; + if (numread < buf.length) { + writebuf = new byte[numread]; + System.arraycopy(buf, 0, writebuf, 0, numread); + } + sb.append(" "); + sb.append(new String(Base64.encode(writebuf))); + sb.append("\n"); + } + sb.append("\n \n"); + } catch (IOException e) { + if (Log.on) Log.log(this, "caught IOException while attempting to send a ByteStream via XML-RPC"); + if (Log.on) Log.log(this, e); + throw new JavaScriptException("caught IOException while attempting to send a ByteStream via XML-RPC"); + } + } else if (o instanceof String) { sb.append(" "); String s = (String)o; @@ -240,55 +255,42 @@ class XMLRPC extends XML implements Function { } } - private Object connect(Object[] args) throws JavaScriptException, IOException { - if (filename == null) { - filename = url.getFile(); - host = url.getHost(); - port = url.getPort(); - - InetAddress addr; - try { addr = InetAddress.getByName(host); } - catch (UnknownHostException uhe) { throw new JavaScriptException("could not resolve hostname \"" + host + "\""); } - byte[] quadbyte = addr.getAddress(); - - if (quadbyte[0] == 10 || - (quadbyte[0] == 192 && quadbyte[1] == 168) || - (quadbyte[0] == 172 && (quadbyte[1] & 0xF0) == 16) && - !addr.equals(Main.originAddr)) { - filename = null; - throw new JavaScriptException("security violation: " + host + " [" + addr.getHostAddress() + "] is in a firewalled netblock"); - } - } + // this is synchronized in case multiple threads try to make a call on the same object... in the future, change this + // behavior to use pipelining. + public synchronized Object call(Object[] args) throws JavaScriptException, IOException { + if (Log.verbose) Log.log(this, "call to " + url + " : " + methodname); - Socket s; - if (ssl) s = Platform.getSocket(host, port == -1 ? 443 : port, true); - else if (url.getProtocol().equals("http")) s = Platform.getSocket(host, port == -1 ? 80 : port, false); - else throw new JavaScriptException("only http[s] is supported"); + if (tracker == null) tracker = new Hash(); + else tracker.clear(); - s.setTcpNoDelay(true); - OutputStream os = new BufferedOutputStream(s.getOutputStream(), 4000); - InputStream is = new BufferedInputStream(new Filter(s.getInputStream())); - - PrintWriter ps; - if (!Log.verbose) ps = new PrintWriter(os); - else ps = new PrintWriter(new FilterWriter(new OutputStreamWriter(os)) { - public void write(int i) throws IOException { - super.write(i); - if (Log.on) Log.log(this, "send: " + ((char)i)); - } - public void write(String s, int start, int len) throws IOException { - super.write(s, start, len); - if (Log.on) Log.log(this, "send: " + s.substring(start, start + len)); - } - public void write(char[] c, int start, int len) throws IOException { - super.write(c, start, len); - if (Log.on) Log.log(this, "send: " + new String(c, start, len)); - } - }); + if (objects == null) objects = new Vec(); + else objects.setSize(0); + + HTTP http = new HTTP(url); + String content = send(args, http); + OutputStream os = new BufferedOutputStream(http.getOutputStream(content.length(), "text/xml"), 4000); + PrintWriter ps = !Log.verbose ? + new PrintWriter(os) : + new PrintWriter(new FilterWriter(new OutputStreamWriter(os)) { + public void write(int i) throws IOException { + super.write(i); + if (Log.on) Log.log(this, "send: " + ((char)i)); + } + public void write(String s, int start, int len) throws IOException { + super.write(s, start, len); + if (Log.on) Log.log(this, "send: " + s.substring(start, start + len)); + } + public void write(char[] c, int start, int len) throws IOException { + super.write(c, start, len); + if (Log.on) Log.log(this, "send: " + new String(c, start, len)); + } + }); + ps.print(content.toString()); + ps.flush(); - BufferedReader br; - if (!Log.verbose) br = new BufferedReader(new InputStreamReader(is)); - else br = new BufferedReader(new FilterReader(new InputStreamReader(is)) { + BufferedReader br = !Log.verbose ? + new BufferedReader(new InputStreamReader(new Filter(http.getInputStream()))) : + new BufferedReader(new FilterReader(new InputStreamReader(new Filter(http.getInputStream()))) { public int read() throws IOException { int i = super.read(); if (Log.on) Log.log(this, "recv: " + ((char)i)); @@ -301,14 +303,10 @@ class XMLRPC extends XML implements Function { return ret; } }); - - if (Log.verbose) Log.log(this, "call to " + url + " : " + methodname); - return send(args, br, ps); + return recieve(br); } - protected Object send(Object[] args, BufferedReader br, PrintWriter ps) throws JavaScriptException, IOException { - - // Construct payload + protected String send(Object[] args, HTTP http) throws JavaScriptException, IOException { StringBuffer content = new StringBuffer(); content.append("\n"); content.append(" \n"); @@ -325,21 +323,10 @@ class XMLRPC extends XML implements Function { content.append(" \n"); } content.append(" "); + return content.toString(); + } - // Header - ps.print("POST " + filename + " HTTP/1.0\r\n"); - ps.print("Host: " + host + "\r\n"); - ps.print("User-Agent: XWT (http://www.xwt.org/)\r\n"); - ps.print("Content-Type: text/xml\r\n"); - ps.print("Content-length: " + (content.length() + 1) + "\r\n"); - ps.print("\r\n"); - ps.print(content); - ps.print("\n"); - ps.flush(); - - // throw away HTTP reply headers - while(!br.readLine().equals("")) { } - + protected Object recieve(BufferedReader br) throws JavaScriptException, IOException { // parse XML reply try { parse(br); @@ -349,33 +336,16 @@ class XMLRPC extends XML implements Function { } if (fault) throw new JavaScriptException(objects.elementAt(0)); - if (objects.size() == 0) return null; return objects.elementAt(0); - } public final Object call(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) throws JavaScriptException { - if (tracker == null) tracker = new Hash(); - else tracker.clear(); - - if (objects == null) objects = new Vec(); - else objects.setSize(0); - - // put ourselves in the background - Thread thread = Thread.currentThread(); - if (!(thread instanceof ThreadMessage)) { - if (Log.on) Log.log(this, "RPC calls may only be made from background threads"); - return null; - } - ThreadMessage mythread = (ThreadMessage)thread; - mythread.setPriority(Thread.MIN_PRIORITY); - mythread.done.release(); + if (!ThreadMessage.suspendThread()) return null; try { - return connect(args); - + return call(args); } catch (IOException se) { if (Log.on) Log.log(this, se); if (Log.on) Log.log(this, " at " + cx.interpreterSourceFile + ":" + cx.interpreterLine); @@ -388,40 +358,23 @@ class XMLRPC extends XML implements Function { if (Log.on) Log.log(this, " at " + cx.interpreterSourceFile + ":" + cx.interpreterLine); } throw jse; - } finally { - // okay, let ourselves be brought to the foreground - MessageQueue.add(mythread); - mythread.setPriority(Thread.NORM_PRIORITY); - mythread.go.block(); + ThreadMessage.resumeThread(); } } /** When you get a property from an XMLRPC, it just returns another XMLRPC with the property name tacked onto methodname. */ public Object get(String name, Scriptable start) { - return new XMLRPC(url.toString(), (methodname.equals("") ? "" : methodname + ".") + name, ssl); + return new XMLRPC(url, (methodname.equals("") ? "" : methodname + ".") + name); } - public XMLRPC(String urlstr, String methodname) { this(urlstr, methodname, false); } - public XMLRPC(String urlstr, String methodname, boolean ssl) { - this.ssl = ssl; - try { - if (urlstr.startsWith("https:")) { - urlstr = "http" + urlstr.substring(5); - this.ssl = true; - } - URL url = new URL(urlstr); - if (methodname == null) methodname = ""; - this.methodname = methodname; - this.url = url; - - } catch (MalformedURLException e) { - if (Log.on) Log.log(this, e); - - } + public XMLRPC(String url, String methodname) { + this.url = url; + this.methodname = methodname; } + // Helper Classes /////////////////////////////////////////////////////////////////////////////////// /** CharArrayWriter that lets us touch its buffer */ @@ -450,6 +403,7 @@ class XMLRPC extends XML implements Function { } } + // Methods Required by Rhino //////////////////////////////////////////////////////// public String getClassName() { return "XMLRPC"; }