2003/09/29 04:05:38
[org.ibex.core.git] / src / org / xwt / HTTP.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3
4 import java.net.*;
5 import java.io.*;
6 import java.util.*;
7 import org.xwt.js.*;
8 import org.xwt.util.*;
9 import org.bouncycastle.util.encoders.Base64;
10 import org.bouncycastle.crypto.digests.*;
11
12 /**
13  *  This object encapsulates a *single* HTTP connection. Multiple requests may be pipelined over a connection (thread-safe),
14  *  although any IOException encountered in a request will invalidate all later requests.
15  */
16 public class HTTP {
17
18     /** the URL as passed to the original constructor; this is never changed */
19     final String originalUrl;
20
21     /** the URL to connect to; this is munged when the url is parsed */
22     URL url = null;
23
24     /** the host to connect to */
25     String host = null;
26
27     /** the port to connect on */
28     int port = -1;
29
30     /** true if SSL (HTTPS) should be used */
31     boolean ssl = false;
32
33     /** the path (URI) to retrieve on the server */
34     String path = null;
35
36     /** the socket */
37     Socket sock = null;
38
39     /** the socket's inputstream */
40     InputStream in = null;
41
42     /** the username and password portions of the URL */
43     String userInfo = null;
44
45     /** cache of userInfo strings, keyed on originalUrl */
46     private static Hashtable authCache = new Hashtable();
47
48     /** this is null if the current request is the first request on
49      *  this HTTP connection; otherwise it is a Semaphore which will be
50      *  released once the request ahead of us has recieved its response
51      */
52     Semaphore okToRecieve = null;
53
54     /** cache for resolveAndCheckIfFirewalled() */
55     static Hashtable resolvedHosts = new Hashtable();
56
57     /** true iff we are allowed to skip the resolve check (only allowed when we're downloading the PAC script) */
58     boolean skipResolveCheck = false;
59
60     /** true iff we're using a proxy */
61     boolean proxied = false;
62
63
64     // Public Methods ////////////////////////////////////////////////////////////////////////////////////////
65
66     public HTTP(String url) { this(url, false); }
67     public HTTP(String url, boolean skipResolveCheck) {
68         originalUrl = url;
69         this.skipResolveCheck = skipResolveCheck;
70     }
71
72     /** Performs an HTTP GET request */
73     public InputStream GET() throws IOException { return makeRequest(null, null); }
74
75     /** Performs an HTTP POST request; content is appended to the headers (so it should include a blank line to delimit the beginning of the body) */
76     public InputStream POST(String contentType, String content) throws IOException { return makeRequest(contentType, content); }
77
78     /**
79      *  This method isn't synchronized; however, only one thread can be in the inner synchronized block at a time, and the rest of
80      *  the method is protected by in-order one-at-a-time semaphore lock-steps
81      */
82     private InputStream makeRequest(String contentType, String content) throws IOException {
83
84         // Step 1: send the request and establish a semaphore to stop any requests that pipeline after us
85         Semaphore blockOn = null;
86         Semaphore releaseMe = null;
87         synchronized(this) {
88             try {
89                 connect();
90                 sendRequest(contentType, content);
91             } catch (IOException e) {
92                 sock = null;
93                 in = null;
94                 throw e;
95             }
96             blockOn = okToRecieve;
97             releaseMe = okToRecieve = new Semaphore();
98         }
99         
100         // Step 2: wait for requests ahead of us to complete, then read the reply off the stream
101         boolean doRelease = true;
102         try {
103             if (blockOn != null) blockOn.block();
104             
105             // previous call wrecked the socket connection, but we already sent our request, so we can't just retry --
106             // this could cause the server to receive the request twice, which could be bad (think of the case where the
107             // server call causes Amazon.com to ship you an item with one-click purchasing).
108             if (sock == null)
109                 throw new HTTPException("a previous pipelined call messed up the socket");
110             
111             Hashtable h = in == null ? null : parseHeaders(in);
112             if (h == null) {
113                 // sometimes the server chooses to close the stream between requests
114                 in = null; sock = null;
115                 releaseMe.release();
116                 return makeRequest(contentType, content);
117             }
118
119             String reply = h.get("STATUSLINE").toString();
120             
121             if (reply.startsWith("407") || reply.startsWith("401")) {
122                 
123                 if (reply.startsWith("407")) doProxyAuth(h, content == null ? "GET" : "POST");
124                 else doWebAuth(h, content == null ? "GET" : "POST");
125                 
126                 if (h.get("HTTP").equals("1.0") && h.get("content-length") == null) {
127                     if (Log.on) Log.log(this, "proxy returned an HTTP/1.0 reply with no content-length...");
128                     in = null; sock = null;
129                 } else {
130                     int cl = h.get("content-length") == null ? -1 : Integer.parseInt(h.get("content-length").toString());
131                     new HTTPInputStream(in, cl, releaseMe).close();
132                 }
133                 releaseMe.release();
134                 return makeRequest(contentType, content);
135                 
136             } else if (reply.startsWith("2")) {
137                 if (h.get("HTTP").equals("1.0") && h.get("content-length") == null)
138                     throw new HTTPException("XWT does not support HTTP/1.0 servers which fail to return the Content-Length header");
139                 int cl = h.get("content-length") == null ? -1 : Integer.parseInt(h.get("content-length").toString());
140                 InputStream ret = new HTTPInputStream(in, cl, releaseMe);
141                 if ("gzip".equals(h.get("content-encoding"))) ret = new java.util.zip.GZIPInputStream(ret);
142                 doRelease = false;
143                 return ret;
144                 
145             } else {
146                 throw new HTTPException("HTTP Error: " + reply);
147                 
148             }
149             
150         } catch (IOException e) { sock = null; in = null; throw e;
151         } finally { if (doRelease) releaseMe.release();
152         }
153     }
154
155
156     // Safeguarded DNS Resolver ///////////////////////////////////////////////////////////////////////////
157
158     /**
159      *  resolves the hostname and returns it as a string in the form "x.y.z.w"
160      *  @throws HTTPException if the host falls within a firewalled netblock
161      */
162     private void resolveAndCheckIfFirewalled(String host) throws HTTPException {
163
164         // cached
165         if (resolvedHosts.get(host) != null) return;
166
167         // if all scripts are trustworthy (local FS), continue
168         if (Main.originAddr == null) return;
169
170         // resolve using DNS
171         try {
172             InetAddress addr = InetAddress.getByName(host);
173             byte[] quadbyte = addr.getAddress();
174             if ((quadbyte[0] == 10 ||
175                  (quadbyte[0] == 192 && quadbyte[1] == 168) ||
176                  (quadbyte[0] == 172 && (quadbyte[1] & 0xF0) == 16)) && !addr.equals(Main.originAddr))
177                 throw new HTTPException("security violation: " + host + " [" + addr.getHostAddress() + "] is in a firewalled netblock");
178             return;
179         } catch (UnknownHostException uhe) { }
180
181         if (Platform.detectProxy() == null) throw new HTTPException("could not resolve hostname \"" + host + "\" and no proxy configured");
182         if (Log.on) Log.log(this, "  could not resolve host " + host + "; using xmlrpc.xwt.org to ensure security");
183         try {
184             JS.Array args = new JS.Array();
185             args.addElement(host);
186             Object ret = new XMLRPC("http://xmlrpc.xwt.org/RPC2/", "dns.resolve").call(args);
187             if (ret == null || !(ret instanceof String)) throw new Exception("    xmlrpc.xwt.org returned non-String: " + ret);
188             resolvedHosts.put(host, ret);
189             return;
190         } catch (Throwable e) {
191             throw new HTTPException("exception while attempting to use xmlrpc.xwt.org to resolve " + host + ": " + e);
192         }
193     }
194
195
196     // Methods to attempt socket creation /////////////////////////////////////////////////////////////////
197
198     /** Attempts a direct connection */
199     public Socket attemptDirect() {
200         try {
201             if (Log.verbose) Log.log(this, "attempting to create unproxied socket to " + host + ":" + port + (ssl ? " [ssl]" : ""));
202             return Platform.getSocket(host, port, ssl, true);
203         } catch (IOException e) {
204             if (Log.on) Log.log(this, "exception in attemptDirect(): " + e);
205             return null;
206         }
207     }
208
209     /** Attempts to use an HTTP proxy, employing the CONNECT method if HTTPS is requested */
210     public Socket attemptHttpProxy(String proxyHost, int proxyPort) {
211         try {
212             if (Log.verbose) Log.log(this, "attempting to create HTTP proxied socket using proxy " + proxyHost + ":" + proxyPort);
213
214             Socket sock = Platform.getSocket(proxyHost, proxyPort, ssl, false);
215             if (!ssl) {
216                 if (!path.startsWith("http://")) path = "http://" + host + ":" + port + path;
217             } else {
218                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
219                 BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
220                 pw.print("CONNECT " + host + ":" + port + " HTTP/1.1\r\n\r\n");
221                 pw.flush();
222                 String s = br.readLine();
223                 if (s.charAt(9) != '2') throw new HTTPException("proxy refused CONNECT method: \"" + s + "\"");
224                 while (br.readLine().length() > 0) { };
225                 ((TinySSL)sock).negotiate();
226             }
227             return sock;
228
229         } catch (IOException e) {
230             if (Log.on) Log.log(this, "exception in attemptHttpProxy(): " + e);
231             return null;
232         }
233     }
234
235     /**
236      *  Implements SOCKSv4 with v4a DNS extension
237      *  @see http://www.socks.nec.com/protocol/socks4.protocol
238      *  @see http://www.socks.nec.com/protocol/socks4a.protocol
239      */
240     public Socket attemptSocksProxy(String proxyHost, int proxyPort) {
241
242         // even if host is already a "x.y.z.w" string, we use this to parse it into bytes
243         InetAddress addr = null;
244         try { addr = InetAddress.getByName(host); } catch (Exception e) { }
245
246         if (Log.verbose) Log.log(this, "attempting to create SOCKSv4" + (addr == null ? "" : "a") +
247                                  " proxied socket using proxy " + proxyHost + ":" + proxyPort);
248
249         try {
250             Socket sock = Platform.getSocket(proxyHost, proxyPort, ssl, false);
251             
252             DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
253             dos.writeByte(0x04);                         // SOCKSv4(a)
254             dos.writeByte(0x01);                         // CONNECT
255             dos.writeShort(port & 0xffff);               // port
256             if (addr == null) dos.writeInt(0x00000001);  // bogus IP
257             else dos.write(addr.getAddress());           // actual IP
258             dos.writeByte(0x00);                         // no userid
259             if (addr == null) {
260                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(dos));
261                 pw.print(host);
262                 pw.flush();
263                 dos.writeByte(0x00);                     // hostname null terminator
264             }
265             dos.flush();
266
267             DataInputStream dis = new DataInputStream(sock.getInputStream());
268             dis.readByte();                              // reply version
269             byte success = dis.readByte();               // success/fail
270             dis.skip(6);                                 // ip/port
271             
272             if ((int)(success & 0xff) == 90) {
273                 if (ssl) ((TinySSL)sock).negotiate();
274                 return sock;
275             }
276             if (Log.on) Log.log(this, "SOCKS server denied access, code " + (success & 0xff));
277             return null;
278
279         } catch (IOException e) {
280             if (Log.on) Log.log(this, "exception in attemptSocksProxy(): " + e);
281             return null;
282         }
283     }
284
285     /** executes the PAC script and dispatches a call to one of the other attempt methods based on the result */
286     public Socket attemptPAC(org.xwt.js.JS.Callable pacFunc) {
287         if (Log.verbose) Log.log(this, "evaluating PAC script");
288         String pac = null;
289         try {
290             org.xwt.js.JS.Array args = new org.xwt.js.JS.Array();
291             args.addElement(url.toString());
292             args.addElement(url.getHost());
293             Object obj = pacFunc.call(args);
294             if (Log.verbose) Log.log(this, "  PAC script returned \"" + obj + "\"");
295             pac = obj.toString();
296         } catch (Throwable e) {
297             if (Log.on) Log.log(this, "PAC script threw exception " + e);
298             return null;
299         }
300
301         StringTokenizer st = new StringTokenizer(pac, ";", false);
302         while (st.hasMoreTokens()) {
303             String token = st.nextToken().trim();
304             if (Log.verbose) Log.log(this, "  trying \"" + token + "\"...");
305             try {
306                 Socket ret = null;
307                 if (token.startsWith("DIRECT"))
308                     ret = attemptDirect();
309                 else if (token.startsWith("PROXY"))
310                     ret = attemptHttpProxy(token.substring(token.indexOf(' ') + 1, token.indexOf(':')),
311                                            Integer.parseInt(token.substring(token.indexOf(':') + 1)));
312                 else if (token.startsWith("SOCKS"))
313                     ret = attemptSocksProxy(token.substring(token.indexOf(' ') + 1, token.indexOf(':')),
314                                             Integer.parseInt(token.substring(token.indexOf(':') + 1)));
315                 if (ret != null) return ret;
316             } catch (Throwable e) {
317                 if (Log.on) Log.log(this, "attempt at \"" + token + "\" failed due to " + e + "; trying next token");
318             }
319         }
320         if (Log.on) Log.log(this, "all PAC results exhausted");
321         return null;
322     }
323
324
325     // Everything Else ////////////////////////////////////////////////////////////////////////////
326
327     private synchronized void connect() throws IOException {
328         if (originalUrl.equals("stdio:")) {
329             in = new BufferedInputStream(System.in);
330             return;
331         }
332         if (sock != null) {
333             if (in == null) in = new BufferedInputStream(sock.getInputStream());
334             return;
335         }
336         // grab the userinfo; gcj doesn't have java.net.URL.getUserInfo()
337         String url = originalUrl;
338         userInfo = url.substring(url.indexOf("://") + 3);
339         userInfo = userInfo.indexOf('/') == -1 ? userInfo : userInfo.substring(0, userInfo.indexOf('/'));
340         if (userInfo.indexOf('@') != -1) {
341             userInfo = userInfo.substring(0, userInfo.indexOf('@'));
342             url = url.substring(0, url.indexOf("://") + 3) + url.substring(url.indexOf('@') + 1);
343         } else {
344             userInfo = null;
345         }
346
347         if (url.startsWith("https:")) {
348             this.url = new URL("http" + url.substring(5));
349             ssl = true;
350         } else if (!url.startsWith("http:")) {
351             throw new MalformedURLException("HTTP only supports http/https urls");
352         } else {
353             this.url = new URL(url);
354         }
355         if (!skipResolveCheck) resolveAndCheckIfFirewalled(this.url.getHost());
356         port = this.url.getPort();
357         path = this.url.getFile();
358         if (port == -1) port = ssl ? 443 : 80;
359         host = this.url.getHost();
360         if (Log.verbose) Log.log(this, "creating HTTP object for connection to " + host + ":" + port);
361
362         Proxy pi = Platform.detectProxy();
363         OUTER: do {
364             if (pi != null) {
365                 for(int i=0; i<pi.excluded.length; i++) if (host.equals(pi.excluded[i])) break OUTER;
366                 if (sock == null && pi.proxyAutoConfigFunction != null) sock = attemptPAC(pi.proxyAutoConfigFunction);
367                 if (sock == null && ssl && pi.httpsProxyHost != null) sock = attemptHttpProxy(pi.httpsProxyHost, pi.httpsProxyPort);
368                 if (sock == null && pi.httpProxyHost != null) sock = attemptHttpProxy(pi.httpProxyHost, pi.httpProxyPort);
369                 if (sock == null && pi.socksProxyHost != null) sock = attemptSocksProxy(pi.socksProxyHost, pi.socksProxyPort);
370             }
371         } while (false);
372         proxied = sock != null;
373         if (sock == null) sock = attemptDirect();
374         if (sock == null) throw new HTTPException("unable to contact host " + host);
375         if (in == null) in = new BufferedInputStream(sock.getInputStream());
376     }
377
378     public void sendRequest(String contentType, String content) throws IOException {
379
380         PrintWriter pw = new PrintWriter(new OutputStreamWriter(originalUrl.equals("stdio:") ? System.out : sock.getOutputStream()));
381         if (content != null) {
382             pw.print("POST " + path + " HTTP/1.1\r\n");
383             int contentLength = content.substring(0, 2).equals("\r\n") ?
384                 content.length() - 2 :
385                 (content.length() - content.indexOf("\r\n\r\n") - 4);
386             pw.print("Content-Length: " + contentLength + "\r\n");
387             if (contentType != null) pw.print("Content-Type: " + contentType + "\r\n");
388         } else {
389             pw.print("GET " + path + " HTTP/1.1\r\n");
390         }
391         
392         pw.print("User-Agent: XWT\r\n");
393         pw.print("Accept-encoding: gzip\r\n");
394         pw.print("Host: " + (host + (port == 80 ? "" : (":" + port))) + "\r\n");
395         if (proxied) pw.print("X-RequestOrigin: " + Main.originHost + "\r\n");
396
397         if (Proxy.Authorization.authorization != null) pw.print("Proxy-Authorization: " + Proxy.Authorization.authorization2 + "\r\n");
398         if (authCache.get(originalUrl) != null) pw.print("Authorization: " + authCache.get(originalUrl) + "\r\n");
399
400         pw.print(content == null ? "\r\n" : content);
401         pw.print("\r\n");
402         pw.flush();
403     }
404
405     private void doWebAuth(Hashtable h0, String method) throws IOException {
406         if (userInfo == null) throw new HTTPException("web server demanded username/password, but none were supplied");
407         Hashtable h = parseAuthenticationChallenge(h0.get("www-authenticate").toString());
408         
409         if (h.get("AUTHTYPE").equals("Basic")) {
410             if (authCache.get(originalUrl) != null) throw new HTTPException("username/password rejected");
411             authCache.put(originalUrl, "Basic " + new String(Base64.encode(userInfo.getBytes("US-ASCII"))));
412             
413         } else if (h.get("AUTHTYPE").equals("Digest")) {
414             if (authCache.get(originalUrl) != null && !"true".equals(h.get("stale"))) throw new HTTPException("username/password rejected");
415             String path2 = path;
416             if (path2.startsWith("http://") || path2.startsWith("https://")) {
417                 path2 = path2.substring(path2.indexOf("://") + 3);
418                 path2 = path2.substring(path2.indexOf('/'));
419             }
420             String A1 = userInfo.substring(0, userInfo.indexOf(':')) + ":" + h.get("realm") + ":" + userInfo.substring(userInfo.indexOf(':') + 1);
421             String A2 = method + ":" + path2;
422             authCache.put(originalUrl,
423                           "Digest " +
424                           "username=\"" + userInfo.substring(0, userInfo.indexOf(':')) + "\", " +
425                           "realm=\"" + h.get("realm") + "\", " +
426                           "nonce=\"" + h.get("nonce") + "\", " +
427                           "uri=\"" + path2 + "\", " +
428                           (h.get("opaque") == null ? "" : ("opaque=\"" + h.get("opaque") + "\", ")) + 
429                           "response=\"" + H(H(A1) + ":" + h.get("nonce") + ":" + H(A2)) + "\", " +
430                           "algorithm=MD5"
431                           );
432             
433         } else {
434             throw new HTTPException("unknown authentication type: " + h.get("AUTHTYPE"));
435         }
436     }
437
438     private void doProxyAuth(Hashtable h0, String method) throws IOException {
439         if (Log.on) Log.log(this, "Proxy AuthChallenge: " + h0.get("proxy-authenticate"));
440         Hashtable h = parseAuthenticationChallenge(h0.get("proxy-authenticate").toString());
441         String style = h.get("AUTHTYPE").toString();
442         String realm = h.get("realm").toString();
443
444         if (!realm.equals("Digest") || Proxy.Authorization.authorization2 == null || !"true".equals(h.get("stale")))
445             Proxy.Authorization.getPassword(realm, style, sock.getInetAddress().getHostAddress(), Proxy.Authorization.authorization);
446
447         if (style.equals("Basic")) {
448             Proxy.Authorization.authorization2 =
449                 "Basic " + new String(Base64.encode(Proxy.Authorization.authorization.getBytes("US-ASCII")));
450             
451         } else if (style.equals("Digest")) {
452             String A1 = Proxy.Authorization.authorization.substring(0, userInfo.indexOf(':')) + ":" + h.get("realm") + ":" +
453                 Proxy.Authorization.authorization.substring(Proxy.Authorization.authorization.indexOf(':') + 1);
454             String A2 = method + ":" + path;
455             Proxy.Authorization.authorization2 = 
456                 "Digest " +
457                 "username=\"" + Proxy.Authorization.authorization.substring(0, Proxy.Authorization.authorization.indexOf(':')) + "\", " +
458                 "realm=\"" + h.get("realm") + "\", " +
459                 "nonce=\"" + h.get("nonce") + "\", " +
460                 "uri=\"" + path + "\", " +
461                 (h.get("opaque") == null ? "" : ("opaque=\"" + h.get("opaque") + "\", ")) + 
462                 "response=\"" + H(H(A1) + ":" + h.get("nonce") + ":" + H(A2)) + "\", " +
463                 "algorithm=MD5";
464         }            
465     }
466
467
468     // HTTPException ///////////////////////////////////////////////////////////////////////////////////
469
470     static class HTTPException extends IOException { public HTTPException(String s) { super(s); } }
471
472
473     // HTTPInputStream ///////////////////////////////////////////////////////////////////////////////////
474
475     /** An input stream that represents a subset of a longer input stream. Supports HTTP chunking as well */
476     public class HTTPInputStream extends FilterInputStream {
477
478         /** if chunking, the number of bytes remaining in this subset; otherwise the remainder of the chunk */
479         private int length = 0;
480
481         /** this semaphore will be released when the stream is closed */
482         private Semaphore releaseMe = null;
483
484         /** indicates that we have encountered the zero-length terminator chunk */
485         boolean chunkedDone = false;
486
487         /** if we're on the first chunk, we don't pre-read a CRLF */
488         boolean firstChunk = true;
489
490         /** the length of the entire content body; -1 if chunked */
491         private int contentLength = 0;
492         public int getContentLength() { return contentLength; }
493
494         HTTPInputStream(InputStream in, int length, Semaphore releaseMe) throws IOException {
495             super(in);
496             this.releaseMe = releaseMe;
497             this.contentLength = length;
498             this.length = length == -1 ? 0 : length;
499         }
500
501         public boolean markSupported() { return false; }
502         public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
503         public long skip(long n) throws IOException { return read(null, -1, (int)n); }
504         public int available() throws IOException {
505             if (contentLength == -1) return java.lang.Math.min(super.available(), length);
506             return super.available();
507         }
508
509         public int read() throws IOException {
510             byte[] b = new byte[1];
511             int ret = read(b, 0, 1);
512             return ret == -1 ? -1 : b[0] & 0xff;
513         }
514
515         private void readChunk() throws IOException {
516             if (chunkedDone) return;
517             if (!firstChunk) super.skip(2); // CRLF
518             firstChunk = false;
519             String chunkLen = "";
520             while(true) {
521                 int i = super.read();
522                 if (i == -1) throw new HTTPException("encountered end of stream while reading chunk length");
523
524                 // FEATURE: handle chunking extensions
525                 if (i == '\r') {
526                     super.read();    // LF
527                     break;
528                 } else {
529                     chunkLen += (char)i;
530                 }
531             }
532             length = Integer.parseInt(chunkLen.trim(), 16);
533             if (length == 0) chunkedDone = true;
534         }
535
536         public int read(byte[] b, int off, int len) throws IOException {
537             boolean good = false;
538             try {
539                 if (length == 0 && contentLength == -1) {
540                     readChunk();
541                     if (chunkedDone) { good = true; return -1; }
542                 } else {
543                     if (length == 0) { good = true; return -1; }
544                 }
545                 if (len > length) len = length;
546                 int ret = b == null ? (int)super.skip(len) : super.read(b, off, len);
547                 if (ret >= 0) {
548                     length -= ret;
549                     good = true;
550                 }
551                 return ret;
552             } finally {
553                 if (!good) { HTTP.this.sock = null; HTTP.this.in = null; }
554             }
555         }
556
557         public void close() throws IOException {
558             if (contentLength == -1) {
559                 while(!chunkedDone) {
560                     if (length != 0) skip(length);
561                     readChunk();
562                 }
563                 skip(2);
564             } else {
565                 if (length != 0) skip(length);
566             }
567             if (releaseMe != null) releaseMe.release();
568         }
569     }
570
571
572     // Misc Helpers ///////////////////////////////////////////////////////////////////////////////////
573
574     /** reads a set of HTTP headers off of the input stream, returning null if the stream is already at its end */
575     private Hashtable parseHeaders(InputStream in) throws IOException {
576         Hashtable ret = new Hashtable();
577
578         // we can't use a BufferedReader directly on the input stream, since it will buffer past the end of the headers
579         byte[] buf = new byte[4096];
580         int buflen = 0;
581         while(true) {
582             int read = in.read();
583             if (read == -1 && buflen == 0) return null;
584             if (read == -1) throw new HTTPException("stream closed while reading headers");
585             buf[buflen++] = (byte)read;
586             if (buflen >= 4 && buf[buflen - 4] == '\r' && buf[buflen - 3] == '\n' && buf[buflen - 2] == '\r' && buf[buflen - 1] == '\n') break;
587             if (buflen == buf.length) {
588                 byte[] newbuf = new byte[buf.length * 2];
589                 System.arraycopy(buf, 0, newbuf, 0, buflen);
590                 buf = newbuf;
591             }
592         }
593
594         BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf, 0, buflen)));
595         String s = br.readLine();
596         if (!s.startsWith("HTTP/")) throw new HTTPException("Expected reply to start with \"HTTP/\", got: " + s);
597         ret.put("STATUSLINE", s.substring(s.indexOf(' ') + 1));
598         ret.put("HTTP", s.substring(5, s.indexOf(' ')));
599
600         while((s = br.readLine()) != null && s.length() > 0) {
601             String front = s.substring(0, s.indexOf(':')).toLowerCase();
602             String back = s.substring(s.indexOf(':') + 1).trim();
603             // ugly hack: we never replace a Digest-auth with a Basic-auth (proxy + www)
604             if (front.endsWith("-authenticate") && ret.get(front) != null && !back.equals("Digest")) continue;
605             ret.put(front, back);
606         }
607         return ret;
608     }
609
610     private Hashtable parseAuthenticationChallenge(String s) {
611         Hashtable ret = new Hashtable();
612
613         s = s.trim();
614         ret.put("AUTHTYPE", s.substring(0, s.indexOf(' ')));
615         s = s.substring(s.indexOf(' ')).trim();
616
617         while (s.length() > 0) {
618             String val = null;
619             String key = s.substring(0, s.indexOf('='));
620             s = s.substring(s.indexOf('=') + 1);
621             if (s.charAt(0) == '\"') {
622                 s = s.substring(1);
623                 val = s.substring(0, s.indexOf('\"'));
624                 s = s.substring(s.indexOf('\"') + 1);
625             } else {
626                 val = s.indexOf(',') == -1 ? s : s.substring(0, s.indexOf(','));
627                 s = s.indexOf(',') == -1 ? "" : s.substring(s.indexOf(',') + 1);
628             }
629             if (s.length() > 0 && s.charAt(0) == ',') s = s.substring(1);
630             s = s.trim();
631             ret.put(key, val);
632         }
633         return ret;
634     }
635
636     private String H(String s) throws IOException {
637         byte[] b = s.getBytes("US-ASCII");
638         MD5Digest md5 = new MD5Digest();
639         md5.update(b, 0, b.length);
640         byte[] out = new byte[md5.getDigestSize()];
641         md5.doFinal(out, 0);
642         String ret = "";
643         for(int i=0; i<out.length; i++) {
644             ret += "0123456789abcdef".charAt((out[i] & 0xf0) >> 4);
645             ret += "0123456789abcdef".charAt(out[i] & 0x0f);
646         }
647         return ret;
648     }
649
650
651     // Proxy ///////////////////////////////////////////////////////////
652
653     /** encapsulates most of the proxy logic; some is shared in HTTP.java */
654     public static class Proxy {
655         
656         public Proxy() { }
657         
658         /** the HTTP Proxy host to use */
659         public String httpProxyHost = null;
660         
661         /** the HTTP Proxy port to use */
662         public int httpProxyPort = -1;
663         
664         /** if a seperate proxy should be used for HTTPS, this is the hostname; otherwise, httpProxyHost is used */
665         public String httpsProxyHost = null;
666     
667         /** if a seperate proxy should be used for HTTPS, this is the port */
668         public int httpsProxyPort = -1;
669     
670         /** the SOCKS Proxy Host to use */
671         public String socksProxyHost = null;
672     
673         /** the SOCKS Proxy Port to use */
674         public int socksProxyPort = -1;
675     
676         /** hosts to be excluded from proxy use; wildcards permitted */
677         public String[] excluded = null;
678     
679         /** the PAC script */
680         public JS.Callable proxyAutoConfigFunction = null;
681     
682         public static Proxy detectProxyViaManual() {
683             Proxy ret = new Proxy();
684         
685             ret.httpProxyHost = Platform.getEnv("http_proxy");
686             if (ret.httpProxyHost != null) {
687                 if (ret.httpProxyHost.startsWith("http://")) ret.httpProxyHost = ret.httpProxyHost.substring(7);
688                 if (ret.httpProxyHost.endsWith("/")) ret.httpProxyHost = ret.httpProxyHost.substring(0, ret.httpProxyHost.length() - 1);
689                 if (ret.httpProxyHost.indexOf(':') != -1) {
690                     ret.httpProxyPort = Integer.parseInt(ret.httpProxyHost.substring(ret.httpProxyHost.indexOf(':') + 1));
691                     ret.httpProxyHost = ret.httpProxyHost.substring(0, ret.httpProxyHost.indexOf(':'));
692                 } else {
693                     ret.httpProxyPort = 80;
694                 }
695             }
696         
697             ret.httpsProxyHost = Platform.getEnv("https_proxy");
698             if (ret.httpsProxyHost != null) {
699                 if (ret.httpsProxyHost.startsWith("https://")) ret.httpsProxyHost = ret.httpsProxyHost.substring(7);
700                 if (ret.httpsProxyHost.endsWith("/")) ret.httpsProxyHost = ret.httpsProxyHost.substring(0, ret.httpsProxyHost.length() - 1);
701                 if (ret.httpsProxyHost.indexOf(':') != -1) {
702                     ret.httpsProxyPort = Integer.parseInt(ret.httpsProxyHost.substring(ret.httpsProxyHost.indexOf(':') + 1));
703                     ret.httpsProxyHost = ret.httpsProxyHost.substring(0, ret.httpsProxyHost.indexOf(':'));
704                 } else {
705                     ret.httpsProxyPort = 80;
706                 }
707             }
708         
709             ret.socksProxyHost = Platform.getEnv("socks_proxy");
710             if (ret.socksProxyHost != null) {
711                 if (ret.socksProxyHost.startsWith("socks://")) ret.socksProxyHost = ret.socksProxyHost.substring(7);
712                 if (ret.socksProxyHost.endsWith("/")) ret.socksProxyHost = ret.socksProxyHost.substring(0, ret.socksProxyHost.length() - 1);
713                 if (ret.socksProxyHost.indexOf(':') != -1) {
714                     ret.socksProxyPort = Integer.parseInt(ret.socksProxyHost.substring(ret.socksProxyHost.indexOf(':') + 1));
715                     ret.socksProxyHost = ret.socksProxyHost.substring(0, ret.socksProxyHost.indexOf(':'));
716                 } else {
717                     ret.socksProxyPort = 80;
718                 }
719             }
720         
721             String noproxy = Platform.getEnv("no_proxy");
722             if (noproxy != null) {
723                 StringTokenizer st = new StringTokenizer(noproxy, ",");
724                 ret.excluded = new String[st.countTokens()];
725                 for(int i=0; st.hasMoreTokens(); i++) ret.excluded[i] = st.nextToken();
726             }
727         
728             if (ret.httpProxyHost == null && ret.socksProxyHost == null) return null;
729             return ret;
730         }
731     
732         public static JS.Scope proxyAutoConfigRootScope = new ProxyAutoConfigRootScope();
733         public static JS.Callable getProxyAutoConfigFunction(String url) {
734             try { 
735                 BufferedReader br = new BufferedReader(new InputStreamReader(new HTTP(url, true).GET()));
736                 String s = null;
737                 String script = "";
738                 while((s = br.readLine()) != null) script += s + "\n";
739                 if (Log.on) Log.log(Proxy.class, "successfully retrieved WPAD PAC:");
740                 if (Log.on) Log.log(Proxy.class, script);
741             
742                 // MS CARP hack
743                 Vector carpHosts = new Vector();
744                 for(int i=0; i<script.length(); i++)
745                     if (script.regionMatches(i, "new Node(", 0, 9)) {
746                         String host = script.substring(i + 10, script.indexOf('\"', i + 11));
747                         if (Log.on) Log.log(Proxy.class, "Detected MS Proxy Server CARP Script, Host=" + host);
748                         carpHosts.addElement(host);
749                     }
750                 if (carpHosts.size() > 0) {
751                     script = "function FindProxyForURL(url, host) {\nreturn \"";
752                     for(int i=0; i<carpHosts.size(); i++)
753                         script += "PROXY " + carpHosts.elementAt(i) + "; ";
754                     script += "\";\n}";
755                     if (Log.on) Log.log(Proxy.class, "DeCARPed PAC script:");
756                     if (Log.on) Log.log(Proxy.class, script);
757                 }
758
759                 JS.CompiledFunction scr = JS.parse("PAC script at " + url, 0, new StringReader(script));
760                 scr.call(new JS.Array(), proxyAutoConfigRootScope);
761                 return (JS.Callable)proxyAutoConfigRootScope.get("FindProxyForURL");
762             } catch (Exception e) {
763                 if (Log.on) {
764                     Log.log(Platform.class, "WPAD detection failed due to:");
765                     if (e instanceof JS.Exn) {
766                         try {
767                             org.xwt.js.JS.Array arr = new org.xwt.js.JS.Array();
768                             arr.addElement(((JS.Exn)e).getObject());
769                         } catch (Exception e2) {
770                             Log.log(Platform.class, e);
771                         }
772                     }
773                     else Log.log(Platform.class, e);
774                 }
775                 return null;
776             }
777         }
778
779
780         // Authorization ///////////////////////////////////////////////////////////////////////////////////
781
782         public static class Authorization {
783
784             static public String authorization = null;
785             static public String authorization2 = null;
786             static public Semaphore waitingForUser = new Semaphore();
787
788             public static synchronized void getPassword(final String realm, final String style, final String proxyIP, String oldAuth) {
789
790                 // this handles cases where multiple threads hit the proxy auth at the same time -- all but one will block on the
791                 // synchronized keyword. If 'authorization' changed while the thread was blocked, it means that the user entered
792                 // a password, so we should reattempt authorization.
793
794                 if (authorization != oldAuth) return;
795                 if (Log.on) Log.log(Authorization.class, "displaying proxy authorization dialog");
796                 Message.Q.add(new Message() {
797                         public void perform() {
798                             Box b = new Box();
799                             Template t = Template.getTemplate((Res)Main.builtin.get("org/xwt/builtin/proxy_authorization.xwt"));
800                             t.apply(b, null, null);
801                             b.put("realm", realm);
802                             b.put("proxyIP", proxyIP);
803                         }
804                     });
805
806                 waitingForUser.block();
807                 if (Log.on) Log.log(Authorization.class, "got proxy authorization info; re-attempting connection");
808             
809             }
810         }
811
812
813         // ProxyAutoConfigRootScope ////////////////////////////////////////////////////////////////////
814
815         public static class ProxyAutoConfigRootScope extends JS.GlobalScope {
816
817             public ProxyAutoConfigRootScope() { super(null); }
818         
819             public Object get(Object name) {
820                 if (name.equals("isPlainHostName")) return isPlainHostName;
821                 else if (name.equals("dnsDomainIs")) return dnsDomainIs;
822                 else if (name.equals("localHostOrDomainIs")) return localHostOrDomainIs;
823                 else if (name.equals("isResolvable")) return isResolvable;
824                 else if (name.equals("isInNet")) return isInNet;
825                 else if (name.equals("dnsResolve")) return dnsResolve;
826                 else if (name.equals("myIpAddress")) return myIpAddress;
827                 else if (name.equals("dnsDomainLevels")) return dnsDomainLevels;
828                 else if (name.equals("shExpMatch")) return shExpMatch;
829                 else if (name.equals("weekdayRange")) return weekdayRange;
830                 else if (name.equals("dateRange")) return dateRange;
831                 else if (name.equals("timeRange")) return timeRange;
832                 else if (name.equals("ProxyConfig")) return ProxyConfig;
833                 else return super.get(name);
834             }
835         
836             private static final JS.Obj proxyConfigBindings = new JS.Obj();
837             private static final JS.Obj ProxyConfig = new JS.Obj() {
838                     public Object get(Object name) {
839                         if (name.equals("bindings")) return proxyConfigBindings;
840                         return null;
841                     }
842                 };
843         
844             private static final JS.Callable isPlainHostName = new JS.Callable() {
845                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
846                         return (args.elementAt(0).toString().indexOf('.') == -1) ? Boolean.TRUE : Boolean.FALSE;
847                     }
848                 };
849         
850             private static final JS.Callable dnsDomainIs = new JS.Callable() {
851                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
852                         return (args.elementAt(0).toString().endsWith(args.elementAt(1).toString())) ? Boolean.TRUE : Boolean.FALSE;
853                     }
854                 };
855         
856             private static final JS.Callable localHostOrDomainIs = new JS.Callable() {
857                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
858                         return (args.elementAt(0).toString().equals(args.elementAt(1).toString()) || 
859                                 (args.elementAt(0).toString().indexOf('.') == -1 && args.elementAt(1).toString().startsWith(args.elementAt(0).toString()))) ?
860                             Boolean.TRUE : Boolean.FALSE;
861                     }
862                 };
863         
864             private static final JS.Callable isResolvable = new JS.Callable() {
865                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
866                         try {
867                             return (InetAddress.getByName(args.elementAt(0).toString()) != null) ? Boolean.TRUE : Boolean.FALSE;
868                         } catch (UnknownHostException e) {
869                             return Boolean.FALSE;
870                         }
871                     }
872                 };
873         
874             private static final JS.Callable isInNet = new JS.Callable() {
875                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
876                         if (args.length() != 3) return Boolean.FALSE;
877                         try {
878                             byte[] host = InetAddress.getByName(args.elementAt(0).toString()).getAddress();
879                             byte[] net = InetAddress.getByName(args.elementAt(1).toString()).getAddress();
880                             byte[] mask = InetAddress.getByName(args.elementAt(2).toString()).getAddress();
881                             return ((host[0] & mask[0]) == net[0] &&
882                                     (host[1] & mask[1]) == net[1] &&
883                                     (host[2] & mask[2]) == net[2] &&
884                                     (host[3] & mask[3]) == net[3]) ?
885                                 Boolean.TRUE : Boolean.FALSE;
886                         } catch (Exception e) {
887                             throw new JS.Exn("exception in isInNet(): " + e);
888                         }
889                     }
890                 };
891         
892             private static final JS.Callable dnsResolve = new JS.Callable() {
893                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
894                         try {
895                             return InetAddress.getByName(args.elementAt(0).toString()).getHostAddress();
896                         } catch (UnknownHostException e) {
897                             return null;
898                         }
899                     }
900                 };
901         
902             private static final JS.Callable myIpAddress = new JS.Callable() {
903                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
904                         try {
905                             return InetAddress.getLocalHost().getHostAddress();
906                         } catch (UnknownHostException e) {
907                             if (Log.on) Log.log(this, "strange... host does not know its own address");
908                             return null;
909                         }
910                     }
911                 };
912         
913             private static final JS.Callable dnsDomainLevels = new JS.Callable() {
914                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
915                         String s = args.elementAt(0).toString();
916                         int i = 0;
917                         while((i = s.indexOf('.', i)) != -1) i++;
918                         return new Integer(i);
919                     }
920                 };
921         
922             private static boolean match(String[] arr, String s, int index) {
923                 if (index >= arr.length) return true;
924                 for(int i=0; i<s.length(); i++) {
925                     String s2 = s.substring(i);
926                     if (s2.startsWith(arr[index]) && match(arr, s2.substring(arr[index].length()), index + 1)) return true;
927                 }
928                 return false;
929             }
930         
931             private static final JS.Callable shExpMatch = new JS.Callable() {
932                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
933                         StringTokenizer st = new StringTokenizer(args.elementAt(1).toString(), "*", false);
934                         String[] arr = new String[st.countTokens()];
935                         String s = args.elementAt(0).toString();
936                         for (int i=0; st.hasMoreTokens(); i++) arr[i] = st.nextToken();
937                         return match(arr, s, 0) ? Boolean.TRUE : Boolean.FALSE;
938                     }
939                 };
940         
941             public static String[] days = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
942         
943             private static final JS.Callable weekdayRange = new JS.Callable() {
944                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
945                         TimeZone tz = (args.length() < 3 || args.elementAt(2) == null || !args.elementAt(2).equals("GMT")) ? TimeZone.getTimeZone("UTC") : TimeZone.getDefault();
946                         Calendar c = new GregorianCalendar();
947                         c.setTimeZone(tz);
948                         c.setTime(new java.util.Date());
949                         java.util.Date d = c.getTime();
950                         int day = d.getDay();
951                     
952                         String d1s = args.elementAt(0).toString().toUpperCase();
953                         int d1 = 0, d2 = 0;
954                         for(int i=0; i<days.length; i++) if (days[i].equals(d1s)) d1 = i;
955                     
956                         if (args.length() == 1)
957                             return d1 == day ? Boolean.TRUE : Boolean.FALSE;
958                     
959                         String d2s = args.elementAt(1).toString().toUpperCase();
960                         for(int i=0; i<days.length; i++) if (days[i].equals(d2s)) d2 = i;
961                     
962                         return
963                             ((d1 <= d2 && day >= d1 && day <= d2) ||
964                              (d1 > d2 && (day >= d1 || day <= d2))) ?
965                             Boolean.TRUE : Boolean.FALSE;
966                     }
967                 };
968         
969             private static final JS.Callable dateRange = new JS.Callable() {
970                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
971                         throw new JS.Exn("XWT does not support dateRange() in PAC scripts");
972                     }
973                 };
974         
975             private static final JS.Callable timeRange = new JS.Callable() {
976                     public Object call(org.xwt.js.JS.Array args) throws JS.Exn {
977                         throw new JS.Exn("XWT does not support timeRange() in PAC scripts");
978                     }
979                 };
980         
981         }
982
983     }
984
985 }