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/10/08 23:28:59 UTC

cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util MessageBytes.java

costin      00/10/08 14:28:59

  Modified:    src/facade22/org/apache/tomcat/facade
                        HttpServletRequestFacade.java
               src/facade23/org/apache/tomcat/facade23
                        HttpServletRequestFacade.java
               src/share/org/apache/tomcat/core Request.java
               src/share/org/apache/tomcat/helper CookieTools.java
                        RequestUtil.java
               src/share/org/apache/tomcat/modules/server Ajp12.java
               src/share/org/apache/tomcat/service/connector
                        Ajp12ConnectionHandler.java
                        Ajp13ConnectorRequest.java
               src/share/org/apache/tomcat/util MessageBytes.java
  Log:
  Request:
  - cookies are no longer stored in a vector, but a resizeable []. We don't
  remove the cookies from the array, we recycle them ( so no allocation
  happens for cookies ).
  
  - removed the string version for contextPath, servletPath, pathInfo
  
  - getPathTranslated is now implemented in facade - it is something specific
  to servlets ( and different from PATH_TRANSLATED ). Since it's specific
  to servlet version ( it had a different meaning in servlet2.0 - no
  context path ) it's better to be placed there.
  
  - moved the code to parse cookies from RequestUtil to CookieTools. Started
  ( only started ) to replace garbage-intensive code ( StringTokenizer,
  multiple trims(), etc ) with MessageBytes-code.
  
  Revision  Changes    Path
  1.7       +10 -1     jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletRequestFacade.java
  
  Index: HttpServletRequestFacade.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/facade22/org/apache/tomcat/facade/HttpServletRequestFacade.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- HttpServletRequestFacade.java	2000/09/30 04:03:33	1.6
  +++ HttpServletRequestFacade.java	2000/10/08 21:28:56	1.7
  @@ -246,7 +246,16 @@
       }
   
       public String getPathTranslated() {
  -        return request.getPathTranslated();
  +	// Servlet 2.2 spec differs from what Apache and
  +	// all other web servers consider to be PATH_TRANSLATED.
  +	// It's important not to use CGI PATH_TRANSLATED - this
  +	// code is specific to servlet 2.2 ( or more )
  +	String path=getPathInfo();
  +	if(path==null || "".equals( path ) ) return null;
  +	String pathTranslated=
  +	    FileUtil.safePath( request.getContext().getAbsolutePath(),
  +			       path);
  +	return pathTranslated;
       }
       
       public String getProtocol() {
  
  
  
  1.6       +10 -1     jakarta-tomcat/src/facade23/org/apache/tomcat/facade23/HttpServletRequestFacade.java
  
  Index: HttpServletRequestFacade.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/facade23/org/apache/tomcat/facade23/HttpServletRequestFacade.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- HttpServletRequestFacade.java	2000/09/30 04:03:36	1.5
  +++ HttpServletRequestFacade.java	2000/10/08 21:28:56	1.6
  @@ -255,7 +255,16 @@
       }
   
       public String getPathTranslated() {
  -        return request.getPathTranslated();
  +	// Servlet 2.2 spec differs from what Apache and
  +	// all other web servers consider to be PATH_TRANSLATED.
  +	// It's important not to use CGI PATH_TRANSLATED - this
  +	// code is specific to servlet 2.2 ( or more )
  +	String path=getPathInfo();
  +	if(path==null || "".equals( path ) ) return null;
  +	String pathTranslated=
  +	    FileUtil.safePath( request.getContext().getAbsolutePath(),
  +			       path);
  +	return pathTranslated;
       }
       
       public String getProtocol() {
  
  
  
  1.69      +31 -68    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.68
  retrieving revision 1.69
  diff -u -r1.68 -r1.69
  --- Request.java	2000/10/06 05:18:17	1.68
  +++ Request.java	2000/10/08 21:28:56	1.69
  @@ -165,22 +165,15 @@
       protected Handler handler = null;
       Container container;
   
  -    //protected ServletInputStream in;
  +    ServerCookie scookies[]=new ServerCookie[4];
  +    // -1 = cookies not processed yet
  +    int cookieCount=-1;
   
       // sub-request support 
       Request top;
       Request parent;
       Request child;
   
  -    protected String contextPath;
  -    protected String servletPath;
  -    protected String pathInfo;
  -    protected String pathTranslated;
  -    protected boolean pathTranslatedIsSet=false;
  -
  -    protected Vector cookies = new Vector();
  -    protected boolean didCookies;
  -
       private Object notes[]=new Object[ContextManager.MAX_NOTES];
       // Accounting
       private Counters cntr=new Counters(ACCOUNTS);
  @@ -312,7 +305,7 @@
       }
   
       public void setPathInfo(String pathInfo) {
  -        this.pathInfo = pathInfo;
  +        pathInfoMB.setString( pathInfo );
       }
   
   //     // What's between context path and servlet name ( /servlet )
  @@ -410,42 +403,11 @@
   	this.contentType=type;
       }
   
  -
  -    /** All adapters that know the PT needs to call this method,
  -	in order to set pathTranslatedIsSet, otherwise tomcat
  -	will try to compute it again
  -    */
  -    public void setPathTranslated(String s ) {
  -	pathTranslated=s;
  -	pathTranslatedIsSet=true;
  -    }
  -
  -    /** Not so usefull - it return the path translated for a
  -	URL relative the the context, i.e. different from
  -	what PATH_TRANSLATED does. Avoid using it.
  -    */
  -    public String getPathTranslated() {
  -	if( pathTranslatedIsSet ) return pathTranslated;
  -
  -	// not set yet - we'll compute it
  -	pathTranslatedIsSet=true;
  -	String path=getPathInfo();
  -	// In CGI spec, PATH_TRANSLATED shouldn't be set if no path
  -	// info is present
  -	pathTranslated=null;
  -	if(path==null || "".equals( path ) ) return null;
  -
  -	pathTranslated=FileUtil.safePath( context.getAbsolutePath(),
  -					  path);
  -	return pathTranslated;
  -    }
  -
  -
       // XXX XXX Servlet API conflicts with the CGI specs -
       // PathInfo should be "" if no path info is requested ( as it is in CGI ).
       // We are following the spec, but IMHO it's a bug ( in the spec )
       public String getPathInfo() {
  -        return pathInfo;
  +        return pathInfoMB.toString();
       }
   
       public void setRemoteUser(String s) {
  @@ -529,11 +491,11 @@
       }
   
       public String getServletPath() {
  -        return servletPath;
  +        return servletPathMB.toString();
       }
   
       public void setServletPath(String servletPath) {
  -	this.servletPath = servletPath;
  +	servletPathMB.setString( servletPath );
       }
   
   
  @@ -617,35 +579,35 @@
       }
   
       // -------------------- Cookies --------------------
  +    
       public int getCookieCount() {
  -	if( ! didCookies ) {
  -	    didCookies=true;
  -	    RequestUtil.processCookies( this );
  +	if( cookieCount == -1 ) {
  +	    cookieCount=0;
  +	    // compute cookies
  +	    CookieTools.processCookies( this );
   	}
  -	return cookies.size();
  +	return cookieCount;
       }
   
       public ServerCookie getCookie( int idx ) {
  -	if( ! didCookies ) {
  +	if( cookieCount == -1 ) {
   	    getCookieCount(); // will also update the cookies
   	}
  -	return (ServerCookie)cookies.elementAt(idx);
  +	return scookies[idx];
       }
   
       public void addCookie( ServerCookie c ) {
  -	cookies.addElement( c );
  -    }
  -
  -    private ServerCookie[] getCookies() {
  -	int count=getCookieCount();
  -	ServerCookie[] cookieArray = new ServerCookie[ count ];
  -
  -	for (int i = 0; i < count; i ++) {
  -	    cookieArray[i] = getCookie( i );
  +	// not really needed - happen in 1 thread
  +	synchronized ( this ) {
  +	    if( cookieCount >= scookies.length  ) {
  +		ServerCookie scookiesTmp[]=new ServerCookie[2*cookieCount];
  +		System.arraycopy( scookies, 0, scookiesTmp, 0, cookieCount);
  +		scookies=scookiesTmp;
  +	    }
  +	    scookies[cookieCount++]=c;
   	}
  -
  -	return cookieArray;
       }
  +
       // -------------------- LookupResult
       public Handler getWrapper() {
   	return handler;
  @@ -861,8 +823,7 @@
           context = null;
           attributes.clear();
           parameters.clear();
  -        cookies.removeAllElements();
  -        contentLength = -1;
  +	contentLength = -1;
           contentType = null;
           charEncoding = null;
           authType = null;
  @@ -871,18 +832,20 @@
           serverSession = null;
           didParameters = false;
           didReadFormData = false;
  -        didCookies = false;
           container=null;
           handler=null;
           jvmRoute = null;
           headers.clear(); // XXX use recycle pattern
           serverName=null;
           serverPort=8080;
  -        pathTranslated=null;
  -        pathInfo=null;
  -        pathTranslatedIsSet=false;
           sessionIdSource = null;
   	sessionId=null;
  +	
  +	for( int i=0; i< cookieCount; i++ ) {
  +	    if( scookies[i]!=null )
  +		scookies[i].recycle();
  +	}
  +	cookieCount=-1;
   
   	// counters and notes
           cntr.recycle();
  
  
  
  1.4       +85 -0     jakarta-tomcat/src/share/org/apache/tomcat/helper/CookieTools.java
  
  Index: CookieTools.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/helper/CookieTools.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CookieTools.java	2000/09/25 07:20:28	1.3
  +++ CookieTools.java	2000/10/08 21:28:57	1.4
  @@ -59,6 +59,8 @@
   
   package org.apache.tomcat.helper;
   import  org.apache.tomcat.util.*;
  +import  org.apache.tomcat.core.*;
  +import  org.apache.tomcat.session.*;
   import java.text.*;
   import java.util.*;
   
  @@ -72,6 +74,89 @@
    */
   public class CookieTools {
   
  +    // -------------------- Cookie parsing tools
  +    /** Process all Cookie headers of a request, setting them
  +     *  in a cookie vector
  +     */
  +    public static void processCookies( Request request ) {
  +	// XXX bug in original RequestImpl - might not work if multiple
  +	// cookie headers.
  +	//
  +	// XXX need to use the cookies hint in RequestAdapter
  +    	MimeHeaders mh=request.getMimeHeaders();
  +	MessageBytes cookieMB= mh.getValue( "cookie" );
  +
  +	if( cookieMB==null ) return;
  +	if( cookieMB.isNull() ) return;
  +
  +	// XXX XXX XXX TODO TODO TODO
  +	// byte b[]=cookieMB.getBytes();
  +	// 	if( b!=null ) {
  +	// 	    // this is a byte header 
  +	// 	    parseCookie( request, b, cookieMB.getLength());
  +	// 	    return;
  +	// 	}
  +
  +	// normal cookie, with a string value.
  +	// This is the original code, un-optimized - it shouldn't
  +	// happen in normal case
  +	String cookieString=cookieMB.toString();
  +	StringTokenizer tok = new StringTokenizer(cookieString,
  +						  ";", false);
  +	while (tok.hasMoreTokens()) {
  +	    String token = tok.nextToken();
  +	    int i = token.indexOf("=");
  +	    if (i > -1) {
  +		
  +		// XXX
  +		// the trims here are a *hack* -- this should
  +		// be more properly fixed to be spec compliant
  +		
  +		String name = token.substring(0, i).trim();
  +		String value = token.substring(i+1, token.length()).trim();
  +		// RFC 2109 and bug 
  +		value=stripQuote( value );
  +		ServerCookie cookie = new ServerCookie();
  +		cookie.getName().setString(name);
  +		cookie.getValue().setString(value);
  +		request.addCookie( cookie );
  +	    } else {
  +		// we have a bad cookie.... just let it go
  +	    }
  +	}
  +    }
  +
  +    /** Parse a cookie from a byte[]
  +     */
  +    public void parseCookie( Request req, byte b[], int len ) {
  +	
  +    }
  +
  +    /**
  +     *
  +     * Strips quotes from the start and end of the cookie string
  +     * This conforms to RFC 2109
  +     * 
  +     * @param value            a <code>String</code> specifying the cookie 
  +     *                         value (possibly quoted).
  +     *
  +     * @see #setValue
  +     *
  +     */
  +    private static String stripQuote( String value )
  +    {
  +	//	log("Strip quote from " + value );
  +	if (((value.startsWith("\"")) && (value.endsWith("\""))) ||
  +	    ((value.startsWith("'") && (value.endsWith("'"))))) {
  +	    try {
  +		return value.substring(1,value.length()-1);
  +	    } catch (Exception ex) { 
  +	    }
  +	}
  +	return value;
  +    }  
  +    
  +    
       /** Return the header name to set the cookie, based on cookie
        *  version
        */
  
  
  
  1.5       +0 -65     jakarta-tomcat/src/share/org/apache/tomcat/helper/RequestUtil.java
  
  Index: RequestUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/helper/RequestUtil.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- RequestUtil.java	2000/09/26 04:58:26	1.4
  +++ RequestUtil.java	2000/10/08 21:28:57	1.5
  @@ -573,69 +573,4 @@
       }
   
   
  -    // -------------------- Cookie parsing tools
  -    /** Process all Cookie headers of a request, setting them
  -     *  in a cookie vector
  -     */
  -    public static void processCookies( Request request ) {
  -	// XXX bug in original RequestImpl - might not work if multiple
  -	// cookie headers.
  -	//
  -	// XXX need to use the cookies hint in RequestAdapter
  -    	String cookieString = request.getHeader("cookie");
  -	
  -	if (cookieString != null) {
  -            StringTokenizer tok = new StringTokenizer(cookieString,
  -                                                      ";", false);
  -            while (tok.hasMoreTokens()) {
  -                String token = tok.nextToken();
  -                int i = token.indexOf("=");
  -                if (i > -1) {
  -
  -                    // XXX
  -                    // the trims here are a *hack* -- this should
  -                    // be more properly fixed to be spec compliant
  -                    
  -                    String name = token.substring(0, i).trim();
  -                    String value = token.substring(i+1, token.length()).trim();
  -		    // RFC 2109 and bug 
  -		    value=stripQuote( value );
  -                    ServerCookie cookie = new ServerCookie();
  -		    cookie.getName().setString(name);
  -		    cookie.getValue().setString(value);
  -                    request.addCookie( cookie );
  -                } else {
  -                    // we have a bad cookie.... just let it go
  -                }
  -            }
  -        }
  -    }
  -
  -    /**
  -     *
  -     * Strips quotes from the start and end of the cookie string
  -     * This conforms to RFC 2109
  -     * 
  -     * @param value            a <code>String</code> specifying the cookie 
  -     *                         value (possibly quoted).
  -     *
  -     * @see #setValue
  -     *
  -     */
  -    private static String stripQuote( String value )
  -    {
  -	//	log("Strip quote from " + value );
  -	if (((value.startsWith("\"")) && (value.endsWith("\""))) ||
  -	    ((value.startsWith("'") && (value.endsWith("'"))))) {
  -	    try {
  -		return value.substring(1,value.length()-1);
  -	    } catch (Exception ex) { 
  -	    }
  -	}
  -	return value;
  -    }  
  -    
  -
  -
  -
   }
  
  
  
  1.5       +1 -1      jakarta-tomcat/src/share/org/apache/tomcat/modules/server/Ajp12.java
  
  Index: Ajp12.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/server/Ajp12.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Ajp12.java	2000/10/06 05:18:47	1.4
  +++ Ajp12.java	2000/10/08 21:28:57	1.5
  @@ -141,7 +141,7 @@
   		    dummy = readString(ajpin, null);               
   		    req.setPathInfo( readString(ajpin, null));               
   		    //Apache parsed path-translated XXX Bug in mod_jserv !!!!!
  -		    req.setPathTranslated( readString(ajpin, null));
  +		    dummy = readString(ajpin, null);
   		    req.setQueryString( readString(ajpin, null));         
   		    req.setRemoteAddr(readString(ajpin, ""));
   		    req.setRemoteHost( readString(ajpin, ""));
  
  
  
  1.41      +10 -8     jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp12ConnectionHandler.java
  
  Index: Ajp12ConnectionHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp12ConnectionHandler.java,v
  retrieving revision 1.40
  retrieving revision 1.41
  diff -u -r1.40 -r1.41
  --- Ajp12ConnectionHandler.java	2000/10/06 05:19:12	1.40
  +++ Ajp12ConnectionHandler.java	2000/10/08 21:28:58	1.41
  @@ -233,12 +233,15 @@
   		case 1: //beginning of request
   		    methodMB.setString( readString(ajpin, null));              //Method
   		    
  -		    contextPath = readString(ajpin, null);               //Zone
  +		    //Zone
  +		    contextMB.setString(readString(ajpin, null));
  +
   		    // GS, the following commented line causes the Apache + Jserv + Tomcat
   		    // combination to hang with a 404!!!
   		    // if("ROOT".equals( contextPath ) ) contextPath="";
  -		    if("ROOT".equalsIgnoreCase( contextPath ) ) contextPath=null;
  -		    if( doLog ) d("AJP: CP=" + contextPath);
  +		    if(contextMB.equalsIgnoreCase( "ROOT" ) )
  +			contextMB.setString(null);
  +		    if( doLog ) d("AJP: CP=" + contextMB.toString());
   
   		    // XXX That wasn't used afaik, need to do something in ajp13
   		    // 		    if( contextPath!= null )
  @@ -252,12 +255,11 @@
   		    
   		    dummy = readString(ajpin, null);               //Apache document root
   		    
  -		    pathInfo = readString(ajpin, null);               //Apache parsed path-info
  -		    if( doLog ) d("AJP: PI=" + pathInfo );
  +		    pathInfoMB.setString( readString(ajpin, null));               //Apache parsed path-info
  +		    if( doLog ) d("AJP: PI=" + pathInfoMB.toString() );
   		    
   		    // XXX Bug in mod_jserv !!!!!
  -		    pathTranslated = readString(ajpin, null);               //Apache parsed path-translated
  -		    if( doLog ) d("AJP: PT=" + pathTranslated );
  +		    dummy = readString(ajpin, null);               //Apache parsed path-translated
   		    
   		    queryMB.setString( readString(ajpin, null));         //query string
   		    if( doLog ) d("AJP: QS=" + queryMB.toString() );
  @@ -280,7 +282,7 @@
   		    if( doLog ) d("AJP: Meth=" + methodMB.toString() );
   		    
   		    uriMB.setString( readString(ajpin, ""));             //request uri
  -		    if( doLog ) d("AJP: URI: " + uriMB.toString() + " CP:" + contextPath );
  +		    if( doLog ) d("AJP: URI: " + uriMB + " CP:" + contextMB );
   
   		    // XXX don't set lookup path - problems with URL rewriting.
   		    // need to be fixed.
  
  
  
  1.14      +4 -4      jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp13ConnectorRequest.java
  
  Index: Ajp13ConnectorRequest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp13ConnectorRequest.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- Ajp13ConnectorRequest.java	2000/10/06 05:19:12	1.13
  +++ Ajp13ConnectorRequest.java	2000/10/08 21:28:58	1.14
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp13ConnectorRequest.java,v 1.13 2000/10/06 05:19:12 costin Exp $
  - * $Revision: 1.13 $
  - * $Date: 2000/10/06 05:19:12 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/service/connector/Ajp13ConnectorRequest.java,v 1.14 2000/10/08 21:28:58 costin Exp $
  + * $Revision: 1.14 $
  + * $Date: 2000/10/08 21:28:58 $
    *
    * ====================================================================
    *
  @@ -173,7 +173,7 @@
               bsc = msg.getByte()) {
               switch(bsc) {
                   case SC_A_CONTEXT      :
  -                    contextPath = msg.getString();
  +                    contextMB.setString( msg.getString());
                   break;
   
                   case SC_A_SERVLET_PATH :
  
  
  
  1.11      +1 -0      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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- MessageBytes.java	2000/10/06 05:19:18	1.10
  +++ MessageBytes.java	2000/10/08 21:28:59	1.11
  @@ -147,6 +147,7 @@
        * Resets the message bytes to an uninitialized state.
        */
       public void recycle() {
  +	type=T_NULL;
   	bytes = null;
   	strValue=null;
   	//	chars=null;