/*
* @(#)Cookie2.java 0.3-3 06/05/2001
*
* This file is part of the HTTPClient package
* Copyright (C) 1996-2001 Ronald Tschalär
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* I may be contacted at:
*
* ronald@innovation.ch
*
* The HTTPClient's home page is located at:
*
* http://www.innovation.ch/java/HTTPClient/
*
*/
package HTTPClient;
import java.io.UnsupportedEncodingException;
import java.net.ProtocolException;
import java.util.Date;
import java.util.Vector;
import java.util.StringTokenizer;
/**
* This class represents an http cookie as specified in the HTTP State Management Mechanism spec
* (also known as a version 1 cookie).
*
* @version 0.3-3 06/05/2001
* @author Ronald Tschalär
* @since V0.3
*/
public class Cookie2 extends Cookie
{
/** Make this compatible with V0.3-2 */
private static final long serialVersionUID = 2208203902820875917L;
protected int version;
protected boolean discard;
protected String comment;
protected URI comment_url;
protected int[] port_list;
protected String port_list_str;
protected boolean path_set;
protected boolean port_set;
protected boolean domain_set;
/**
* Create a cookie.
*
* @param name the cookie name
* @param value the cookie value
* @param domain the host this cookie will be sent to
* @param port_list an array of allowed server ports for this cookie,
* or null if the the cookie may be sent to any port
* @param path the path prefix for which this cookie will be sent
* @param epxires the Date this cookie expires, or null if never
* @param discard if true then the cookie will be discarded at the
* end of the session regardless of expiry
* @param secure if true this cookie will only be over secure connections
* @param comment the comment associated with this cookie, or null if none
* @param comment_url the comment URL associated with this cookie, or null
* if none
* @exception NullPointerException if name, value,
* domain, or path
* is null
*/
public Cookie2(String name, String value, String domain, int[] port_list,
String path, Date expires, boolean discard, boolean secure,
String comment, URI comment_url)
{
super(name, value, domain, path, expires, secure);
this.discard = discard;
this.port_list = port_list;
this.comment = comment;
this.comment_url = comment_url;
path_set = true;
domain_set = true;
if (port_list != null && port_list.length > 0)
{
StringBuffer tmp = new StringBuffer();
tmp.append(port_list[0]);
for (int idx=1; idxparse() to create cookies.
*
* @see #parse(java.lang.String, HTTPClient.RoRequest)
*/
protected Cookie2(RoRequest req)
{
super(req);
path = Util.getPath(req.getRequestURI());
int slash = path.lastIndexOf('/');
if (slash != -1) path = path.substring(0, slash+1);
if (domain.indexOf('.') == -1) domain += ".local";
version = -1;
discard = false;
comment = null;
comment_url = null;
port_list = null;
port_list_str = null;
path_set = false;
port_set = false;
domain_set = false;
}
/**
* Parses the Set-Cookie2 header into an array of Cookies.
*
* @param set_cookie the Set-Cookie2 header received from the server
* @param req the request used
* @return an array of Cookies as parsed from the Set-Cookie2 header
* @exception ProtocolException if an error occurs during parsing
*/
protected static Cookie[] parse(String set_cookie, RoRequest req)
throws ProtocolException
{
Vector cookies;
try
{ cookies = Util.parseHeader(set_cookie); }
catch (ParseException pe)
{ throw new ProtocolException(pe.getMessage()); }
Cookie cookie_arr[] = new Cookie[cookies.size()];
int cidx=0;
for (int idx=0; idx