123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029 |
- #region License
- /*
- * Cookie.cs
- *
- * This code is derived from Cookie.cs (System.Net) of Mono
- * (http://www.mono-project.com).
- *
- * The MIT License
- *
- * Copyright (c) 2004,2009 Novell, Inc. (http://www.novell.com)
- * Copyright (c) 2012-2023 sta.blockhead
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- #endregion
- #region Authors
- /*
- * Authors:
- * - Lawrence Pit <loz@cable.a2000.nl>
- * - Gonzalo Paniagua Javier <gonzalo@ximian.com>
- * - Daniel Nauck <dna@mono-project.de>
- * - Sebastien Pouliot <sebastien@ximian.com>
- */
- #endregion
- using System;
- using System.Globalization;
- using System.Text;
- namespace WebSocketSharp.Net
- {
- /// <summary>
- /// Provides a set of methods and properties used to manage an HTTP cookie.
- /// </summary>
- /// <remarks>
- /// <para>
- /// This class refers to the following specifications:
- /// </para>
- /// <list type="bullet">
- /// <item>
- /// <term>
- /// <see href="http://web.archive.org/web/20020803110822/http://wp.netscape.com/newsref/std/cookie_spec.html">
- /// Netscape specification</see>
- /// </term>
- /// </item>
- /// <item>
- /// <term>
- /// <see href="https://tools.ietf.org/html/rfc2109">RFC 2109</see>
- /// </term>
- /// </item>
- /// <item>
- /// <term>
- /// <see href="https://tools.ietf.org/html/rfc2965">RFC 2965</see>
- /// </term>
- /// </item>
- /// <item>
- /// <term>
- /// <see href="https://tools.ietf.org/html/rfc6265">RFC 6265</see>
- /// </term>
- /// </item>
- /// </list>
- /// <para>
- /// This class cannot be inherited.
- /// </para>
- /// </remarks>
- [Serializable]
- public sealed class Cookie
- {
- #region Private Fields
- private string _comment;
- private Uri _commentUri;
- private bool _discard;
- private string _domain;
- private static readonly int[] _emptyPorts;
- private DateTime _expires;
- private bool _httpOnly;
- private string _name;
- private string _path;
- private string _port;
- private int[] _ports;
- private static readonly char[] _reservedCharsForValue;
- private string _sameSite;
- private bool _secure;
- private DateTime _timeStamp;
- private string _value;
- private int _version;
- #endregion
- #region Static Constructor
- static Cookie ()
- {
- _emptyPorts = new int[0];
- _reservedCharsForValue = new[] { ';', ',' };
- }
- #endregion
- #region Internal Constructors
- internal Cookie ()
- {
- init (String.Empty, String.Empty, String.Empty, String.Empty);
- }
- #endregion
- #region Public Constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="Cookie"/> class with
- /// the specified name and value.
- /// </summary>
- /// <param name="name">
- /// <para>
- /// A <see cref="string"/> that specifies the name of the cookie.
- /// </para>
- /// <para>
- /// The name must be a token defined in
- /// <see href="http://tools.ietf.org/html/rfc2616#section-2.2">
- /// RFC 2616</see>.
- /// </para>
- /// </param>
- /// <param name="value">
- /// A <see cref="string"/> that specifies the value of the cookie.
- /// </param>
- /// <exception cref="ArgumentNullException">
- /// <paramref name="name"/> is <see langword="null"/>.
- /// </exception>
- /// <exception cref="ArgumentException">
- /// <para>
- /// <paramref name="name"/> is an empty string.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// <paramref name="name"/> starts with a dollar sign.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// <paramref name="name"/> contains an invalid character.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// <paramref name="value"/> is a string not enclosed in double quotes
- /// although it contains a reserved character.
- /// </para>
- /// </exception>
- public Cookie (string name, string value)
- : this (name, value, String.Empty, String.Empty)
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Cookie"/> class with
- /// the specified name, value, and path.
- /// </summary>
- /// <param name="name">
- /// <para>
- /// A <see cref="string"/> that specifies the name of the cookie.
- /// </para>
- /// <para>
- /// The name must be a token defined in
- /// <see href="http://tools.ietf.org/html/rfc2616#section-2.2">
- /// RFC 2616</see>.
- /// </para>
- /// </param>
- /// <param name="value">
- /// A <see cref="string"/> that specifies the value of the cookie.
- /// </param>
- /// <param name="path">
- /// A <see cref="string"/> that specifies the value of the Path
- /// attribute of the cookie.
- /// </param>
- /// <exception cref="ArgumentNullException">
- /// <paramref name="name"/> is <see langword="null"/>.
- /// </exception>
- /// <exception cref="ArgumentException">
- /// <para>
- /// <paramref name="name"/> is an empty string.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// <paramref name="name"/> starts with a dollar sign.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// <paramref name="name"/> contains an invalid character.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// <paramref name="value"/> is a string not enclosed in double quotes
- /// although it contains a reserved character.
- /// </para>
- /// </exception>
- public Cookie (string name, string value, string path)
- : this (name, value, path, String.Empty)
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Cookie"/> class with
- /// the specified name, value, path, and domain.
- /// </summary>
- /// <param name="name">
- /// <para>
- /// A <see cref="string"/> that specifies the name of the cookie.
- /// </para>
- /// <para>
- /// The name must be a token defined in
- /// <see href="http://tools.ietf.org/html/rfc2616#section-2.2">
- /// RFC 2616</see>.
- /// </para>
- /// </param>
- /// <param name="value">
- /// A <see cref="string"/> that specifies the value of the cookie.
- /// </param>
- /// <param name="path">
- /// A <see cref="string"/> that specifies the value of the Path
- /// attribute of the cookie.
- /// </param>
- /// <param name="domain">
- /// A <see cref="string"/> that specifies the value of the Domain
- /// attribute of the cookie.
- /// </param>
- /// <exception cref="ArgumentNullException">
- /// <paramref name="name"/> is <see langword="null"/>.
- /// </exception>
- /// <exception cref="ArgumentException">
- /// <para>
- /// <paramref name="name"/> is an empty string.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// <paramref name="name"/> starts with a dollar sign.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// <paramref name="name"/> contains an invalid character.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// <paramref name="value"/> is a string not enclosed in double quotes
- /// although it contains a reserved character.
- /// </para>
- /// </exception>
- public Cookie (string name, string value, string path, string domain)
- {
- if (name == null)
- throw new ArgumentNullException ("name");
- if (name.Length == 0)
- throw new ArgumentException ("An empty string.", "name");
- if (name[0] == '$') {
- var msg = "It starts with a dollar sign.";
- throw new ArgumentException (msg, "name");
- }
- if (!name.IsToken ()) {
- var msg = "It contains an invalid character.";
- throw new ArgumentException (msg, "name");
- }
- if (value == null)
- value = String.Empty;
- if (value.Contains (_reservedCharsForValue)) {
- if (!value.IsEnclosedIn ('"')) {
- var msg = "A string not enclosed in double quotes.";
- throw new ArgumentException (msg, "value");
- }
- }
- init (name, value, path ?? String.Empty, domain ?? String.Empty);
- }
- #endregion
- #region Internal Properties
- internal bool ExactDomain {
- get {
- return _domain.Length == 0 || _domain[0] != '.';
- }
- }
- internal int MaxAge {
- get {
- if (_expires == DateTime.MinValue)
- return 0;
- var expires = _expires.Kind != DateTimeKind.Local
- ? _expires.ToLocalTime ()
- : _expires;
- var span = expires - DateTime.Now;
- return span > TimeSpan.Zero
- ? (int) span.TotalSeconds
- : 0;
- }
- set {
- _expires = value > 0
- ? DateTime.Now.AddSeconds ((double) value)
- : DateTime.Now;
- }
- }
- internal int[] Ports {
- get {
- return _ports ?? _emptyPorts;
- }
- }
- internal string SameSite {
- get {
- return _sameSite;
- }
- set {
- _sameSite = value;
- }
- }
- #endregion
- #region Public Properties
- /// <summary>
- /// Gets the value of the Comment attribute of the cookie.
- /// </summary>
- /// <value>
- /// <para>
- /// A <see cref="string"/> that represents the comment to document
- /// intended use of the cookie.
- /// </para>
- /// <para>
- /// <see langword="null"/> if not present.
- /// </para>
- /// <para>
- /// The default value is <see langword="null"/>.
- /// </para>
- /// </value>
- public string Comment {
- get {
- return _comment;
- }
- internal set {
- _comment = value;
- }
- }
- /// <summary>
- /// Gets the value of the CommentURL attribute of the cookie.
- /// </summary>
- /// <value>
- /// <para>
- /// A <see cref="Uri"/> that represents the URI that provides
- /// the comment to document intended use of the cookie.
- /// </para>
- /// <para>
- /// <see langword="null"/> if not present.
- /// </para>
- /// <para>
- /// The default value is <see langword="null"/>.
- /// </para>
- /// </value>
- public Uri CommentUri {
- get {
- return _commentUri;
- }
- internal set {
- _commentUri = value;
- }
- }
- /// <summary>
- /// Gets a value indicating whether the client discards the cookie
- /// unconditionally when the client terminates.
- /// </summary>
- /// <value>
- /// <para>
- /// <c>true</c> if the client discards the cookie unconditionally
- /// when the client terminates; otherwise, <c>false</c>.
- /// </para>
- /// <para>
- /// The default value is <c>false</c>.
- /// </para>
- /// </value>
- public bool Discard {
- get {
- return _discard;
- }
- internal set {
- _discard = value;
- }
- }
- /// <summary>
- /// Gets or sets the value of the Domain attribute of the cookie.
- /// </summary>
- /// <value>
- /// <para>
- /// A <see cref="string"/> that represents the domain name that
- /// the cookie is valid for.
- /// </para>
- /// <para>
- /// An empty string if not necessary.
- /// </para>
- /// </value>
- public string Domain {
- get {
- return _domain;
- }
- set {
- _domain = value ?? String.Empty;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the cookie has expired.
- /// </summary>
- /// <value>
- /// <para>
- /// <c>true</c> if the cookie has expired; otherwise, <c>false</c>.
- /// </para>
- /// <para>
- /// The default value is <c>false</c>.
- /// </para>
- /// </value>
- public bool Expired {
- get {
- return _expires != DateTime.MinValue && _expires <= DateTime.Now;
- }
- set {
- _expires = value ? DateTime.Now : DateTime.MinValue;
- }
- }
- /// <summary>
- /// Gets or sets the value of the Expires attribute of the cookie.
- /// </summary>
- /// <value>
- /// <para>
- /// A <see cref="DateTime"/> that represents the date and time that
- /// the cookie expires on.
- /// </para>
- /// <para>
- /// <see cref="DateTime.MinValue"/> if not necessary.
- /// </para>
- /// <para>
- /// The default value is <see cref="DateTime.MinValue"/>.
- /// </para>
- /// </value>
- public DateTime Expires {
- get {
- return _expires;
- }
- set {
- _expires = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether non-HTTP APIs can access
- /// the cookie.
- /// </summary>
- /// <value>
- /// <para>
- /// <c>true</c> if non-HTTP APIs cannot access the cookie; otherwise,
- /// <c>false</c>.
- /// </para>
- /// <para>
- /// The default value is <c>false</c>.
- /// </para>
- /// </value>
- public bool HttpOnly {
- get {
- return _httpOnly;
- }
- set {
- _httpOnly = value;
- }
- }
- /// <summary>
- /// Gets or sets the name of the cookie.
- /// </summary>
- /// <value>
- /// <para>
- /// A <see cref="string"/> that represents the name of the cookie.
- /// </para>
- /// <para>
- /// The name must be a token defined in
- /// <see href="http://tools.ietf.org/html/rfc2616#section-2.2">
- /// RFC 2616</see>.
- /// </para>
- /// </value>
- /// <exception cref="ArgumentNullException">
- /// The value specified for a set operation is <see langword="null"/>.
- /// </exception>
- /// <exception cref="ArgumentException">
- /// <para>
- /// The value specified for a set operation is an empty string.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// The value specified for a set operation starts with a dollar sign.
- /// </para>
- /// <para>
- /// -or-
- /// </para>
- /// <para>
- /// The value specified for a set operation contains an invalid character.
- /// </para>
- /// </exception>
- public string Name {
- get {
- return _name;
- }
- set {
- if (value == null)
- throw new ArgumentNullException ("value");
- if (value.Length == 0)
- throw new ArgumentException ("An empty string.", "value");
- if (value[0] == '$') {
- var msg = "It starts with a dollar sign.";
- throw new ArgumentException (msg, "value");
- }
- if (!value.IsToken ()) {
- var msg = "It contains an invalid character.";
- throw new ArgumentException (msg, "value");
- }
- _name = value;
- }
- }
- /// <summary>
- /// Gets or sets the value of the Path attribute of the cookie.
- /// </summary>
- /// <value>
- /// A <see cref="string"/> that represents the subset of URI on
- /// the origin server that the cookie applies to.
- /// </value>
- public string Path {
- get {
- return _path;
- }
- set {
- _path = value ?? String.Empty;
- }
- }
- /// <summary>
- /// Gets the value of the Port attribute of the cookie.
- /// </summary>
- /// <value>
- /// <para>
- /// A <see cref="string"/> that represents the list of TCP ports
- /// that the cookie applies to.
- /// </para>
- /// <para>
- /// <see langword="null"/> if not present.
- /// </para>
- /// <para>
- /// The default value is <see langword="null"/>.
- /// </para>
- /// </value>
- public string Port {
- get {
- return _port;
- }
- internal set {
- int[] ports;
- if (!tryCreatePorts (value, out ports))
- return;
- _ports = ports;
- _port = value;
- }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the security level of
- /// the cookie is secure.
- /// </summary>
- /// <remarks>
- /// When this property is <c>true</c>, the cookie may be included in
- /// the request only if the request is transmitted over HTTPS.
- /// </remarks>
- /// <value>
- /// <para>
- /// <c>true</c> if the security level of the cookie is secure;
- /// otherwise, <c>false</c>.
- /// </para>
- /// <para>
- /// The default value is <c>false</c>.
- /// </para>
- /// </value>
- public bool Secure {
- get {
- return _secure;
- }
- set {
- _secure = value;
- }
- }
- /// <summary>
- /// Gets the time when the cookie was issued.
- /// </summary>
- /// <value>
- /// A <see cref="DateTime"/> that represents the time when
- /// the cookie was issued.
- /// </value>
- public DateTime TimeStamp {
- get {
- return _timeStamp;
- }
- }
- /// <summary>
- /// Gets or sets the value of the cookie.
- /// </summary>
- /// <value>
- /// A <see cref="string"/> that represents the value of the cookie.
- /// </value>
- /// <exception cref="ArgumentException">
- /// The value specified for a set operation is a string not enclosed in
- /// double quotes although it contains a reserved character.
- /// </exception>
- public string Value {
- get {
- return _value;
- }
- set {
- if (value == null)
- value = String.Empty;
- if (value.Contains (_reservedCharsForValue)) {
- if (!value.IsEnclosedIn ('"')) {
- var msg = "A string not enclosed in double quotes.";
- throw new ArgumentException (msg, "value");
- }
- }
- _value = value;
- }
- }
- /// <summary>
- /// Gets the value of the Version attribute of the cookie.
- /// </summary>
- /// <value>
- /// <para>
- /// An <see cref="int"/> that represents the version of HTTP state
- /// management that the cookie conforms to.
- /// </para>
- /// <para>
- /// 0 or 1.
- /// </para>
- /// <para>
- /// 0 if not present.
- /// </para>
- /// <para>
- /// The default value is 0.
- /// </para>
- /// </value>
- public int Version {
- get {
- return _version;
- }
- internal set {
- if (value < 0 || value > 1)
- return;
- _version = value;
- }
- }
- #endregion
- #region Private Methods
- private static int hash (int i, int j, int k, int l, int m)
- {
- return i
- ^ (j << 13 | j >> 19)
- ^ (k << 26 | k >> 6)
- ^ (l << 7 | l >> 25)
- ^ (m << 20 | m >> 12);
- }
- private void init (string name, string value, string path, string domain)
- {
- _name = name;
- _value = value;
- _path = path;
- _domain = domain;
- _expires = DateTime.MinValue;
- _timeStamp = DateTime.Now;
- }
- private string toResponseStringVersion0 ()
- {
- var buff = new StringBuilder (64);
- buff.AppendFormat ("{0}={1}", _name, _value);
- if (_expires != DateTime.MinValue) {
- var expires = _expires
- .ToUniversalTime ()
- .ToString (
- "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
- CultureInfo.CreateSpecificCulture ("en-US")
- );
- buff.AppendFormat ("; Expires={0}", expires);
- }
- if (!_path.IsNullOrEmpty ())
- buff.AppendFormat ("; Path={0}", _path);
- if (!_domain.IsNullOrEmpty ())
- buff.AppendFormat ("; Domain={0}", _domain);
- if (!_sameSite.IsNullOrEmpty ())
- buff.AppendFormat ("; SameSite={0}", _sameSite);
- if (_secure)
- buff.Append ("; Secure");
- if (_httpOnly)
- buff.Append ("; HttpOnly");
- return buff.ToString ();
- }
- private string toResponseStringVersion1 ()
- {
- var buff = new StringBuilder (64);
- buff.AppendFormat ("{0}={1}; Version={2}", _name, _value, _version);
- if (_expires != DateTime.MinValue)
- buff.AppendFormat ("; Max-Age={0}", MaxAge);
- if (!_path.IsNullOrEmpty ())
- buff.AppendFormat ("; Path={0}", _path);
- if (!_domain.IsNullOrEmpty ())
- buff.AppendFormat ("; Domain={0}", _domain);
- if (_port != null) {
- if (_port != "\"\"")
- buff.AppendFormat ("; Port={0}", _port);
- else
- buff.Append ("; Port");
- }
- if (_comment != null) {
- var comment = HttpUtility.UrlEncode (_comment);
- buff.AppendFormat ("; Comment={0}", comment);
- }
- if (_commentUri != null) {
- var url = _commentUri.OriginalString;
- buff.AppendFormat (
- "; CommentURL={0}", !url.IsToken () ? url.Quote () : url
- );
- }
- if (_discard)
- buff.Append ("; Discard");
- if (_secure)
- buff.Append ("; Secure");
- return buff.ToString ();
- }
- private static bool tryCreatePorts (string value, out int[] result)
- {
- result = null;
- var arr = value.Trim ('"').Split (',');
- var len = arr.Length;
- var res = new int[len];
- for (var i = 0; i < len; i++) {
- var s = arr[i].Trim ();
- if (s.Length == 0) {
- res[i] = Int32.MinValue;
- continue;
- }
- if (!Int32.TryParse (s, out res[i]))
- return false;
- }
- result = res;
- return true;
- }
- #endregion
- #region Internal Methods
- internal bool EqualsWithoutValue (Cookie cookie)
- {
- var caseSensitive = StringComparison.InvariantCulture;
- var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
- return _name.Equals (cookie._name, caseInsensitive)
- && _path.Equals (cookie._path, caseSensitive)
- && _domain.Equals (cookie._domain, caseInsensitive)
- && _version == cookie._version;
- }
- internal bool EqualsWithoutValueAndVersion (Cookie cookie)
- {
- var caseSensitive = StringComparison.InvariantCulture;
- var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
- return _name.Equals (cookie._name, caseInsensitive)
- && _path.Equals (cookie._path, caseSensitive)
- && _domain.Equals (cookie._domain, caseInsensitive);
- }
- internal string ToRequestString (Uri uri)
- {
- if (_name.Length == 0)
- return String.Empty;
- if (_version == 0)
- return String.Format ("{0}={1}", _name, _value);
- var buff = new StringBuilder (64);
- buff.AppendFormat ("$Version={0}; {1}={2}", _version, _name, _value);
- if (!_path.IsNullOrEmpty ())
- buff.AppendFormat ("; $Path={0}", _path);
- else if (uri != null)
- buff.AppendFormat ("; $Path={0}", uri.GetAbsolutePath ());
- else
- buff.Append ("; $Path=/");
- if (!_domain.IsNullOrEmpty ()) {
- if (uri == null || uri.Host != _domain)
- buff.AppendFormat ("; $Domain={0}", _domain);
- }
- if (_port != null) {
- if (_port != "\"\"")
- buff.AppendFormat ("; $Port={0}", _port);
- else
- buff.Append ("; $Port");
- }
- return buff.ToString ();
- }
- internal string ToResponseString ()
- {
- if (_name.Length == 0)
- return String.Empty;
- if (_version == 0)
- return toResponseStringVersion0 ();
- return toResponseStringVersion1 ();
- }
- internal static bool TryCreate (
- string name, string value, out Cookie result
- )
- {
- result = null;
- try {
- result = new Cookie (name, value);
- }
- catch {
- return false;
- }
- return true;
- }
- #endregion
- #region Public Methods
- /// <summary>
- /// Determines whether the current cookie instance is equal to
- /// the specified <see cref="object"/> instance.
- /// </summary>
- /// <param name="comparand">
- /// <para>
- /// An <see cref="object"/> instance to compare with
- /// the current cookie instance.
- /// </para>
- /// <para>
- /// An reference to a <see cref="Cookie"/> instance.
- /// </para>
- /// </param>
- /// <returns>
- /// <c>true</c> if the current cookie instance is equal to
- /// <paramref name="comparand"/>; otherwise, <c>false</c>.
- /// </returns>
- public override bool Equals (object comparand)
- {
- var cookie = comparand as Cookie;
- if (cookie == null)
- return false;
- var caseSensitive = StringComparison.InvariantCulture;
- var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
- return _name.Equals (cookie._name, caseInsensitive)
- && _value.Equals (cookie._value, caseSensitive)
- && _path.Equals (cookie._path, caseSensitive)
- && _domain.Equals (cookie._domain, caseInsensitive)
- && _version == cookie._version;
- }
- /// <summary>
- /// Gets a hash code for the current cookie instance.
- /// </summary>
- /// <returns>
- /// An <see cref="int"/> that represents the hash code.
- /// </returns>
- public override int GetHashCode ()
- {
- var i = StringComparer.InvariantCultureIgnoreCase.GetHashCode (_name);
- var j = _value.GetHashCode ();
- var k = _path.GetHashCode ();
- var l = StringComparer.InvariantCultureIgnoreCase.GetHashCode (_domain);
- var m = _version;
- return hash (i, j, k, l, m);
- }
- /// <summary>
- /// Returns a string that represents the current cookie instance.
- /// </summary>
- /// <returns>
- /// A <see cref="string"/> that is suitable for the Cookie request header.
- /// </returns>
- public override string ToString ()
- {
- return ToRequestString (null);
- }
- #endregion
- }
- }
|