2002/05/05 09:16:14
[org.ibex.core.git] / src / org / xwt / HTTP.java
1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]package org.xwt;
2 package org.xwt;
3
4 import java.net.*;
5 import java.io.*;
6 import java.util.*;
7 import org.xwt.util.*;
8 import org.mozilla.javascript.*;
9
10 /**
11  *  A crude HTTP[S] connection implementation with support for proxies, since not all Java libraries
12  *  (particularly GCJ's) support proxies.
13  *
14  *  FEATURE: implement pipelining
15  */
16 public class HTTP {
17
18     /** the URL to connect to */
19     URL url = null;
20
21     /** the host to connect to */
22     String host = null;
23
24     /** the port to connect on */
25     int port = -1;
26
27     /** true if SSL (HTTPS) should be used */
28     boolean ssl = false;
29
30     /** the path (URI) to retrieve on the server */
31     String path = null;
32
33     /** the socket */
34     Socket sock = null;
35
36     /** the socket's inputstream */
37     InputStream in = null;
38
39     /** the socket's outputstream */
40     OutputStream out = null;
41
42     /** the content-type of the data being received */
43     String contentType = null;
44
45     /** the content-length of the data being recieved */
46     int contentLength = 0;
47
48     /** additional headers to be transmitted */
49     String headers = "";
50
51     /** cache for resolveAndCheckIfFirewalled() */
52     static Hashtable resolvedHosts = new Hashtable();
53
54
55     // Constructor ////////////////////////////////////////////////////////////////////////////////////////
56
57     public HTTP(String url) throws MalformedURLException, IOException { this(url, false); }
58     public HTTP(String url, boolean skipResolveCheck) throws MalformedURLException, IOException {
59         if (url.startsWith("https:")) { url = "http" + url.substring(5); ssl = true; }
60         if (!url.startsWith("http:")) throw new HTTPException("HTTP only supports http/https urls");
61         this.url = new URL(url);
62         port = this.url.getPort();
63         path = this.url.getFile();
64         if (port == -1) port = ssl ? 443 : 80;
65         host = this.url.getHost();
66         if (Log.on) Log.log(this, "creating HTTP object for connection to " + host + ":" + port);
67         addHeader("Host", host);                  // host header is always sent verbatim
68         if (!skipResolveCheck) host = resolveAndCheckIfFirewalled(host);   // might have to use the strict IP if behind a proxy
69
70         ProxyInfo pi = Platform.detectProxy();
71         if (sock == null && pi != null && pi.proxyAutoConfigFunction != null) sock = attemptPAC(pi.proxyAutoConfigFunction);
72         if (sock == null && pi != null && ssl && pi.httpsProxyHost != null) sock = attemptHttpProxy(pi.httpsProxyHost, pi.httpsProxyPort);
73         if (sock == null && pi != null && pi.httpProxyHost != null) sock = attemptHttpProxy(pi.httpProxyHost, pi.httpProxyPort);
74         if (sock == null && pi != null && pi.socksProxyHost != null) sock = attemptSocksProxy(pi.socksProxyHost, pi.socksProxyPort);
75         if (sock == null) sock = attemptDirect();
76         if (sock == null) throw new HTTPException("all socket creation attempts have failed");
77         sock.setTcpNoDelay(true);
78     }
79
80
81     // Safeguarded DNS Resolver ///////////////////////////////////////////////////////////////////////////
82
83     /**
84      *  resolves the hostname and returns it as a string in the form "x.y.z.w", except for the special case "xmlrpc.xwt.org".
85      *  @throws HTTPException if the host falls within a firewalled netblock
86      */
87     private String resolveAndCheckIfFirewalled(String host) throws HTTPException {
88
89         // special case
90         if (host.equals("xmlrpc.xwt.org")) return host;
91
92         // cached
93         if (resolvedHosts.get(host) != null) return (String)resolvedHosts.get(host);
94
95         if (Log.on) Log.log(this, "  resolveAndCheckIfFirewalled: resolving " + host);
96
97         // resolve using DNS
98         try {
99             InetAddress addr = InetAddress.getByName(host);
100             byte[] quadbyte = addr.getAddress();
101             if ((quadbyte[0] == 10 ||
102                  (quadbyte[0] == 192 && quadbyte[1] == 168) ||
103                  (quadbyte[0] == 172 && (quadbyte[1] & 0xF0) == 16)) && !addr.equals(Main.originAddr))
104                 throw new HTTPException("security violation: " + host + " [" + addr.getHostAddress() + "] is in a firewalled netblock");
105             return addr.getHostAddress();
106         } catch (UnknownHostException uhe) { }
107
108         // resolve using xmlrpc.xwt.org
109         if (Platform.detectProxy() == null) throw new HTTPException("could not resolve hostname \"" + host + "\" and no proxy configured");
110         if (Log.on) Log.log(this, "  could not resolve host " + host + "; using xmlrpc.xwt.org to ensure security");
111         try {
112             Object ret = new XMLRPC("http://xmlrpc.xwt.org/RPC2/", "dns.resolve").call(new Object[] { host });
113             if (ret == null || !(ret instanceof String)) throw new Exception("    xmlrpc.xwt.org returned non-String: " + ret);
114             resolvedHosts.put(host, ret);
115             return (String)ret;
116         } catch (Throwable e) {
117             throw new HTTPException("exception while attempting to use xmlrpc.xwt.org to resolve " + host + ": " + e);
118         }
119     }
120
121
122     // Methods to attempt socket creation /////////////////////////////////////////////////////////////////
123
124     /** Attempts a direct connection */
125     public Socket attemptDirect() {
126         try {
127             if (Log.on) Log.log(this, "attempting to create unproxied socket to " + host + ":" + port + (ssl ? " [ssl]" : ""));
128             return Platform.getSocket(host, port, ssl, true);
129         } catch (IOException e) {
130             if (Log.on) Log.log(this, "exception in attemptDirect(): " + e);
131             return null;
132         }
133     }
134
135     /** Attempts to use an HTTP proxy, employing the CONNECT method if HTTPS is requested */
136     public Socket attemptHttpProxy(String proxyHost, int proxyPort) {
137         try {
138             if (Log.on) Log.log(this, "attempting to create HTTP proxied socket using proxy " + proxyHost + ":" + proxyPort);
139
140             Socket sock = Platform.getSocket(proxyHost, proxyPort, ssl, false);
141             if (!ssl) {
142                 path = "http://" + host + ":" + port + path;
143             } else {
144                 if (Log.on) Log.log(this, "attempting to create HTTP proxied socket using proxy " + proxyHost + ":" + proxyPort);
145                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
146                 BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
147                 pw.print("CONNECT " + host + ":" + port + " HTTP/1.0\r\n\r\n");
148                 String s = br.readLine();
149                 if (s.charAt(9) != '2') throw new HTTPException("proxy refused CONNECT method: \"" + s + "\"");
150                 while (br.readLine().length() > 0) { };
151                 ((TinySSL)sock).negotiate();
152             }
153             return sock;
154
155         } catch (IOException e) {
156             if (Log.on) Log.log(this, "exception in attemptHttpProxy(): " + e);
157             return null;
158         }
159     }
160
161     /**
162      *  Implements SOCKSv4 with v4a DNS extension
163      *  @see http://www.socks.nec.com/protocol/socks4.protocol
164      *  @see http://www.socks.nec.com/protocol/socks4a.protocol
165      */
166     public Socket attemptSocksProxy(String proxyHost, int proxyPort) {
167
168         // even if host is already a "x.y.z.w" string, we use this to parse it into bytes
169         InetAddress addr = null;
170         try { addr = InetAddress.getByName(host); } catch (Exception e) { }
171
172         if (Log.on) Log.log(this, "attempting to create SOCKSv4" + (addr == null ? "" : "a") +
173                             " proxied socket using proxy " + proxyHost + ":" + proxyPort);
174
175         try {
176             Socket sock = Platform.getSocket(proxyHost, proxyPort, ssl, false);
177             
178             DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
179             dos.writeByte(0x04);                         // SOCKSv4(a)
180             dos.writeByte(0x01);                         // CONNECT
181             dos.writeShort(port & 0xffff);               // port
182             if (addr == null) dos.writeInt(0x00000001);  // bogus IP
183             else dos.write(addr.getAddress());           // actual IP
184             dos.writeByte(0x00);                         // no userid
185             if (addr == null) {
186                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(dos));
187                 pw.print(host);
188                 pw.flush();
189                 dos.writeByte(0x00);                     // hostname null terminator
190             }
191             dos.flush();
192
193             DataInputStream dis = new DataInputStream(sock.getInputStream());
194             dis.readByte();                              // reply version
195             byte success = dis.readByte();               // success/fail
196             dis.skip(6);                                 // ip/port
197             
198             if ((int)(success & 0xff) == 90) {
199                 if (ssl) ((TinySSL)sock).negotiate();
200                 return sock;
201             }
202             if (Log.on) Log.log(this, "SOCKS server denied access, code " + (success & 0xff));
203             return null;
204
205         } catch (IOException e) {
206             if (Log.on) Log.log(this, "exception in attemptSocksProxy(): " + e);
207             return null;
208         }
209     }
210
211     /** executes the PAC script and dispatches a call to one of the other attempt methods based on the result */
212     public Socket attemptPAC(Function pacFunc) {
213         if (Log.on) Log.log(this, "evaluating PAC script");
214         String pac = null;
215         try {
216             Object obj = pacFunc.call(Context.enter(), ProxyInfo.proxyAutoConfigRootScope, null, new Object[] { url.toString(), url.getHost() });
217             if (Log.on) Log.log(this, "  PAC script returned \"" + obj + "\"");
218             pac = obj.toString();
219         } catch (Throwable e) {
220             if (Log.on) Log.log(this, "PAC script threw exception " + e);
221             return null;
222         }
223
224         StringTokenizer st = new StringTokenizer(pac, ";", false);
225         while (st.hasMoreTokens()) {
226             String token = st.nextToken().trim();
227             if (Log.on) Log.log(this, "  trying \"" + token + "\"...");
228             try {
229                 Socket ret = null;
230                 if (token.startsWith("DIRECT"))
231                     ret = attemptDirect();
232                 else if (token.startsWith("PROXY"))
233                     ret = attemptHttpProxy(token.substring(token.indexOf(' ') + 1, token.indexOf(':')),
234                                            Integer.parseInt(token.substring(token.indexOf(':') + 1)));
235                 else if (token.startsWith("SOCKS"))
236                     ret = attemptSocksProxy(token.substring(token.indexOf(' ') + 1, token.indexOf(':')),
237                                             Integer.parseInt(token.substring(token.indexOf(':') + 1)));
238                 if (ret != null) return ret;
239             } catch (Throwable e) {
240                 if (Log.on) Log.log(this, "attempt at \"" + token + "\" failed due to " + e + "; trying next token");
241             }
242         }
243         if (Log.on) Log.log(this, "all PAC results exhausted");
244         return null;
245     }
246
247
248     // Everything Else ////////////////////////////////////////////////////////////////////////////
249
250     /** returns the content-type of the reply */
251     public String getContentType() throws IOException {
252         getInputStream();
253         return contentType;
254     }
255
256     /** returns the content-length of the reply */
257     public int getContentLength() throws IOException {
258         getInputStream();
259         return contentLength;
260     }
261
262     /** adds a header to the outbound transmission */
263     public void addHeader(String header, String value) throws HTTPException {
264         if (in != null) throw new HTTPException("attempt to add header after connection has been made");
265         headers += header + ": " + value + "\r\n";
266     }
267
268     public OutputStream getOutputStream(int contentLength, String contentType) throws IOException {
269         if (out != null) return out;
270         if (in != null) throw new HTTPException("attempt to getOutputStream() after getInputStream()");
271         out = sock.getOutputStream();
272         PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
273         pw.print("POST " + path + " HTTP/1.0\r\n");
274         pw.print("Host: " + host + "\r\n");
275         pw.print("User-Agent: XWT\r\n");
276         pw.print("Content-length: " + contentLength + "\r\n");
277         pw.print(headers);
278         if (contentType != null) pw.print("Content-Type: " + contentType + "\r\n");
279         pw.print("\r\n");
280         pw.flush();
281         return out;
282     }
283
284     public InputStream getInputStream() throws IOException {
285         if (in != null) return in;
286         if (out != null) {
287             out.flush();
288         } else {
289             PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
290             pw.print("GET " + path + " HTTP/1.0\r\n");
291             pw.print("Host: " + host + "\r\n");
292             pw.print("User-Agent: XWT\r\n");
293             pw.print(headers);
294             pw.print("\r\n");
295             pw.flush();
296         }
297
298         in = new BufferedInputStream(sock.getInputStream());
299
300         // we can't use a BufferedReader directly on the input stream,
301         // since it will buffer beyond the end of the headers
302         byte[] buf = new byte[4096];
303         int buflen = 0;
304         while(true) {
305             int read = in.read();
306             if (read == -1) throw new HTTPException("stream closed while reading headers");
307             buf[buflen++] = (byte)read;
308             if (buflen >= 4 && buf[buflen - 4] == '\r' && buf[buflen - 3] == '\n' && buf[buflen - 2] == '\r' && buf[buflen - 1] == '\n') break;
309             if (buflen == buf.length) {
310                 byte[] newbuf = new byte[buf.length * 2];
311                 System.arraycopy(buf, 0, newbuf, 0, buflen);
312                 buf = newbuf;
313             }
314         }
315         
316         BufferedReader headerReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf, 0, buflen)));
317         String s = headerReader.readLine();
318         if (!s.startsWith("HTTP/")) throw new HTTPException("Expected reply to start with \"HTTP/\"");
319         String reply = s.substring(s.indexOf(' ') + 1);
320         if (!reply.startsWith("2")) throw new HTTPException("HTTP Error: " + reply);
321         while((s = headerReader.readLine()) != null) {
322             if (s.length() > 15 && s.substring(0, 15).equalsIgnoreCase("content-length: "))
323                 contentLength = Integer.parseInt(s.substring(15));
324         }
325
326         return in;
327     }
328
329
330
331     // HTTPException ///////////////////////////////////////////////////////////////////////////////////
332
333     static class HTTPException extends IOException {
334         public HTTPException(String s) { super(s); }
335     }
336
337
338     // ProxyInfo ///////////////////////////////////////////////////////////////////////////////////
339
340     public static class ProxyInfo {
341
342         public ProxyInfo() { }
343
344         /** the HTTP Proxy host to use */
345         public String httpProxyHost = null;
346
347         /** the HTTP Proxy port to use */
348         public int httpProxyPort = -1;
349
350         /** if a seperate proxy should be used for HTTPS, this is the hostname; otherwise, httpProxyHost is used */
351         public String httpsProxyHost = null;
352
353         /** if a seperate proxy should be used for HTTPS, this is the port */
354         public int httpsProxyPort = -1;
355
356         /** the SOCKS Proxy Host to use */
357         public String socksProxyHost = null;
358
359         /** the SOCKS Proxy Port to use */
360         public int socksProxyPort = -1;
361
362         /** hosts to be excluded from proxy use; wildcards permitted */
363         public String[] excluded = null;
364
365         /** the PAC script */
366         public Function proxyAutoConfigFunction = null;
367
368         public static ProxyInfo detectProxyViaManual() {
369             try {
370                 // continue iff one of the two resolves
371                 try { InetAddress.getByName("xwt-proxy-httpHost"); }
372                 catch (UnknownHostException e) { InetAddress.getByName("xwt-proxy-socksHost"); }
373
374                 if (Log.on) Log.log(Platform.class, "using xwt-proxy-* configuration");
375                 ProxyInfo ret = new ProxyInfo();
376                 try {
377                     ret.httpProxyHost = InetAddress.getByName("xwt-proxy-httpHost").getHostAddress();
378                     byte[] quadbyte = InetAddress.getByName("xwt-proxy-httpPort").getAddress();
379                     ret.httpProxyPort = ((quadbyte[1] & 0xff) * 10000) + ((quadbyte[2] & 0xff) * 100) + (quadbyte[3] & 0xff);
380                 } catch (UnknownHostException e) { }
381                 try {
382                     ret.httpsProxyHost = InetAddress.getByName("xwt-proxy-httpsHost").getHostAddress();
383                     byte[] quadbyte = InetAddress.getByName("xwt-proxy-httpsPort").getAddress();
384                     ret.httpsProxyPort = ((quadbyte[1] & 0xff) * 10000) + ((quadbyte[2] & 0xff) * 100) + (quadbyte[3] & 0xff);
385                 } catch (UnknownHostException e) { }
386                 try {
387                     ret.socksProxyHost = InetAddress.getByName("xwt-proxy-socksHost").getHostAddress();
388                     byte[] quadbyte = InetAddress.getByName("xwt-proxy-socksPort").getAddress();
389                     ret.socksProxyPort = ((quadbyte[1] & 0xff) * 10000) + ((quadbyte[2] & 0xff) * 100) + (quadbyte[3] & 0xff);
390                 } catch (UnknownHostException e) { }
391                 return ret;
392             } catch (UnknownHostException e) {
393                 if (Log.on) Log.log(Platform.class, "xwt-proxy-* detection failed due to: " + e);
394                 return null;
395             }
396         }
397
398         // FIXME: search up from default domain
399         public static ProxyInfo detectProxyViaWPAD() {
400             try {
401                 InetAddress wpad = InetAddress.getByName("wpad");
402                 if (Log.on) Log.log(Platform.class, "using Web Proxy Auto Detection to detect proxy settings");
403                 ProxyInfo ret = new ProxyInfo();
404                 ret.proxyAutoConfigFunction = getProxyAutoConfigFunction("http://wpad/wpad.dat");
405                 if (ret.proxyAutoConfigFunction != null) return ret;
406             } catch (UnknownHostException e) {
407                 if (Log.on) Log.log(HTTP.class, "couldn't find WPAD server: " + e);
408             }
409             return null;
410         }
411
412         public static Scriptable proxyAutoConfigRootScope = new ProxyAutoConfigRootScope();
413
414         public static Function getProxyAutoConfigFunction(String url) {
415             try { 
416                 Context cx = Context.enter();
417                 cx.setOptimizationLevel(-1);
418                 BufferedReader br = new BufferedReader(new InputStreamReader(new HTTP(url, true).getInputStream()));
419                 String s = null;
420                 String script = "";
421                 while((s = br.readLine()) != null) script += s + "\n";
422                 if (Log.on) Log.log(HTTP.ProxyInfo.class, "successfully retrieved WPAD PAC:");
423                 if (Log.on) Log.log(HTTP.ProxyInfo.class, script);
424                 Script scr = cx.compileReader(proxyAutoConfigRootScope, new StringReader(script), "PAC script at " + url, 0, null);
425                 scr.exec(cx, proxyAutoConfigRootScope);
426                 return (Function)proxyAutoConfigRootScope.get("FindProxyForURL", null);
427             } catch (Exception e) {
428                 if (Log.on) {
429                     Log.log(Platform.class, "WPAD detection failed due to:");
430                     if (e instanceof EcmaError) Log.log(HTTP.class, ((EcmaError)e).getMessage() + " at " +
431                                                         ((EcmaError)e).getSourceName() + ":" + ((EcmaError)e).getLineNumber());
432                     else Log.log(Platform.class, e);
433                 }
434                 return null;
435             }
436         }
437
438         public static class ProxyAutoConfigRootScope extends ScriptableObject {
439
440             public String getClassName() { return "ProxyAutoConfigRootScope"; }
441             ProxyAutoConfigRootScope() { Context.enter().initStandardObjects(this); }
442
443             public Object get(String name, Scriptable start) {
444                 if (name.equals("isPlainHostName")) return isPlainHostName;
445                 else if (name.equals("dnsDomainIs")) return dnsDomainIs;
446                 else if (name.equals("localHostOrDomainIs")) return localHostOrDomainIs;
447                 else if (name.equals("isResolvable")) return isResolvable;
448                 else if (name.equals("dnsResolve")) return dnsResolve;
449                 else if (name.equals("myIpAddress")) return myIpAddress;
450                 else if (name.equals("dnsDomainLevels")) return dnsDomainLevels;
451                 else if (name.equals("shExpMatch")) return shExpMatch;
452                 else if (name.equals("weekdayRange")) return weekdayRange;
453                 else if (name.equals("dateRange")) return dateRange;
454                 else if (name.equals("timeRange")) return timeRange;
455                 else if (name.equals("ProxyConfig")) return ProxyConfig;
456                 else return super.get(name, start);
457             }
458
459             private static final JSObject proxyConfigBindings = new JSObject();
460             private static final JSObject ProxyConfig = new JSObject() {
461                     public Object get(String name, Scriptable start) {
462                         if (name.equals("bindings")) return proxyConfigBindings;
463                         return null;
464                     }
465                 };
466             
467             private static abstract class JSFunction extends JSObject implements Function {
468                 JSFunction() { setSeal(true); }
469                 public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
470             }
471
472             private static final JSFunction isPlainHostName = new JSFunction() {
473                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
474                         return (args[0].toString().indexOf('.') == -1) ? Boolean.TRUE : Boolean.FALSE;
475                     }
476                 };
477
478             private static final JSFunction dnsDomainIs = new JSFunction() {
479                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
480                         return (args[0].toString().endsWith(args[1].toString())) ? Boolean.TRUE : Boolean.FALSE;
481                     }
482                 };
483             
484             private static final JSFunction localHostOrDomainIs = new JSFunction() {
485                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
486                         return (args[0].toString().equals(args[1].toString()) || 
487                                 (args[0].toString().indexOf('.') == -1 && args[1].toString().startsWith(args[0].toString()))) ?
488                             Boolean.TRUE : Boolean.FALSE;
489                     }
490                 };
491             
492             private static final JSFunction isResolvable = new JSFunction() {
493                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
494                         try {
495                             return (InetAddress.getByName(args[0].toString()) != null) ? Boolean.TRUE : Boolean.FALSE;
496                         } catch (UnknownHostException e) {
497                             return Boolean.FALSE;
498                         }
499                     }
500                 };
501
502             private static final JSFunction isInNet = new JSFunction() {
503                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
504                         // FIXME
505                         return null;
506                     }
507                 };
508
509             private static final JSFunction dnsResolve = new JSFunction() {
510                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
511                         try {
512                             return InetAddress.getByName(args[0].toString()).getHostAddress();
513                         } catch (UnknownHostException e) {
514                             return null;
515                         }
516                     }
517                 };
518
519             private static final JSFunction myIpAddress = new JSFunction() {
520                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
521                         try {
522                             return InetAddress.getLocalHost().getHostAddress();
523                         } catch (UnknownHostException e) {
524                             if (Log.on) Log.log(this, "strange... host does not know its own address");
525                             return null;
526                         }
527                     }
528                 };
529
530             private static final JSFunction dnsDomainLevels = new JSFunction() {
531                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
532                         String s = args[0].toString();
533                         int i = 0;
534                         while((i = s.indexOf('.', i)) != -1) i++;
535                         return new Integer(i);
536                     }
537                 };
538         
539             // FIXME: test this!
540             private static boolean match(String[] arr, String s, int index) {
541                 if (index == arr.length) return true;
542                 for(int i=0; i<s.length(); i++) {
543                     String s2 = s.substring(i);
544                     if (s2.startsWith(arr[index]) && match(arr, s.substring(arr[index].length()), index + 1)) return true;
545                 }
546                 return false;
547             }
548
549             private static final JSFunction shExpMatch = new JSFunction() {
550                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
551                         StringTokenizer st = new StringTokenizer(args[1].toString(), "*", false);
552                         String[] arr = new String[st.countTokens()];
553                         String s = args[0].toString();
554                         for (int i=0; st.hasMoreTokens(); i++) arr[i] = st.nextToken();
555                         return match(arr, s, 0) ? Boolean.TRUE : Boolean.FALSE;
556                     }
557                 };
558
559             private static final JSFunction weekdayRange = new JSFunction() {
560                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
561                         throw new JavaScriptException("XWT does not support weekdayRange() in PAC scripts");
562                         /*
563                         TimeZone tz = (args.length < 3 || args[2] == null || !args[2].equals("GMT")) ? TimeZone.getTimeZone("UTC") : TimeZone.getDefault();
564                         Calendar c = new Calendar();
565                         c.setTimeZone(tz);
566                         c.setTime(new Date());
567                         Date d = c.getTime();
568                         if (args.length == 1) return 
569                         */
570                     }
571                 };
572
573             private static final JSFunction dateRange = new JSFunction() {
574                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
575                         throw new JavaScriptException("XWT does not support dateRange() in PAC scripts");
576                     }
577                 };
578
579             private static final JSFunction timeRange = new JSFunction() {
580                     public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
581                         throw new JavaScriptException("XWT does not support timeRange() in PAC scripts");
582                     }
583                 };
584
585         }
586
587     }
588
589 }