src/packages/EmbedIO/Net/CookieList.cs
/// <summary>Creates a <see cref="CookieList"/> by parsing
/// the value of one or more <c>Cookie</c> or <c>Set-Cookie</c> headers.</summary>
/// <param name="headerValue">The value, or comma-separated list of values,
/// of the header or headers.</param>
/// <returns>A newly-created instance of <see cref="CookieList"/>.</returns>
Creates a  by parsing
the value of one or more CookieSet-Cookie
Creates a by parsing the value of one or more CookieSet - Cookie
public static CookieList Parse(string headerValue)
        {
            var cookies = new CookieList();
            Cookie? cookie = null;
            var pairs = SplitCookieHeaderValue(headerValue);
            for (var i = 0; i < pairs.Length; i++)
            {
                var pair = pairs[i].Trim();
                if (pair.Length == 0)
                    continue;
                if (pair.StartsWith("version", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    cookie.Version = int.Parse(GetValue(pair, true), CultureInfo.InvariantCulture);
                }
                else if (pair.StartsWith("expires", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    var buff = new StringBuilder(GetValue(pair), 32);
                    if (i < pairs.Length - 1)
                        buff.AppendFormat(CultureInfo.InvariantCulture, ", {0}", pairs[++i].Trim());
                    if (!HttpDate.TryParse(buff.ToString(), out var expires))
                        expires = DateTimeOffset.Now;
                    if (cookie.Expires == DateTime.MinValue)
                        cookie.Expires = expires.LocalDateTime;
                }
                else if (pair.StartsWith("max-age", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    var max = int.Parse(GetValue(pair, true), CultureInfo.InvariantCulture);
                    cookie.Expires = DateTime.Now.AddSeconds(max);
                }
                else if (pair.StartsWith("path", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    cookie.Path = GetValue(pair);
                }
                else if (pair.StartsWith("domain", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    cookie.Domain = GetValue(pair);
                }
                else if (pair.StartsWith("port", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    cookie.Port = pair.Equals("port", StringComparison.OrdinalIgnoreCase)
                        ? "\"\""
                        : GetValue(pair);
                }
                else if (pair.StartsWith("comment", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    cookie.Comment = WebUtility.UrlDecode(GetValue(pair));
                }
                else if (pair.StartsWith("commenturl", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    cookie.CommentUri = UriUtility.StringToUri(GetValue(pair, true));
                }
                else if (pair.StartsWith("discard", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    cookie.Discard = true;
                }
                else if (pair.StartsWith("secure", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    cookie.Secure = true;
                }
                else if (pair.StartsWith("httponly", StringComparison.OrdinalIgnoreCase) && cookie != null)
                {
                    cookie.HttpOnly = true;
                }
                else
                {
                    if (cookie != null)
                        cookies.Add(cookie);
                    cookie = ParseCookie(pair);
                }
            }
            if (cookie != null)
                cookies.Add(cookie);
            return cookies;
        }
public static CookieList Parse ( string headerValue ) { var cookies = new CookieList ( ) ; Cookie ? cookie = null ; var pairs = SplitCookieHeaderValue ( headerValue ) ; for ( var i = 0 ; i < pairs . Length ; i ++ ) { var pair = pairs [ i ] . Trim ( ) ; if ( pair . Length == 0 ) continue ; if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { cookie . Version = int . Parse ( GetValue ( pair , true ) , CultureInfo . InvariantCulture ) ; } else if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { var buff = new StringBuilder ( GetValue ( pair ) , 32 ) ; if ( i < pairs . Length - 1 ) buff . AppendFormat ( CultureInfo . InvariantCulture , " " , pairs [ ++ i ] . Trim ( ) ) ; if ( ! HttpDate . TryParse ( buff . ToString ( ) , out var expires ) ) expires = DateTimeOffset . Now ; if ( cookie . Expires == DateTime . MinValue ) cookie . Expires = expires . LocalDateTime ; } else if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { var max = int . Parse ( GetValue ( pair , true ) , CultureInfo . InvariantCulture ) ; cookie . Expires = DateTime . Now . AddSeconds ( max ) ; } else if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { cookie . Path = GetValue ( pair ) ; } else if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { cookie . Domain = GetValue ( pair ) ; } else if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { cookie . Port = pair . Equals ( " " , StringComparison . OrdinalIgnoreCase ) ? " \" \" " : GetValue ( pair ) ; } else if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { cookie . Comment = WebUtility . UrlDecode ( GetValue ( pair ) ) ; } else if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { cookie . CommentUri = UriUtility . StringToUri ( GetValue ( pair , true ) ) ; } else if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { cookie . Discard = true ; } else if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { cookie . Secure = true ; } else if ( pair . StartsWith ( " " , StringComparison . OrdinalIgnoreCase ) && cookie != null ) { cookie . HttpOnly = true ; } else { if ( cookie != null ) cookies . Add ( cookie ) ; cookie = ParseCookie ( pair ) ; } } if ( cookie != null ) cookies . Add ( cookie ) ; return cookies ; }
Creates a  by parsing
the value of one or more CookieSet-Cookie
Creates a by parsing the value of one or more CookieSet - Cookie
A newly-created instance of .
A newly - created instance of .
headerValue
string
The value, or comma-separated list of values,
of the header or headers.
The value or comma - separated list of values of the header or headers .