hgomez      01/09/27 03:28:12

  Modified:    jk/java/org/apache/ajp/tomcat4 Ajp13Processor.java
               jk/java/org/apache/ajp Ajp13.java
  Log:
  remove hard-coded debug in ajp13 and make use
  of debug properties in server.xml.
  
     <Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
                port="8009" minProcessors="5" maxProcessors="75"
                acceptCount="10" debug="0"/>
  
  Make block read safer backporting readN from TC 3.3
  
  Revision  Changes    Path
  1.3       +5 -4      
jakarta-tomcat-connectors/jk/java/org/apache/ajp/tomcat4/Ajp13Processor.java
  
  Index: Ajp13Processor.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-connectors/jk/java/org/apache/ajp/tomcat4/Ajp13Processor.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Ajp13Processor.java       2001/05/15 15:22:47     1.2
  +++ Ajp13Processor.java       2001/09/27 10:28:12     1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-tomcat-connectors/jk/java/org/apache/ajp/tomcat4/Ajp13Processor.java,v
 1.2 2001/05/15 15:22:47 seguin Exp $
  - * $Revision: 1.2 $
  - * $Date: 2001/05/15 15:22:47 $
  + * $Header: 
/home/cvs/jakarta-tomcat-connectors/jk/java/org/apache/ajp/tomcat4/Ajp13Processor.java,v
 1.3 2001/09/27 10:28:12 hgomez Exp $
  + * $Revision: 1.3 $
  + * $Date: 2001/09/27 10:28:12 $
    *
    * ====================================================================
    *
  @@ -102,7 +102,7 @@
   
   /**
    * @author Kevin Seguin
  - * @version $Revision: 1.2 $ $Date: 2001/05/15 15:22:47 $
  + * @version $Revision: 1.3 $ $Date: 2001/09/27 10:28:12 $
    */
   
   final class Ajp13Processor
  @@ -320,6 +320,7 @@
       private void process(Socket socket) {
   
           Ajp13 ajp13 = new Ajp13();
  +        ajp13.setDebug(debug);
           Ajp13InputStream input = new Ajp13InputStream(ajp13);
           Ajp13OutputStream output = new Ajp13OutputStream(ajp13);
           response.setAjp13(ajp13);
  
  
  
  1.15      +97 -33    jakarta-tomcat-connectors/jk/java/org/apache/ajp/Ajp13.java
  
  Index: Ajp13.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/jk/java/org/apache/ajp/Ajp13.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Ajp13.java        2001/09/17 15:45:50     1.14
  +++ Ajp13.java        2001/09/27 10:28:12     1.15
  @@ -112,6 +112,14 @@
       public static final byte JK_AJP13_END_RESPONSE      = 5;
       public static final byte JK_AJP13_GET_BODY_CHUNK    = 6;
       
  +    // Error code for Ajp13
  +    public static final int  JK_AJP13_BAD_HEADER        = -100;
  +    public static final int  JK_AJP13_NO_HEADER         = -101;
  +    public static final int  JK_AJP13_COMM_CLOSED       = -102;
  +    public static final int  JK_AJP13_COMM_BROKEN       = -103;
  +    public static final int  JK_AJP13_BAD_BODY          = -104;
  +    public static final int  JK_AJP13_INCOMPLETE_BODY   = -105;
  +
       // Integer codes for common response header strings
       public static final int SC_RESP_CONTENT_TYPE        = 0xA001;
       public static final int SC_RESP_CONTENT_LANGUAGE    = 0xA002;
  @@ -159,7 +167,7 @@
           "MOVE",
           "LOCK",
           "UNLOCK",
  -        "ACL",
  +        "ACL",
           "REPORT"
       };
   
  @@ -214,8 +222,8 @@
       byte []bodyBuff = new byte[MAX_READ_SIZE];
       
       int blen;  // Length of current chunk of body data in buffer
  -    int pos;   // Current read position within that buffer
  -
  +    int pos;   // Current read position within that buffer
  +
       boolean end_of_stream;  // true if we've received an empty packet
   
       public Ajp13() {
  @@ -238,7 +246,7 @@
   
           // This is a touch cargo-cultish, but I think wise.
           blen = 0; 
  -        pos = 0;
  +        pos = 0;
           end_of_stream = false;
       }
       
  @@ -589,9 +597,9 @@
           }
   
        // If the server returns an empty packet, assume that that end of
  -     // the stream has been reached (yuck -- fix protocol??).
  -        if (end_of_stream) {
  -            return false;
  +     // the stream has been reached (yuck -- fix protocol??).
  +        if (end_of_stream) {
  +            return false;
           }
   
        // Why not use outBuf??
  @@ -605,14 +613,14 @@
            throw new IOException();
        }
        
  -        // No data received.
  -        if( inBuf.getLen() == 0 ) {
  -            pos=0;
  -            blen=0;
  -            end_of_stream = true;
  -            return false;
  -        }
  -
  +        // No data received.
  +        if( inBuf.getLen() == 0 ) {
  +            pos=0;
  +            blen=0;
  +            end_of_stream = true;
  +            return false;
  +        }
  +
        blen = inBuf.peekInt();
        pos = 0;
        inBuf.getBytes(bodyBuff);
  @@ -815,6 +823,49 @@
       // ========= Internal Packet-Handling Methods =================
   
       /**
  +     * Read N bytes from the InputStream, and ensure we got them all
  +     * Under heavy load we could experience many fragmented packets
  +     * just read Unix Network Programming to recall that a call to
  +     * read didn't ensure you got all the data you want
  +     *
  +     * from read() Linux manual
  +     *
  +     * On success, the number of bytes read is returned (zero indicates end of 
file),
  +     * and the file position is advanced by this number.
  +     * It is not an error if this number is smaller than the number of bytes 
requested;
  +     * this may happen for example because fewer bytes
  +     * are actually available right now (maybe because we were close to end-of-file,
  +     * or because we are reading from a pipe, or  from  a
  +     * terminal),  or  because  read()  was interrupted by a signal.
  +     * On error, -1 is returned, and errno is set appropriately. In this
  +     * case it is left unspecified whether the file position (if any) changes.
  +     *
  +     **/
  +    private int readN(InputStream in, byte[] b, int offset, int len) throws 
