home *** CD-ROM | disk | FTP | other *** search
- package sun.net.www.protocol.http;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.net.URL;
- import sun.net.ProgressData;
- import sun.net.www.MessageHeader;
- import sun.net.www.MeteredStream;
- import sun.net.www.URLConnection;
- import sun.net.www.http.AuthenticationInfo;
- import sun.net.www.http.HttpClient;
-
- public class HttpURLConnection extends URLConnection {
- static final String EOL = "\r\n";
- static final String version = System.getProperty("java.version");
- public static final String userAgent;
- static final String acceptString = "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n";
- static final int maxRedirections = 5;
- HttpClient http;
- Handler handler;
- InputStream inputStream;
- boolean outputStreamOpen = false;
-
- HttpURLConnection(URL var1, Handler var2) {
- super(var1);
- this.handler = var2;
- }
-
- public HttpURLConnection(URL var1, String var2, int var3) {
- super(var1);
- this.handler = new Handler(var2, var3);
- }
-
- public void connect() throws IOException {
- if (!super.connected) {
- super.connected = true;
- ProgressData.pdata.register(super.url);
- this.http = null;
-
- try {
- this.http = new HttpClient(super.url, this.handler.proxy, this.handler.proxyPort);
- } catch (Throwable var2) {
- if (this.http != null) {
- this.http.closeServer();
- }
-
- ProgressData.pdata.unregister(super.url);
- if (var2 instanceof SecurityException) {
- throw (SecurityException)var2;
- }
-
- throw var2 instanceof IOException ? (IOException)var2 : new IOException(var2.toString());
- }
-
- if (this.http == null) {
- throw new IOException("Couldn't connect to " + super.url.toExternalForm());
- }
- }
- }
-
- private static AuthenticationInfo getAuthentication(URL var0, String var1, String var2) {
- return null;
- }
-
- public OutputStream getOutputStream() throws IOException {
- String var1 = ((java.net.URLConnection)this).getRequestProperty("content-type");
- this.connect();
- if (this.inputStream != null) {
- throw new IOException("Cannot write output after reading input.");
- } else if (this.outputStreamOpen) {
- throw new IOException("Cannot open output twice.");
- } else {
- OutputStream var2 = this.http.getOutputStream();
- this.outputStreamOpen = true;
- PrintStream var3 = new PrintStream(var2);
- String var4 = "POST " + this.http.getURLFile(super.url) + " HTTP/1.0" + "\r\n" + userAgent + "Referer: " + super.url + "\r\n" + "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n";
- if (var1 == null) {
- var1 = "application/x-www-form-urlencoded";
- }
-
- var4 = var4 + "Content-type: " + var1 + "\r\n";
- var3.print(var4);
- return new HttpPostBufferStream(var2);
- }
- }
-
- public InputStream getInputStream() throws IOException {
- if (this.inputStream != null) {
- return this.inputStream;
- } else {
- int var2 = 0;
-
- while(true) {
- this.connect();
- if (!this.outputStreamOpen) {
- String var3 = "";
- String var4 = "GET " + this.http.getURLFile(super.url) + " HTTP/1.0" + "\r\n" + userAgent + var3 + "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n" + "\r\n";
- PrintStream var5 = (PrintStream)this.http.getOutputStream();
- var5.print(var4);
- var5.flush();
- }
-
- try {
- this.http.processRequest(super.url.getFile());
- Object var1 = this.http.getInputStream();
- String var7 = this.http.getHeaderField("location");
- if (var7 != null) {
- if (this.outputStreamOpen) {
- this.http.closeServer();
- super.connected = false;
- this.http = null;
- throw new IOException("Cannot redirect with POST: " + super.url);
- }
-
- ++var2;
- if (var2 > 5) {
- throw new IOException("Too many redirections (" + var2 + "): " + super.url);
- }
-
- ProgressData.pdata.unregister(super.url);
- URL var8 = new URL(super.url, var7);
- super.url = var8;
- if (!"http".equals(super.url.getProtocol())) {
- this.http.closeServer();
- super.connected = false;
- this.http = null;
- throw new IOException("Cannot redirect to other protocol: " + super.url);
- }
-
- ProgressData.pdata.register(super.url);
- }
-
- if (this.http.getStatus() == 200 || var7 == null) {
- ((URLConnection)this).setProperties(this.http.getMimeHeader());
- if (((URLConnection)this).getProperties() == null) {
- ((URLConnection)this).setProperties(new MessageHeader());
- }
-
- String var9 = this.http.getHeaderField("content-length");
- int var10 = 0;
- if (var9 != null && (var10 = Integer.parseInt(var9)) != 0) {
- var1 = new MeteredStream((InputStream)var1, var10, super.url);
- } else {
- ProgressData.pdata.unregister(super.url);
- }
-
- this.inputStream = (InputStream)var1;
- return (InputStream)var1;
- }
-
- this.http.closeServer();
- ProgressData.pdata.unregister(super.url);
- this.http = null;
- this.inputStream = null;
- super.connected = false;
- } catch (IOException var6) {
- ProgressData.pdata.unregister(super.url);
- throw var6;
- }
- }
- }
- }
-
- public String getHeaderField(String var1) {
- if (this.http == null) {
- try {
- this.getInputStream();
- } catch (Exception var2) {
- }
- }
-
- return this.http.getHeaderField(var1);
- }
-
- static {
- userAgent = "User-Agent: " + System.getProperty("http.agent", "Java" + version) + "\r\n";
- }
- }
-