Ejemplo n.º 1
0
        public static RequestCookieCollection Parse(IList <string> values)
        {
            if (values.Count == 0)
            {
                return(Empty);
            }

            IList <CookieHeaderValue> cookies;

            if (CookieHeaderValue.TryParseList(values, out cookies))
            {
                if (cookies.Count == 0)
                {
                    return(Empty);
                }

                var store = new Dictionary <string, string>(cookies.Count);
                for (var i = 0; i < cookies.Count; i++)
                {
                    var cookie = cookies[i];
                    var name   = Uri.UnescapeDataString(cookie.Name.Value);
                    var value  = Uri.UnescapeDataString(cookie.Value.Value);
                    store[name] = value;
                }

                return(new RequestCookieCollection(store));
            }
            return(Empty);
        }
Ejemplo n.º 2
0
        internal static RequestCookieCollection ParseInternal(IList <string> values, bool enableCookieNameEncoding)
        {
            if (values.Count == 0)
            {
                return(Empty);
            }

            if (CookieHeaderValue.TryParseList(values, out var cookies))
            {
                if (cookies.Count == 0)
                {
                    return(Empty);
                }

                var collection = new RequestCookieCollection(cookies.Count);
                var store      = collection.Store !;
                for (var i = 0; i < cookies.Count; i++)
                {
                    var cookie = cookies[i];
                    var name   = enableCookieNameEncoding ? Uri.UnescapeDataString(cookie.Name.Value) : cookie.Name.Value;
                    var value  = Uri.UnescapeDataString(cookie.Value.Value);
                    store[name] = value;
                }

                return(collection);
            }
            return(Empty);
        }
Ejemplo n.º 3
0
    public void CookieHeaderValue_TryParseList_ExcludesInvalidValues(IList <CookieHeaderValue> cookies, string[] input)
    {
        var result = CookieHeaderValue.TryParseList(input, out var results);

        Assert.Equal(cookies, results);
        Assert.Equal(cookies?.Count > 0, result);
    }
Ejemplo n.º 4
0
    public void CookieHeaderValue_TryParseList_AcceptsValidValues(IList <CookieHeaderValue> cookies, string[] input)
    {
        var result = CookieHeaderValue.TryParseList(input, out var results);

        Assert.True(result);

        Assert.Equal(cookies, results);
    }
Ejemplo n.º 5
0
        public static bool TryGetSetCookeToken(this HttpResponse response, out string token)
        {
            if (response.Headers.TryGetValue("Set-Cookie", out var cookieHeader) &&
                CookieHeaderValue.TryParseList(cookieHeader, out var cookies))
            {
                var tokenCookie = cookies.SingleOrDefault(x => x.Name.Equals("TOKEN", StringComparison.OrdinalIgnoreCase));
                token = tokenCookie?.Value.ToString();
            }
            else
            {
                token = default;
            }

            return(token != default);
        }
Ejemplo n.º 6
0
        public void Reparse(IList <string> values)
        {
            _dictionary.Clear();

            IList <CookieHeaderValue> cookies;

            if (CookieHeaderValue.TryParseList(values, out cookies))
            {
                foreach (var cookie in cookies)
                {
                    var name  = Uri.UnescapeDataString(cookie.Name.Replace('+', ' '));
                    var value = Uri.UnescapeDataString(cookie.Value.Replace('+', ' '));
                    _dictionary[name] = value;
                }
            }
        }