/[classpath]/classpath/gnu/java/net/protocol/http/Connection.java
ViewVC logotype

Diff of /classpath/gnu/java/net/protocol/http/Connection.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by mkoch, Sat Oct 18 10:04:11 2003 UTC revision 1.2 by mkoch, Mon Oct 20 14:42:08 2003 UTC
# Line 1  Line 1 
1  /* HttpURLConnection.java -- URLConnection class for HTTP protocol  /* HttpURLConnection.java -- URLConnection class for HTTP protocol
2     Copyright (C) 1998, 2002, 2003 Free Software Foundation, Inc.     Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 60  import gnu.java.net.HeaderFieldHelper; Line 60  import gnu.java.net.HeaderFieldHelper;
60   * This subclass of java.net.URLConnection models a URLConnection via   * This subclass of java.net.URLConnection models a URLConnection via
61   * the HTTP protocol.   * the HTTP protocol.
62   *   *
63   * @author Aaron M. Renn (arenn@urbanophile.com)   * Status: Minimal subset of functionality.  Proxies only partially
64     * handled; Redirects not yet handled.  FileNameMap handling needs to
65     * be considered.  useCaches, ifModifiedSince, and
66     * allowUserInteraction need consideration as well as doInput and
67     * doOutput.
68     *
69     * @author Aaron M. Renn <arenn@urbanophile.com>
70     * @author Warren Levy <warrenl@cygnus.com>
71   */   */
72  public class Connection extends HttpURLConnection  public class Connection extends HttpURLConnection
73  {  {
# Line 70  public class Connection extends HttpURLC Line 77  public class Connection extends HttpURLC
77    private Socket socket;    private Socket socket;
78    
79    /**    /**
80     * The InputStream for this connection     * The InputStream for this connection.
81     */     */
82    private DataInputStream in_stream;    private DataInputStream inputStream;
83    
84    /**    /**
85     * The OutputStream for this connection     * The OutputStream for this connection
86     */     */
87    private OutputStream out_stream;    private OutputStream outputStream;
88    
89    /**    /**
90     * buffered_out_stream is a buffer to contain content of the HTTP request,     * bufferedOutputStream is a buffer to contain content of the HTTP request,
91     * and will be written to out_stream all at once     * and will be written to outputStream all at once
92     */     */
93    private ByteArrayOutputStream buffered_out_stream;    private ByteArrayOutputStream bufferedOutputStream;
94    
95    /**    /**
96     * The PrintWriter for this connection (used internally)     * The PrintWriter for this connection (used internally)
97     */     */
98    private PrintWriter out_writer;    private PrintWriter outputWriter;
99    
100    /**    /**
101     * This is the object that holds the header field information     * This is the object that holds the header field information
# Line 118  public class Connection extends HttpURLC Line 125  public class Connection extends HttpURLC
125      else      else
126        socket = new Socket(url.getHost(), url.getPort());        socket = new Socket(url.getHost(), url.getPort());
127    
128      out_stream = new BufferedOutputStream(socket.getOutputStream());      outputStream = new BufferedOutputStream(socket.getOutputStream());
129      out_writer = new PrintWriter(new OutputStreamWriter(out_stream, "8859_1"));      outputWriter = new PrintWriter(new OutputStreamWriter(outputStream, "8859_1"));
130    
131      connected = true;      connected = true;
132    }    }
133    
134    /**    /**
135     * write HTTP request header and content to out_writer     * Write HTTP request header and content to outputWriter.
136     */     */
137    void SendRequest() throws IOException    void SendRequest() throws IOException
138    {    {
139      // Send the request      // Send request including any request properties that were set.
140      out_writer.print(getRequestMethod() + " " + getURL().getFile()      outputWriter.print (getRequestMethod() + " " + url.getFile()
141                       + " HTTP/1.1\r\n");                          + " HTTP/1.1\r\n");
142    
143        // Set additional HTTP headers.
144      if (getRequestProperty ("host") == null)      if (getRequestProperty ("host") == null)
145        {        {
146          setRequestProperty ("Host", getURL().getHost());          setRequestProperty ("Host", url.getHost());
147        }        }
148            
149      if (getRequestProperty ("Connection") == null)      if (getRequestProperty ("Connection") == null)
# Line 159  public class Connection extends HttpURLC Line 167  public class Connection extends HttpURLC
167          setRequestProperty ("Content-type", "application/x-www-form-urlencoded");          setRequestProperty ("Content-type", "application/x-www-form-urlencoded");
168        }        }
169    
170      // Write all req_props name-value pairs to the output writer      // Write all req_props name-value pairs to the output writer.
171      Iterator itr = getRequestProperties().entrySet().iterator();      Iterator itr = getRequestProperties().entrySet().iterator();
172    
173      while (itr.hasNext())      while (itr.hasNext())
174        {        {
175          Map.Entry e = (Map.Entry) itr.next();          Map.Entry e = (Map.Entry) itr.next();
176          out_writer.print (e.getKey() + ": " + e.getValue() + "\r\n");          outputWriter.print (e.getKey() + ": " + e.getValue() + "\r\n");
177        }        }
178    
179      // Write Content-type and length      // Write Content-type and length
180      if (buffered_out_stream != null)      if (bufferedOutputStream != null)
181        {        {
182          out_writer.print ("Content-type: application/x-www-form-urlencoded\r\n");          outputWriter.print ("Content-type: application/x-www-form-urlencoded\r\n");
183          out_writer.print ("Content-length: "          outputWriter.print ("Content-length: "
184                            + String.valueOf (buffered_out_stream.size()) + "\r\n");                              + String.valueOf (bufferedOutputStream.size()) + "\r\n");
185        }        }
186    
187      // One more CR-LF indicates end of header      // One more CR-LF indicates end of header.
188      out_writer.print ("\r\n");      outputWriter.print ("\r\n");
189      out_writer.flush();      outputWriter.flush();
190    
191      // Write content      // Write content
192      if (buffered_out_stream != null)      if (bufferedOutputStream != null)
193        {        {
194          buffered_out_stream.writeTo (out_stream);          bufferedOutputStream.writeTo (outputStream);
195          out_stream.flush();          outputStream.flush();
196        }        }
197    }    }
198    
199    /**    /**
200     * Read HTTP reply from in_stream     * Read HTTP reply from inputStream.
201     */     */
202    void ReceiveReply() throws IOException    void ReceiveReply() throws IOException
203    {    {
204      // Parse the reply      // Parse the reply
205      String line = in_stream.readLine();      String line = inputStream.readLine();
206      String saveline = line;      String saveline = line;
207      int idx = line.indexOf (" ");      int idx = line.indexOf (" ");
208            
209      if ((idx == -1)      if ((idx == -1)
210          || (line.length() < (idx + 6)))          || (line.length() < (idx + 6)))
211        throw new IOException("Server reply was unparseable: " + saveline);        throw new IOException ("Server reply was unparseable: " + saveline);
212    
213      line = line.substring (idx + 1);      line = line.substring (idx + 1);
214      String code = line.substring (0, 3);      String code = line.substring (0, 3);
# Line 221  public class Connection extends HttpURLC Line 229  public class Connection extends HttpURLC
229            
230      for (;;)      for (;;)
231        {        {
232          line = in_stream.readLine();          line = inputStream.readLine();
233                    
234          if (line.equals(""))          if (line.equals(""))
235            break;            break;
# Line 317  public class Connection extends HttpURLC Line 325  public class Connection extends HttpURLC
325        throw new ProtocolException ("Unsupported or unknown request method " +        throw new ProtocolException ("Unsupported or unknown request method " +
326                                     method);                                     method);
327    }    }
328      
329    /**    /**
330     * Return a boolean indicating whether or not this connection is     * Return a boolean indicating whether or not this connection is
331     * going through a proxy     * going through a proxy
# Line 368  public class Connection extends HttpURLC Line 376  public class Connection extends HttpURLC
376     */     */
377    public InputStream getInputStream() throws IOException    public InputStream getInputStream() throws IOException
378    {    {
379      if(in_stream != null)      if(inputStream != null)
380        return in_stream;        return inputStream;
381    
382      if (!connected)      if (!connected)
383        connect();        connect();
384    
385      in_stream      inputStream
386        = new DataInputStream (new BufferedInputStream (socket.getInputStream()));        = new DataInputStream (new BufferedInputStream (socket.getInputStream()));
387        
388      SendRequest();      SendRequest();
389      ReceiveReply();      ReceiveReply();
390    
391      return in_stream;      return inputStream;
392    }    }
393    
394    public OutputStream getOutputStream() throws IOException    public OutputStream getOutputStream() throws IOException
# Line 395  public class Connection extends HttpURLC Line 403  public class Connection extends HttpURLC
403      if (!connected)      if (!connected)
404        connect();        connect();
405        
406      if(buffered_out_stream == null)      if(bufferedOutputStream == null)
407        buffered_out_stream = new ByteArrayOutputStream (256); //default is too small        bufferedOutputStream = new ByteArrayOutputStream (256); //default is too small
408            
409      return buffered_out_stream;      return bufferedOutputStream;
410    }    }
411    
412  } // class HttpURLConnection  } // class Connection
413    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26