private static CookieCollection parseResponse(string value) { var ret = new CookieCollection(); Cookie cookie = null; var caseInsensitive = StringComparison.InvariantCultureIgnoreCase; var pairs = value.SplitHeaderValue(',', ';').ToList(); for (var i = 0; i < pairs.Count; i++) { var pair = pairs[i].Trim(); if (pair.Length == 0) { continue; } var idx = pair.IndexOf('='); if (idx == -1) { if (cookie == null) { continue; } if (pair.Equals("port", caseInsensitive)) { cookie.Port = "\"\""; continue; } if (pair.Equals("discard", caseInsensitive)) { cookie.Discard = true; continue; } if (pair.Equals("secure", caseInsensitive)) { cookie.Secure = true; continue; } if (pair.Equals("httponly", caseInsensitive)) { cookie.HttpOnly = true; continue; } continue; } if (idx == 0) { if (cookie != null) { ret.add(cookie); cookie = null; } continue; } var name = pair.Substring(0, idx).TrimEnd(' '); var val = idx < pair.Length - 1 ? pair.Substring(idx + 1).TrimStart(' ') : String.Empty; if (name.Equals("version", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } int num; if (!Int32.TryParse(val.Unquote(), out num)) { continue; } cookie.Version = num; continue; } if (name.Equals("expires", caseInsensitive)) { if (val.Length == 0) { continue; } if (i == pairs.Count - 1) { break; } i++; if (cookie == null) { continue; } if (cookie.Expires != DateTime.MinValue) { continue; } var buff = new StringBuilder(val, 32); buff.AppendFormat(", {0}", pairs[i].Trim()); DateTime expires; if ( !DateTime.TryParseExact( buff.ToString(), new[] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" }, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out expires ) ) { continue; } cookie.Expires = expires.ToLocalTime(); continue; } if (name.Equals("max-age", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } int num; if (!Int32.TryParse(val.Unquote(), out num)) { continue; } cookie.MaxAge = num; continue; } if (name.Equals("path", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } cookie.Path = val; continue; } if (name.Equals("domain", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } cookie.Domain = val; continue; } if (name.Equals("port", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } cookie.Port = val; continue; } if (name.Equals("comment", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } cookie.Comment = urlDecode(val, Encoding.UTF8); continue; } if (name.Equals("commenturl", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } cookie.CommentUri = val.Unquote().ToUri(); continue; } if (name.Equals("samesite", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } cookie.SameSite = val.Unquote(); continue; } if (cookie != null) { ret.add(cookie); } Cookie.TryCreate(name, val, out cookie); } if (cookie != null) { ret.add(cookie); } return(ret); }
private static CookieCollection parseRequest(string value) { var ret = new CookieCollection(); Cookie cookie = null; var ver = 0; var caseInsensitive = StringComparison.InvariantCultureIgnoreCase; var pairs = value.SplitHeaderValue(',', ';').ToList(); for (var i = 0; i < pairs.Count; i++) { var pair = pairs[i].Trim(); if (pair.Length == 0) { continue; } var idx = pair.IndexOf('='); if (idx == -1) { if (cookie == null) { continue; } if (pair.Equals("$port", caseInsensitive)) { cookie.Port = "\"\""; continue; } continue; } if (idx == 0) { if (cookie != null) { ret.add(cookie); cookie = null; } continue; } var name = pair.Substring(0, idx).TrimEnd(' '); var val = idx < pair.Length - 1 ? pair.Substring(idx + 1).TrimStart(' ') : String.Empty; if (name.Equals("$version", caseInsensitive)) { if (val.Length == 0) { continue; } int num; if (!Int32.TryParse(val.Unquote(), out num)) { continue; } ver = num; continue; } if (name.Equals("$path", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } cookie.Path = val; continue; } if (name.Equals("$domain", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } cookie.Domain = val; continue; } if (name.Equals("$port", caseInsensitive)) { if (cookie == null) { continue; } if (val.Length == 0) { continue; } cookie.Port = val; continue; } if (cookie != null) { ret.add(cookie); } if (!Cookie.TryCreate(name, val, out cookie)) { continue; } if (ver != 0) { cookie.Version = ver; } } if (cookie != null) { ret.add(cookie); } return(ret); }