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 / protocol / http / HttpURLConnection.class (.txt) < prev   
Encoding:
Java Class File  |  1997-07-08  |  9.3 KB  |  376 lines

  1. package sun.net.www.protocol.http;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.PrintStream;
  9. import java.net.ProtocolException;
  10. import java.net.URL;
  11. import sun.net.ProgressData;
  12. import sun.net.ProgressEntry;
  13. import sun.net.www.HeaderParser;
  14. import sun.net.www.MessageHeader;
  15. import sun.net.www.http.HttpClient;
  16.  
  17. public class HttpURLConnection extends java.net.HttpURLConnection {
  18.    static final String version = System.getProperty("java.version");
  19.    public static final String userAgent;
  20.    static final String httpVersion = "HTTP/1.0";
  21.    static final String acceptString = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
  22.    static final int maxRedirects = 5;
  23.    protected HttpClient http;
  24.    protected Handler handler;
  25.    // $FF: renamed from: ps java.io.PrintStream
  26.    protected PrintStream field_0;
  27.    private static HttpAuthenticator defaultAuth;
  28.    private MessageHeader requests = new MessageHeader();
  29.    // $FF: renamed from: pe sun.net.ProgressEntry
  30.    protected ProgressEntry field_1;
  31.    private MessageHeader responses = new MessageHeader();
  32.    private InputStream inputStream;
  33.    private ByteArrayOutputStream poster;
  34.    private boolean setRequests = false;
  35.    private boolean failedOnce = false;
  36.    private boolean shouldFollowRedirects = true;
  37.  
  38.    private void writeRequests() throws IOException {
  39.       if (!this.setRequests) {
  40.          this.requests.prepend(super.method + " " + this.http.getURLFile() + " " + "HTTP/1.0", (String)null);
  41.          this.requests.setIfNotSet("User-Agent", userAgent);
  42.          int var1 = super.url.getPort();
  43.          String var2 = super.url.getHost();
  44.          if (var1 != -1 && var1 != 80) {
  45.             var2 = var2 + ":" + var1;
  46.          }
  47.  
  48.          this.requests.setIfNotSet("Host", var2);
  49.          this.requests.setIfNotSet("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
  50.          if (!this.failedOnce && HttpClient.httpKeepAliveSet) {
  51.             if (this.http.usingProxy && !super.method.equals("POST") && !super.method.equals("PUT")) {
  52.                this.requests.set("Proxy-Connection", "keep-alive");
  53.             } else if (!this.http.usingProxy) {
  54.                this.requests.set("Connection", "keep-alive");
  55.             }
  56.          }
  57.  
  58.          AuthenticationInfo var3 = AuthenticationInfo.getAuth(super.url);
  59.          if (var3 != null) {
  60.             this.requests.setIfNotSet("Authorization", var3.getAuthString());
  61.          }
  62.  
  63.          if (this.poster != null) {
  64.             ByteArrayOutputStream var4 = this.poster;
  65.             synchronized(var4){}
  66.  
  67.             try {
  68.                this.requests.setIfNotSet("Content-type", "application/x-www-form-urlencoded");
  69.                this.requests.set("Content-length", String.valueOf(this.poster.size()));
  70.             } catch (Throwable var6) {
  71.                throw var6;
  72.             }
  73.          }
  74.  
  75.          this.setRequests = true;
  76.       }
  77.  
  78.       this.http.writeRequests(this.requests);
  79.       if (this.poster != null) {
  80.          this.poster.writeTo(this.field_0);
  81.          this.field_0.flush();
  82.       }
  83.  
  84.       if (this.field_0.checkError()) {
  85.          this.disconnect();
  86.          if (this.failedOnce) {
  87.             throw new IOException("Error writing to server");
  88.          }
  89.  
  90.          this.failedOnce = true;
  91.          this.http = this.getNewClient(super.url);
  92.          this.field_0 = (PrintStream)this.http.getOutputStream();
  93.          super.connected = true;
  94.          this.responses = new MessageHeader();
  95.          this.requests = new MessageHeader();
  96.          this.setRequests = false;
  97.          this.writeRequests();
  98.       }
  99.  
  100.    }
  101.  
  102.    protected HttpClient getNewClient(URL var1) throws IOException {
  103.       return new HttpClient(var1, (String)null, -1);
  104.    }
  105.  
  106.    protected HttpClient getProxiedClient(URL var1, String var2, int var3) throws IOException {
  107.       return new HttpClient(var1, var2, var3);
  108.    }
  109.  
  110.    protected HttpURLConnection(URL var1, Handler var2) throws IOException {
  111.       super(var1);
  112.       this.handler = var2;
  113.    }
  114.  
  115.    public HttpURLConnection(URL var1, String var2, int var3) {
  116.       super(var1);
  117.       this.handler = new Handler(var2, var3);
  118.    }
  119.  
  120.    public static void setDefaultAuthenticator(HttpAuthenticator var0) {
  121.       defaultAuth = var0;
  122.    }
  123.  
  124.    public void connect() throws IOException {
  125.       if (!super.connected) {
  126.          if ("http".equals(super.url.getProtocol()) && !this.failedOnce) {
  127.             this.http = HttpClient.New(super.url);
  128.          } else {
  129.             this.http = new HttpClient(super.url, this.handler.proxy, this.handler.proxyPort);
  130.          }
  131.  
  132.          this.field_0 = (PrintStream)this.http.getOutputStream();
  133.          super.connected = true;
  134.       }
  135.    }
  136.  
  137.    public synchronized OutputStream getOutputStream() throws IOException {
  138.       try {
  139.          if (!super.doOutput) {
  140.             throw new ProtocolException("cannot write to a URLConnection if doOutput=false - call setDoOutput(true)");
  141.          } else {
  142.             if (super.method.equals("GET")) {
  143.                ((java.net.HttpURLConnection)this).setRequestMethod("POST");
  144.             }
  145.  
  146.             if (!"POST".equals(super.method) && !"PUT".equals(super.method) && "http".equals(super.url.getProtocol())) {
  147.                throw new ProtocolException("HTTP method " + super.method + " doesn't support output");
  148.             } else if (this.inputStream != null) {
  149.                throw new ProtocolException("Cannot write output after reading input.");
  150.             } else {
  151.                this.connect();
  152.                this.field_0 = (PrintStream)this.http.getOutputStream();
  153.                if (this.poster == null) {
  154.                   this.poster = new ByteArrayOutputStream();
  155.                }
  156.  
  157.                return this.poster;
  158.             }
  159.          }
  160.       } catch (RuntimeException var2) {
  161.          this.disconnect();
  162.          throw var2;
  163.       } catch (IOException var3) {
  164.          this.disconnect();
  165.          throw var3;
  166.       }
  167.    }
  168.  
  169.    public synchronized InputStream getInputStream() throws IOException {
  170.       if (!super.doInput) {
  171.          throw new ProtocolException("Cannot read from URLConnection if doInput=false (call setDoInput(true))");
  172.       } else if (this.inputStream != null) {
  173.          return this.inputStream;
  174.       } else {
  175.          int var1 = 0;
  176.          AuthenticationInfo var2 = null;
  177.  
  178.          try {
  179.             int var3;
  180.             while(true) {
  181.                this.field_1 = new ProgressEntry(super.url.getFile(), (String)null);
  182.                ProgressData.pdata.register(this.field_1);
  183.                this.connect();
  184.                this.field_0 = (PrintStream)this.http.getOutputStream();
  185.                this.writeRequests();
  186.                this.http.parseHTTP(this.responses, this.field_1);
  187.                this.inputStream = this.http.getInputStream();
  188.                if (this.followRedirect()) {
  189.                   ++var1;
  190.                } else {
  191.                   if (super.method.equals("HEAD") || super.method.equals("TRACE")) {
  192.                      this.disconnect();
  193.                      return this.inputStream = new EmptyInputStream();
  194.                   }
  195.  
  196.                   var3 = ((java.net.HttpURLConnection)this).getResponseCode();
  197.                   if (var3 != 401) {
  198.                      break;
  199.                   }
  200.  
  201.                   if (var2 != null) {
  202.                      AuthenticationInfo.uncacheInfo(var2);
  203.                      break;
  204.                   }
  205.  
  206.                   if ((var2 = this.getAuthInfo()) == null) {
  207.                      break;
  208.                   }
  209.  
  210.                   this.requests.add("Authorization", var2.getAuthString());
  211.                   this.disconnect();
  212.                   ++var1;
  213.                }
  214.  
  215.                if (var1 >= 5) {
  216.                   throw new ProtocolException("Server redirected too many times (" + var1 + ")");
  217.                }
  218.             }
  219.  
  220.             if (var2 != null && var3 == 200) {
  221.                AuthenticationInfo.cacheInfo(var2, super.url);
  222.             }
  223.  
  224.             String var4 = super.url.getFile();
  225.             if (var3 >= 400 && var4 != null && !var4.endsWith(".html") && !var4.endsWith(".htm") && !var4.endsWith("/") && !var4.endsWith(".txt")) {
  226.                throw new FileNotFoundException(super.url.toString());
  227.             } else {
  228.                return this.inputStream;
  229.             }
  230.          } catch (RuntimeException var5) {
  231.             this.disconnect();
  232.             throw var5;
  233.          } catch (IOException var6) {
  234.             this.disconnect();
  235.             throw var6;
  236.          }
  237.       }
  238.    }
  239.  
  240.    private AuthenticationInfo getAuthInfo() {
  241.       if (defaultAuth == null) {
  242.          return null;
  243.       } else {
  244.          AuthenticationInfo var1 = null;
  245.          String var2 = this.getHeaderField("WWW-Authenticate");
  246.          if (var2 != null) {
  247.             HeaderParser var3 = new HeaderParser(var2);
  248.             String var4 = var3.findValue("realm");
  249.             String var5 = var3.findKey(0);
  250.             var1 = AuthenticationInfo.getAuth(super.url, var4);
  251.             if (var1 == null && defaultAuth.schemeSupported(var5)) {
  252.                String var6 = defaultAuth.authString(super.url, var5, var4);
  253.                if (var6 != null) {
  254.                   var1 = new AuthenticationInfo(super.url, var4, var6);
  255.                }
  256.             }
  257.          }
  258.  
  259.          return var1;
  260.       }
  261.    }
  262.  
  263.    private boolean followRedirect() throws IOException {
  264.       if (!this.shouldFollowRedirects) {
  265.          return false;
  266.       } else if (!java.net.HttpURLConnection.getFollowRedirects()) {
  267.          return false;
  268.       } else {
  269.          int var1 = ((java.net.HttpURLConnection)this).getResponseCode();
  270.          if (var1 >= 300 && var1 <= 305 && var1 != 304) {
  271.             String var2 = this.getHeaderField("Location");
  272.             if (var2 == null) {
  273.                return false;
  274.             } else {
  275.                this.disconnect();
  276.                this.responses = new MessageHeader();
  277.                if (var1 == 305) {
  278.                   URL var3 = new URL(var2);
  279.                   this.http = this.getProxiedClient(super.url, var3.getHost(), var3.getPort());
  280.                   this.requests.set(0, super.method + " " + this.http.getURLFile() + " " + "HTTP/1.0", (String)null);
  281.                   super.connected = true;
  282.                } else {
  283.                   super.url = new URL(super.url, var2);
  284.                   if (super.method.equals("POST") && !Boolean.getBoolean("http.strictPostRedirect")) {
  285.                      this.requests = new MessageHeader();
  286.                      this.setRequests = false;
  287.                      ((java.net.HttpURLConnection)this).setRequestMethod("GET");
  288.                      this.poster = null;
  289.                      this.connect();
  290.                   } else {
  291.                      this.connect();
  292.                      this.requests.set(0, super.method + " " + this.http.getURLFile() + " " + "HTTP/1.0", (String)null);
  293.                      this.requests.set("Host", super.url.getHost() + (super.url.getPort() != -1 && super.url.getPort() != 80 ? ":" + String.valueOf(super.url.getPort()) : ""));
  294.                   }
  295.                }
  296.  
  297.                return true;
  298.             }
  299.          } else {
  300.             return false;
  301.          }
  302.       }
  303.    }
  304.  
  305.    public void disconnect() {
  306.       super.responseCode = -1;
  307.       if (this.field_1 != null) {
  308.          ProgressData.pdata.unregister(this.field_1);
  309.          this.field_1 = null;
  310.       }
  311.  
  312.       if (this.http != null) {
  313.          this.http.closeServer();
  314.          this.http = null;
  315.          super.connected = false;
  316.       }
  317.  
  318.    }
  319.  
  320.    public boolean usingProxy() {
  321.       return this.http != null ? this.http.usingProxy : false;
  322.    }
  323.  
  324.    public String getHeaderField(String var1) {
  325.       try {
  326.          this.getInputStream();
  327.       } catch (IOException var2) {
  328.       }
  329.  
  330.       return this.responses.findValue(var1);
  331.    }
  332.  
  333.    public String getHeaderField(int var1) {
  334.       try {
  335.          this.getInputStream();
  336.       } catch (IOException var2) {
  337.       }
  338.  
  339.       return this.responses.getValue(var1);
  340.    }
  341.  
  342.    public String getHeaderFieldKey(int var1) {
  343.       try {
  344.          this.getInputStream();
  345.       } catch (IOException var2) {
  346.       }
  347.  
  348.       return this.responses.getKey(var1);
  349.    }
  350.  
  351.    public void setRequestProperty(String var1, String var2) {
  352.       if (super.connected) {
  353.          throw new IllegalAccessError("Already connected");
  354.       } else if ("set-cookie".equalsIgnoreCase(var1)) {
  355.          this.requests.add(var1, var2);
  356.       } else {
  357.          this.requests.set(var1, var2);
  358.       }
  359.    }
  360.  
  361.    public String getRequestProperty(String var1) {
  362.       return this.requests.findValue(var1);
  363.    }
  364.  
  365.    public void finalize() {
  366.    }
  367.  
  368.    public void setInstanceFollowRedirects(boolean var1) {
  369.       this.shouldFollowRedirects = var1;
  370.    }
  371.  
  372.    static {
  373.       userAgent = System.getProperty("http.agent", "Java" + version);
  374.    }
  375. }
  376.