Class Cookie

  • All Implemented Interfaces:
    Serializable
    Direct Known Subclasses:
    Cookie2

    public class Cookie
    extends Object
    implements Serializable
    This class represents an http cookie as specified in Netscape's cookie spec; however, because not even Netscape follows their own spec, and because very few folks out there actually read specs but instead just look whether Netscape accepts their stuff, the Set-Cookie header field parser actually tries to follow what Netscape has implemented, instead of what the spec says. Additionally, the parser it will also recognize the Max-Age parameter from rfc-2109, as that uses the same header field (Set-Cookie).

    Some notes about how Netscape (4.7) parses:

    • Quoting: only quotes around the expires value are recognized as such; quotes around any other value are treated as part of the value.
    • White space: white space around names and values is ignored
    • Default path: if no path parameter is given, the path defaults to the path in the request-uri up to, but not including, the last '/'. Note that this is entirely different from what the spec says.
    • Commas and other delimiters: Netscape just parses until the next ';'. This means will allow commas etc inside values.
    Since:
    V0.3
    Version:
    0.3-3 06/05/2001
    Author:
    Ronald Tschal�r
    See Also:
    Serialized Form
    • Field Detail

      • value

        protected String value
      • expires

        protected Date expires
      • domain

        protected String domain
      • secure

        protected boolean secure
      • httpOnly

        protected boolean httpOnly
    • Constructor Detail

      • Cookie

        public Cookie​(String name,
                      String value,
                      String domain,
                      String path,
                      Date expires,
                      boolean secure)
        Create a cookie.
        Parameters:
        name - the cookie name
        value - the cookie value
        domain - the host this cookie will be sent to
        path - the path prefix for which this cookie will be sent
        expires - the Date this cookie expires, null if at end of session
        secure - if true this cookie will only be over secure connections
        Throws:
        NullPointerException - if name, value, domain, or path is null
        Since:
        V0.3-1
    • Method Detail

      • parse

        protected static Cookie[] parse​(String set_cookie,
                                        RoRequest req)
                                 throws ProtocolException
        Parses the Set-Cookie header into an array of Cookies.
        Parameters:
        set_cookie - the Set-Cookie header received from the server
        req - the request used
        Returns:
        an array of Cookies as parsed from the Set-Cookie header
        Throws:
        ProtocolException - if an error occurs during parsing
      • getName

        public String getName()
        Return the name of this cookie.
      • getValue

        public String getValue()
        Return the value of this cookie.
      • expires

        public Date expires()
        Returns:
        the expiry date of this cookie, or null if none set.
      • discard

        public boolean discard()
        Returns:
        true if the cookie should be discarded at the end of the session; false otherwise
      • getDomain

        public String getDomain()
        Return the domain this cookie is valid in.
      • getPath

        public String getPath()
        Return the path this cookie is associated with.
      • isSecure

        public boolean isSecure()
        Return whether this cookie should only be sent over secure connections.
      • isHttpOnly

        public boolean isHttpOnly()
        Return true if cookie have httponly activated.
      • hasExpired

        public boolean hasExpired()
        Returns:
        true if this cookie has expired
      • sendWith

        protected boolean sendWith​(RoRequest req)
        Parameters:
        req - the request to be sent
        Returns:
        true if this cookie should be sent with the request
      • hashCode

        public int hashCode()
        Hash up name, path and domain into new hash.
        Overrides:
        hashCode in class Object
      • equals

        public boolean equals​(Object obj)
        Two cookies match if the name, path and domain match.
        Overrides:
        equals in class Object
      • toExternalForm

        protected String toExternalForm()
        Returns:
        a string suitable for sending in a Cookie header.
      • toString

        public String toString()
        Create a string containing all the cookie fields. The format is that used in the Set-Cookie header.
        Overrides:
        toString in class Object