Cookie.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. #region License
  2. /*
  3. * Cookie.cs
  4. *
  5. * This code is derived from Cookie.cs (System.Net) of Mono
  6. * (http://www.mono-project.com).
  7. *
  8. * The MIT License
  9. *
  10. * Copyright (c) 2004,2009 Novell, Inc. (http://www.novell.com)
  11. * Copyright (c) 2012-2023 sta.blockhead
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. #endregion
  32. #region Authors
  33. /*
  34. * Authors:
  35. * - Lawrence Pit <loz@cable.a2000.nl>
  36. * - Gonzalo Paniagua Javier <gonzalo@ximian.com>
  37. * - Daniel Nauck <dna@mono-project.de>
  38. * - Sebastien Pouliot <sebastien@ximian.com>
  39. */
  40. #endregion
  41. using System;
  42. using System.Globalization;
  43. using System.Text;
  44. namespace WebSocketSharp.Net
  45. {
  46. /// <summary>
  47. /// Provides a set of methods and properties used to manage an HTTP cookie.
  48. /// </summary>
  49. /// <remarks>
  50. /// <para>
  51. /// This class refers to the following specifications:
  52. /// </para>
  53. /// <list type="bullet">
  54. /// <item>
  55. /// <term>
  56. /// <see href="http://web.archive.org/web/20020803110822/http://wp.netscape.com/newsref/std/cookie_spec.html">
  57. /// Netscape specification</see>
  58. /// </term>
  59. /// </item>
  60. /// <item>
  61. /// <term>
  62. /// <see href="https://tools.ietf.org/html/rfc2109">RFC 2109</see>
  63. /// </term>
  64. /// </item>
  65. /// <item>
  66. /// <term>
  67. /// <see href="https://tools.ietf.org/html/rfc2965">RFC 2965</see>
  68. /// </term>
  69. /// </item>
  70. /// <item>
  71. /// <term>
  72. /// <see href="https://tools.ietf.org/html/rfc6265">RFC 6265</see>
  73. /// </term>
  74. /// </item>
  75. /// </list>
  76. /// <para>
  77. /// This class cannot be inherited.
  78. /// </para>
  79. /// </remarks>
  80. [Serializable]
  81. public sealed class Cookie
  82. {
  83. #region Private Fields
  84. private string _comment;
  85. private Uri _commentUri;
  86. private bool _discard;
  87. private string _domain;
  88. private static readonly int[] _emptyPorts;
  89. private DateTime _expires;
  90. private bool _httpOnly;
  91. private string _name;
  92. private string _path;
  93. private string _port;
  94. private int[] _ports;
  95. private static readonly char[] _reservedCharsForValue;
  96. private string _sameSite;
  97. private bool _secure;
  98. private DateTime _timeStamp;
  99. private string _value;
  100. private int _version;
  101. #endregion
  102. #region Static Constructor
  103. static Cookie ()
  104. {
  105. _emptyPorts = new int[0];
  106. _reservedCharsForValue = new[] { ';', ',' };
  107. }
  108. #endregion
  109. #region Internal Constructors
  110. internal Cookie ()
  111. {
  112. init (String.Empty, String.Empty, String.Empty, String.Empty);
  113. }
  114. #endregion
  115. #region Public Constructors
  116. /// <summary>
  117. /// Initializes a new instance of the <see cref="Cookie"/> class with
  118. /// the specified name and value.
  119. /// </summary>
  120. /// <param name="name">
  121. /// <para>
  122. /// A <see cref="string"/> that specifies the name of the cookie.
  123. /// </para>
  124. /// <para>
  125. /// The name must be a token defined in
  126. /// <see href="http://tools.ietf.org/html/rfc2616#section-2.2">
  127. /// RFC 2616</see>.
  128. /// </para>
  129. /// </param>
  130. /// <param name="value">
  131. /// A <see cref="string"/> that specifies the value of the cookie.
  132. /// </param>
  133. /// <exception cref="ArgumentNullException">
  134. /// <paramref name="name"/> is <see langword="null"/>.
  135. /// </exception>
  136. /// <exception cref="ArgumentException">
  137. /// <para>
  138. /// <paramref name="name"/> is an empty string.
  139. /// </para>
  140. /// <para>
  141. /// -or-
  142. /// </para>
  143. /// <para>
  144. /// <paramref name="name"/> starts with a dollar sign.
  145. /// </para>
  146. /// <para>
  147. /// -or-
  148. /// </para>
  149. /// <para>
  150. /// <paramref name="name"/> contains an invalid character.
  151. /// </para>
  152. /// <para>
  153. /// -or-
  154. /// </para>
  155. /// <para>
  156. /// <paramref name="value"/> is a string not enclosed in double quotes
  157. /// although it contains a reserved character.
  158. /// </para>
  159. /// </exception>
  160. public Cookie (string name, string value)
  161. : this (name, value, String.Empty, String.Empty)
  162. {
  163. }
  164. /// <summary>
  165. /// Initializes a new instance of the <see cref="Cookie"/> class with
  166. /// the specified name, value, and path.
  167. /// </summary>
  168. /// <param name="name">
  169. /// <para>
  170. /// A <see cref="string"/> that specifies the name of the cookie.
  171. /// </para>
  172. /// <para>
  173. /// The name must be a token defined in
  174. /// <see href="http://tools.ietf.org/html/rfc2616#section-2.2">
  175. /// RFC 2616</see>.
  176. /// </para>
  177. /// </param>
  178. /// <param name="value">
  179. /// A <see cref="string"/> that specifies the value of the cookie.
  180. /// </param>
  181. /// <param name="path">
  182. /// A <see cref="string"/> that specifies the value of the Path
  183. /// attribute of the cookie.
  184. /// </param>
  185. /// <exception cref="ArgumentNullException">
  186. /// <paramref name="name"/> is <see langword="null"/>.
  187. /// </exception>
  188. /// <exception cref="ArgumentException">
  189. /// <para>
  190. /// <paramref name="name"/> is an empty string.
  191. /// </para>
  192. /// <para>
  193. /// -or-
  194. /// </para>
  195. /// <para>
  196. /// <paramref name="name"/> starts with a dollar sign.
  197. /// </para>
  198. /// <para>
  199. /// -or-
  200. /// </para>
  201. /// <para>
  202. /// <paramref name="name"/> contains an invalid character.
  203. /// </para>
  204. /// <para>
  205. /// -or-
  206. /// </para>
  207. /// <para>
  208. /// <paramref name="value"/> is a string not enclosed in double quotes
  209. /// although it contains a reserved character.
  210. /// </para>
  211. /// </exception>
  212. public Cookie (string name, string value, string path)
  213. : this (name, value, path, String.Empty)
  214. {
  215. }
  216. /// <summary>
  217. /// Initializes a new instance of the <see cref="Cookie"/> class with
  218. /// the specified name, value, path, and domain.
  219. /// </summary>
  220. /// <param name="name">
  221. /// <para>
  222. /// A <see cref="string"/> that specifies the name of the cookie.
  223. /// </para>
  224. /// <para>
  225. /// The name must be a token defined in
  226. /// <see href="http://tools.ietf.org/html/rfc2616#section-2.2">
  227. /// RFC 2616</see>.
  228. /// </para>
  229. /// </param>
  230. /// <param name="value">
  231. /// A <see cref="string"/> that specifies the value of the cookie.
  232. /// </param>
  233. /// <param name="path">
  234. /// A <see cref="string"/> that specifies the value of the Path
  235. /// attribute of the cookie.
  236. /// </param>
  237. /// <param name="domain">
  238. /// A <see cref="string"/> that specifies the value of the Domain
  239. /// attribute of the cookie.
  240. /// </param>
  241. /// <exception cref="ArgumentNullException">
  242. /// <paramref name="name"/> is <see langword="null"/>.
  243. /// </exception>
  244. /// <exception cref="ArgumentException">
  245. /// <para>
  246. /// <paramref name="name"/> is an empty string.
  247. /// </para>
  248. /// <para>
  249. /// -or-
  250. /// </para>
  251. /// <para>
  252. /// <paramref name="name"/> starts with a dollar sign.
  253. /// </para>
  254. /// <para>
  255. /// -or-
  256. /// </para>
  257. /// <para>
  258. /// <paramref name="name"/> contains an invalid character.
  259. /// </para>
  260. /// <para>
  261. /// -or-
  262. /// </para>
  263. /// <para>
  264. /// <paramref name="value"/> is a string not enclosed in double quotes
  265. /// although it contains a reserved character.
  266. /// </para>
  267. /// </exception>
  268. public Cookie (string name, string value, string path, string domain)
  269. {
  270. if (name == null)
  271. throw new ArgumentNullException ("name");
  272. if (name.Length == 0)
  273. throw new ArgumentException ("An empty string.", "name");
  274. if (name[0] == '$') {
  275. var msg = "It starts with a dollar sign.";
  276. throw new ArgumentException (msg, "name");
  277. }
  278. if (!name.IsToken ()) {
  279. var msg = "It contains an invalid character.";
  280. throw new ArgumentException (msg, "name");
  281. }
  282. if (value == null)
  283. value = String.Empty;
  284. if (value.Contains (_reservedCharsForValue)) {
  285. if (!value.IsEnclosedIn ('"')) {
  286. var msg = "A string not enclosed in double quotes.";
  287. throw new ArgumentException (msg, "value");
  288. }
  289. }
  290. init (name, value, path ?? String.Empty, domain ?? String.Empty);
  291. }
  292. #endregion
  293. #region Internal Properties
  294. internal bool ExactDomain {
  295. get {
  296. return _domain.Length == 0 || _domain[0] != '.';
  297. }
  298. }
  299. internal int MaxAge {
  300. get {
  301. if (_expires == DateTime.MinValue)
  302. return 0;
  303. var expires = _expires.Kind != DateTimeKind.Local
  304. ? _expires.ToLocalTime ()
  305. : _expires;
  306. var span = expires - DateTime.Now;
  307. return span > TimeSpan.Zero
  308. ? (int) span.TotalSeconds
  309. : 0;
  310. }
  311. set {
  312. _expires = value > 0
  313. ? DateTime.Now.AddSeconds ((double) value)
  314. : DateTime.Now;
  315. }
  316. }
  317. internal int[] Ports {
  318. get {
  319. return _ports ?? _emptyPorts;
  320. }
  321. }
  322. internal string SameSite {
  323. get {
  324. return _sameSite;
  325. }
  326. set {
  327. _sameSite = value;
  328. }
  329. }
  330. #endregion
  331. #region Public Properties
  332. /// <summary>
  333. /// Gets the value of the Comment attribute of the cookie.
  334. /// </summary>
  335. /// <value>
  336. /// <para>
  337. /// A <see cref="string"/> that represents the comment to document
  338. /// intended use of the cookie.
  339. /// </para>
  340. /// <para>
  341. /// <see langword="null"/> if not present.
  342. /// </para>
  343. /// <para>
  344. /// The default value is <see langword="null"/>.
  345. /// </para>
  346. /// </value>
  347. public string Comment {
  348. get {
  349. return _comment;
  350. }
  351. internal set {
  352. _comment = value;
  353. }
  354. }
  355. /// <summary>
  356. /// Gets the value of the CommentURL attribute of the cookie.
  357. /// </summary>
  358. /// <value>
  359. /// <para>
  360. /// A <see cref="Uri"/> that represents the URI that provides
  361. /// the comment to document intended use of the cookie.
  362. /// </para>
  363. /// <para>
  364. /// <see langword="null"/> if not present.
  365. /// </para>
  366. /// <para>
  367. /// The default value is <see langword="null"/>.
  368. /// </para>
  369. /// </value>
  370. public Uri CommentUri {
  371. get {
  372. return _commentUri;
  373. }
  374. internal set {
  375. _commentUri = value;
  376. }
  377. }
  378. /// <summary>
  379. /// Gets a value indicating whether the client discards the cookie
  380. /// unconditionally when the client terminates.
  381. /// </summary>
  382. /// <value>
  383. /// <para>
  384. /// <c>true</c> if the client discards the cookie unconditionally
  385. /// when the client terminates; otherwise, <c>false</c>.
  386. /// </para>
  387. /// <para>
  388. /// The default value is <c>false</c>.
  389. /// </para>
  390. /// </value>
  391. public bool Discard {
  392. get {
  393. return _discard;
  394. }
  395. internal set {
  396. _discard = value;
  397. }
  398. }
  399. /// <summary>
  400. /// Gets or sets the value of the Domain attribute of the cookie.
  401. /// </summary>
  402. /// <value>
  403. /// <para>
  404. /// A <see cref="string"/> that represents the domain name that
  405. /// the cookie is valid for.
  406. /// </para>
  407. /// <para>
  408. /// An empty string if not necessary.
  409. /// </para>
  410. /// </value>
  411. public string Domain {
  412. get {
  413. return _domain;
  414. }
  415. set {
  416. _domain = value ?? String.Empty;
  417. }
  418. }
  419. /// <summary>
  420. /// Gets or sets a value indicating whether the cookie has expired.
  421. /// </summary>
  422. /// <value>
  423. /// <para>
  424. /// <c>true</c> if the cookie has expired; otherwise, <c>false</c>.
  425. /// </para>
  426. /// <para>
  427. /// The default value is <c>false</c>.
  428. /// </para>
  429. /// </value>
  430. public bool Expired {
  431. get {
  432. return _expires != DateTime.MinValue && _expires <= DateTime.Now;
  433. }
  434. set {
  435. _expires = value ? DateTime.Now : DateTime.MinValue;
  436. }
  437. }
  438. /// <summary>
  439. /// Gets or sets the value of the Expires attribute of the cookie.
  440. /// </summary>
  441. /// <value>
  442. /// <para>
  443. /// A <see cref="DateTime"/> that represents the date and time that
  444. /// the cookie expires on.
  445. /// </para>
  446. /// <para>
  447. /// <see cref="DateTime.MinValue"/> if not necessary.
  448. /// </para>
  449. /// <para>
  450. /// The default value is <see cref="DateTime.MinValue"/>.
  451. /// </para>
  452. /// </value>
  453. public DateTime Expires {
  454. get {
  455. return _expires;
  456. }
  457. set {
  458. _expires = value;
  459. }
  460. }
  461. /// <summary>
  462. /// Gets or sets a value indicating whether non-HTTP APIs can access
  463. /// the cookie.
  464. /// </summary>
  465. /// <value>
  466. /// <para>
  467. /// <c>true</c> if non-HTTP APIs cannot access the cookie; otherwise,
  468. /// <c>false</c>.
  469. /// </para>
  470. /// <para>
  471. /// The default value is <c>false</c>.
  472. /// </para>
  473. /// </value>
  474. public bool HttpOnly {
  475. get {
  476. return _httpOnly;
  477. }
  478. set {
  479. _httpOnly = value;
  480. }
  481. }
  482. /// <summary>
  483. /// Gets or sets the name of the cookie.
  484. /// </summary>
  485. /// <value>
  486. /// <para>
  487. /// A <see cref="string"/> that represents the name of the cookie.
  488. /// </para>
  489. /// <para>
  490. /// The name must be a token defined in
  491. /// <see href="http://tools.ietf.org/html/rfc2616#section-2.2">
  492. /// RFC 2616</see>.
  493. /// </para>
  494. /// </value>
  495. /// <exception cref="ArgumentNullException">
  496. /// The value specified for a set operation is <see langword="null"/>.
  497. /// </exception>
  498. /// <exception cref="ArgumentException">
  499. /// <para>
  500. /// The value specified for a set operation is an empty string.
  501. /// </para>
  502. /// <para>
  503. /// -or-
  504. /// </para>
  505. /// <para>
  506. /// The value specified for a set operation starts with a dollar sign.
  507. /// </para>
  508. /// <para>
  509. /// -or-
  510. /// </para>
  511. /// <para>
  512. /// The value specified for a set operation contains an invalid character.
  513. /// </para>
  514. /// </exception>
  515. public string Name {
  516. get {
  517. return _name;
  518. }
  519. set {
  520. if (value == null)
  521. throw new ArgumentNullException ("value");
  522. if (value.Length == 0)
  523. throw new ArgumentException ("An empty string.", "value");
  524. if (value[0] == '$') {
  525. var msg = "It starts with a dollar sign.";
  526. throw new ArgumentException (msg, "value");
  527. }
  528. if (!value.IsToken ()) {
  529. var msg = "It contains an invalid character.";
  530. throw new ArgumentException (msg, "value");
  531. }
  532. _name = value;
  533. }
  534. }
  535. /// <summary>
  536. /// Gets or sets the value of the Path attribute of the cookie.
  537. /// </summary>
  538. /// <value>
  539. /// A <see cref="string"/> that represents the subset of URI on
  540. /// the origin server that the cookie applies to.
  541. /// </value>
  542. public string Path {
  543. get {
  544. return _path;
  545. }
  546. set {
  547. _path = value ?? String.Empty;
  548. }
  549. }
  550. /// <summary>
  551. /// Gets the value of the Port attribute of the cookie.
  552. /// </summary>
  553. /// <value>
  554. /// <para>
  555. /// A <see cref="string"/> that represents the list of TCP ports
  556. /// that the cookie applies to.
  557. /// </para>
  558. /// <para>
  559. /// <see langword="null"/> if not present.
  560. /// </para>
  561. /// <para>
  562. /// The default value is <see langword="null"/>.
  563. /// </para>
  564. /// </value>
  565. public string Port {
  566. get {
  567. return _port;
  568. }
  569. internal set {
  570. int[] ports;
  571. if (!tryCreatePorts (value, out ports))
  572. return;
  573. _ports = ports;
  574. _port = value;
  575. }
  576. }
  577. /// <summary>
  578. /// Gets or sets a value indicating whether the security level of
  579. /// the cookie is secure.
  580. /// </summary>
  581. /// <remarks>
  582. /// When this property is <c>true</c>, the cookie may be included in
  583. /// the request only if the request is transmitted over HTTPS.
  584. /// </remarks>
  585. /// <value>
  586. /// <para>
  587. /// <c>true</c> if the security level of the cookie is secure;
  588. /// otherwise, <c>false</c>.
  589. /// </para>
  590. /// <para>
  591. /// The default value is <c>false</c>.
  592. /// </para>
  593. /// </value>
  594. public bool Secure {
  595. get {
  596. return _secure;
  597. }
  598. set {
  599. _secure = value;
  600. }
  601. }
  602. /// <summary>
  603. /// Gets the time when the cookie was issued.
  604. /// </summary>
  605. /// <value>
  606. /// A <see cref="DateTime"/> that represents the time when
  607. /// the cookie was issued.
  608. /// </value>
  609. public DateTime TimeStamp {
  610. get {
  611. return _timeStamp;
  612. }
  613. }
  614. /// <summary>
  615. /// Gets or sets the value of the cookie.
  616. /// </summary>
  617. /// <value>
  618. /// A <see cref="string"/> that represents the value of the cookie.
  619. /// </value>
  620. /// <exception cref="ArgumentException">
  621. /// The value specified for a set operation is a string not enclosed in
  622. /// double quotes although it contains a reserved character.
  623. /// </exception>
  624. public string Value {
  625. get {
  626. return _value;
  627. }
  628. set {
  629. if (value == null)
  630. value = String.Empty;
  631. if (value.Contains (_reservedCharsForValue)) {
  632. if (!value.IsEnclosedIn ('"')) {
  633. var msg = "A string not enclosed in double quotes.";
  634. throw new ArgumentException (msg, "value");
  635. }
  636. }
  637. _value = value;
  638. }
  639. }
  640. /// <summary>
  641. /// Gets the value of the Version attribute of the cookie.
  642. /// </summary>
  643. /// <value>
  644. /// <para>
  645. /// An <see cref="int"/> that represents the version of HTTP state
  646. /// management that the cookie conforms to.
  647. /// </para>
  648. /// <para>
  649. /// 0 or 1.
  650. /// </para>
  651. /// <para>
  652. /// 0 if not present.
  653. /// </para>
  654. /// <para>
  655. /// The default value is 0.
  656. /// </para>
  657. /// </value>
  658. public int Version {
  659. get {
  660. return _version;
  661. }
  662. internal set {
  663. if (value < 0 || value > 1)
  664. return;
  665. _version = value;
  666. }
  667. }
  668. #endregion
  669. #region Private Methods
  670. private static int hash (int i, int j, int k, int l, int m)
  671. {
  672. return i
  673. ^ (j << 13 | j >> 19)
  674. ^ (k << 26 | k >> 6)
  675. ^ (l << 7 | l >> 25)
  676. ^ (m << 20 | m >> 12);
  677. }
  678. private void init (string name, string value, string path, string domain)
  679. {
  680. _name = name;
  681. _value = value;
  682. _path = path;
  683. _domain = domain;
  684. _expires = DateTime.MinValue;
  685. _timeStamp = DateTime.Now;
  686. }
  687. private string toResponseStringVersion0 ()
  688. {
  689. var buff = new StringBuilder (64);
  690. buff.AppendFormat ("{0}={1}", _name, _value);
  691. if (_expires != DateTime.MinValue) {
  692. var expires = _expires
  693. .ToUniversalTime ()
  694. .ToString (
  695. "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
  696. CultureInfo.CreateSpecificCulture ("en-US")
  697. );
  698. buff.AppendFormat ("; Expires={0}", expires);
  699. }
  700. if (!_path.IsNullOrEmpty ())
  701. buff.AppendFormat ("; Path={0}", _path);
  702. if (!_domain.IsNullOrEmpty ())
  703. buff.AppendFormat ("; Domain={0}", _domain);
  704. if (!_sameSite.IsNullOrEmpty ())
  705. buff.AppendFormat ("; SameSite={0}", _sameSite);
  706. if (_secure)
  707. buff.Append ("; Secure");
  708. if (_httpOnly)
  709. buff.Append ("; HttpOnly");
  710. return buff.ToString ();
  711. }
  712. private string toResponseStringVersion1 ()
  713. {
  714. var buff = new StringBuilder (64);
  715. buff.AppendFormat ("{0}={1}; Version={2}", _name, _value, _version);
  716. if (_expires != DateTime.MinValue)
  717. buff.AppendFormat ("; Max-Age={0}", MaxAge);
  718. if (!_path.IsNullOrEmpty ())
  719. buff.AppendFormat ("; Path={0}", _path);
  720. if (!_domain.IsNullOrEmpty ())
  721. buff.AppendFormat ("; Domain={0}", _domain);
  722. if (_port != null) {
  723. if (_port != "\"\"")
  724. buff.AppendFormat ("; Port={0}", _port);
  725. else
  726. buff.Append ("; Port");
  727. }
  728. if (_comment != null) {
  729. var comment = HttpUtility.UrlEncode (_comment);
  730. buff.AppendFormat ("; Comment={0}", comment);
  731. }
  732. if (_commentUri != null) {
  733. var url = _commentUri.OriginalString;
  734. buff.AppendFormat (
  735. "; CommentURL={0}", !url.IsToken () ? url.Quote () : url
  736. );
  737. }
  738. if (_discard)
  739. buff.Append ("; Discard");
  740. if (_secure)
  741. buff.Append ("; Secure");
  742. return buff.ToString ();
  743. }
  744. private static bool tryCreatePorts (string value, out int[] result)
  745. {
  746. result = null;
  747. var arr = value.Trim ('"').Split (',');
  748. var len = arr.Length;
  749. var res = new int[len];
  750. for (var i = 0; i < len; i++) {
  751. var s = arr[i].Trim ();
  752. if (s.Length == 0) {
  753. res[i] = Int32.MinValue;
  754. continue;
  755. }
  756. if (!Int32.TryParse (s, out res[i]))
  757. return false;
  758. }
  759. result = res;
  760. return true;
  761. }
  762. #endregion
  763. #region Internal Methods
  764. internal bool EqualsWithoutValue (Cookie cookie)
  765. {
  766. var caseSensitive = StringComparison.InvariantCulture;
  767. var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
  768. return _name.Equals (cookie._name, caseInsensitive)
  769. && _path.Equals (cookie._path, caseSensitive)
  770. && _domain.Equals (cookie._domain, caseInsensitive)
  771. && _version == cookie._version;
  772. }
  773. internal bool EqualsWithoutValueAndVersion (Cookie cookie)
  774. {
  775. var caseSensitive = StringComparison.InvariantCulture;
  776. var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
  777. return _name.Equals (cookie._name, caseInsensitive)
  778. && _path.Equals (cookie._path, caseSensitive)
  779. && _domain.Equals (cookie._domain, caseInsensitive);
  780. }
  781. internal string ToRequestString (Uri uri)
  782. {
  783. if (_name.Length == 0)
  784. return String.Empty;
  785. if (_version == 0)
  786. return String.Format ("{0}={1}", _name, _value);
  787. var buff = new StringBuilder (64);
  788. buff.AppendFormat ("$Version={0}; {1}={2}", _version, _name, _value);
  789. if (!_path.IsNullOrEmpty ())
  790. buff.AppendFormat ("; $Path={0}", _path);
  791. else if (uri != null)
  792. buff.AppendFormat ("; $Path={0}", uri.GetAbsolutePath ());
  793. else
  794. buff.Append ("; $Path=/");
  795. if (!_domain.IsNullOrEmpty ()) {
  796. if (uri == null || uri.Host != _domain)
  797. buff.AppendFormat ("; $Domain={0}", _domain);
  798. }
  799. if (_port != null) {
  800. if (_port != "\"\"")
  801. buff.AppendFormat ("; $Port={0}", _port);
  802. else
  803. buff.Append ("; $Port");
  804. }
  805. return buff.ToString ();
  806. }
  807. internal string ToResponseString ()
  808. {
  809. if (_name.Length == 0)
  810. return String.Empty;
  811. if (_version == 0)
  812. return toResponseStringVersion0 ();
  813. return toResponseStringVersion1 ();
  814. }
  815. internal static bool TryCreate (
  816. string name, string value, out Cookie result
  817. )
  818. {
  819. result = null;
  820. try {
  821. result = new Cookie (name, value);
  822. }
  823. catch {
  824. return false;
  825. }
  826. return true;
  827. }
  828. #endregion
  829. #region Public Methods
  830. /// <summary>
  831. /// Determines whether the current cookie instance is equal to
  832. /// the specified <see cref="object"/> instance.
  833. /// </summary>
  834. /// <param name="comparand">
  835. /// <para>
  836. /// An <see cref="object"/> instance to compare with
  837. /// the current cookie instance.
  838. /// </para>
  839. /// <para>
  840. /// An reference to a <see cref="Cookie"/> instance.
  841. /// </para>
  842. /// </param>
  843. /// <returns>
  844. /// <c>true</c> if the current cookie instance is equal to
  845. /// <paramref name="comparand"/>; otherwise, <c>false</c>.
  846. /// </returns>
  847. public override bool Equals (object comparand)
  848. {
  849. var cookie = comparand as Cookie;
  850. if (cookie == null)
  851. return false;
  852. var caseSensitive = StringComparison.InvariantCulture;
  853. var caseInsensitive = StringComparison.InvariantCultureIgnoreCase;
  854. return _name.Equals (cookie._name, caseInsensitive)
  855. && _value.Equals (cookie._value, caseSensitive)
  856. && _path.Equals (cookie._path, caseSensitive)
  857. && _domain.Equals (cookie._domain, caseInsensitive)
  858. && _version == cookie._version;
  859. }
  860. /// <summary>
  861. /// Gets a hash code for the current cookie instance.
  862. /// </summary>
  863. /// <returns>
  864. /// An <see cref="int"/> that represents the hash code.
  865. /// </returns>
  866. public override int GetHashCode ()
  867. {
  868. var i = StringComparer.InvariantCultureIgnoreCase.GetHashCode (_name);
  869. var j = _value.GetHashCode ();
  870. var k = _path.GetHashCode ();
  871. var l = StringComparer.InvariantCultureIgnoreCase.GetHashCode (_domain);
  872. var m = _version;
  873. return hash (i, j, k, l, m);
  874. }
  875. /// <summary>
  876. /// Returns a string that represents the current cookie instance.
  877. /// </summary>
  878. /// <returns>
  879. /// A <see cref="string"/> that is suitable for the Cookie request header.
  880. /// </returns>
  881. public override string ToString ()
  882. {
  883. return ToRequestString (null);
  884. }
  885. #endregion
  886. }
  887. }