public void SetCookies (Uri uri, string cookieHeader)
{
if (uri == null)
throw new ArgumentNullException ("uri");
if (cookieHeader == null)
throw new ArgumentNullException ("cookieHeader");
if (cookieHeader.Length == 0)
return;
// Cookies must be separated by ',' (like documented on MSDN)
// but expires uses DAY, DD-MMM-YYYY HH:MM:SS GMT, so simple ',' search is wrong.
// See http://msdn.microsoft.com/en-us/library/aa384321%28VS.85%29.aspx
string [] jar = cookieHeader.Split (',');
string tmpCookie;
for (int i = 0; i < jar.Length; i++) {
tmpCookie = jar [i];
if (jar.Length > i + 1
&& Regex.IsMatch (jar[i],
@".*expires\s*=\s*(Mon|Tue|Wed|Thu|Fri|Sat|Sun)",
RegexOptions.IgnoreCase)
&& Regex.IsMatch (jar[i+1],
@"\s\d{2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4} \d{2}:\d{2}:\d{2} GMT",
RegexOptions.IgnoreCase)) {
tmpCookie = new StringBuilder (tmpCookie).Append (",").Append (jar [++i]).ToString ();
}
try {
Cookie c = Parse (tmpCookie);
// add default values from URI if missing from the string
if (c.Path.Length == 0) {
c.Path = uri.AbsolutePath;
} else if (!uri.AbsolutePath.StartsWith (c.Path)) {
string msg = String.Format ("'Path'='{0}' is invalid with URI", c.Path);
throw new CookieException (msg);
}
if (c.Domain.Length == 0) {
c.Domain = uri.Host;
// don't consider domain "a.b.com" as ".a.b.com"
c.ExactDomain = true;
}
AddCookie (c);
}
catch (Exception e) {
string msg = String.Format ("Could not parse cookies for '{0}'.", uri);
throw new CookieException (msg, e);
}
}
}