/// <summary> /// Adds or updates a <see cref="Cookie"/> in the <see cref="Cookies"/> sent with the response. /// </summary> /// <param name="cookie"> /// A <see cref="Cookie"/> to set. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="cookie"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="cookie"/> already exists in the <see cref="Cookies"/> and /// could not be replaced. /// </exception> public void SetCookie(Cookie cookie) { if (cookie.IsNull()) { throw new ArgumentNullException("cookie"); } if (!CanAddOrUpdate(cookie)) { throw new ArgumentException("Cannot be replaced.", "cookie"); } Cookies.Add(cookie); }
/// <summary> /// Add the specified <see cref="Cookie"/> to the <see cref="CookieCollection"/>. /// </summary> /// <param name="cookie"> /// A <see cref="Cookie"/> to add to the <see cref="CookieCollection"/>. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="cookie"/> is <see langword="null"/>. /// </exception> public void Add(Cookie cookie) { if (cookie.IsNull()) { throw new ArgumentNullException("cookie"); } int pos = SearchCookie(cookie); if (pos == -1) { list.Add(cookie); } else { list [pos] = cookie; } }
static CookieCollection ParseResponse(string value) { var cookies = new CookieCollection(); Cookie cookie = null; string [] pairs = Split(value).ToArray(); for (int i = 0; i < pairs.Length; i++) { string pair = pairs [i].Trim(); if (pair.IsEmpty()) { continue; } if (pair.StartsWith("version", StringComparison.InvariantCultureIgnoreCase)) { if (!cookie.IsNull()) { cookie.Version = Int32.Parse(pair.GetValueInternal("=").Trim('"')); } } else if (pair.StartsWith("expires", StringComparison.InvariantCultureIgnoreCase)) { var buffer = new StringBuilder(pair.GetValueInternal("="), 32); if (i < pairs.Length - 1) { buffer.AppendFormat(", {0}", pairs [++i].Trim()); } DateTime expires; if (!DateTime.TryParseExact(buffer.ToString(), new string [] { "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", "r" }, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out expires)) { expires = DateTime.Now; } if (!cookie.IsNull() && cookie.Expires == DateTime.MinValue) { cookie.Expires = expires.ToLocalTime(); } } else if (pair.StartsWith("max-age", StringComparison.InvariantCultureIgnoreCase)) { int max = Int32.Parse(pair.GetValueInternal("=").Trim('"')); var expires = DateTime.Now.AddSeconds((double)max); if (!cookie.IsNull()) { cookie.Expires = expires; } } else if (pair.StartsWith("path", StringComparison.InvariantCultureIgnoreCase)) { if (!cookie.IsNull()) { cookie.Path = pair.GetValueInternal("="); } } else if (pair.StartsWith("domain", StringComparison.InvariantCultureIgnoreCase)) { if (!cookie.IsNull()) { cookie.Domain = pair.GetValueInternal("="); } } else if (pair.StartsWith("port", StringComparison.InvariantCultureIgnoreCase)) { var port = pair.Equals("port", StringComparison.InvariantCultureIgnoreCase) ? "\"\"" : pair.GetValueInternal("="); if (!cookie.IsNull()) { cookie.Port = port; } } else if (pair.StartsWith("comment", StringComparison.InvariantCultureIgnoreCase)) { if (!cookie.IsNull()) { cookie.Comment = pair.GetValueInternal("=").UrlDecode(); } } else if (pair.StartsWith("commenturl", StringComparison.InvariantCultureIgnoreCase)) { if (!cookie.IsNull()) { cookie.CommentUri = pair.GetValueInternal("=").Trim('"').ToUri(); } } else if (pair.StartsWith("discard", StringComparison.InvariantCultureIgnoreCase)) { if (!cookie.IsNull()) { cookie.Discard = true; } } else if (pair.StartsWith("secure", StringComparison.InvariantCultureIgnoreCase)) { if (!cookie.IsNull()) { cookie.Secure = true; } } else if (pair.StartsWith("httponly", StringComparison.InvariantCultureIgnoreCase)) { if (!cookie.IsNull()) { cookie.HttpOnly = true; } } else { if (!cookie.IsNull()) { cookies.Add(cookie); } string name; string val = String.Empty; int pos = pair.IndexOf('='); if (pos == -1) { name = pair; } else if (pos == pair.Length - 1) { name = pair.Substring(0, pos).TrimEnd(' '); } else { name = pair.Substring(0, pos).TrimEnd(' '); val = pair.Substring(pos + 1).TrimStart(' '); } cookie = new Cookie(name, val); } } if (!cookie.IsNull()) { cookies.Add(cookie); } return(cookies); }
/// <summary> /// Sets a <see cref="Cookie"/> used in the WebSocket opening handshake. /// </summary> /// <param name="cookie"> /// A <see cref="Cookie"/> that contains an HTTP Cookie to set. /// </param> public void SetCookie(Cookie cookie) { var msg = _readyState == WsState.OPEN ? "The WebSocket connection has been established already." : cookie.IsNull() ? "'cookie' must not be null." : String.Empty; if (!msg.IsEmpty()) { onError(msg); return; } lock (_cookies.SyncRoot) { _cookies.SetOrRemove(cookie); } }
static CookieCollection ParseRequest(string value) { var cookies = new CookieCollection(); Cookie cookie = null; int version = 0; string [] pairs = Split(value).ToArray(); for (int i = 0; i < pairs.Length; i++) { string pair = pairs [i].Trim(); if (pair.IsEmpty()) { continue; } if (pair.StartsWith("$version", StringComparison.InvariantCultureIgnoreCase)) { version = Int32.Parse(pair.GetValueInternal("=").Trim('"')); } else if (pair.StartsWith("$path", StringComparison.InvariantCultureIgnoreCase)) { if (!cookie.IsNull()) { cookie.Path = pair.GetValueInternal("="); } } else if (pair.StartsWith("$domain", StringComparison.InvariantCultureIgnoreCase)) { if (!cookie.IsNull()) { cookie.Domain = pair.GetValueInternal("="); } } else if (pair.StartsWith("$port", StringComparison.InvariantCultureIgnoreCase)) { var port = pair.Equals("$port", StringComparison.InvariantCultureIgnoreCase) ? "\"\"" : pair.GetValueInternal("="); if (!cookie.IsNull()) { cookie.Port = port; } } else { if (!cookie.IsNull()) { cookies.Add(cookie); } string name; string val = String.Empty; int pos = pair.IndexOf('='); if (pos == -1) { name = pair; } else if (pos == pair.Length - 1) { name = pair.Substring(0, pos).TrimEnd(' '); } else { name = pair.Substring(0, pos).TrimEnd(' '); val = pair.Substring(pos + 1).TrimStart(' '); } cookie = new Cookie(name, val); if (version != 0) { cookie.Version = version; } } } if (!cookie.IsNull()) { cookies.Add(cookie); } return(cookies); }