using System; using System.Collections; using System.Collections.Generic; using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.PipelineCore.Infrastructure; namespace Microsoft.AspNet.PipelineCore.Collections { public class RequestCookiesCollection : IReadableStringCollection { private readonly IDictionary _dictionary; public RequestCookiesCollection() { _dictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); } public IEnumerator> GetEnumerator() { foreach (var pair in _dictionary) { yield return new KeyValuePair(pair.Key, new[] { pair.Value }); } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public string this[string key] { get { return Get(key); } } public string Get(string key) { string value; return _dictionary.TryGetValue(key, out value) ? value : null; } public IList GetValues(string key) { string value; return _dictionary.TryGetValue(key, out value) ? new[] { value } : null; } private static readonly char[] SemicolonAndComma = { ';', ',' }; public void Reparse(string cookiesHeader) { _dictionary.Clear(); ParsingHelpers.ParseDelimited(cookiesHeader, SemicolonAndComma, AddCookieCallback, _dictionary); } private static readonly Action AddCookieCallback = (name, value, state) => { var dictionary = (IDictionary)state; if (!dictionary.ContainsKey(name)) { dictionary.Add(name, value); } }; } }