home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / ie4 / IE4 / JAVI386.CAB / classr.exe / sun / net / www / protocol / http / HttpURLConnection.class (.txt) < prev   
Encoding:
Java Class File  |  1997-01-31  |  5.1 KB  |  181 lines

  1. package sun.net.www.protocol.http;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.io.PrintStream;
  7. import java.net.URL;
  8. import sun.net.ProgressData;
  9. import sun.net.www.MessageHeader;
  10. import sun.net.www.MeteredStream;
  11. import sun.net.www.URLConnection;
  12. import sun.net.www.http.AuthenticationInfo;
  13. import sun.net.www.http.HttpClient;
  14.  
  15. public class HttpURLConnection extends URLConnection {
  16.    static final String EOL = "\r\n";
  17.    static final String version = System.getProperty("java.version");
  18.    public static final String userAgent;
  19.    static final String acceptString = "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n";
  20.    static final int maxRedirections = 5;
  21.    HttpClient http;
  22.    Handler handler;
  23.    InputStream inputStream;
  24.    boolean outputStreamOpen = false;
  25.  
  26.    HttpURLConnection(URL var1, Handler var2) {
  27.       super(var1);
  28.       this.handler = var2;
  29.    }
  30.  
  31.    public HttpURLConnection(URL var1, String var2, int var3) {
  32.       super(var1);
  33.       this.handler = new Handler(var2, var3);
  34.    }
  35.  
  36.    public void connect() throws IOException {
  37.       if (!super.connected) {
  38.          super.connected = true;
  39.          ProgressData.pdata.register(super.url);
  40.          this.http = null;
  41.  
  42.          try {
  43.             this.http = new HttpClient(super.url, this.handler.proxy, this.handler.proxyPort);
  44.          } catch (Throwable var2) {
  45.             if (this.http != null) {
  46.                this.http.closeServer();
  47.             }
  48.  
  49.             ProgressData.pdata.unregister(super.url);
  50.             if (var2 instanceof SecurityException) {
  51.                throw (SecurityException)var2;
  52.             }
  53.  
  54.             throw var2 instanceof IOException ? (IOException)var2 : new IOException(var2.toString());
  55.          }
  56.  
  57.          if (this.http == null) {
  58.             throw new IOException("Couldn't connect to " + super.url.toExternalForm());
  59.          }
  60.       }
  61.    }
  62.  
  63.    private static AuthenticationInfo getAuthentication(URL var0, String var1, String var2) {
  64.       return null;
  65.    }
  66.  
  67.    public OutputStream getOutputStream() throws IOException {
  68.       String var1 = ((java.net.URLConnection)this).getRequestProperty("content-type");
  69.       this.connect();
  70.       if (this.inputStream != null) {
  71.          throw new IOException("Cannot write output after reading input.");
  72.       } else if (this.outputStreamOpen) {
  73.          throw new IOException("Cannot open output twice.");
  74.       } else {
  75.          OutputStream var2 = this.http.getOutputStream();
  76.          this.outputStreamOpen = true;
  77.          PrintStream var3 = new PrintStream(var2);
  78.          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";
  79.          if (var1 == null) {
  80.             var1 = "application/x-www-form-urlencoded";
  81.          }
  82.  
  83.          var4 = var4 + "Content-type: " + var1 + "\r\n";
  84.          var3.print(var4);
  85.          return new HttpPostBufferStream(var2);
  86.       }
  87.    }
  88.  
  89.    public InputStream getInputStream() throws IOException {
  90.       if (this.inputStream != null) {
  91.          return this.inputStream;
  92.       } else {
  93.          int var2 = 0;
  94.  
  95.          while(true) {
  96.             this.connect();
  97.             if (!this.outputStreamOpen) {
  98.                String var3 = "";
  99.                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";
  100.                PrintStream var5 = (PrintStream)this.http.getOutputStream();
  101.                var5.print(var4);
  102.                var5.flush();
  103.             }
  104.  
  105.             try {
  106.                this.http.processRequest(super.url.getFile());
  107.                Object var1 = this.http.getInputStream();
  108.                String var7 = this.http.getHeaderField("location");
  109.                if (var7 != null) {
  110.                   if (this.outputStreamOpen) {
  111.                      this.http.closeServer();
  112.                      super.connected = false;
  113.                      this.http = null;
  114.                      throw new IOException("Cannot redirect with POST: " + super.url);
  115.                   }
  116.  
  117.                   ++var2;
  118.                   if (var2 > 5) {
  119.                      throw new IOException("Too many redirections (" + var2 + "): " + super.url);
  120.                   }
  121.  
  122.                   ProgressData.pdata.unregister(super.url);
  123.                   URL var8 = new URL(super.url, var7);
  124.                   super.url = var8;
  125.                   if (!"http".equals(super.url.getProtocol())) {
  126.                      this.http.closeServer();
  127.                      super.connected = false;
  128.                      this.http = null;
  129.                      throw new IOException("Cannot redirect to other protocol: " + super.url);
  130.                   }
  131.  
  132.                   ProgressData.pdata.register(super.url);
  133.                }
  134.  
  135.                if (this.http.getStatus() == 200 || var7 == null) {
  136.                   ((URLConnection)this).setProperties(this.http.getMimeHeader());
  137.                   if (((URLConnection)this).getProperties() == null) {
  138.                      ((URLConnection)this).setProperties(new MessageHeader());
  139.                   }
  140.  
  141.                   String var9 = this.http.getHeaderField("content-length");
  142.                   int var10 = 0;
  143.                   if (var9 != null && (var10 = Integer.parseInt(var9)) != 0) {
  144.                      var1 = new MeteredStream((InputStream)var1, var10, super.url);
  145.                   } else {
  146.                      ProgressData.pdata.unregister(super.url);
  147.                   }
  148.  
  149.                   this.inputStream = (InputStream)var1;
  150.                   return (InputStream)var1;
  151.                }
  152.  
  153.                this.http.closeServer();
  154.                ProgressData.pdata.unregister(super.url);
  155.                this.http = null;
  156.                this.inputStream = null;
  157.                super.connected = false;
  158.             } catch (IOException var6) {
  159.                ProgressData.pdata.unregister(super.url);
  160.                throw var6;
  161.             }
  162.          }
  163.       }
  164.    }
  165.  
  166.    public String getHeaderField(String var1) {
  167.       if (this.http == null) {
  168.          try {
  169.             this.getInputStream();
  170.          } catch (Exception var2) {
  171.          }
  172.       }
  173.  
  174.       return this.http.getHeaderField(var1);
  175.    }
  176.  
  177.    static {
  178.       userAgent = "User-Agent: " + System.getProperty("http.agent", "Java" + version) + "\r\n";
  179.    }
  180. }
  181.