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/09/24 19:33:45 UTC

cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util/net PoolTcpEndpoint.java TcpConnection.java TcpConnectionHandler.java

costin      00/09/24 10:33:45

  Modified:    src/share/org/apache/tomcat/util MessageBytes.java
               src/share/org/apache/tomcat/util/net PoolTcpEndpoint.java
                        TcpConnection.java TcpConnectionHandler.java
  Added:       src/share/org/apache/tomcat/util ServerCookie.java
  Log:
  - Added ServerCookie - a recyclable Cookie that will be used internaly.
  This follow the same pattern of recyclable objects that proved to be so
  good for performance.
  
  - comments to TcpConnectionHandler
  
  - added the very comonly called readLine to TcpConnection. This is a general
  purpose method that was replicated in too many places, and the reason for a
  lot of mess in internal interfaces ( interceptors needing to call back to
  upper facade layer just to access this method from ServletInputStream )
  
  Revision  Changes    Path
  1.8       +3 -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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- MessageBytes.java	2000/08/28 06:08:18	1.7
  +++ MessageBytes.java	2000/09/24 17:33:44	1.8
  @@ -62,6 +62,9 @@
   import java.text.*;
   import java.util.*;
   
  +// XXX XXX Need StringBuffer support !
  +
  +
   /**
    * This class is used to represent a subarray of bytes in an HTTP message.
    *
  
  
  
  1.1                  jakarta-tomcat/src/share/org/apache/tomcat/util/ServerCookie.java
  
  Index: ServerCookie.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */ 
  package org.apache.tomcat.util;
  
  import java.io.*;
  
  
  /**
   *  Server-side cookie representation.
   *   Allows recycling and uses MessageBytes as low-level
   *  representation ( and thus the byte-> char conversion can be delayed
   *  until we know the charset ).
   * 
   *  Tomcat.core uses this recyclable object to represent cookies,
   *  and the facade will convert it to the external representation.
   */
  public class ServerCookie implements Serializable {
      private MessageBytes name=new MessageBytes();
      private MessageBytes value=new MessageBytes();
  
      private MessageBytes comment=new MessageBytes();    // ;Comment=VALUE
      
      private MessageBytes domain;    // ;Domain=VALUE ... 
  
      private int maxAge = -1;	// ;Max-Age=VALUE
  				// ;Discard ... implied by maxAge < 0
      private MessageBytes path;	// ;Path=VALUE .
      private boolean secure;	// ;Secure 
      private int version = 0;	// ;Version=1
  
      
      public ServerCookie() {
  
      }
  
      public void recycle() {
  	name.recycle();
  	value.recycle();
  	comment.recycle();
  	maxAge=-1;
  	path.recycle();
  	version=0;
  	secure=false;
      }
  
      public void parse( MessageBytes input ) {
  	// Not implemented
      }
      
      
      public MessageBytes getComment() {
  	return comment;
      }
      
      public MessageBytes getDomain() {
  	return domain;
      }
  
      public void setMaxAge(int expiry) {
  	maxAge = expiry;
      }
  
      public int getMaxAge() {
  	return maxAge;
      }
      
  
      public MessageBytes getPath() {
  	return path;
      }
  
      public void setSecure(boolean flag) {
  	secure = flag;
      }
  
      public boolean getSecure() {
  	return secure;
      }
  
      public MessageBytes getName() {
  	return name;
      }
  
      public MessageBytes getValue() {
  	return value;
      }
  
      public int getVersion() {
  	return version;
      }
  
  
      public void setVersion(int v) {
  	version = v;
      }
  
      // Note -- disabled for now to allow full Netscape compatibility
      // from RFC 2068, token special case characters
      // 
      // private static final String tspecials = "()<>@,;:\\\"/[]?={} \t";
      private static final String tspecials = ",;";
  
      /*
       * Tests a string and returns true if the string counts as a 
       * reserved token in the Java language.
       * 
       * @param value		the <code>String</code> to be tested
       *
       * @return			<code>true</code> if the <code>String</code> is
       *				a reserved token; <code>false</code>
       *				if it is not			
       */
      public static boolean isToken(String value) {
  	int len = value.length();
  
  	for (int i = 0; i < len; i++) {
  	    char c = value.charAt(i);
  
  	    if (c < 0x20 || c >= 0x7f || tspecials.indexOf(c) != -1)
  		return false;
  	}
  	return true;
      }
  
  
      public static boolean checkName( String name ) {
  	if (!isToken(name)
  		|| name.equalsIgnoreCase("Comment")	// rfc2019
  		|| name.equalsIgnoreCase("Discard")	// 2019++
  		|| name.equalsIgnoreCase("Domain")
  		|| name.equalsIgnoreCase("Expires")	// (old cookies)
  		|| name.equalsIgnoreCase("Max-Age")	// rfc2019
  		|| name.equalsIgnoreCase("Path")
  		|| name.equalsIgnoreCase("Secure")
  		|| name.equalsIgnoreCase("Version")
  	    ) {
  	    return false;
  	}
  	return true;
      }
  
  }
  
  
  
  
  1.4       +5 -3      jakarta-tomcat/src/share/org/apache/tomcat/util/net/PoolTcpEndpoint.java
  
  Index: PoolTcpEndpoint.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/net/PoolTcpEndpoint.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PoolTcpEndpoint.java	2000/09/17 06:15:29	1.3
  +++ PoolTcpEndpoint.java	2000/09/24 17:33:44	1.4
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/net/PoolTcpEndpoint.java,v 1.3 2000/09/17 06:15:29 costin Exp $
  - * $Revision: 1.3 $
  - * $Date: 2000/09/17 06:15:29 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/net/PoolTcpEndpoint.java,v 1.4 2000/09/24 17:33:44 costin Exp $
  + * $Revision: 1.4 $
  + * $Date: 2000/09/24 17:33:44 $
    *
    * ====================================================================
    *
  @@ -410,4 +410,6 @@
   	    }
   	}
       }
  +
  +    
   }
  
  
  
  1.2       +45 -3     jakarta-tomcat/src/share/org/apache/tomcat/util/net/TcpConnection.java
  
  Index: TcpConnection.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/net/TcpConnection.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TcpConnection.java	2000/08/14 21:54:37	1.1
  +++ TcpConnection.java	2000/09/24 17:33:44	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/net/TcpConnection.java,v 1.1 2000/08/14 21:54:37 costin Exp $
  - * $Revision: 1.1 $
  - * $Date: 2000/08/14 21:54:37 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/net/TcpConnection.java,v 1.2 2000/09/24 17:33:44 costin Exp $
  + * $Revision: 1.2 $
  + * $Date: 2000/09/24 17:33:44 $
    *
    * ====================================================================
    *
  @@ -100,6 +100,48 @@
       public void recycle() {
           endpoint = null;
           socket = null;
  +    }
  +
  +    // Another frequent repetition
  +    public static int readLine(InputStream in, byte[] b, int off, int len)
  +	throws IOException
  +    {
  +	if (len <= 0) {
  +	    return 0;
  +	}
  +	int count = 0, c;
  +
  +	while ((c = in.read()) != -1) {
  +	    b[off++] = (byte)c;
  +	    count++;
  +	    if (c == '\n' || count == len) {
  +		break;
  +	    }
  +	}
  +	return count > 0 ? count : -1;
  +    }
  +
  +    
  +    // Usefull stuff - avoid having it replicated everywhere
  +    public static void shutdownInput(Socket socket)
  +	throws IOException
  +    {
  +	try {
  +	    InputStream is = socket.getInputStream();
  +	    int available = is.available ();
  +	    
  +	    // XXX on JDK 1.3 just socket.shutdownInput () which
  +	    // was added just to deal with such issues.
  +	    
  +	    // skip any unread (bogus) bytes
  +	    if (available > 1) {
  +		is.skip (available);
  +	    }
  +	}catch(NullPointerException npe) {
  +	    // do nothing - we are just cleaning up, this is
  +	    // a workaround for Netscape \n\r in POST - it is supposed
  +	    // to be ignored
  +	}
       }
   }
   
  
  
  
  1.2       +20 -7     jakarta-tomcat/src/share/org/apache/tomcat/util/net/TcpConnectionHandler.java
  
  Index: TcpConnectionHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/net/TcpConnectionHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TcpConnectionHandler.java	2000/08/14 21:54:37	1.1
  +++ TcpConnectionHandler.java	2000/09/24 17:33:45	1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/net/TcpConnectionHandler.java,v 1.1 2000/08/14 21:54:37 costin Exp $
  - * $Revision: 1.1 $
  - * $Date: 2000/08/14 21:54:37 $
  + * $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/net/TcpConnectionHandler.java,v 1.2 2000/09/24 17:33:45 costin Exp $
  + * $Revision: 1.2 $
  + * $Date: 2000/09/24 17:33:45 $
    *
    * ====================================================================
    *
  @@ -69,17 +69,27 @@
   import org.apache.tomcat.util.*;
   
   /**
  - * 
  + * This interface will be implemented by any object that
  + * uses TcpConnections. It is supported by the pool tcp
  + * connection manager and should be supported by future
  + * managers.
  + * The goal is to decouple the connection handler from
  + * the thread, socket and pooling complexity.
    */
   public interface TcpConnectionHandler {
  +    
       /** Add informations about the a "controler" object
        *  specific to the server. In tomcat it will be a
        *  ContextManager.
  +     *  @deprecated This has nothing to do with TcpHandling,
  +     *  was used as a workaround
        */
       public void setServer(Object manager);
   
       
       /** Used to pass config informations to the handler
  +     *  @deprecated. This has nothing to do with Tcp,
  +     *  was used as a workaround 
        */
       public void setAttribute(String name, Object value );
       
  @@ -88,15 +98,18 @@
        *
        *  It may look strange, but it's a _very_ good way to avoid synchronized
        *  methods and keep per thread data.
  -     *  You are not required to implement it, but if you do - you can save a lot
  -     *  in allocation ( since this will be called outside critical path ).
  +     *
  +     *  Assert: the object returned from init() will be passed to
  +     *  all processConnection() methods happening in the same thread.
  +     * 
        */
       public Object[] init( );
   
       /**
        *  Assert: connection!=null
        *  Assert: connection.getSocket() != null
  -     *  Assert: thData != null, result of calling init()
  +     *  Assert: thData != null and is the result of calling init()
  +     *  Assert: thData is preserved per Thread.
        */
       public void processConnection(TcpConnection connection, Object thData[]);    
   }