{
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 < cookie_arr.length; idx++)
{
HttpHeaderElement c_elem = (HttpHeaderElement)cookies.elementAt(idx);
// set NAME and VALUE
if (c_elem.getValue() == null)
throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nMissing value " + "for cookie '"
+ c_elem.getName() + "'");
Cookie2 curr = new Cookie2(req);
curr.name = c_elem.getName();
curr.value = c_elem.getValue();
// set all params
NVPair[] params = c_elem.getParams();
boolean discard_set = false, secure_set = false;
for (int idx2 = 0; idx2 < params.length; idx2++)
{
String name = params[idx2].getName().toLowerCase();
// check for required value parts
if ((name.equals("version") || name.equals("max-age") || name.equals("domain") || name.equals("path")
|| name.equals("comment") || name.equals("commenturl"))
&& params[idx2].getValue() == null)
{
throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nMissing value " + "for "
+ params[idx2].getName() + " attribute in cookie '" + c_elem.getName() + "'");
}
if (name.equals("version")) // Version
{
if (curr.version != -1)
continue;
try
{
curr.version = Integer.parseInt(params[idx2].getValue());
}
catch (NumberFormatException nfe)
{
throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nVersion '"
+ params[idx2].getValue() + "' not a number");
}
}
else if (name.equals("path")) // Path
{
if (curr.path_set)
continue;
curr.path = params[idx2].getValue();
curr.path_set = true;
}
else if (name.equals("domain")) // Domain
{
if (curr.domain_set)
continue;
String d = params[idx2].getValue().toLowerCase();
// add leading dot if not present and if domain is
// not the full host name
if (d.charAt(0) != '.' && !d.equals(curr.domain))
curr.domain = "." + d;
else
curr.domain = d;
curr.domain_set = true;
}
else if (name.equals("max-age")) // Max-Age
{
if (curr.expires != null)
continue;
int age;
try
{
age = Integer.parseInt(params[idx2].getValue());
}
catch (NumberFormatException nfe)
{
throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nMax-Age '"
+ params[idx2].getValue() + "' not a number");
}
curr.expires = new Date(System.currentTimeMillis() + age * 1000L);
}
else if (name.equals("port")) // Port
{
if (curr.port_set)
continue;
if (params[idx2].getValue() == null)
{
curr.port_list = new int[1];
curr.port_list[0] = req.getConnection().getPort();
curr.port_set = true;
continue;
}
curr.port_list_str = params[idx2].getValue();
StringTokenizer tok = new StringTokenizer(params[idx2].getValue(), ",");
curr.port_list = new int[tok.countTokens()];
for (int idx3 = 0; idx3 < curr.port_list.length; idx3++)
{
String port = tok.nextToken().trim();
try
{
curr.port_list[idx3] = Integer.parseInt(port);
}
catch (NumberFormatException nfe)
{
throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nPort '" + port
+ "' not a number");
}
}
curr.port_set = true;
}
else if (name.equals("discard")) // Domain
{
if (discard_set)
continue;
curr.discard = true;
discard_set = true;
}
else if (name.equals("secure")) // Secure
{
if (secure_set)
continue;
curr.secure = true;
secure_set = true;
}
else if (name.equals("comment")) // Comment
{
if (curr.comment != null)
continue;
try
{
curr.comment = new String(params[idx2].getValue().getBytes("8859_1"), "UTF8");
}
catch (UnsupportedEncodingException usee)
{
throw new Error(usee.toString()); /* shouldn't happen */
}
}
else if (name.equals("commenturl")) // CommentURL
{
if (curr.comment_url != null)
continue;
try
{
curr.comment_url = new URI(params[idx2].getValue());
}
catch (ParseException pe)
{
throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nCommentURL '"
+ params[idx2].getValue() + "' not a valid URL");
}
}
// ignore unknown element
}