IOException {
  +        int pos = 0;
  +        int got;
  +
  +        while(pos < len) {
  +            got = in.read(b, pos + offset, len - pos);
  +
  +            if (debug > 10) {
  +                logger.log("read got # " + got);
  +            }
  +
  +            // connection just closed by remote
  +            if (got == 0)
  +                return JK_AJP13_COMM_CLOSED;
  +
  +            // connection dropped by remote
  +            if (got < 0)
  +                return JK_AJP13_COMM_BROKEN;
  +
  +            pos += got;
  +        }
  +        return pos;
  +     }
  +
  +    /**
        * Read in a packet from the web server and store it in the passed-in
        * <CODE>Ajp13Packet</CODE> object.
        *
  @@ -834,30 +885,39 @@
        // value.  Also, callers of this method never use the length
        // returned -- should probably return true/false instead.
        byte b[] = msg.getBuff();
  -     
  -     int rd = in.read( b, 0, H_SIZE );
  -     if(rd <= 0) {
  -            logger.log("bad read: " + rd);
  -         return rd;
  -     }
        
  +    int rd = readN(in, b, 0, H_SIZE );
  +
  +    // XXX - connection closed (JK_AJP13_COMM_CLOSED)
  +    //     - connection broken (JK_AJP13_COMM_BROKEN)
  +    //
  +    if(rd < 0) {
  +        logger.log("bad read: " + rd);
  +         return rd;
  +    }
  +
        int len = msg.checkIn();
  +    if (debug > 0) {
           logger.log("receive:  len = " + len);
  -     
  +     }
  +
        // XXX check if enough space - it's assert()-ed !!!
   
        int total_read = 0;
  -     while (total_read < len) {
  -         rd = in.read( b, 4 + total_read, len - total_read);
  -            if (rd == -1) {
  -             System.out.println( "Incomplete read, deal with it " + len + " " + rd);
  -                break;
  -             // XXX log
  -             // XXX Return an error code?
  -         }
  -                 total_read += rd;
  -     }
   
  +    total_read = readN(in, b, H_SIZE, len);
  +
  +    if (total_read < 0) {
  +        logger.log("can't read body, waited #" + len);
  +        return JK_AJP13_BAD_BODY;
  +    }
  +
  +    if (total_read != len) {
  +        logger.log( "incomplete read, waited #" + len + " got only " + total_read);
  +        return JK_AJP13_INCOMPLETE_BODY;
  +    }
  +
  +    if (debug > 0)
           logger.log("receive:  total read = " + total_read);
        return total_read;
       }
  @@ -905,7 +965,11 @@
       }
   
       // -------------------- Debug --------------------
  -    private int debug = 10;
  +    private int debug = 0;
  +
  +    public void setDebug(int debug) {
  +        this.debug = debug;
  +    }
   
       /**
        * XXX place holder...
  
  
  


Reply via email to