home *** CD-ROM | disk | FTP | other *** search
- package sun.net.www.protocol.http;
-
- import java.io.ByteArrayOutputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.net.ProtocolException;
- import java.net.URL;
- import sun.net.ProgressData;
- import sun.net.ProgressEntry;
- import sun.net.www.HeaderParser;
- import sun.net.www.MessageHeader;
- import sun.net.www.http.HttpClient;
-
- public class HttpURLConnection extends java.net.HttpURLConnection {
- static final String version = System.getProperty("java.version");
- public static final String userAgent;
- static final String httpVersion = "HTTP/1.0";
- static final String acceptString = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
- static final int maxRedirects = 5;
- protected HttpClient http;
- protected Handler handler;
- // $FF: renamed from: ps java.io.PrintStream
- protected PrintStream field_0;
- private static HttpAuthenticator defaultAuth;
- private MessageHeader requests = new MessageHeader();
- // $FF: renamed from: pe sun.net.ProgressEntry
- protected ProgressEntry field_1;
- private MessageHeader responses = new MessageHeader();
- private InputStream inputStream;
- private ByteArrayOutputStream poster;
- private boolean setRequests = false;
- private boolean failedOnce = false;
- private boolean shouldFollowRedirects = true;
-
- private void writeRequests() throws IOException {
- if (!this.setRequests) {
- this.requests.prepend(super.method + " " + this.http.getURLFile() + " " + "HTTP/1.0", (String)null);
- this.requests.setIfNotSet("User-Agent", userAgent);
- int var1 = super.url.getPort();
- String var2 = super.url.getHost();
- if (var1 != -1 && var1 != 80) {
- var2 = var2 + ":" + var1;
- }
-
- this.requests.setIfNotSet("Host", var2);
- this.requests.setIfNotSet("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
- if (!this.failedOnce && HttpClient.httpKeepAliveSet) {
- if (this.http.usingProxy && !super.method.equals("POST") && !super.method.equals("PUT")) {
- this.requests.set("Proxy-Connection", "keep-alive");
- } else if (!this.http.usingProxy) {
- this.requests.set("Connection", "keep-alive");
- }
- }
-
- AuthenticationInfo var3 = AuthenticationInfo.getAuth(super.url);
- if (var3 != null) {
- this.requests.setIfNotSet("Authorization", var3.getAuthString());
- }
-
- if (this.poster != null) {
- ByteArrayOutputStream var4 = this.poster;
- synchronized(var4){}
-
- try {
- this.requests.setIfNotSet("Content-type", "application/x-www-form-urlencoded");
- this.requests.set("Content-length", String.valueOf(this.poster.size()));
- } catch (Throwable var6) {
- throw var6;
- }
- }
-
- this.setRequests = true;
- }
-
- this.http.writeRequests(this.requests);
- if (this.poster != null) {
- this.poster.writeTo(this.field_0);
- this.field_0.flush();
- }
-
- if (this.field_0.checkError()) {
- this.disconnect();
- if (this.failedOnce) {
- throw new IOException("Error writing to server");
- }
-
- this.failedOnce = true;
- this.http = this.getNewClient(super.url);
- this.field_0 = (PrintStream)this.http.getOutputStream();
- super.connected = true;
- this.responses = new MessageHeader();
- this.requests = new MessageHeader();
- this.setRequests = false;
- this.writeRequests();
- }
-
- }
-
- protected HttpClient getNewClient(URL var1) throws IOException {
- return new HttpClient(var1, (String)null, -1);
- }
-
- protected HttpClient getProxiedClient(URL var1, String var2, int var3) throws IOException {
- return new HttpClient(var1, var2, var3);
- }
-
- protected HttpURLConnection(URL var1, Handler var2) throws IOException {
- super(var1);
- this.handler = var2;
- }
-
- public HttpURLConnection(URL var1, String var2, int var3) {
- super(var1);
- this.handler = new Handler(var2, var3);
- }
-
- public static void setDefaultAuthenticator(HttpAuthenticator var0) {
- defaultAuth = var0;
- }
-
- public void connect() throws IOException {
- if (!super.connected) {
- if ("http".equals(super.url.getProtocol()) && !this.failedOnce) {
- this.http = HttpClient.New(super.url);
- } else {
- this.http = new HttpClient(super.url, this.handler.proxy, this.handler.proxyPort);
- }
-
- this.field_0 = (PrintStream)this.http.getOutputStream();
- super.connected = true;
- }
- }
-
- public synchronized OutputStream getOutputStream() throws IOException {
- try {
- if (!super.doOutput) {
- throw new ProtocolException("cannot write to a URLConnection if doOutput=false - call setDoOutput(true)");
- } else {
- if (super.method.equals("GET")) {
- ((java.net.HttpURLConnection)this).setRequestMethod("POST");
- }
-
- if (!"POST".equals(super.method) && !"PUT".equals(super.method) && "http".equals(super.url.getProtocol())) {
- throw new ProtocolException("HTTP method " + super.method + " doesn't support output");
- } else if (this.inputStream != null) {
- throw new ProtocolException("Cannot write output after reading input.");
- } else {
- this.connect();
- this.field_0 = (PrintStream)this.http.getOutputStream();
- if (this.poster == null) {
- this.poster = new ByteArrayOutputStream();
- }
-
- return this.poster;
- }
- }
- } catch (RuntimeException var2) {
- this.disconnect();
- throw var2;
- } catch (IOException var3) {
- this.disconnect();
- throw var3;
- }
- }
-
- public synchronized InputStream getInputStream() throws IOException {
- if (!super.doInput) {
- throw new ProtocolException("Cannot read from URLConnection if doInput=false (call setDoInput(true))");
- } else if (this.inputStream != null) {
- return this.inputStream;
- } else {
- int var1 = 0;
- AuthenticationInfo var2 = null;
-
- try {
- int var3;
- while(true) {
- this.field_1 = new ProgressEntry(super.url.getFile(), (String)null);
- ProgressData.pdata.register(this.field_1);
- this.connect();
- this.field_0 = (PrintStream)this.http.getOutputStream();
- this.writeRequests();
- this.http.parseHTTP(this.responses, this.field_1);
- this.inputStream = this.http.getInputStream();
- if (this.followRedirect()) {
- ++var1;
- } else {
- if (super.method.equals("HEAD") || super.method.equals("TRACE")) {
- this.disconnect();
- return this.inputStream = new EmptyInputStream();
- }
-
- var3 = ((java.net.HttpURLConnection)this).getResponseCode();
- if (var3 != 401) {
- break;
- }
-
- if (var2 != null) {
- AuthenticationInfo.uncacheInfo(var2);
- break;
- }
-
- if ((var2 = this.getAuthInfo()) == null) {
- break;
- }
-
- this.requests.add("Authorization", var2.getAuthString());
- this.disconnect();
- ++var1;
- }
-
- if (var1 >= 5) {
- throw new ProtocolException("Server redirected too many times (" + var1 + ")");
- }
- }
-
- if (var2 != null && var3 == 200) {
- AuthenticationInfo.cacheInfo(var2, super.url);
- }
-
- String var4 = super.url.getFile();
- if (var3 >= 400 && var4 != null && !var4.endsWith(".html") && !var4.endsWith(".htm") && !var4.endsWith("/") && !var4.endsWith(".txt")) {
- throw new FileNotFoundException(super.url.toString());
- } else {
- return this.inputStream;
- }
- } catch (RuntimeException var5) {
- this.disconnect();
- throw var5;
- } catch (IOException var6) {
- this.disconnect();
- throw var6;
- }
- }
- }
-
- private AuthenticationInfo getAuthInfo() {
- if (defaultAuth == null) {
- return null;
- } else {
- AuthenticationInfo var1 = null;
- String var2 = this.getHeaderField("WWW-Authenticate");
- if (var2 != null) {
- HeaderParser var3 = new HeaderParser(var2);
- String var4 = var3.findValue("realm");
- String var5 = var3.findKey(0);
- var1 = AuthenticationInfo.getAuth(super.url, var4);
- if (var1 == null && defaultAuth.schemeSupported(var5)) {
- String var6 = defaultAuth.authString(super.url, var5, var4);
- if (var6 != null) {
- var1 = new AuthenticationInfo(super.url, var4, var6);
- }
- }
- }
-
- return var1;
- }
- }
-
- private boolean followRedirect() throws IOException {
- if (!this.shouldFollowRedirects) {
- return false;
- } else if (!java.net.HttpURLConnection.getFollowRedirects()) {
- return false;
- } else {
- int var1 = ((java.net.HttpURLConnection)this).getResponseCode();
- if (var1 >= 300 && var1 <= 305 && var1 != 304) {
- String var2 = this.getHeaderField("Location");
- if (var2 == null) {
- return false;
- } else {
- this.disconnect();
- this.responses = new MessageHeader();
- if (var1 == 305) {
- URL var3 = new URL(var2);
- this.http = this.getProxiedClient(super.url, var3.getHost(), var3.getPort());
- this.requests.set(0, super.method + " " + this.http.getURLFile() + " " + "HTTP/1.0", (String)null);
- super.connected = true;
- } else {
- super.url = new URL(super.url, var2);
- if (super.method.equals("POST") && !Boolean.getBoolean("http.strictPostRedirect")) {
- this.requests = new MessageHeader();
- this.setRequests = false;
- ((java.net.HttpURLConnection)this).setRequestMethod("GET");
- this.poster = null;
- this.connect();
- } else {
- this.connect();
- this.requests.set(0, super.method + " " + this.http.getURLFile() + " " + "HTTP/1.0", (String)null);
- this.requests.set("Host", super.url.getHost() + (super.url.getPort() != -1 && super.url.getPort() != 80 ? ":" + String.valueOf(super.url.getPort()) : ""));
- }
- }
-
- return true;
- }
- } else {
- return false;
- }
- }
- }
-
- public void disconnect() {
- super.responseCode = -1;
- if (this.field_1 != null) {
- ProgressData.pdata.unregister(this.field_1);
- this.field_1 = null;
- }
-
- if (this.http != null) {
- this.http.closeServer();
- this.http = null;
- super.connected = false;
- }
-
- }
-
- public boolean usingProxy() {
- return this.http != null ? this.http.usingProxy : false;
- }
-
- public String getHeaderField(String var1) {
- try {
- this.getInputStream();
- } catch (IOException var2) {
- }
-
- return this.responses.findValue(var1);
- }
-
- public String getHeaderField(int var1) {
- try {
- this.getInputStream();
- } catch (IOException var2) {
- }
-
- return this.responses.getValue(var1);
- }
-
- public String getHeaderFieldKey(int var1) {
- try {
- this.getInputStream();
- } catch (IOException var2) {
- }
-
- return this.responses.getKey(var1);
- }
-
- public void setRequestProperty(String var1, String var2) {
- if (super.connected) {
- throw new IllegalAccessError("Already connected");
- } else if ("set-cookie".equalsIgnoreCase(var1)) {
- this.requests.add(var1, var2);
- } else {
- this.requests.set(var1, var2);
- }
- }
-
- public String getRequestProperty(String var1) {
- return this.requests.findValue(var1);
- }
-
- public void finalize() {
- }
-
- public void setInstanceFollowRedirects(boolean var1) {
- this.shouldFollowRedirects = var1;
- }
-
- static {
- userAgent = System.getProperty("http.agent", "Java" + version);
- }
- }
-