// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections; using System.Collections.Generic; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNet.Http.Internal { public class RequestCookiesCollection : IReadableStringCollection { private readonly IDictionary _dictionary; public RequestCookiesCollection() { _dictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); } public string this[string key] { get { return Get(key); } } /// /// Gets the number of elements contained in the collection. /// public int Count { get { return _dictionary.Count; } } /// /// Gets a collection containing the keys. /// public ICollection Keys { get { return _dictionary.Keys; } } /// /// Determines whether the collection contains an element with the specified key. /// /// /// public bool ContainsKey(string key) { return _dictionary.ContainsKey(key); } /// /// Get the associated value from the collection. Multiple values will be merged. /// Returns null if the key is not present. /// /// /// public string Get(string key) { string value; return _dictionary.TryGetValue(key, out value) ? value : null; } /// /// Get the associated values from the collection in their original format. /// Returns null if the key is not present. /// /// /// public IList GetValues(string key) { string value; return _dictionary.TryGetValue(key, out value) ? new[] { value } : null; } public void Reparse(IList values) { _dictionary.Clear(); IList 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; } } } public IEnumerator> GetEnumerator() { foreach (var pair in _dictionary) { yield return new KeyValuePair(pair.Key, new[] { pair.Value }); } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } }