// Copyright (c) Microsoft Open Technologies, Inc. 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 System.Linq;
using Microsoft.AspNet.Http.Infrastructure;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.PipelineCore.Infrastructure;
namespace Microsoft.AspNet.PipelineCore.Collections
{
///
/// Represents a wrapper for owin.RequestHeaders and owin.ResponseHeaders.
///
public class HeaderDictionary : IHeaderDictionary
{
///
/// Initializes a new instance of the class.
///
/// The underlying data store.
public HeaderDictionary(IDictionary store)
{
if (store == null)
{
throw new ArgumentNullException("store");
}
Store = store;
}
private IDictionary Store { get; set; }
///
/// Gets an that contains the keys in the ;.
///
/// An that contains the keys in the .
public ICollection Keys
{
get { return Store.Keys; }
}
///
///
///
public ICollection Values
{
get { return Store.Values; }
}
///
/// Gets the number of elements contained in the ;.
///
/// The number of elements contained in the .
public int Count
{
get { return Store.Count; }
}
///
/// Gets a value that indicates whether the is in read-only mode.
///
/// true if the is in read-only mode; otherwise, false.
public bool IsReadOnly
{
get { return Store.IsReadOnly; }
}
///
/// Get or sets the associated value from the collection as a single string.
///
/// The header name.
/// the associated value from the collection as a single string or null if the key is not present.
public string this[string key]
{
get { return Get(key); }
set { Set(key, value); }
}
///
/// Throws KeyNotFoundException if the key is not present.
///
/// The header name.
///
string[] IDictionary.this[string key]
{
get { return Store[key]; }
set { Store[key] = value; }
}
///
/// Returns an enumerator that iterates through a collection.
///
/// An object that can be used to iterate through the collection.
public IEnumerator> GetEnumerator()
{
return Store.GetEnumerator();
}
///
/// Returns an enumerator that iterates through a collection.
///
/// An object that can be used to iterate through the collection.
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
///
/// Get the associated value from the collection as a single string.
///
/// The header name.
/// the associated value from the collection as a single string or null if the key is not present.
public string Get(string key)
{
return ParsingHelpers.GetHeader(Store, key);
}
///
/// Get the associated values from the collection without modification.
///
/// The header name.
/// the associated value from the collection without modification, or null if the key is not present.
public IList GetValues(string key)
{
return ParsingHelpers.GetHeaderUnmodified(Store, key);
}
///
/// Get the associated values from the collection separated into individual values.
/// Quoted values will not be split, and the quotes will be removed.
///
/// The header name.
/// the associated values from the collection separated into individual values, or null if the key is not present.
public IList GetCommaSeparatedValues(string key)
{
IEnumerable values = ParsingHelpers.GetHeaderSplit(Store, key);
return values == null ? null : values.ToList();
}
///
/// Add a new value. Appends to the header if already present
///
/// The header name.
/// The header value.
public void Append(string key, string value)
{
ParsingHelpers.AppendHeader(Store, key, value);
}
///
/// Add new values. Each item remains a separate array entry.
///
/// The header name.
/// The header values.
public void AppendValues(string key, params string[] values)
{
ParsingHelpers.AppendHeaderUnmodified(Store, key, values);
}
///
/// Quotes any values containing comas, and then coma joins all of the values with any existing values.
///
/// The header name.
/// The header values.
public void AppendCommaSeparatedValues(string key, params string[] values)
{
ParsingHelpers.AppendHeaderJoined(Store, key, values);
}
///
/// Sets a specific header value.
///
/// The header name.
/// The header value.
public void Set(string key, string value)
{
ParsingHelpers.SetHeader(Store, key, value);
}
///
/// Sets the specified header values without modification.
///
/// The header name.
/// The header values.
public void SetValues(string key, params string[] values)
{
ParsingHelpers.SetHeaderUnmodified(Store, key, values);
}
///
/// Quotes any values containing comas, and then coma joins all of the values.
///
/// The header name.
/// The header values.
public void SetCommaSeparatedValues(string key, params string[] values)
{
ParsingHelpers.SetHeaderJoined(Store, key, values);
}
///
/// Adds the given header and values to the collection.
///
/// The header name.
/// The header values.
public void Add(string key, string[] value)
{
Store.Add(key, value);
}
///
/// Determines whether the contains a specific key.
///
/// The key.
/// true if the contains a specific key; otherwise, false.
public bool ContainsKey(string key)
{
return Store.ContainsKey(key);
}
///
/// Removes the given header from the collection.
///
/// The header name.
/// true if the specified object was removed from the collection; otherwise, false.
public bool Remove(string key)
{
return Store.Remove(key);
}
///
/// Retrieves a value from the dictionary.
///
/// The header name.
/// The value.
/// true if the contains the key; otherwise, false.
public bool TryGetValue(string key, out string[] value)
{
return Store.TryGetValue(key, out value);
}
///
/// Adds a new list of items to the collection.
///
/// The item to add.
public void Add(KeyValuePair item)
{
Store.Add(item);
}
///
/// Clears the entire list of objects.
///
public void Clear()
{
Store.Clear();
}
///
/// Returns a value indicating whether the specified object occurs within this collection.
///
/// The item.
/// true if the specified object occurs within this collection; otherwise, false.
public bool Contains(KeyValuePair item)
{
return Store.Contains(item);
}
///
/// Copies the elements to a one-dimensional Array instance at the specified index.
///
/// The one-dimensional Array that is the destination of the specified objects copied from the .
/// The zero-based index in at which copying begins.
public void CopyTo(KeyValuePair[] array, int arrayIndex)
{
Store.CopyTo(array, arrayIndex);
}
///
/// Removes the given item from the the collection.
///
/// The item.
/// true if the specified object was removed from the collection; otherwise, false.
public bool Remove(KeyValuePair item)
{
return Store.Remove(item);
}
}
}