1 // Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]package org.xwt;
7 import org.mozilla.javascript.*;
10 * A crude HTTP[S] connection implementation with support for proxies, since not all Java libraries
11 * (particularly GCJ's) support proxies.
13 * FEATURE: implement pipelining
17 /** the URL to connect to */
20 /** the host to connect to */
23 /** the port to connect on */
26 /** true if SSL (HTTPS) should be used */
29 /** the path (URI) to retrieve on the server */
35 /** the socket's inputstream */
36 InputStream in = null;
38 /** the socket's outputstream */
39 OutputStream out = null;
41 /** the content-type of the data being received */
42 String contentType = null;
44 /** the content-length of the data being recieved */
45 int contentLength = 0;
47 /** additional headers to be transmitted */
50 /** cache for resolveAndCheckIfFirewalled() */
51 static Hashtable resolvedHosts = new Hashtable();
54 // Constructor ////////////////////////////////////////////////////////////////////////////////////////
56 public HTTP(String url) throws MalformedURLException, IOException { this(url, false); }
57 public HTTP(String url, boolean skipResolveCheck) throws MalformedURLException, IOException {
58 if (url.startsWith("https:")) { url = "http" + url.substring(5); ssl = true; }
59 if (!url.startsWith("http:")) throw new HTTPException("HTTP only supports http/https urls");
60 this.url = new URL(url);
61 port = this.url.getPort();
62 path = this.url.getFile();
63 if (port == -1) port = ssl ? 443 : 80;
64 host = this.url.getHost();
65 if (Log.on) Log.log(this, "creating HTTP object for connection to " + host + ":" + port);
66 addHeader("Host", host); // host header is always sent verbatim
67 if (!skipResolveCheck) host = resolveAndCheckIfFirewalled(host); // might have to use the strict IP if behind a proxy
69 ProxyInfo pi = Platform.detectProxy();
70 if (sock == null && pi != null && pi.proxyAutoConfigFunction != null) sock = attemptPAC(pi.proxyAutoConfigFunction);
71 if (sock == null && pi != null && ssl && pi.httpsProxyHost != null) sock = attemptHttpProxy(pi.httpsProxyHost, pi.httpsProxyPort);
72 if (sock == null && pi != null && pi.httpProxyHost != null) sock = attemptHttpProxy(pi.httpProxyHost, pi.httpProxyPort);
73 if (sock == null && pi != null && pi.socksProxyHost != null) sock = attemptSocksProxy(pi.socksProxyHost, pi.socksProxyPort);
74 if (sock == null) sock = attemptDirect();
75 if (sock == null) throw new HTTPException("all socket creation attempts have failed");
76 sock.setTcpNoDelay(true);
80 // Safeguarded DNS Resolver ///////////////////////////////////////////////////////////////////////////
83 * resolves the hostname and returns it as a string in the form "x.y.z.w", except for the special case "xmlrpc.xwt.org".
84 * @throws HTTPException if the host falls within a firewalled netblock
86 private String resolveAndCheckIfFirewalled(String host) throws HTTPException {
89 if (host.equals("xmlrpc.xwt.org")) return host;
92 if (resolvedHosts.get(host) != null) return (String)resolvedHosts.get(host);
94 if (Log.on) Log.log(this, " resolveAndCheckIfFirewalled: resolving " + host);
98 InetAddress addr = InetAddress.getByName(host);
99 byte[] quadbyte = addr.getAddress();
100 if ((quadbyte[0] == 10 ||
101 (quadbyte[0] == 192 && quadbyte[1] == 168) ||
102 (quadbyte[0] == 172 && (quadbyte[1] & 0xF0) == 16)) && !addr.equals(Main.originAddr))
103 throw new HTTPException("security violation: " + host + " [" + addr.getHostAddress() + "] is in a firewalled netblock");
104 return addr.getHostAddress();
105 } catch (UnknownHostException uhe) { }
107 // resolve using xmlrpc.xwt.org
108 if (Platform.detectProxy() == null) throw new HTTPException("could not resolve hostname \"" + host + "\" and no proxy configured");
109 if (Log.on) Log.log(this, " could not resolve host " + host + "; using xmlrpc.xwt.org to ensure security");
111 Object ret = new XMLRPC("http://xmlrpc.xwt.org/RPC2/", "dns.resolve").call(new Object[] { host });
112 if (ret == null || !(ret instanceof String)) throw new Exception(" xmlrpc.xwt.org returned non-String: " + ret);
113 resolvedHosts.put(host, ret);
115 } catch (Throwable e) {
116 throw new HTTPException("exception while attempting to use xmlrpc.xwt.org to resolve " + host + ": " + e);
121 // Methods to attempt socket creation /////////////////////////////////////////////////////////////////
123 /** Attempts a direct connection */
124 public Socket attemptDirect() {
126 if (Log.on) Log.log(this, "attempting to create unproxied socket to " + host + ":" + port + (ssl ? " [ssl]" : ""));
127 return Platform.getSocket(host, port, ssl, true);
128 } catch (IOException e) {
129 if (Log.on) Log.log(this, "exception in attemptDirect(): " + e);
134 /** Attempts to use an HTTP proxy, employing the CONNECT method if HTTPS is requested */
135 public Socket attemptHttpProxy(String proxyHost, int proxyPort) {
137 if (Log.on) Log.log(this, "attempting to create HTTP proxied socket using proxy " + proxyHost + ":" + proxyPort);
139 Socket sock = Platform.getSocket(proxyHost, proxyPort, ssl, false);
141 path = "http://" + host + ":" + port + path;
143 if (Log.on) Log.log(this, "attempting to create HTTP proxied socket using proxy " + proxyHost + ":" + proxyPort);
144 PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
145 BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
146 pw.print("CONNECT " + host + ":" + port + " HTTP/1.0\r\n\r\n");
147 String s = br.readLine();
148 if (s.charAt(9) != '2') throw new HTTPException("proxy refused CONNECT method: \"" + s + "\"");
149 while (br.readLine().length() > 0) { };
150 ((TinySSL)sock).negotiate();
154 } catch (IOException e) {
155 if (Log.on) Log.log(this, "exception in attemptHttpProxy(): " + e);
161 * Implements SOCKSv4 with v4a DNS extension
162 * @see http://www.socks.nec.com/protocol/socks4.protocol
163 * @see http://www.socks.nec.com/protocol/socks4a.protocol
165 public Socket attemptSocksProxy(String proxyHost, int proxyPort) {
167 // even if host is already a "x.y.z.w" string, we use this to parse it into bytes
168 InetAddress addr = null;
169 try { addr = InetAddress.getByName(host); } catch (Exception e) { }
171 if (Log.on) Log.log(this, "attempting to create SOCKSv4" + (addr == null ? "" : "a") +
172 " proxied socket using proxy " + proxyHost + ":" + proxyPort);
175 Socket sock = Platform.getSocket(proxyHost, proxyPort, ssl, false);
177 DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
178 dos.writeByte(0x04); // SOCKSv4(a)
179 dos.writeByte(0x01); // CONNECT
180 dos.writeShort(port & 0xffff); // port
181 if (addr == null) dos.writeInt(0x00000001); // bogus IP
182 else dos.write(addr.getAddress()); // actual IP
183 dos.writeByte(0x00); // no userid
185 PrintWriter pw = new PrintWriter(new OutputStreamWriter(dos));
188 dos.writeByte(0x00); // hostname null terminator
192 DataInputStream dis = new DataInputStream(sock.getInputStream());
193 dis.readByte(); // reply version
194 byte success = dis.readByte(); // success/fail
195 dis.skip(6); // ip/port
197 if ((int)(success & 0xff) == 90) {
198 if (ssl) ((TinySSL)sock).negotiate();
201 if (Log.on) Log.log(this, "SOCKS server denied access, code " + (success & 0xff));
204 } catch (IOException e) {
205 if (Log.on) Log.log(this, "exception in attemptSocksProxy(): " + e);
210 /** executes the PAC script and dispatches a call to one of the other attempt methods based on the result */
211 public Socket attemptPAC(Function pacFunc) {
212 if (Log.on) Log.log(this, "evaluating PAC script");
215 Object obj = pacFunc.call(Context.enter(), ProxyInfo.proxyAutoConfigRootScope, null, new Object[] { url.toString(), url.getHost() });
216 if (Log.on) Log.log(this, " PAC script returned \"" + obj + "\"");
217 pac = obj.toString();
218 } catch (Throwable e) {
219 if (Log.on) Log.log(this, "PAC script threw exception " + e);
223 StringTokenizer st = new StringTokenizer(pac, ";", false);
224 while (st.hasMoreTokens()) {
225 String token = st.nextToken().trim();
226 if (Log.on) Log.log(this, " trying \"" + token + "\"...");
229 if (token.startsWith("DIRECT"))
230 ret = attemptDirect();
231 else if (token.startsWith("PROXY"))
232 ret = attemptHttpProxy(token.substring(token.indexOf(' ') + 1, token.indexOf(':')),
233 Integer.parseInt(token.substring(token.indexOf(':') + 1)));
234 else if (token.startsWith("SOCKS"))
235 ret = attemptSocksProxy(token.substring(token.indexOf(' ') + 1, token.indexOf(':')),
236 Integer.parseInt(token.substring(token.indexOf(':') + 1)));
237 if (ret != null) return ret;
238 } catch (Throwable e) {
239 if (Log.on) Log.log(this, "attempt at \"" + token + "\" failed due to " + e + "; trying next token");
242 if (Log.on) Log.log(this, "all PAC results exhausted");
247 // Everything Else ////////////////////////////////////////////////////////////////////////////
249 /** returns the content-type of the reply */
250 public String getContentType() throws IOException {
255 /** returns the content-length of the reply */
256 public int getContentLength() throws IOException {
258 return contentLength;
261 /** adds a header to the outbound transmission */
262 public void addHeader(String header, String value) throws HTTPException {
263 if (in != null) throw new HTTPException("attempt to add header after connection has been made");
264 headers += header + ": " + value + "\r\n";
267 public OutputStream getOutputStream(int contentLength, String contentType) throws IOException {
268 if (out != null) return out;
269 if (in != null) throw new HTTPException("attempt to getOutputStream() after getInputStream()");
270 out = sock.getOutputStream();
271 PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
272 pw.print("POST " + path + " HTTP/1.0\r\n");
273 pw.print("Host: " + host + "\r\n");
274 pw.print("User-Agent: XWT\r\n");
275 pw.print("Content-length: " + contentLength + "\r\n");
277 if (contentType != null) pw.print("Content-Type: " + contentType + "\r\n");
283 public InputStream getInputStream() throws IOException {
284 if (in != null) return in;
288 PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
289 pw.print("GET " + path + " HTTP/1.0\r\n");
290 pw.print("Host: " + host + "\r\n");
291 pw.print("User-Agent: XWT\r\n");
297 in = new BufferedInputStream(sock.getInputStream());
299 // we can't use a BufferedReader directly on the input stream,
300 // since it will buffer beyond the end of the headers
301 byte[] buf = new byte[4096];
304 int read = in.read();
305 if (read == -1) throw new HTTPException("stream closed while reading headers");
306 buf[buflen++] = (byte)read;
307 if (buflen >= 4 && buf[buflen - 4] == '\r' && buf[buflen - 3] == '\n' && buf[buflen - 2] == '\r' && buf[buflen - 1] == '\n') break;
308 if (buflen == buf.length) {
309 byte[] newbuf = new byte[buf.length * 2];
310 System.arraycopy(buf, 0, newbuf, 0, buflen);
315 BufferedReader headerReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf, 0, buflen)));
316 String s = headerReader.readLine();
317 if (!s.startsWith("HTTP/")) throw new HTTPException("Expected reply to start with \"HTTP/\"");
318 String reply = s.substring(s.indexOf(' ') + 1);
319 if (!reply.startsWith("2")) throw new HTTPException("HTTP Error: " + reply);
320 while((s = headerReader.readLine()) != null) {
321 if (s.length() > 15 && s.substring(0, 15).equalsIgnoreCase("content-length: "))
322 contentLength = Integer.parseInt(s.substring(15));
330 // HTTPException ///////////////////////////////////////////////////////////////////////////////////
332 static class HTTPException extends IOException {
333 public HTTPException(String s) { super(s); }
337 // ProxyInfo ///////////////////////////////////////////////////////////////////////////////////
339 public static class ProxyInfo {
341 public ProxyInfo() { }
343 /** the HTTP Proxy host to use */
344 public String httpProxyHost = null;
346 /** the HTTP Proxy port to use */
347 public int httpProxyPort = -1;
349 /** if a seperate proxy should be used for HTTPS, this is the hostname; otherwise, httpProxyHost is used */
350 public String httpsProxyHost = null;
352 /** if a seperate proxy should be used for HTTPS, this is the port */
353 public int httpsProxyPort = -1;
355 /** the SOCKS Proxy Host to use */
356 public String socksProxyHost = null;
358 /** the SOCKS Proxy Port to use */
359 public int socksProxyPort = -1;
361 /** hosts to be excluded from proxy use; wildcards permitted */
362 public String[] excluded = null;
364 /** the PAC script */
365 public Function proxyAutoConfigFunction = null;
367 public static ProxyInfo detectProxyViaManual() {
369 // continue iff one of the two resolves
370 try { InetAddress.getByName("xwt-proxy-httpHost"); }
371 catch (UnknownHostException e) { InetAddress.getByName("xwt-proxy-socksHost"); }
373 if (Log.on) Log.log(Platform.class, "using xwt-proxy-* configuration");
374 ProxyInfo ret = new ProxyInfo();
376 ret.httpProxyHost = InetAddress.getByName("xwt-proxy-httpHost").getHostAddress();
377 byte[] quadbyte = InetAddress.getByName("xwt-proxy-httpPort").getAddress();
378 ret.httpProxyPort = ((quadbyte[1] & 0xff) * 10000) + ((quadbyte[2] & 0xff) * 100) + (quadbyte[3] & 0xff);
379 } catch (UnknownHostException e) { }
381 ret.httpsProxyHost = InetAddress.getByName("xwt-proxy-httpsHost").getHostAddress();
382 byte[] quadbyte = InetAddress.getByName("xwt-proxy-httpsPort").getAddress();
383 ret.httpsProxyPort = ((quadbyte[1] & 0xff) * 10000) + ((quadbyte[2] & 0xff) * 100) + (quadbyte[3] & 0xff);
384 } catch (UnknownHostException e) { }
386 ret.socksProxyHost = InetAddress.getByName("xwt-proxy-socksHost").getHostAddress();
387 byte[] quadbyte = InetAddress.getByName("xwt-proxy-socksPort").getAddress();
388 ret.socksProxyPort = ((quadbyte[1] & 0xff) * 10000) + ((quadbyte[2] & 0xff) * 100) + (quadbyte[3] & 0xff);
389 } catch (UnknownHostException e) { }
391 } catch (UnknownHostException e) {
392 if (Log.on) Log.log(Platform.class, "xwt-proxy-* detection failed due to: " + e);
397 // FIXME: search up from default domain
398 public static ProxyInfo detectProxyViaWPAD() {
400 InetAddress wpad = InetAddress.getByName("wpad");
401 if (Log.on) Log.log(Platform.class, "using Web Proxy Auto Detection to detect proxy settings");
402 ProxyInfo ret = new ProxyInfo();
403 ret.proxyAutoConfigFunction = getProxyAutoConfigFunction("http://wpad/wpad.dat");
404 if (ret.proxyAutoConfigFunction != null) return ret;
405 } catch (UnknownHostException e) {
406 if (Log.on) Log.log(HTTP.class, "couldn't find WPAD server: " + e);
411 public static Scriptable proxyAutoConfigRootScope = new ProxyAutoConfigRootScope();
413 public static Function getProxyAutoConfigFunction(String url) {
415 Context cx = Context.enter();
416 cx.setOptimizationLevel(-1);
417 BufferedReader br = new BufferedReader(new InputStreamReader(new HTTP(url, true).getInputStream()));
420 while((s = br.readLine()) != null) script += s + "\n";
421 if (Log.on) Log.log(HTTP.ProxyInfo.class, "successfully retrieved WPAD PAC:");
422 if (Log.on) Log.log(HTTP.ProxyInfo.class, script);
423 Script scr = cx.compileReader(proxyAutoConfigRootScope, new StringReader(script), "PAC script at " + url, 0, null);
424 scr.exec(cx, proxyAutoConfigRootScope);
425 return (Function)proxyAutoConfigRootScope.get("FindProxyForURL", null);
426 } catch (Exception e) {
428 Log.log(Platform.class, "WPAD detection failed due to:");
429 if (e instanceof EcmaError) Log.log(HTTP.class, ((EcmaError)e).getMessage() + " at " +
430 ((EcmaError)e).getSourceName() + ":" + ((EcmaError)e).getLineNumber());
431 else Log.log(Platform.class, e);
437 public static class ProxyAutoConfigRootScope extends ScriptableObject {
439 public String getClassName() { return "ProxyAutoConfigRootScope"; }
440 ProxyAutoConfigRootScope() { Context.enter().initStandardObjects(this); }
442 public Object get(String name, Scriptable start) {
443 if (name.equals("isPlainHostName")) return isPlainHostName;
444 else if (name.equals("dnsDomainIs")) return dnsDomainIs;
445 else if (name.equals("localHostOrDomainIs")) return localHostOrDomainIs;
446 else if (name.equals("isResolvable")) return isResolvable;
447 else if (name.equals("dnsResolve")) return dnsResolve;
448 else if (name.equals("myIpAddress")) return myIpAddress;
449 else if (name.equals("dnsDomainLevels")) return dnsDomainLevels;
450 else if (name.equals("shExpMatch")) return shExpMatch;
451 else if (name.equals("weekdayRange")) return weekdayRange;
452 else if (name.equals("dateRange")) return dateRange;
453 else if (name.equals("timeRange")) return timeRange;
454 else if (name.equals("ProxyConfig")) return ProxyConfig;
455 else return super.get(name, start);
458 private static final JSObject proxyConfigBindings = new JSObject();
459 private static final JSObject ProxyConfig = new JSObject() {
460 public Object get(String name, Scriptable start) {
461 if (name.equals("bindings")) return proxyConfigBindings;
466 private static abstract class JSFunction extends JSObject implements Function {
467 JSFunction() { setSeal(true); }
468 public Scriptable construct(Context cx, Scriptable scope, java.lang.Object[] args) { return null; }
471 private static final JSFunction isPlainHostName = new JSFunction() {
472 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
473 return (args[0].toString().indexOf('.') == -1) ? Boolean.TRUE : Boolean.FALSE;
477 private static final JSFunction dnsDomainIs = new JSFunction() {
478 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
479 return (args[0].toString().endsWith(args[1].toString())) ? Boolean.TRUE : Boolean.FALSE;
483 private static final JSFunction localHostOrDomainIs = new JSFunction() {
484 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
485 return (args[0].toString().equals(args[1].toString()) ||
486 (args[0].toString().indexOf('.') == -1 && args[1].toString().startsWith(args[0].toString()))) ?
487 Boolean.TRUE : Boolean.FALSE;
491 private static final JSFunction isResolvable = new JSFunction() {
492 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
494 return (InetAddress.getByName(args[0].toString()) != null) ? Boolean.TRUE : Boolean.FALSE;
495 } catch (UnknownHostException e) {
496 return Boolean.FALSE;
501 private static final JSFunction isInNet = new JSFunction() {
502 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
508 private static final JSFunction dnsResolve = new JSFunction() {
509 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
511 return InetAddress.getByName(args[0].toString()).getHostAddress();
512 } catch (UnknownHostException e) {
518 private static final JSFunction myIpAddress = new JSFunction() {
519 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
521 return InetAddress.getLocalHost().getHostAddress();
522 } catch (UnknownHostException e) {
523 if (Log.on) Log.log(this, "strange... host does not know its own address");
529 private static final JSFunction dnsDomainLevels = new JSFunction() {
530 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
531 String s = args[0].toString();
533 while((i = s.indexOf('.', i)) != -1) i++;
534 return new Integer(i);
539 private static boolean match(String[] arr, String s, int index) {
540 if (index == arr.length) return true;
541 for(int i=0; i<s.length(); i++) {
542 String s2 = s.substring(i);
543 if (s2.startsWith(arr[index]) && match(arr, s.substring(arr[index].length()), index + 1)) return true;
548 private static final JSFunction shExpMatch = new JSFunction() {
549 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
550 StringTokenizer st = new StringTokenizer(args[1].toString(), "*", false);
551 String[] arr = new String[st.countTokens()];
552 String s = args[0].toString();
553 for (int i=0; st.hasMoreTokens(); i++) arr[i] = st.nextToken();
554 return match(arr, s, 0) ? Boolean.TRUE : Boolean.FALSE;
558 private static final JSFunction weekdayRange = new JSFunction() {
559 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
560 throw new JavaScriptException("XWT does not support weekdayRange() in PAC scripts");
562 TimeZone tz = (args.length < 3 || args[2] == null || !args[2].equals("GMT")) ? TimeZone.getTimeZone("UTC") : TimeZone.getDefault();
563 Calendar c = new Calendar();
565 c.setTime(new Date());
566 Date d = c.getTime();
567 if (args.length == 1) return
572 private static final JSFunction dateRange = new JSFunction() {
573 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
574 throw new JavaScriptException("XWT does not support dateRange() in PAC scripts");
578 private static final JSFunction timeRange = new JSFunction() {
579 public Object call(Context cx, Scriptable thisObj, Scriptable ctorObj, Object[] args) throws JavaScriptException {
580 throw new JavaScriptException("XWT does not support timeRange() in PAC scripts");