home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VPage / Java.bin / CLASSES.ZIP / sun / net / www / http / HttpClient.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-08  |  8.3 KB  |  340 lines

  1. package sun.net.www.http;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.PrintStream;
  9. import java.io.PushbackInputStream;
  10. import java.net.InetAddress;
  11. import java.net.SocketException;
  12. import java.net.URL;
  13. import java.net.UnknownHostException;
  14. import java.util.StringTokenizer;
  15. import sun.misc.RegexpPool;
  16. import sun.net.NetworkClient;
  17. import sun.net.ProgressData;
  18. import sun.net.ProgressEntry;
  19. import sun.net.www.HeaderParser;
  20. import sun.net.www.MessageHeader;
  21. import sun.net.www.MeteredStream;
  22.  
  23. public class HttpClient extends NetworkClient {
  24.    MessageHeader requests;
  25.    boolean failedOnce;
  26.    KeepAliveStream kas;
  27.    private static RegexpPool dontProxy = null;
  28.    static final int httpPortNumber = 80;
  29.    public static String proxyHost = null;
  30.    public static int proxyPort = 80;
  31.    public static boolean httpKeepAliveSet = true;
  32.    private String instProxy;
  33.    private int instProxyPort;
  34.    protected boolean proxyDisabled;
  35.    public boolean usingProxy;
  36.    private String host;
  37.    private int port;
  38.    protected static KeepAliveCache kac = new KeepAliveCache();
  39.    boolean keepingAlive;
  40.    int keepAliveConnections;
  41.    int keepAliveTimeout;
  42.    protected URL url;
  43.  
  44.    protected int getDefaultPort() {
  45.       return 80;
  46.    }
  47.  
  48.    public static synchronized void resetProperties() {
  49.       proxyHost = System.getProperty("http.proxyHost");
  50.       proxyPort = Integer.getInteger("http.proxyPort", 80);
  51.       if (proxyHost == null) {
  52.          proxyHost = System.getProperty("proxyHost");
  53.          proxyPort = Integer.getInteger("proxyPort", 80);
  54.       }
  55.  
  56.       if (proxyHost != null && proxyHost.length() == 0) {
  57.          proxyHost = null;
  58.       }
  59.  
  60.       KeepAliveCache.MAXCONNS = Integer.getInteger("http.maxConnections", 2);
  61.       if (KeepAliveCache.MAXCONNS <= 0) {
  62.          KeepAliveCache.MAXCONNS = 2;
  63.       }
  64.  
  65.       String var0 = System.getProperty("http.keepAlive");
  66.       if (var0 != null) {
  67.          httpKeepAliveSet = Boolean.valueOf(var0);
  68.       }
  69.  
  70.       dontProxy = new RegexpPool();
  71.       String var1 = System.getProperty("http.nonProxyHosts");
  72.       if (var1 != null) {
  73.          StringTokenizer var2 = new StringTokenizer(var1, "|", false);
  74.  
  75.          try {
  76.             while(var2.hasMoreTokens()) {
  77.                dontProxy.add(var2.nextToken().toLowerCase(), new Boolean(true));
  78.             }
  79.  
  80.          } catch (Exception var4) {
  81.             ((Throwable)var4).printStackTrace();
  82.          }
  83.       }
  84.    }
  85.  
  86.    public HttpClient(URL var1, String var2, int var3) throws IOException {
  87.       this(var1, var2, var3, false);
  88.    }
  89.  
  90.    private HttpClient(URL var1, String var2, int var3, boolean var4) throws IOException {
  91.       this.failedOnce = false;
  92.       this.instProxyPort = -1;
  93.       this.usingProxy = false;
  94.       this.keepingAlive = false;
  95.       this.keepAliveConnections = -1;
  96.       this.proxyDisabled = var4;
  97.       if (!var4) {
  98.          this.instProxy = var2;
  99.          this.instProxyPort = var3 < 0 ? this.getDefaultPort() : var3;
  100.       }
  101.  
  102.       try {
  103.          InetAddress var5 = InetAddress.getByName(var1.getHost());
  104.          this.host = var5.getHostAddress();
  105.       } catch (UnknownHostException var6) {
  106.          this.host = var1.getHost();
  107.       }
  108.  
  109.       this.url = var1;
  110.       this.port = var1.getPort();
  111.       if (this.port == -1) {
  112.          this.port = this.getDefaultPort();
  113.       }
  114.  
  115.       this.openServer();
  116.    }
  117.  
  118.    protected HttpClient(URL var1, boolean var2) throws IOException {
  119.       this(var1, (String)null, -1, var2);
  120.    }
  121.  
  122.    private HttpClient(URL var1) throws IOException {
  123.       this(var1, (String)null, -1, false);
  124.    }
  125.  
  126.    public static HttpClient New(URL var0) throws IOException {
  127.       HttpClient var1 = (HttpClient)kac.get(var0);
  128.       if (var1 == null) {
  129.          var1 = new HttpClient(var0);
  130.       } else {
  131.          var1.url = var0;
  132.       }
  133.  
  134.       return var1;
  135.    }
  136.  
  137.    public static void finished(HttpClient var0) {
  138.       --var0.keepAliveConnections;
  139.       if (var0.keepAliveConnections > 0 && var0.keepingAlive && !var0.serverOutput.checkError()) {
  140.          kac.put(var0.url, var0);
  141.       } else {
  142.          var0.closeServer();
  143.       }
  144.    }
  145.  
  146.    public void openServer(String var1, int var2) throws IOException {
  147.       super.serverSocket = ((NetworkClient)this).doConnect(var1, var2);
  148.       super.serverOutput = new PrintStream(new BufferedOutputStream(super.serverSocket.getOutputStream()));
  149.    }
  150.  
  151.    private synchronized void openServer() throws IOException {
  152.       SecurityManager var1 = System.getSecurityManager();
  153.       if (var1 != null) {
  154.          var1.checkConnect(this.host, this.port);
  155.       }
  156.  
  157.       if (!this.keepingAlive) {
  158.          if (this.instProxy != null) {
  159.             this.openServer(this.instProxy, this.instProxyPort);
  160.             this.usingProxy = true;
  161.          } else if (!this.proxyDisabled && dontProxy.match(this.url.getHost().toLowerCase()) == null && dontProxy.match(this.host) == null) {
  162.             if (!this.url.getProtocol().equals("http")) {
  163.                if (this.instProxy != null) {
  164.                   try {
  165.                      super.openServer(this.instProxy, this.instProxyPort);
  166.                      this.usingProxy = true;
  167.                   } catch (IOException var2) {
  168.                   }
  169.                } else if (proxyHost != null) {
  170.                   try {
  171.                      super.openServer(proxyHost, proxyPort);
  172.                      this.usingProxy = true;
  173.                   } catch (IOException var3) {
  174.                   }
  175.                } else {
  176.                   super.openServer(this.host, this.port);
  177.                }
  178.             } else {
  179.                if (proxyHost != null) {
  180.                   try {
  181.                      this.openServer(proxyHost, proxyPort);
  182.                      this.usingProxy = true;
  183.                      return;
  184.                   } catch (IOException var4) {
  185.                   }
  186.                }
  187.  
  188.                this.openServer(this.host, this.port);
  189.             }
  190.          } else {
  191.             this.openServer(this.host, this.port);
  192.             this.usingProxy = false;
  193.          }
  194.       }
  195.    }
  196.  
  197.    public String getURLFile() {
  198.       if (this.usingProxy) {
  199.          String var1 = this.url.getProtocol() + "://" + this.url.getHost();
  200.          if (this.url.getPort() != -1) {
  201.             var1 = var1 + ":" + this.url.getPort();
  202.          }
  203.  
  204.          return var1 + this.url.getFile();
  205.       } else {
  206.          return this.url.getFile();
  207.       }
  208.    }
  209.  
  210.    public void writeRequests(MessageHeader var1) {
  211.       this.requests = var1;
  212.       this.requests.print(super.serverOutput);
  213.       super.serverOutput.flush();
  214.    }
  215.  
  216.    public boolean parseHTTP(MessageHeader var1, ProgressEntry var2) throws IOException {
  217.       this.keepAliveConnections = -1;
  218.       this.keepAliveTimeout = 0;
  219.       boolean var3 = false;
  220.       byte[] var4 = new byte[7];
  221.  
  222.       try {
  223.          super.serverInput = new PushbackInputStream(super.serverSocket.getInputStream(), 7);
  224.  
  225.          int var5;
  226.          int var6;
  227.          for(var5 = 0; var5 < 7; var5 += var6) {
  228.             var6 = super.serverInput.read(var4, var5, 7 - var5);
  229.             if (var6 < 0) {
  230.                break;
  231.             }
  232.          }
  233.  
  234.          Object var12 = null;
  235.          var3 = var4[0] == 72 && var4[1] == 84 && var4[2] == 84 && var4[3] == 80 && var4[4] == 47 && var4[5] == 49 && var4[6] == 46;
  236.          ((PushbackInputStream)super.serverInput).unread(var4);
  237.          if (var3) {
  238.             var1.parseHeader(super.serverInput);
  239.             String var13;
  240.             if (this.usingProxy) {
  241.                var13 = var1.findValue("Proxy-Connection");
  242.             } else {
  243.                var13 = var1.findValue("Connection");
  244.             }
  245.  
  246.             if (var13 != null && var13.toLowerCase().equals("keep-alive")) {
  247.                HeaderParser var7 = new HeaderParser(var1.findValue("Keep-Alive"));
  248.                this.keepAliveConnections = var7.findInt("max", 5);
  249.                this.keepAliveTimeout = var7.findInt("timeout", 5);
  250.             }
  251.          } else {
  252.             if (var5 != 7) {
  253.                if (!this.failedOnce && this.requests != null) {
  254.                   this.failedOnce = true;
  255.                   this.closeServer();
  256.                   this.openServer();
  257.                   this.writeRequests(this.requests);
  258.                   return this.parseHTTP(var1, var2);
  259.                }
  260.  
  261.                throw new SocketException("Unexpected end of file from server");
  262.             }
  263.  
  264.             var1.set("Content-type", "unknown/unknown");
  265.          }
  266.       } catch (IOException var9) {
  267.          this.closeServer();
  268.          if (!this.failedOnce && this.requests != null) {
  269.             this.failedOnce = true;
  270.             this.openServer();
  271.             this.writeRequests(this.requests);
  272.             return this.parseHTTP(var1, var2);
  273.          }
  274.  
  275.          throw var9;
  276.       }
  277.  
  278.       int var11 = -1;
  279.  
  280.       try {
  281.          var11 = Integer.parseInt(var1.findValue("content-length"));
  282.       } catch (Exception var8) {
  283.       }
  284.  
  285.       if (this.keepAliveConnections > 1 && var11 > 0) {
  286.          this.keepingAlive = true;
  287.       } else if (this.keepingAlive) {
  288.          this.keepingAlive = false;
  289.       }
  290.  
  291.       if (var11 > 0) {
  292.          var2.setType(this.url.getFile(), var1.findValue("content-type"));
  293.          var2.update(0, var11);
  294.          if (this.keepingAlive) {
  295.             this.kas = new KeepAliveStream(super.serverSocket.getInputStream(), var2, this);
  296.             super.serverInput = new BufferedInputStream(this.kas);
  297.             this.failedOnce = false;
  298.          } else {
  299.             super.serverInput = new BufferedInputStream(new MeteredStream(super.serverSocket.getInputStream(), var2));
  300.          }
  301.       } else {
  302.          super.serverInput = new BufferedInputStream(super.serverSocket.getInputStream());
  303.          ProgressData.pdata.unregister(var2);
  304.       }
  305.  
  306.       return var3;
  307.    }
  308.  
  309.    public synchronized InputStream getInputStream() {
  310.       return super.serverInput;
  311.    }
  312.  
  313.    public OutputStream getOutputStream() {
  314.       return super.serverOutput;
  315.    }
  316.  
  317.    public String toString() {
  318.       return this.getClass().getName() + "(" + this.url + ")";
  319.    }
  320.  
  321.    public final boolean isKeepingAlive() {
  322.       return httpKeepAliveSet && this.keepingAlive;
  323.    }
  324.  
  325.    protected void finalize() throws Throwable {
  326.    }
  327.  
  328.    public void closeServer() {
  329.       try {
  330.          this.keepingAlive = false;
  331.          super.serverSocket.close();
  332.       } catch (Exception var1) {
  333.       }
  334.    }
  335.  
  336.    static {
  337.       resetProperties();
  338.    }
  339. }
  340.