205 lines
7.1 KiB
C#
205 lines
7.1 KiB
C#
// 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
|
|
{
|
|
/// <summary>
|
|
/// Represents a wrapper for RequestHeaders and ResponseHeaders.
|
|
/// </summary>
|
|
internal class HeaderDictionary : IHeaderDictionary
|
|
{
|
|
public HeaderDictionary(IDictionary<string, StringValues> store)
|
|
{
|
|
Store = store;
|
|
}
|
|
|
|
private IDictionary<string, StringValues> Store { get; set; }
|
|
|
|
/// <summary>
|
|
/// Get or sets the associated value from the collection as a single string.
|
|
/// </summary>
|
|
/// <param name="key">The header name.</param>
|
|
/// <returns>the associated value from the collection as a StringValues or StringValues.Empty if the key is not present.</returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Throws KeyNotFoundException if the key is not present.
|
|
/// </summary>
|
|
/// <param name="key">The header name.</param>
|
|
/// <returns></returns>
|
|
StringValues IDictionary<string, StringValues>.this[string key]
|
|
{
|
|
get { return Store[key]; }
|
|
set { this[key] = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the number of elements contained in the <see cref="T:HeaderDictionary" />;.
|
|
/// </summary>
|
|
/// <returns>The number of elements contained in the <see cref="T:HeaderDictionary" />.</returns>
|
|
public int Count
|
|
{
|
|
get { return Store.Count; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a value that indicates whether the <see cref="T:HeaderDictionary" /> is in read-only mode.
|
|
/// </summary>
|
|
/// <returns>true if the <see cref="T:HeaderDictionary" /> is in read-only mode; otherwise, false.</returns>
|
|
public bool IsReadOnly
|
|
{
|
|
get { return Store.IsReadOnly; }
|
|
}
|
|
|
|
public ICollection<string> Keys
|
|
{
|
|
get { return Store.Keys; }
|
|
}
|
|
|
|
public ICollection<StringValues> Values
|
|
{
|
|
get { return Store.Values; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a new list of items to the collection.
|
|
/// </summary>
|
|
/// <param name="item">The item to add.</param>
|
|
public void Add(KeyValuePair<string, StringValues> item)
|
|
{
|
|
Store.Add(item.Key, item.Value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds the given header and values to the collection.
|
|
/// </summary>
|
|
/// <param name="key">The header name.</param>
|
|
/// <param name="value">The header values.</param>
|
|
public void Add(string key, StringValues value)
|
|
{
|
|
Store.Add(key, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears the entire list of objects.
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
Store.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a value indicating whether the specified object occurs within this collection.
|
|
/// </summary>
|
|
/// <param name="item">The item.</param>
|
|
/// <returns>true if the specified object occurs within this collection; otherwise, false.</returns>
|
|
public bool Contains(KeyValuePair<string, StringValues> item)
|
|
{
|
|
return Store.Contains(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the <see cref="T:HeaderDictionary" /> contains a specific key.
|
|
/// </summary>
|
|
/// <param name="key">The key.</param>
|
|
/// <returns>true if the <see cref="T:HeaderDictionary" /> contains a specific key; otherwise, false.</returns>
|
|
public bool ContainsKey(string key)
|
|
{
|
|
return Store.ContainsKey(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copies the <see cref="T:HeaderDictionary" /> elements to a one-dimensional Array instance at the specified index.
|
|
/// </summary>
|
|
/// <param name="array">The one-dimensional Array that is the destination of the specified objects copied from
|
|
/// the <see cref="T:HeaderDictionary" />.</param>
|
|
/// <param name="arrayIndex">The zero-based index in <paramref name="array" /> at which copying begins.</param>
|
|
public void CopyTo(KeyValuePair<string, StringValues>[] array, int arrayIndex)
|
|
{
|
|
Store.CopyTo(array, arrayIndex);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the given item from the the collection.
|
|
/// </summary>
|
|
/// <param name="item">The item.</param>
|
|
/// <returns>true if the specified object was removed from the collection; otherwise, false.</returns>
|
|
public bool Remove(KeyValuePair<string, StringValues> item)
|
|
{
|
|
return Store.Remove(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the given header from the collection.
|
|
/// </summary>
|
|
/// <param name="key">The header name.</param>
|
|
/// <returns>true if the specified object was removed from the collection; otherwise, false.</returns>
|
|
public bool Remove(string key)
|
|
{
|
|
return Store.Remove(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves a value from the dictionary.
|
|
/// </summary>
|
|
/// <param name="key">The header name.</param>
|
|
/// <param name="value">The value.</param>
|
|
/// <returns>true if the <see cref="T:HeaderDictionary" /> contains the key; otherwise, false.</returns>
|
|
public bool TryGetValue(string key, out StringValues value)
|
|
{
|
|
return Store.TryGetValue(key, out value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns an enumerator that iterates through a collection.
|
|
/// </summary>
|
|
/// <returns>An <see cref="T:IEnumerator" /> object that can be used to iterate through the collection.</returns>
|
|
public IEnumerator GetEnumerator()
|
|
{
|
|
return Store.GetEnumerator();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns an enumerator that iterates through a collection.
|
|
/// </summary>
|
|
/// <returns>An <see cref="T:IEnumerator" /> object that can be used to iterate through the collection.</returns>
|
|
IEnumerator<KeyValuePair<string, StringValues>> IEnumerable<KeyValuePair<string, StringValues>>.GetEnumerator()
|
|
{
|
|
return Store.GetEnumerator();
|
|
}
|
|
}
|
|
}
|