You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@locus.apache.org on 2000/05/24 19:19:59 UTC

cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util CookieTools.java MessageBytes.java MessageString.java MimeHeaderField.java MimeHeaders.java HttpDate.java

costin      00/05/24 10:19:59

  Modified:    src/share/org/apache/tomcat/core Request.java
                        RequestImpl.java
               src/share/org/apache/tomcat/util CookieTools.java
                        MessageBytes.java MessageString.java
                        MimeHeaderField.java MimeHeaders.java
  Removed:     src/share/org/apache/tomcat/util HttpDate.java
  Log:
  - Removed HttpDate. It was supposed to be an efficient implementation for date
  conversion, but it wasn't. All static fields are now part of DateTool - you don't need
  to create SimpleDateFormat objects, just use the static ones.
  
  The non-static part of HttpDate consisted in a Calendar used only to store the time,
  plus code to parse/format it. The code was moved in MimeHeaderField ( 2 methods),
  and need to be replaced with an efficient version.
  
  - Removed code to setDate() with current time - just use setDate( System.currentTimeMillis()).
  The code is not used in tomcat.
  
  - removed toByte conversions from MimeHeader - it was using the default encoding, and
  we had no control. The conversion have to be consistent with other components.
  
  - CookieTool.FieldPosition can't be static - it's changed during parsing
  
  - remove (unused) [] getCookies() method from Request. If you use it in your code -
  there is a simple workaround or it can be added back.
  
  Revision  Changes    Path
  1.41      +1 -3      jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java
  
  Index: Request.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java,v
  retrieving revision 1.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- Request.java	2000/05/24 16:34:06	1.40
  +++ Request.java	2000/05/24 17:19:49	1.41
  @@ -113,10 +113,8 @@
       //-------------------- "Computed" properties --------------------
       // ( directly derived from headers or request paths )
       public Cookie getCookie( int idx );
  +
       public int getCookieCount();
  -    /** Return the cookies
  -     */
  -    public Cookie[] getCookies() ;
   
       public int getContentLength() ;
   
  
  
  
  1.42      +2 -2      jakarta-tomcat/src/share/org/apache/tomcat/core/RequestImpl.java
  
  Index: RequestImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/RequestImpl.java,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -r1.41 -r1.42
  --- RequestImpl.java	2000/05/24 16:34:07	1.41
  +++ RequestImpl.java	2000/05/24 17:19:49	1.42
  @@ -435,11 +435,11 @@
       }
       
       public Cookie[] getCookies() {
  -	int count=request.getCookieCount();
  +	int count=getCookieCount();
   	Cookie[] cookieArray = new Cookie[ count ];
   
   	for (int i = 0; i < count; i ++) {
  -	    cookieArray[i] = request.getCookie( i );
  +	    cookieArray[i] = getCookie( i );
   	}
   
   	return cookieArray;
  
  
  
  1.7       +6 -6      jakarta-tomcat/src/share/org/apache/tomcat/util/CookieTools.java
  
  Index: CookieTools.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/CookieTools.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- CookieTools.java	2000/05/02 19:58:41	1.6
  +++ CookieTools.java	2000/05/24 17:19:53	1.7
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/CookieTools.java,v 1.6 2000/05/02 19:58:41 costin Exp $
  - * $Revision: 1.6 $
  - * $Date: 2000/05/02 19:58:41 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/CookieTools.java,v 1.7 2000/05/24 17:19:53 costin Exp $
  + * $Revision: 1.7 $
  + * $Date: 2000/05/24 17:19:53 $
    *
    * ====================================================================
    *
  @@ -78,8 +78,6 @@
    */
   public class CookieTools {
   
  -    static FieldPosition FieldPosition0=new FieldPosition(0);
  -
       /** Return the header name to set the cookie, based on cookie
        *  version
        */
  @@ -136,7 +134,9 @@
   	if (cookie.getMaxAge() >= 0) {
   	    if (version == 0) {
   		buf.append (";Expires=");
  -		HttpDate.oldCookieFormat.format(new Date( System.currentTimeMillis() + cookie.getMaxAge() *1000) ,buf, FieldPosition0 );
  +		DateTool.oldCookieFormat.format(new Date( System.currentTimeMillis() + cookie.getMaxAge() *1000) ,buf,
  +						new FieldPosition(0));
  +
   	    } else {
   		buf.append (";Max-Age=");
   		buf.append (cookie.getMaxAge());
  
  
  
  1.4       +3 -19     jakarta-tomcat/src/share/org/apache/tomcat/util/MessageBytes.java
  
  Index: MessageBytes.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageBytes.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MessageBytes.java	2000/02/14 04:59:43	1.3
  +++ MessageBytes.java	2000/05/24 17:19:54	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageBytes.java,v 1.3 2000/02/14 04:59:43 costin Exp $
  - * $Revision: 1.3 $
  - * $Date: 2000/02/14 04:59:43 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageBytes.java,v 1.4 2000/05/24 17:19:54 costin Exp $
  + * $Revision: 1.4 $
  + * $Date: 2000/05/24 17:19:54 $
    *
    * ====================================================================
    *
  @@ -191,22 +191,6 @@
        */
       public int toInteger() throws NumberFormatException {
   	return parseInt(bytes, offset, length);
  -    }
  -
  -    /**
  -     * Returns the message bytes parsed as a date.
  -     * @param d the HttpDate object to use for parsing
  -     * @exception IllegalArgumentException if the date format was invalid
  -     */
  -    public long toDate(HttpDate d) throws IllegalArgumentException {
  -	if (bytes != null) {
  -	    d.parse(bytes, offset, length);
  -	    return d.getTime();
  -	} else {
  -            String msg = sm.getString("messageBytes.iae", bytes);
  -
  -	    throw new IllegalArgumentException(msg);
  -	}
       }
   
       /**
  
  
  
  1.3       +3 -17     jakarta-tomcat/src/share/org/apache/tomcat/util/MessageString.java
  
  Index: MessageString.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageString.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MessageString.java	2000/05/23 20:58:26	1.2
  +++ MessageString.java	2000/05/24 17:19:55	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageString.java,v 1.2 2000/05/23 20:58:26 costin Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/05/23 20:58:26 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MessageString.java,v 1.3 2000/05/24 17:19:55 costin Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/05/24 17:19:55 $
    *
    * ====================================================================
    *
  @@ -173,20 +173,6 @@
        */
       public int toInteger() throws NumberFormatException {
   	return str != null ? Integer.parseInt(str) : super.toInteger();
  -    }
  -
  -    /**
  -     * Returns the message string parsed as a date.
  -     * @param d the HttpDate object to use for parsing
  -     * @exception IllegalArgumentException if the date format was invalid
  -     */
  -    public long toDate(HttpDate d) throws IllegalArgumentException {
  -	if (str != null) {
  -	    d.parse(str);
  -	    return d.getTime();
  -	} else {
  -	    return super.toDate(d);
  -	}
       }
   
       /**
  
  
  
  1.8       +39 -128   jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaderField.java
  
  Index: MimeHeaderField.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaderField.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- MimeHeaderField.java	2000/05/24 04:41:56	1.7
  +++ MimeHeaderField.java	2000/05/24 17:19:55	1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaderField.java,v 1.7 2000/05/24 04:41:56 costin Exp $
  - * $Revision: 1.7 $
  - * $Date: 2000/05/24 04:41:56 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaderField.java,v 1.8 2000/05/24 17:19:55 costin Exp $
  + * $Revision: 1.8 $
  + * $Date: 2000/05/24 17:19:55 $
    *
    * ====================================================================
    *
  @@ -67,6 +67,8 @@
   import javax.servlet.*;
   import java.io.IOException;
   import java.io.OutputStream;
  +import java.util.*;
  +import java.text.*;
   
   /**
    * This class is used to represent a MIME header field.
  @@ -104,7 +106,7 @@
       /**
        * The header field Date value.
        */
  -    protected final HttpDate dateValue = new HttpDate(0);
  +    protected Date dateValue = null;
   
       StringBuffer sb=null;
       // Will be used to conver date value - _never_ call toString()
  @@ -190,19 +192,14 @@
        * @param t the time in milliseconds since the epoch
        */
       public void setDateValue(long t) {
  -	dateValue.setTime(t);
  +	if( dateValue==null)
  +	    dateValue=new Date(t);
  +	else
  +	    dateValue.setTime(t);
   	type = T_DATE;
       }
   
       /**
  -     * Sets the header field date value to the current time.
  -     */
  -    public void setDateValue() {
  -	dateValue.setTime();
  -	type = T_DATE;
  -    }
  -
  -    /**
        * Returns the header field name as a String.
        */
       public String getName() {
  @@ -219,7 +216,7 @@
   	case T_INT:
   	    return String.valueOf(intValue);
   	case T_DATE:
  -	    return dateValue.toString();
  +	    return formatDate(dateValue);
   	default:
   	    return null;
   	}
  @@ -262,114 +259,39 @@
       {
   	switch (type) {
   	case T_DATE:
  +	    if( dateValue==null) break;
   	    return dateValue.getTime();
   	case T_STR:
  -	    return value.toDate(dateValue);
  -	default:
  -            String msg = sm.getString("mimeHeaderField.date.iae");
  -
  -	    throw new IllegalArgumentException(msg);
  +	    return parseDate( value );
   	}
  +	String msg = sm.getString("mimeHeaderField.date.iae");
  +	throw new IllegalArgumentException(msg);
       }
  -
  -    /** 
  -     * Place a byte representation of target into the byte array buf.
  -     * @param target - the integer to convert.  Must be in the range [0..2^31].
  -     * @return the number of bytes added to buf
  -     */
  -    private int intGetBytes(int target, byte buf[], int offset) {
  -	int power = 1000000000;  // magnitude of highest digit we can handle
  -	int this_digit;
  -	int bytes_emitted = 0;
  -
  -	// special case target is zero
  -	if (target == 0) {
  -	    buf[offset] = charval[0];
  -
  -	    return (1);
  -	}
  -
  -	// iterate until there are no more digits to emit
  -	while (power > 0) {
  -	    this_digit = target / power;
  -
  -	    if (this_digit != 0 || bytes_emitted > 0) {
  -		// emit this digit
  -		buf[offset + bytes_emitted++] = charval[this_digit];
  -	    }
  -
  -	    target = target % power;
  -	    power = power / 10;
  -	}
  -
  -	return bytes_emitted;
  -    }
  -
  -    /**
  -     * Put the bytes for this header into buf starting at offset buf_offset.
  -     * @return the length of what was added
  -     */
  -    public int getBytes(byte buf[], int buf_offset) {
  -	int len;
  -	int start_pt = buf_offset;
  -
  -	buf_offset += name.getBytes(buf, buf_offset);
  -	buf[buf_offset++] = (byte) ':';
  -	buf[buf_offset++] = (byte) ' ';
  -
  -	switch (type) {
  -	case T_STR:
  -	    buf_offset += value.getBytes(buf, buf_offset);
  -	    break;
  -	case T_INT:
  -	    buf_offset += intGetBytes(intValue, buf, buf_offset);
  -	    break;
  -	case T_DATE:
  -	    buf_offset += getBytes(dateValue, buf, buf_offset,
  -					     DATELEN);
  -	    break;
  -	}
  -
  -	buf[buf_offset++] = (byte) '\r';
  -	buf[buf_offset++] = (byte) '\n';
  -
  -	return buf_offset - start_pt;
  -    }
  -
       
  -    private static final String DATESTR = "Sun, 06 Nov 1994 08:49:37 GMT";
  -    private static final int DATELEN = DATESTR.length();
  -    
  -    private int getBytes(HttpDate dateValue, byte[] buf, int off, int len) {
  -	String dateString = HttpDate.rfc1123Format.format(dateValue.getCalendar().getTime());
  -	byte[] b = dateString.getBytes();
  -	System.arraycopy(b, 0, buf, off, DATELEN);
  -	return DATELEN;
  -    }    
  -
  -    /**
  -     * Writes this header field to the specified servlet output stream.
  -     */
  -    public void write(ServletOutputStream out)
  -    throws IOException {
  -	name.write(out);
  -	out.print(": ");
  +    String formatDate( Date value ) {
  +	return DateTool.rfc1123Format.format(value);
  +    }
   
  -	switch (type) {
  -	case T_STR:
  -	    value.write(out);
  -	    out.println();
  -	    break;
  -	case T_INT:
  -	    out.println(intValue);
  -	    break;
  -	case T_DATE:
  -	    dateValue.write(out);
  -	    out.println();
  -	    break;
  -	default:
  -	    out.println();
  -	}
  +    long parseDate( MessageString value ) {
  +	String dateString=value.toString();
  +	Date date=null;
  +        try {
  +            date = DateTool.rfc1123Format.parse(dateString);
  +	    return date.getTime();
  +	} catch (ParseException e) { }
  +
  +        try {
  +	    date = DateTool.rfc1036Format.parse(dateString);
  +	    return date.getTime();
  +	} catch (ParseException e) { }
  +	
  +        try {
  +            date = DateTool.asctimeFormat.parse(dateString);
  +	    return date.getTime();
  +        } catch (ParseException pe) {
  +        }
  +	String msg = sm.getString("httpDate.pe", dateString);
  +	throw new IllegalArgumentException(msg);
       }
   
       /**
  @@ -400,18 +322,7 @@
   
   	sb.append(name.toString());
   	sb.append(": ");
  -
  -	switch (type) {
  -	case T_STR:
  -	    sb.append(value.toString());
  -	    break;
  -	case T_INT:
  -	    sb.append(intValue);
  -	    break;
  -	case T_DATE:
  -	    sb.append(dateValue.toString());
  -	    break;
  -	}
  +	sb.append( getValue() );
   	return sb.toString();
       }
   }
  
  
  
  1.8       +3 -42     jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaders.java
  
  Index: MimeHeaders.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaders.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- MimeHeaders.java	2000/05/24 04:41:57	1.7
  +++ MimeHeaders.java	2000/05/24 17:19:56	1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaders.java,v 1.7 2000/05/24 04:41:57 costin Exp $
  - * $Revision: 1.7 $
  - * $Date: 2000/05/24 04:41:57 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/MimeHeaders.java,v 1.8 2000/05/24 17:19:56 costin Exp $
  + * $Revision: 1.8 $
  + * $Date: 2000/05/24 17:19:56 $
    *
    * ====================================================================
    *
  @@ -282,18 +282,6 @@
       }
   
       /**
  -     * Creates a new header field whose value is the current date and time.
  -     * @param name the header name
  -     */
  -    public void putDateHeader(String name) {
  -	putHeader(name).setDateValue();
  -    }
  -
  -    public void addDateHeader(String name) {
  -        addHeader(name).setDateValue();
  -    }
  -
  -    /**
        * Returns the string value of one of the headers with the
        * specified name.
        * @see getHeaders
  @@ -430,18 +418,6 @@
   	return find(name) != null;
       }
   
  -    /**
  -     * Writes out header fields to the specified servlet output stream.
  -     * @param out the servlet output stream
  -     * @exception IOException if an I/O error has occurred
  -     */
  -    public void write(ServletOutputStream out) throws IOException {
  -	for (int i = 0; i < count; i++) {
  -	    headers[i].write(out);
  -	}
  -
  -	out.println();
  -    }
   
       /**
        * Finds a header field given name.  If the header doesn't exist,
  @@ -478,21 +454,6 @@
   	mh.setValue(s);
       }
       
  -    /** 
  -     * Get the current header fields in the byte array buf.  The headers
  -     * fields are placed starting at offset buf_offset.  
  -     * @return the number of bytes written into buf.
  -     */
  -    public int getAll(byte buf[], int buf_offset) {
  -	int start_pt = buf_offset;
  -
  -	for (int i = 0; i < count; i++) {
  -	    buf_offset += headers[i].getBytes(buf, buf_offset);
  -	}
  -
  -	return buf_offset - start_pt;
  -    }
  -
       
       /**
        * Returns a lengthly string representation of the current header fields.