// get cookie name and value first
end = set_cookie.indexOf('=', beg);
if (end == -1)
throw new ProtocolException("Bad Set-Cookie header: " + set_cookie + "\nNo '=' found "
+ "for token starting at " + "position " + beg);
curr.name = set_cookie.substring(beg, end).trim();
beg = Util.skipSpace(buf, end + 1);
int comma = set_cookie.indexOf(',', beg);
int semic = set_cookie.indexOf(';', beg);
if (comma == -1 && semic == -1)
end = len;
else if (comma == -1)
end = semic;
else if (semic == -1)
end = comma;
else
{
if (comma > semic)
end = semic;
else
{
// try to handle broken servers which put commas
// into cookie values
int eq = set_cookie.indexOf('=', comma);
if (eq > 0 && eq < semic)
end = set_cookie.lastIndexOf(',', eq);
else
end = semic;
}
}
curr.value = set_cookie.substring(beg, end).trim();
beg = end;
// now parse attributes
boolean legal = true;
parts : while (true) // parse all parts
{
if (beg >= len || buf[beg] == ',')
break;
// skip empty fields
if (buf[beg] == ';')
{
beg = Util.skipSpace(buf, beg + 1);
continue;
}
// first check for secure, as this is the only one w/o a '='
if ((beg + 6 <= len) && set_cookie.regionMatches(true, beg, "secure", 0, 6))
{
curr.secure = true;
beg += 6;
beg = Util.skipSpace(buf, beg);
if (beg < len && buf[beg] == ';') // consume ";"
beg = Util.skipSpace(buf, beg + 1);
else if (beg < len && buf[beg] != ',')
throw new ProtocolException("Bad Set-Cookie header: " + set_cookie + "\nExpected "
+ "';' or ',' at position " + beg);
continue;
}
// alright, must now be of the form x=y
end = set_cookie.indexOf('=', beg);
if (end == -1)
throw new ProtocolException("Bad Set-Cookie header: " + set_cookie + "\nNo '=' found "
+ "for token starting at " + "position " + beg);
String name = set_cookie.substring(beg, end).trim();
beg = Util.skipSpace(buf, end + 1);