*/
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);
}
}
+
// Methods to make outbound XML-RPC request ///////////////////////////////////////////////////
/** Appends the XML-RPC representation of <code>o</code> to <code>sb</code> */
}
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");
- }
- }
-
- 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");
-
- 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 (Log.verbose) Log.log(this, "call to " + url + " : " + methodname);
+ 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(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();
+ System.out.println("X " + i);
if (Log.on) Log.log(this, "recv: " + ((char)i));
return i;
}
public int read(char[] c, int off, int len) throws IOException {
int ret = super.read(c, off, len);
+ System.out.println("Y " + ret);
if (ret == -1) return ret;
if (Log.on) Log.log(this, "recv: " + new String(c, off, ret));
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 {
StringBuffer content = new StringBuffer();
content.append("<?xml version=\"1.0\"?>\n");
content.append(" <methodCall>\n");
content.append(" </params>\n");
}
content.append(" </methodCall>");
+ 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);
}
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 {
/** 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 */
}
}
+
// Methods Required by Rhino ////////////////////////////////////////////////////////
public String getClassName() { return "XMLRPC"; }