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.security.AccessController;
- import java.security.PrivilegedActionException;
- import java.util.StringTokenizer;
- import sun.misc.REException;
- 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;
- import sun.security.action.GetPropertyAction;
-
- public class HttpClient extends NetworkClient {
- MessageHeader requests;
- boolean failedOnce;
- KeepAliveStream kas;
- private static RegexpPool dontProxy = new RegexpPool();
- private static String dontProxySource = null;
- private static final int HTTP_CONTINUE = 100;
- static final int httpPortNumber = 80;
- public static String proxyHost = null;
- public static int proxyPort = 80;
- 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() {
- }
-
- private String getProxyHost() {
- String var1 = (String)AccessController.doPrivileged(new GetPropertyAction("http.proxyHost"));
- if (var1 == null) {
- var1 = (String)AccessController.doPrivileged(new GetPropertyAction("proxyHost"));
- }
-
- if (var1 != null && var1.length() == 0) {
- var1 = null;
- }
-
- return var1;
- }
-
- private int getProxyPort() {
- int[] var1 = new int[]{0};
- AccessController.doPrivileged(new 1(this, var1));
- return var1[0];
- }
-
- public boolean getHttpKeepAliveSet() {
- boolean var1 = true;
- String var2 = (String)AccessController.doPrivileged(new 2(this));
- if (var2 != null) {
- var1 = Boolean.valueOf(var2);
- }
-
- return var1;
- }
-
- private static RegexpPool getDontProxy() {
- RegexpPool var0 = dontProxy;
- synchronized(var0) {
- String var1 = (String)AccessController.doPrivileged(new GetPropertyAction("http.nonProxyHosts"));
- if (var1 == dontProxySource) {
- RegexpPool var8 = dontProxy;
- return var8;
- }
-
- RegexpPool var2 = new RegexpPool();
- if (var1 != null) {
- StringTokenizer var3 = new StringTokenizer(var1, "|", false);
-
- try {
- while(var3.hasMoreTokens()) {
- var2.add(var3.nextToken().toLowerCase(), Boolean.TRUE);
- }
- } catch (REException var6) {
- System.err.println("Error in http.nonProxyHosts system property: " + var6);
- }
- }
-
- dontProxySource = var1;
- dontProxy = var2;
- }
-
- return dontProxy;
- }
-
- 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.instProxy = null;
- this.instProxyPort = -1;
- this.usingProxy = false;
- this.keepingAlive = false;
- this.keepAliveConnections = -1;
- this.keepAliveTimeout = 0;
- 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 {
- SecurityManager var2 = System.getSecurityManager();
- if (var2 != null) {
- var2.checkConnect(var0.getHost(), var0.getPort());
- }
-
- 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()));
- super.serverSocket.setTcpNoDelay(true);
- }
-
- private synchronized void privilegedOpenServer(String var1, int var2) throws IOException {
- try {
- AccessController.doPrivileged(new 3(this, var1, var2));
- } catch (PrivilegedActionException var4) {
- throw (IOException)var4.getException();
- }
- }
-
- private void superOpenServer(String var1, int var2) throws IOException, UnknownHostException {
- super.openServer(var1, var2);
- }
-
- private synchronized void privilegedSuperOpenServer(String var1, int var2) throws IOException {
- try {
- AccessController.doPrivileged(new 4(this, var1, var2));
- } catch (PrivilegedActionException var4) {
- throw (IOException)var4.getException();
- }
- }
-
- private boolean isLoopback(String var1) {
- if (var1 != null && var1.length() != 0) {
- if (var1.equalsIgnoreCase("localhost")) {
- return true;
- } else if (!Character.isDigit(var1.charAt(0))) {
- return false;
- } else {
- boolean var2 = true;
- int var3 = 0;
- char[] var4 = var1.toCharArray();
- int var5 = 0;
-
- label64:
- while(var5 < var4.length) {
- char var6 = var4[var5];
- if (var6 >= '0' && var6 <= '9') {
- int var7 = 0;
-
- while(true) {
- if (var6 != '.') {
- if (var6 < '0' || var6 > '9') {
- return false;
- }
-
- var7 = var7 * 10 + var6 - 48;
- ++var5;
- if (var5 < var4.length) {
- var6 = var4[var5];
- continue;
- }
- }
-
- if (var7 > 255) {
- return false;
- }
-
- if (var2) {
- var2 = false;
- if (var7 != 127) {
- return false;
- }
- }
-
- ++var3;
- ++var5;
- continue label64;
- }
- }
-
- return false;
- }
-
- if (var3 == 4 && !var1.endsWith(".")) {
- return true;
- } else {
- return false;
- }
- }
- } else {
- return false;
- }
- }
-
- private synchronized void openServer() throws IOException {
- SecurityManager var1 = System.getSecurityManager();
- if (var1 != null) {
- var1.checkConnect(this.host, this.port);
- }
-
- if (!this.keepingAlive) {
- RegexpPool var2 = getDontProxy();
- String var3 = this.url.getHost().toLowerCase();
- boolean var4 = this.isLoopback(var3);
- if (this.url.getProtocol().equals("http")) {
- if (this.instProxy != null && !var4) {
- this.privilegedOpenServer(this.instProxy, this.instProxyPort);
- this.usingProxy = true;
- } else {
- String var9 = this.getProxyHost();
- if (var9 != null && !this.proxyDisabled && !var4 && var2.match(var3) == null && var2.match(this.host) == null) {
- try {
- int var10 = this.getProxyPort();
- this.privilegedOpenServer(var9, var10);
- this.instProxy = var9;
- this.instProxyPort = var10;
- this.usingProxy = true;
- return;
- } catch (IOException var7) {
- }
- }
-
- this.openServer(this.host, this.port);
- this.usingProxy = false;
- }
- } else if (this.instProxy != null && !var4) {
- this.privilegedSuperOpenServer(this.instProxy, this.instProxyPort);
- this.usingProxy = true;
- } else {
- String var5 = this.getProxyHost();
- if (var5 != null && !this.proxyDisabled && !var4 && var2.match(var3) == null && var2.match(this.host) == null) {
- try {
- int var6 = this.getProxyPort();
- this.privilegedSuperOpenServer(var5, var6);
- this.instProxy = var5;
- this.instProxyPort = var6;
- this.usingProxy = true;
- return;
- } catch (IOException var8) {
- }
- }
-
- super.openServer(this.host, this.port);
- this.usingProxy = false;
- }
- }
- }
-
- public String getURLFile() {
- String var1 = this.url.getFile();
- if (var1 == null || var1.length() == 0) {
- var1 = "/";
- }
-
- if (this.usingProxy) {
- String var2 = this.url.getProtocol() + "://" + this.url.getHost();
- if (this.url.getPort() != -1) {
- var2 = var2 + ":" + this.url.getPort();
- }
-
- return var2 + var1;
- } else {
- return var1;
- }
- }
-
- 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 {
- try {
- super.serverInput = super.serverSocket.getInputStream();
- super.serverInput = new BufferedInputStream(super.serverInput);
- super.serverInput = new PushbackInputStream(super.serverInput, 8);
- return this.parseHTTPHeader(var1, var2);
- } catch (IOException var4) {
- this.closeServer();
- if (!this.failedOnce && this.requests != null) {
- this.failedOnce = true;
- this.openServer();
- this.writeRequests(this.requests);
- return this.parseHTTP(var1, var2);
- } else {
- throw var4;
- }
- }
- }
-
- private boolean parseHTTPHeader(MessageHeader var1, ProgressEntry var2) throws IOException {
- this.keepAliveConnections = -1;
- this.keepAliveTimeout = 0;
- boolean var3 = false;
- byte[] var4 = new byte[8];
-
- try {
- int var5;
- int var6;
- for(var5 = 0; var5 < 8; var5 += var6) {
- var6 = super.serverInput.read(var4, var5, 8 - var5);
- if (var6 < 0) {
- break;
- }
- }
-
- String var15 = 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);
- if (this.usingProxy) {
- var15 = var1.findValue("Proxy-Connection");
- }
-
- if (var15 == null) {
- var15 = var1.findValue("Connection");
- }
-
- if (var15 != null && var15.toLowerCase().equals("keep-alive")) {
- HeaderParser var7 = new HeaderParser(var1.findValue("Keep-Alive"));
- if (var7 != null) {
- this.keepAliveConnections = var7.findInt("max", 5);
- this.keepAliveTimeout = var7.findInt("timeout", 5);
- }
- } else if (var4[7] != 48) {
- if (var15 != null) {
- this.keepAliveConnections = 1;
- } else {
- this.keepAliveConnections = 2;
- }
- }
- } else {
- if (var5 != 8) {
- 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 var12) {
- throw var12;
- }
-
- int var14 = -1;
-
- try {
- String var16 = var1.getValue(0);
-
- int var18;
- for(var18 = var16.indexOf(32); var16.charAt(var18) == ' '; ++var18) {
- }
-
- var14 = Integer.parseInt(var16.substring(var18, var18 + 3));
- } catch (Exception var11) {
- }
-
- if (var14 == 100) {
- return this.parseHTTPHeader(var1, var2);
- } else {
- String var17 = null;
-
- try {
- var17 = var1.findValue("Transfer-Encoding");
- } catch (Exception var10) {
- }
-
- if (var17 != null && var17.equalsIgnoreCase("chunked")) {
- InputStream var19 = super.serverInput;
- super.serverInput = new ChunkedInputStream(super.serverInput);
- var1.set("content-length", Integer.toString(super.serverInput.available()));
- var1.mergeHeader(var19);
- }
-
- int var20 = -1;
-
- try {
- var20 = Integer.parseInt(var1.findValue("content-length"));
- } catch (Exception var9) {
- }
-
- if (this.keepAliveConnections > 1 && var20 > 0) {
- this.keepingAlive = true;
- } else if (this.keepingAlive) {
- this.keepingAlive = false;
- }
-
- if (var20 > 0) {
- var2.setType(this.url.getFile(), var1.findValue("content-type"));
- var2.update(0, var20);
- if (this.keepingAlive) {
- this.kas = new KeepAliveStream(super.serverInput, var2, this);
- super.serverInput = this.kas;
- this.failedOnce = false;
- } else {
- super.serverInput = new MeteredStream(super.serverInput, var2);
- }
- } else {
- 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 this.getHttpKeepAliveSet() && this.keepingAlive;
- }
-
- protected void finalize() throws Throwable {
- }
-
- public void closeServer() {
- try {
- this.keepingAlive = false;
- super.serverSocket.close();
- } catch (Exception var2) {
- }
-
- }
-
- public String getProxyHostUsed() {
- return !this.usingProxy ? null : this.instProxy;
- }
-
- public int getProxyPortUsed() {
- return this.instProxyPort;
- }
-
- // $FF: synthetic method
- static void access$000(HttpClient var0, String var1, int var2) throws IOException, UnknownHostException {
- var0.superOpenServer(var1, var2);
- }
- }
-