// 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.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.Server.HttpSys
{
///
/// Represents a wrapper for RequestHeaders and ResponseHeaders.
///
internal class HeaderDictionary : IHeaderDictionary
{
public HeaderDictionary(IDictionary store)
{
Store = store;
}
private IDictionary Store { get; set; }
///
/// Get or sets the associated value from the collection as a single string.
///
/// The header name.
/// the associated value from the collection as a StringValues or StringValues.Empty if the key is not present.
public StringValues this[string key]
{
get
{
StringValues value;
if (TryGetValue(key, out value))
{
return value;
}
return StringValues.Empty;
}
set
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (StringValues.IsNullOrEmpty(value))
{
Store.Remove(key);
}
else
{
Store[key] = value;
}
}
}
///
/// Throws KeyNotFoundException if the key is not present.
///
/// The header name.
///
StringValues IDictionary.this[string key]
{
get { return Store[key]; }
set { this[key] = value; }
}
///
/// 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; }
}
public ICollection Keys
{
get { return Store.Keys; }
}
public ICollection Values
{
get { return Store.Values; }
}
///
/// Adds a new list of items to the collection.
///
/// The item to add.
public void Add(KeyValuePair item)
{
Store.Add(item.Key, item.Value);
}
///
/// Adds the given header and values to the collection.
///
/// The header name.
/// The header values.
public void Add(string key, StringValues value)
{
Store.Add(key, value);
}
///
/// 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);
}
///
/// 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);
}
///
/// 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);
}
///
/// 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 StringValues value)
{
return Store.TryGetValue(key, out 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 Store.GetEnumerator();
}
}
}