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 2002/03/09 00:50:31 UTC

cvs commit: jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4 CoyoteProcessor.java CoyoteRequest.java CoyoteResponse.java

remm        02/03/08 15:50:31

  Modified:    coyote/src/java/org/apache/coyote/tomcat4
                        CoyoteProcessor.java CoyoteRequest.java
                        CoyoteResponse.java
  Log:
  - Implement cookies and sessions.
  - One difference with Catalina is that the session cookie is added when the
    session is created. I couldn't find any drawback to this, and this is easier to
    implement. If there is a problem with tha, let me know.
  
  Revision  Changes    Path
  1.9       +43 -5     jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteProcessor.java
  
  Index: CoyoteProcessor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteProcessor.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- CoyoteProcessor.java	8 Mar 2002 20:52:27 -0000	1.8
  +++ CoyoteProcessor.java	8 Mar 2002 23:50:31 -0000	1.9
  @@ -1,6 +1,6 @@
  -/* * $Header: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteProcessor.java,v 1.8 2002/03/08 20:52:27 remm Exp $
  - * $Revision: 1.8 $
  - * $Date: 2002/03/08 20:52:27 $
  +/* * $Header: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteProcessor.java,v 1.9 2002/03/08 23:50:31 remm Exp $
  + * $Revision: 1.9 $
  + * $Date: 2002/03/08 23:50:31 $
    *
    * ====================================================================
    *
  @@ -73,6 +73,7 @@
   import java.net.InetAddress;
   import java.net.Socket;
   import java.util.ArrayList;
  +import java.util.Enumeration;
   import java.util.Iterator;
   import java.util.Locale;
   import java.util.StringTokenizer;
  @@ -111,7 +112,7 @@
    *
    * @author Craig R. McClanahan
    * @author Remy Maucherat
  - * @version $Revision: 1.8 $ $Date: 2002/03/08 20:52:27 $
  + * @version $Revision: 1.9 $ $Date: 2002/03/08 23:50:31 $
    */
   
   final class CoyoteProcessor
  @@ -372,7 +373,7 @@
           req.scheme().setString(connector.getScheme());
   
           parseHost();
  -        // parseSession(req);
  +        parseCookies();
   
       }
   
  @@ -415,6 +416,43 @@
                       }
                       request.setServerPort(port);
                   }
  +            }
  +        }
  +
  +    }
  +
  +
  +    /**
  +     * Parse cookies.
  +     * Note: Using Coyote native cookie parser to parse cookies would be faster
  +     * but a conversion to Catalina own cookies would then be needed, which 
  +     * would take away most if not all of theperformance benefit.
  +     */
  +    protected void parseCookies() {
  +
  +        Enumeration values = request.getHeaders("cookie");
  +        while (values.hasMoreElements()) {
  +            String value = values.nextElement().toString();
  +            Cookie cookies[] = RequestUtil.parseCookieHeader(value);
  +            for (int i = 0; i < cookies.length; i++) {
  +                if (cookies[i].getName().equals
  +                    (Globals.SESSION_COOKIE_NAME)) {
  +                    // Override anything requested in the URL
  +                    if (!request.isRequestedSessionIdFromCookie()) {
  +                        // Accept only the first session id cookie
  +                        request.setRequestedSessionId(cookies[i].getValue());
  +                        request.setRequestedSessionCookie(true);
  +                        request.setRequestedSessionURL(false);
  +                        if (debug >= 1)
  +                            log(" Requested cookie session id is " +
  +                                ((HttpServletRequest) request.getRequest())
  +                                .getRequestedSessionId());
  +                    }
  +                }
  +                if (debug >= 1)
  +                    log(" Adding cookie " + cookies[i].getName() + "=" +
  +                        cookies[i].getValue());
  +                request.addCookie(cookies[i]);
               }
           }
   
  
  
  
  1.6       +182 -14   jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteRequest.java
  
  Index: CoyoteRequest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteRequest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- CoyoteRequest.java	8 Mar 2002 20:52:27 -0000	1.5
  +++ CoyoteRequest.java	8 Mar 2002 23:50:31 -0000	1.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteRequest.java,v 1.5 2002/03/08 20:52:27 remm Exp $
  - * $Revision: 1.5 $
  - * $Date: 2002/03/08 20:52:27 $
  + * $Header: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteRequest.java,v 1.6 2002/03/08 23:50:31 remm Exp $
  + * $Revision: 1.6 $
  + * $Date: 2002/03/08 23:50:31 $
    *
    * ====================================================================
    *
  @@ -71,7 +71,9 @@
   import java.io.BufferedReader;
   import java.io.UnsupportedEncodingException;
   import java.net.Socket;
  +import java.security.AccessController;
   import java.security.Principal;
  +import java.security.PrivilegedAction;
   import java.text.ParseException;
   import java.text.SimpleDateFormat;
   import java.util.ArrayList;
  @@ -89,6 +91,7 @@
   import javax.servlet.ServletRequest;
   import javax.servlet.http.Cookie;
   import javax.servlet.http.HttpServletRequest;
  +import javax.servlet.http.HttpServletResponse;
   import javax.servlet.http.HttpSession;
   
   import org.apache.tomcat.util.http.Parameters;
  @@ -117,13 +120,32 @@
    *
    * @author Remy Maucherat
    * @author Craig R. McClanahan
  - * @version $Revision: 1.5 $ $Date: 2002/03/08 20:52:27 $
  + * @version $Revision: 1.6 $ $Date: 2002/03/08 23:50:31 $
    */
   
   public class CoyoteRequest
       implements HttpRequest, HttpServletRequest {
   
   
  +    // --------------------------------------- PrivilegedGetSession Inner Class
  +
  +
  +    protected class PrivilegedGetSession
  +        implements PrivilegedAction {
  +
  +        private boolean create;
  +
  +        PrivilegedGetSession(boolean create) {
  +            this.create = create;
  +        }
  +
  +        public Object run() {
  +            return doGetSession(create);
  +        }
  +
  +    }
  +
  +
       // ------------------------------------------------------------- Properties
   
   
  @@ -301,6 +323,36 @@
       protected ParameterMap parameterMap = new ParameterMap();
   
   
  +    /**
  +     * The currently active session for this request.
  +     */
  +    protected Session session = null;
  +
  +
  +    /**
  +     * Was the requested session ID received in a cookie?
  +     */
  +    protected boolean requestedSessionCookie = false;
  +
  +
  +    /**
  +     * The requested session ID (if any) for this request.
  +     */
  +    protected String requestedSessionId = null;
  +
  +
  +    /**
  +     * Was the requested session ID received in a URL?
  +     */
  +    protected boolean requestedSessionURL = false;
  +
  +
  +    /**
  +     * The socket through which this Request was received.
  +     */
  +    protected Socket socket = null;
  +
  +
       // --------------------------------------------------------- Public Methods
   
   
  @@ -310,6 +362,8 @@
        */
       public void recycle() {
   
  +        socket = null;
  +
           context = null;
           wrapper = null;
   
  @@ -331,6 +385,11 @@
           notes.clear();
           cookies.clear();
   
  +        session = null;
  +        requestedSessionCookie = false;
  +        requestedSessionId = null;
  +        requestedSessionURL = false;
  +
           parameterMap.setLocked(false);
           parameterMap.clear();
   
  @@ -475,7 +534,7 @@
        * an SSLSocket.
        */
       public Socket getSocket() {
  -        return (null); // FIXME: Return a note
  +        return (socket);
       }
   
       /**
  @@ -484,6 +543,7 @@
        * @param socket The socket through which this request was received
        */
       public void setSocket(Socket socket) {
  +        this.socket = socket;
       }
   
   
  @@ -1084,8 +1144,7 @@
        * @param value The new header value
        */
       public void addHeader(String name, String value) {
  -        // Not used ?
  -        System.out.println("coyoteRequest.addHeader(name, value)");
  +        // Not used
       }
   
   
  @@ -1216,7 +1275,9 @@
        * @param flag The new flag
        */
       public void setRequestedSessionCookie(boolean flag) {
  -        // Not used
  +
  +        this.requestedSessionCookie = flag;
  +
       }
   
   
  @@ -1227,7 +1288,9 @@
        * @param id The new session id
        */
       public void setRequestedSessionId(String id) {
  -        // Not used
  +
  +        this.requestedSessionId = id;
  +
       }
   
   
  @@ -1239,7 +1302,9 @@
        * @param flag The new flag
        */
       public void setRequestedSessionURL(boolean flag) {
  -        // Not used
  +
  +        this.requestedSessionURL = flag;
  +
       }
   
   
  @@ -1532,7 +1597,13 @@
        * @param create Create a new session if one does not exist
        */
       public HttpSession getSession(boolean create) {
  -        return null;
  +
  +        if (System.getSecurityManager() != null) {
  +            PrivilegedGetSession dp = new PrivilegedGetSession(create);
  +            return (HttpSession) AccessController.doPrivileged(dp);
  +        }
  +        return doGetSession(create);
  +
       }
   
   
  @@ -1541,7 +1612,12 @@
        * request came from a cookie.
        */
       public boolean isRequestedSessionIdFromCookie() {
  -        return false;
  +
  +        if (requestedSessionId != null)
  +            return (requestedSessionCookie);
  +        else
  +            return (false);
  +
       }
   
   
  @@ -1550,7 +1626,12 @@
        * request came from the request URI.
        */
       public boolean isRequestedSessionIdFromURL() {
  -        return false;
  +
  +        if (requestedSessionId != null)
  +            return (requestedSessionURL);
  +        else
  +            return (false);
  +
       }
   
   
  @@ -1571,7 +1652,25 @@
        * request identifies a valid session.
        */
       public boolean isRequestedSessionIdValid() {
  -        return false;
  +
  +        if (requestedSessionId == null)
  +            return (false);
  +        if (context == null)
  +            return (false);
  +        Manager manager = context.getManager();
  +        if (manager == null)
  +            return (false);
  +        Session session = null;
  +        try {
  +            session = manager.findSession(requestedSessionId);
  +        } catch (IOException e) {
  +            session = null;
  +        }
  +        if ((session != null) && session.isValid())
  +            return (true);
  +        else
  +            return (false);
  +
       }
   
   
  @@ -1617,6 +1716,75 @@
   
   
       // ------------------------------------------------------ Protected Methods
  +
  +
  +    protected HttpSession doGetSession(boolean create) {
  +
  +        // There cannot be a session if no context has been assigned yet
  +        if (context == null)
  +            return (null);
  +
  +        // Return the current session if it exists and is valid
  +        if ((session != null) && !session.isValid())
  +            session = null;
  +        if (session != null)
  +            return (session.getSession());
  +
  +        // Return the requested session if it exists and is valid
  +        Manager manager = null;
  +        if (context != null)
  +            manager = context.getManager();
  +        if (manager == null)
  +            return (null);      // Sessions are not supported
  +        if (requestedSessionId != null) {
  +            try {
  +                session = manager.findSession(requestedSessionId);
  +            } catch (IOException e) {
  +                session = null;
  +            }
  +            if ((session != null) && !session.isValid())
  +                session = null;
  +            if (session != null) {
  +                return (session.getSession());
  +            }
  +        }
  +
  +        // Create a new session if requested and the response is not committed
  +        if (!create)
  +            return (null);
  +        if ((context != null) && (response != null) &&
  +            context.getCookies() &&
  +            response.getResponse().isCommitted()) {
  +            throw new IllegalStateException
  +              (sm.getString("httpRequestBase.createCommitted"));
  +        }
  +
  +        session = manager.createSession();
  +
  +        // Creating a new session cookie based on that session
  +        if ((session != null) && (getContext() != null)
  +            && getContext().getCookies()) {
  +            Cookie cookie = new Cookie(Globals.SESSION_COOKIE_NAME,
  +                                       session.getId());
  +            cookie.setMaxAge(-1);
  +            String contextPath = null;
  +            if (context != null)
  +                contextPath = context.getPath();
  +            if ((contextPath != null) && (contextPath.length() > 0))
  +                cookie.setPath(contextPath);
  +            else
  +                cookie.setPath("/");
  +            if (isSecure())
  +                cookie.setSecure(true);
  +            ((HttpServletResponse) response).addCookie(cookie);
  +        }
  +
  +        if (session != null)
  +            return (session.getSession());
  +        else
  +            return (null);
  +
  +    }
   
   
       /**
  
  
  
  1.5       +15 -4     jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteResponse.java
  
  Index: CoyoteResponse.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteResponse.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CoyoteResponse.java	8 Mar 2002 05:13:28 -0000	1.4
  +++ CoyoteResponse.java	8 Mar 2002 23:50:31 -0000	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteResponse.java,v 1.4 2002/03/08 05:13:28 remm Exp $
  - * $Revision: 1.4 $
  - * $Date: 2002/03/08 05:13:28 $
  + * $Header: /home/cvs/jakarta-tomcat-connectors/coyote/src/java/org/apache/coyote/tomcat4/CoyoteResponse.java,v 1.5 2002/03/08 23:50:31 remm Exp $
  + * $Revision: 1.5 $
  + * $Date: 2002/03/08 23:50:31 $
    *
    * ====================================================================
    *
  @@ -90,6 +90,7 @@
   import javax.servlet.http.HttpUtils;
   
   import org.apache.tomcat.util.http.MimeHeaders;
  +import org.apache.tomcat.util.http.ServerCookie;
   
   import org.apache.coyote.Response;
   
  @@ -112,7 +113,7 @@
    *
    * @author Remy Maucherat
    * @author Craig R. McClanahan
  - * @version $Revision: 1.4 $ $Date: 2002/03/08 05:13:28 $
  + * @version $Revision: 1.5 $ $Date: 2002/03/08 23:50:31 $
    */
   
   public class CoyoteResponse
  @@ -805,6 +806,16 @@
               return;
   
           cookies.add(cookie);
  +
  +        StringBuffer sb = new StringBuffer();
  +        ServerCookie.appendCookieValue
  +            (sb, cookie.getVersion(), cookie.getName(), cookie.getValue(),
  +             cookie.getPath(), cookie.getDomain(), cookie.getComment(), 
  +             cookie.getMaxAge(), cookie.getSecure());
  +        // the header name is Set-Cookie for both "old" and v.1 ( RFC2109 )
  +        // RFC2965 is not supported by browsers and the Servlet spec
  +        // asks for 2109.
  +        addHeader("Set-Cookie", sb.toString());
   
       }
   
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>