2 // System.Net.CookieParser
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Daniel Nauck (dna(at)mono-project(dot)de)
9 // (c) 2002 Lawrence Pit
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
11 // (c) 2008 Daniel Nauck
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.Globalization;
39 namespace System.Net {
46 public CookieParser (string header) : this (header, 0)
50 public CookieParser (string header, int position)
54 this.length = header.Length;
57 public bool GetNextNameValue (out string name, out string val)
65 name = GetCookieName ();
66 if (pos < header.Length && header [pos] == '=') {
68 val = GetCookieValue ();
71 if (pos < length && header [pos] == ';')
77 string GetCookieName ()
80 while (k < length && Char.IsWhiteSpace (header [k]))
84 while (k < length && header [k] != ';' && header [k] != '=')
88 return header.Substring (begin, k - begin).Trim ();
91 string GetCookieValue ()
97 while (k < length && Char.IsWhiteSpace (header [k]))
101 if (header [k] == '"'){
105 while (k < length && header [k] != '"')
108 for (j = k; j < length && header [j] != ';'; j++)
113 while (k < length && header [k] != ';')
118 return header.Substring (begin, k - begin).Trim ();
121 static string[] cookieExpiresFormats =
123 "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'",
124 "ddd, dd'-'MMM'-'yy HH':'mm':'ss 'GMT'" };
126 static public DateTime TryParseCookieExpires (string value)
128 if (String.IsNullOrEmpty (value))
129 return DateTime.MinValue;
131 for (int i = 0; i < cookieExpiresFormats.Length; i++) {
133 DateTime cookieExpiresUtc = DateTime.ParseExact (value, cookieExpiresFormats [i], CultureInfo.InvariantCulture);
135 //convert UTC/GMT time to local time
136 cookieExpiresUtc = DateTime.SpecifyKind (cookieExpiresUtc, DateTimeKind.Utc);
137 return TimeZone.CurrentTimeZone.ToLocalTime (cookieExpiresUtc);
141 //If we can't parse Expires, use cookie as session cookie (expires is DateTime.MinValue)
142 return DateTime.MinValue;