Ensure HeaderDictionary store is initialized consistently (#979)

This commit is contained in:
user1336 2017-12-21 17:50:32 +01:00 committed by Chris Ross
parent 150bb3faf4
commit db3c3ba589
1 changed files with 12 additions and 15 deletions

View File

@ -32,11 +32,19 @@ namespace Microsoft.AspNetCore.Http
public HeaderDictionary(int capacity) public HeaderDictionary(int capacity)
{ {
Store = new Dictionary<string, StringValues>(capacity, StringComparer.OrdinalIgnoreCase); EnsureStore(capacity);
} }
private Dictionary<string, StringValues> Store { get; set; } private Dictionary<string, StringValues> Store { get; set; }
private void EnsureStore(int capacity)
{
if (Store == null)
{
Store = new Dictionary<string, StringValues>(capacity, StringComparer.OrdinalIgnoreCase);
}
}
/// <summary> /// <summary>
/// Get or sets the associated value from the collection as a single string. /// Get or sets the associated value from the collection as a single string.
/// </summary> /// </summary>
@ -72,11 +80,7 @@ namespace Microsoft.AspNetCore.Http
} }
else else
{ {
if (Store == null) EnsureStore(1);
{
Store = new Dictionary<string, StringValues>(1, StringComparer.OrdinalIgnoreCase);
}
Store[key] = value; Store[key] = value;
} }
} }
@ -173,10 +177,7 @@ namespace Microsoft.AspNetCore.Http
throw new ArgumentNullException("The key is null"); throw new ArgumentNullException("The key is null");
} }
ThrowIfReadOnly(); ThrowIfReadOnly();
if (Store == null) EnsureStore(1);
{
Store = new Dictionary<string, StringValues>(1, StringComparer.OrdinalIgnoreCase);
}
Store.Add(item.Key, item.Value); Store.Add(item.Key, item.Value);
} }
@ -192,11 +193,7 @@ namespace Microsoft.AspNetCore.Http
throw new ArgumentNullException(nameof(key)); throw new ArgumentNullException(nameof(key));
} }
ThrowIfReadOnly(); ThrowIfReadOnly();
EnsureStore(1);
if (Store == null)
{
Store = new Dictionary<string, StringValues>(1);
}
Store.Add(key, value); Store.Add(key, value);
} }