home *** CD-ROM | disk | FTP | other *** search
- package sun.net.www.http;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.io.PushbackInputStream;
- import java.net.InetAddress;
- import java.net.SocketException;
- import java.net.URL;
- import java.net.UnknownHostException;
- import java.util.StringTokenizer;
- import sun.misc.RegexpPool;
- import sun.net.NetworkClient;
- import sun.net.ProgressData;
- import sun.net.ProgressEntry;
- import sun.net.www.HeaderParser;
- import sun.net.www.MessageHeader;
- import sun.net.www.MeteredStream;
-
- public class HttpClient extends NetworkClient {
- MessageHeader requests;
- boolean failedOnce;
- KeepAliveStream kas;
- private static RegexpPool dontProxy = null;
- static final int httpPortNumber = 80;
- public static String proxyHost = null;
- public static int proxyPort = 80;
- public static boolean httpKeepAliveSet = true;
- private String instProxy;
- private int instProxyPort;
- protected boolean proxyDisabled;
- public boolean usingProxy;
- private String host;
- private int port;
- protected static KeepAliveCache kac = new KeepAliveCache();
- boolean keepingAlive;
- int keepAliveConnections;
- int keepAliveTimeout;
- protected URL url;
-
- protected int getDefaultPort() {
- return 80;
- }
-
- public static synchronized void resetProperties() {
- proxyHost = System.getProperty("http.proxyHost");
- proxyPort = Integer.getInteger("http.proxyPort", 80);
- if (proxyHost == null) {
- proxyHost = System.getProperty("proxyHost");
- proxyPort = Integer.getInteger("proxyPort", 80);
- }
-
- if (proxyHost != null && proxyHost.length() == 0) {
- proxyHost = null;
- }
-
- KeepAliveCache.MAXCONNS = Integer.getInteger("http.maxConnections", 2);
- if (KeepAliveCache.MAXCONNS <= 0) {
- KeepAliveCache.MAXCONNS = 2;
- }
-
- String var0 = System.getProperty("http.keepAlive");
- if (var0 != null) {
- httpKeepAliveSet = Boolean.valueOf(var0);
- }
-
- dontProxy = new RegexpPool();
- String var1 = System.getProperty("http.nonProxyHosts");
- if (var1 != null) {
- StringTokenizer var2 = new StringTokenizer(var1, "|", false);
-
- try {
- while(var2.hasMoreTokens()) {
- dontProxy.add(var2.nextToken().toLowerCase(), new Boolean(true));
- }
-
- } catch (Exception var4) {
- ((Throwable)var4).printStackTrace();
- }
- }
- }
-
- public HttpClient(URL var1, String var2, int var3) throws IOException {
- this(var1, var2, var3, false);
- }
-
- private HttpClient(URL var1, String var2, int var3, boolean var4) throws IOException {
- this.failedOnce = false;
- this.instProxyPort = -1;
- this.usingProxy = false;
- this.keepingAlive = false;
- this.keepAliveConnections = -1;
- this.proxyDisabled = var4;
- if (!var4) {
- this.instProxy = var2;
- this.instProxyPort = var3 < 0 ? this.getDefaultPort() : var3;
- }
-
- try {
- InetAddress var5 = InetAddress.getByName(var1.getHost());
- this.host = var5.getHostAddress();
- } catch (UnknownHostException var6) {
- this.host = var1.getHost();
- }
-
- this.url = var1;
- this.port = var1.getPort();
- if (this.port == -1) {
- this.port = this.getDefaultPort();
- }
-
- this.openServer();
- }
-
- protected HttpClient(URL var1, boolean var2) throws IOException {
- this(var1, (String)null, -1, var2);
- }
-
- private HttpClient(URL var1) throws IOException {
- this(var1, (String)null, -1, false);
- }
-
- public static HttpClient New(URL var0) throws IOException {
- HttpClient var1 = (HttpClient)kac.get(var0);
- if (var1 == null) {
- var1 = new HttpClient(var0);
- } else {
- var1.url = var0;
- }
-
- return var1;
- }
-
- public static void finished(HttpClient var0) {
- --var0.keepAliveConnections;
- if (var0.keepAliveConnections > 0 && var0.keepingAlive && !var0.serverOutput.checkError()) {
- kac.put(var0.url, var0);
- } else {
- var0.closeServer();
- }
- }
-
- public void openServer(String var1, int var2) throws IOException {
- super.serverSocket = ((NetworkClient)this).doConnect(var1, var2);
- super.serverOutput = new PrintStream(new BufferedOutputStream(super.serverSocket.getOutputStream()));
- }
-
- private synchronized void openServer() throws IOException {
- SecurityManager var1 = System.getSecurityManager();
- if (var1 != null) {
- var1.checkConnect(this.host, this.port);
- }
-
- if (!this.keepingAlive) {
- if (this.instProxy != null) {
- this.openServer(this.instProxy, this.instProxyPort);
- this.usingProxy = true;
- } else if (!this.proxyDisabled && dontProxy.match(this.url.getHost().toLowerCase()) == null && dontProxy.match(this.host) == null) {
- if (!this.url.getProtocol().equals("http")) {
- if (this.instProxy != null) {
- try {
- super.openServer(this.instProxy, this.instProxyPort);
- this.usingProxy = true;
- } catch (IOException var2) {
- }
- } else if (proxyHost != null) {
- try {
- super.openServer(proxyHost, proxyPort);
- this.usingProxy = true;
- } catch (IOException var3) {
- }
- } else {
- super.openServer(this.host, this.port);
- }
- } else {
- if (proxyHost != null) {
- try {
- this.openServer(proxyHost, proxyPort);
- this.usingProxy = true;
- return;
- } catch (IOException var4) {
- }
- }
-
- this.openServer(this.host, this.port);
- }
- } else {
- this.openServer(this.host, this.port);
- this.usingProxy = false;
- }
- }
- }
-
- public String getURLFile() {
- if (this.usingProxy) {
- String var1 = this.url.getProtocol() + "://" + this.url.getHost();
- if (this.url.getPort() != -1) {
- var1 = var1 + ":" + this.url.getPort();
- }
-
- return var1 + this.url.getFile();
- } else {
- return this.url.getFile();
- }
- }
-
- public void writeRequests(MessageHeader var1) {
- this.requests = var1;
- this.requests.print(super.serverOutput);
- super.serverOutput.flush();
- }
-
- public boolean parseHTTP(MessageHeader var1, ProgressEntry var2) throws IOException {
- this.keepAliveConnections = -1;
- this.keepAliveTimeout = 0;
- boolean var3 = false;
- byte[] var4 = new byte[7];
-
- try {
- super.serverInput = new PushbackInputStream(super.serverSocket.getInputStream(), 7);
-
- int var5;
- int var6;
- for(var5 = 0; var5 < 7; var5 += var6) {
- var6 = super.serverInput.read(var4, var5, 7 - var5);
- if (var6 < 0) {
- break;
- }
- }
-
- Object var12 = null;
- var3 = var4[0] == 72 && var4[1] == 84 && var4[2] == 84 && var4[3] == 80 && var4[4] == 47 && var4[5] == 49 && var4[6] == 46;
- ((PushbackInputStream)super.serverInput).unread(var4);
- if (var3) {
- var1.parseHeader(super.serverInput);
- String var13;
- if (this.usingProxy) {
- var13 = var1.findValue("Proxy-Connection");
- } else {
- var13 = var1.findValue("Connection");
- }
-
- if (var13 != null && var13.toLowerCase().equals("keep-alive")) {
- HeaderParser var7 = new HeaderParser(var1.findValue("Keep-Alive"));
- this.keepAliveConnections = var7.findInt("max", 5);
- this.keepAliveTimeout = var7.findInt("timeout", 5);
- }
- } else {
- if (var5 != 7) {
- if (!this.failedOnce && this.requests != null) {
- this.failedOnce = true;
- this.closeServer();
- this.openServer();
- this.writeRequests(this.requests);
- return this.parseHTTP(var1, var2);
- }
-
- throw new SocketException("Unexpected end of file from server");
- }
-
- var1.set("Content-type", "unknown/unknown");
- }
- } catch (IOException var9) {
- this.closeServer();
- if (!this.failedOnce && this.requests != null) {
- this.failedOnce = true;
- this.openServer();
- this.writeRequests(this.requests);
- return this.parseHTTP(var1, var2);
- }
-
- throw var9;
- }
-
- int var11 = -1;
-
- try {
- var11 = Integer.parseInt(var1.findValue("content-length"));
- } catch (Exception var8) {
- }
-
- if (this.keepAliveConnections > 1 && var11 > 0) {
- this.keepingAlive = true;
- } else if (this.keepingAlive) {
- this.keepingAlive = false;
- }
-
- if (var11 > 0) {
- var2.setType(this.url.getFile(), var1.findValue("content-type"));
- var2.update(0, var11);
- if (this.keepingAlive) {
- this.kas = new KeepAliveStream(super.serverSocket.getInputStream(), var2, this);
- super.serverInput = new BufferedInputStream(this.kas);
- this.failedOnce = false;
- } else {
- super.serverInput = new BufferedInputStream(new MeteredStream(super.serverSocket.getInputStream(), var2));
- }
- } else {
- super.serverInput = new BufferedInputStream(super.serverSocket.getInputStream());
- ProgressData.pdata.unregister(var2);
- }
-
- return var3;
- }
-
- public synchronized InputStream getInputStream() {
- return super.serverInput;
- }
-
- public OutputStream getOutputStream() {
- return super.serverOutput;
- }
-
- public String toString() {
- return this.getClass().getName() + "(" + this.url + ")";
- }
-
- public final boolean isKeepingAlive() {
- return httpKeepAliveSet && this.keepingAlive;
- }
-
- protected void finalize() throws Throwable {
- }
-
- public void closeServer() {
- try {
- this.keepingAlive = false;
- super.serverSocket.close();
- } catch (Exception var1) {
- }
- }
-
- static {
- resetProperties();
- }
- }
-