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); }
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 collection = new RequestCookieCollection(cookies.Count); var store = collection.Store; 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(collection); } return(Empty); }
internal static RequestCookieCollection ParseInternal(StringValues values, bool enableCookieNameEncoding) { if (values.Count == 0) { return(Empty); } var collection = new RequestCookieCollection(values.Count); var store = collection.Store !; if (CookieHeaderParserShared.TryParseValues(values, store, enableCookieNameEncoding, supportsMultipleValues: true)) { if (store.Count == 0) { return(Empty); } return(collection); } return(Empty); }
internal static RequestCookieCollection ParseInternal(StringValues values, bool enableCookieNameEncoding) { if (values.Count == 0) { return(Empty); } // Do not set the collection capacity based on StringValues.Count, the Cookie header is supposed to be a single combined value. var collection = new RequestCookieCollection(); var store = collection.Store !; if (CookieHeaderParserShared.TryParseValues(values, store, enableCookieNameEncoding, supportsMultipleValues: true)) { if (store.Count == 0) { return(Empty); } return(collection); } return(Empty); }
public void Cookies_GetAndSet() { var request = new DefaultHttpContext().Request; var cookieHeaders = request.Headers["Cookie"]; Assert.Empty(cookieHeaders); var cookies0 = request.Cookies; Assert.Empty(cookies0); Assert.Null(cookies0["key0"]); Assert.False(cookies0.ContainsKey("key0")); var newCookies = new[] { "name0=value0%2C", "name1=value1" }; request.Headers["Cookie"] = newCookies; cookies0 = RequestCookieCollection.Parse(newCookies); var cookies1 = request.Cookies; Assert.Equal(cookies0, cookies1); Assert.Equal(2, cookies1.Count); Assert.Equal("value0,", cookies1["name0"]); Assert.Equal("value1", cookies1["name1"]); Assert.Equal(newCookies, request.Headers["Cookie"]); var cookies2 = new RequestCookieCollection(new Dictionary <string, string>() { { "name2", "value2" } }); request.Cookies = cookies2; Assert.Equal(cookies2, request.Cookies); Assert.Equal("value2", request.Cookies["name2"]); cookieHeaders = request.Headers["Cookie"]; Assert.Equal(new[] { "name2=value2" }, cookieHeaders); }
public void Parse_TypicalCookie() { _ = RequestCookieCollection.Parse(_cookie); }