You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2001/01/23 04:53:01 UTC

cvs commit: jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http HttpConnector.java HttpProcessor.java HttpRequestStream.java SocketInputStream.java

remm        01/01/22 19:53:01

  Modified:    catalina/src/share/org/apache/catalina/connector/http
                        HttpConnector.java HttpProcessor.java
                        HttpRequestStream.java SocketInputStream.java
  Log:
  - Backports :
  - SIS doesn't extend BufferedInputStream.
  - Fix for a chunking bug in HttpRequestStream.
  
  Revision  Changes    Path
  1.8       +6 -5      jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpConnector.java
  
  Index: HttpConnector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpConnector.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- HttpConnector.java	2001/01/11 01:40:27	1.7
  +++ HttpConnector.java	2001/01/23 03:53:00	1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpConnector.java,v 1.7 2001/01/11 01:40:27 remm Exp $
  - * $Revision: 1.7 $
  - * $Date: 2001/01/11 01:40:27 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpConnector.java,v 1.8 2001/01/23 03:53:00 remm Exp $
  + * $Revision: 1.8 $
  + * $Date: 2001/01/23 03:53:00 $
    *
    * ====================================================================
    *
  @@ -94,7 +94,7 @@
    *
    * @author Craig R. McClanahan
    * @author Remy Maucherat
  - * @version $Revision: 1.7 $ $Date: 2001/01/11 01:40:27 $
  + * @version $Revision: 1.8 $ $Date: 2001/01/23 03:53:00 $
    */
   
   
  @@ -762,7 +762,8 @@
   	    Socket socket = null;
   	    try {
   		socket = serverSocket.accept();
  -                socket.setSoTimeout(connectionTimeout);
  +                if (connectionTimeout > 0)
  +                    socket.setSoTimeout(connectionTimeout);
   	    } catch (IOException e) {
   		if (started && !stopped)
   		    log("accept: ", e);
  
  
  
  1.20      +4 -4      jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpProcessor.java
  
  Index: HttpProcessor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpProcessor.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- HttpProcessor.java	2000/12/23 19:39:54	1.19
  +++ HttpProcessor.java	2001/01/23 03:53:00	1.20
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpProcessor.java,v 1.19 2000/12/23 19:39:54 craigmcc Exp $
  - * $Revision: 1.19 $
  - * $Date: 2000/12/23 19:39:54 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpProcessor.java,v 1.20 2001/01/23 03:53:00 remm Exp $
  + * $Revision: 1.20 $
  + * $Date: 2001/01/23 03:53:00 $
    *
    * ====================================================================
    *
  @@ -107,7 +107,7 @@
    *
    * @author Craig R. McClanahan
    * @author Remy Maucherat
  - * @version $Revision: 1.19 $ $Date: 2000/12/23 19:39:54 $
  + * @version $Revision: 1.20 $ $Date: 2001/01/23 03:53:00 $
    */
   
   final class HttpProcessor
  
  
  
  1.7       +109 -52   jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpRequestStream.java
  
  Index: HttpRequestStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpRequestStream.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- HttpRequestStream.java	2000/12/14 07:49:16	1.6
  +++ HttpRequestStream.java	2001/01/23 03:53:00	1.7
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpRequestStream.java,v 1.6 2000/12/14 07:49:16 remm Exp $
  - * $Revision: 1.6 $
  - * $Date: 2000/12/14 07:49:16 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpRequestStream.java,v 1.7 2001/01/23 03:53:00 remm Exp $
  + * $Revision: 1.7 $
  + * $Date: 2001/01/23 03:53:00 $
    *
    * ====================================================================
    * 
  @@ -202,56 +202,8 @@
               
               if ((chunkBuffer == null)
                   || (chunkPos >= chunkLength)) {
  -                
  -                chunkPos = 0;
  -                
  -                try {
  -                    chunkLength = 
  -                        Integer.parseInt(readLineFromStream().trim(), 16);
  -                } catch (NumberFormatException e) {
  -                    // Critical error, unable to parse the chunk length
  -                    chunkLength = 0;
  -                    chunk = false;
  -                    close();
  -                    return -1;
  -                }
  -                
  -                if (chunkLength == 0) {
  -                    
  -                    // Skipping trailing headers, if any
  -                    String trailingLine = readLineFromStream();
  -                    while (!trailingLine.equals(""))
  -                        trailingLine = readLineFromStream();
  -                    endChunk = true;
  +                if (!fillChunkBuffer())
                       return (-1);
  -                    // TODO : Should the stream be automatically closed ?
  -                    
  -                } else {
  -                    
  -                    if ((chunkBuffer == null)
  -                        || (chunkLength > chunkBuffer.length))
  -                        chunkBuffer = new byte[chunkLength];
  -                    
  -                    // Now read the whole chunk into the buffer
  -                    
  -                    int nbRead = 0;
  -                    int currentRead = 0;
  -                    
  -                    while (nbRead < chunkLength) {
  -                        currentRead = 
  -                            stream.read(chunkBuffer, nbRead, 
  -                                       chunkLength - nbRead);
  -                        if (currentRead == -1)
  -                            throw new IOException
  -                                (sm.getString("requestStream.read.error"));
  -                        nbRead += currentRead;
  -                    }
  -                    
  -                    // Skipping the CRLF
  -                    readLineFromStream();
  -                    
  -                }
  -                
               }
               
               return (chunkBuffer[chunkPos++]);
  @@ -265,7 +217,112 @@
       }
   
   
  +    /**
  +     * Read up to <code>len</code> bytes of data from the input stream
  +     * into an array of bytes.  An attempt is made to read as many as
  +     * <code>len</code> bytes, but a smaller number may be read,
  +     * possibly zero.  The number of bytes actually read is returned as
  +     * an integer.  This method blocks until input data is available,
  +     * end of file is detected, or an exception is thrown.
  +     *
  +     * @param b The buffer into which the data is read
  +     * @param off The start offset into array <code>b</code> at which
  +     *  the data is written
  +     * @param len The maximum number of bytes to read
  +     *
  +     * @exception IOException if an input/output error occurs
  +     */
  +    public int read(byte b[], int off, int len) throws IOException {
  +        if (chunk) {
  +            
  +            int avail = chunkLength - chunkPos;
  +            if (avail == 0)
  +                fillChunkBuffer();
  +            avail = chunkLength - chunkPos;
  +            if (avail == 0)
  +                return (-1);
  +            
  +            int toCopy = avail;
  +            if (avail > len)
  +                toCopy = len;
  +            System.arraycopy(chunkBuffer, chunkPos, b, off, toCopy);
  +            chunkPos += toCopy;
  +            return toCopy;
  +            
  +        } else {
  +            return super.read(b, off, len);
  +        }
  +    }
  +    
  +    
       // -------------------------------------------------------- Private Methods
  +    
  +    
  +    /**
  +     * Fill the chunk buffer.
  +     */
  +    private synchronized boolean fillChunkBuffer() 
  +        throws IOException {
  +        
  +        chunkPos = 0;
  +        
  +        try {
  +            String numberValue = readLineFromStream().trim();
  +            chunkLength = 
  +                Integer.parseInt(numberValue, 16);
  +        } catch (NumberFormatException e) {
  +            // Critical error, unable to parse the chunk length
  +            chunkLength = 0;
  +            chunk = false;
  +            close();
  +            return false;
  +        }
  +        
  +        if (chunkLength == 0) {
  +                    
  +            // Skipping trailing headers, if any
  +            String trailingLine = readLineFromStream();
  +            while (!trailingLine.equals(""))
  +                trailingLine = readLineFromStream();
  +            endChunk = true;
  +            return false;
  +            // TODO : Should the stream be automatically closed ?
  +            
  +        } else {
  +            
  +            if ((chunkBuffer == null)
  +                || (chunkLength > chunkBuffer.length))
  +                chunkBuffer = new byte[chunkLength];
  +            
  +            // Now read the whole chunk into the buffer
  +            
  +            int nbRead = 0;
  +            int currentRead = 0;
  +            
  +            while (nbRead < chunkLength) {
  +                try {
  +                    currentRead = 
  +                        stream.read(chunkBuffer, nbRead, 
  +                                    chunkLength - nbRead);
  +                } catch (Throwable t) {
  +                    t.printStackTrace();
  +                    throw new IOException();
  +                }
  +                if (currentRead < 0) {
  +                    throw new IOException
  +                        (sm.getString("requestStream.read.error"));
  +                }
  +                nbRead += currentRead;
  +            }
  +            
  +            // Skipping the CRLF
  +            String blank = readLineFromStream();
  +            
  +        }
  +        
  +        return true;
  +        
  +    }
       
   
       /**
  
  
  
  1.6       +107 -10   jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/SocketInputStream.java
  
  Index: SocketInputStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/SocketInputStream.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SocketInputStream.java	2001/01/04 19:49:20	1.5
  +++ SocketInputStream.java	2001/01/23 03:53:00	1.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/SocketInputStream.java,v 1.5 2001/01/04 19:49:20 remm Exp $
  - * $Revision: 1.5 $
  - * $Date: 2001/01/04 19:49:20 $
  + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/SocketInputStream.java,v 1.6 2001/01/23 03:53:00 remm Exp $
  + * $Revision: 1.6 $
  + * $Date: 2001/01/23 03:53:00 $
    *
    * ====================================================================
    * 
  @@ -65,18 +65,17 @@
   package org.apache.catalina.connector.http;
   
   import java.io.IOException;
  -import java.io.BufferedInputStream;
   import java.io.InputStream;
   import java.io.EOFException;
   import org.apache.catalina.util.StringManager;
   
   /**
  - * Extends BufferedInputStream to be more efficient reading lines during HTTP
  + * Extends InputStream to be more efficient reading lines during HTTP
    * header processing.
    *
    * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
    */
  -public class SocketInputStream extends BufferedInputStream {
  +public class SocketInputStream extends InputStream {
   
   
       // -------------------------------------------------------------- Constants
  @@ -112,6 +111,30 @@
       private static final int LC_OFFSET = 'A' - 'a';
   
   
  +    /**
  +     * Internal buffer.
  +     */
  +    protected byte buf[];
  +
  +
  +    /**
  +     * Last valid byte.
  +     */
  +    protected int count;
  +
  +
  +    /**
  +     * Position in the buffer.
  +     */
  +    protected int pos;
  +
  +
  +    /**
  +     * Underlying input stream.
  +     */
  +    protected InputStream is;
  +
  +
       // ----------------------------------------------------------- Constructors
   
   
  @@ -124,7 +147,8 @@
        */
       public SocketInputStream(InputStream is, int bufferSize) {
   
  -	super(is, bufferSize);
  +	this.is = is;
  +        buf = new byte[bufferSize];
   
       }
   
  @@ -174,9 +198,7 @@
           if (chr == -1)
               throw new EOFException
                   (sm.getString("requestStream.readline.error"));
  -        if ((chr != CR) || (chr != LF)) {
  -            pos--;
  -        }
  +        pos--;
   
           // Reading the method name
   
  @@ -484,6 +506,81 @@
           
           header.valueEnd = readCount;
           
  +    }
  +
  +
  +    /**
  +     * Read byte.
  +     */
  +    public int read()
  +        throws IOException {
  +        if (pos >= count) {
  +            fill();
  +	    if (pos >= count)
  +		return -1;
  +        }
  +	return buf[pos++] & 0xff;
  +    }
  +
  +
  +    /**
  +     * 
  +     */
  +    /*
  +    public int read(byte b[], int off, int len)
  +        throws IOException {
  +        
  +    }
  +    */
  +
  +
  +    /**
  +     * 
  +     */
  +    /*
  +    public long skip(long n)
  +        throws IOException {
  +        
  +    }
  +    */
  +
  +
  +    /**
  +     * Returns the number of bytes that can be read from this input 
  +     * stream without blocking.
  +     */
  +    public int available()
  +        throws IOException {
  +        return (count - pos) + is.available();
  +    }
  +
  +
  +    /**
  +     * Close the input stream.
  +     */
  +    public void close()
  +        throws IOException {
  +        if (is == null)
  +            return;
  +        is.close();
  +        is = null;
  +        buf = null;
  +    }
  +
  +
  +    // ------------------------------------------------------ Protected Methods
  +
  +
  +    /**
  +     * Fill the internal buffer using data from the undelying input stream.
  +     */
  +    protected void fill()
  +        throws IOException {
  +        pos = 0;
  +        count = 0;
  +        int nRead = is.read(buf, 0, buf.length);
  +        if (nRead > 0)
  +            count = nRead;
       }