Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
#361 Introduce StringValues to replace string[] usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Aug 18, 2015
1 parent 8487e42 commit 087080a
Show file tree
Hide file tree
Showing 29 changed files with 784 additions and 348 deletions.
40 changes: 9 additions & 31 deletions src/Microsoft.AspNet.Http.Abstractions/IHeaderDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.AspNet.Http
{
/// <summary>
/// Represents request and response headers
/// </summary>
public interface IHeaderDictionary : IReadableStringCollection, IDictionary<string, string[]>
public interface IHeaderDictionary : IReadableStringCollection, IDictionary<string, StringValues>
{
// This property is duplicated to resolve an ambiguity between IReadableStringCollection and IDictionary<string, string[]>
/// <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 single string or null if the key is not present.</returns>
new string this[string key] { get; set; }
/// <param name="key"></param>
/// <returns>The stored value, or StringValues.Empty if the key is not present.</returns>
new StringValues this[string key] { get; set; }

// This property is duplicated to resolve an ambiguity between IReadableStringCollection.Count and IDictionary<string, string[]>.Count
/// <summary>
Expand All @@ -36,21 +36,14 @@ public interface IHeaderDictionary : IReadableStringCollection, IDictionary<stri
/// </summary>
/// <param name="key">The header name.</param>
/// <returns>the associated values from the collection separated into individual values, or null if the key is not present.</returns>
IList<string> GetCommaSeparatedValues(string key);
StringValues GetCommaSeparatedValues(string key);

/// <summary>
/// Add a new value. Appends to the header if already present
/// Add a new value. Appends to the header list if already present
/// </summary>
/// <param name="key">The header name.</param>
/// <param name="value">The header value.</param>
void Append(string key, string value);

/// <summary>
/// Add new values. Each item remains a separate array entry.
/// </summary>
/// <param name="key">The header name.</param>
/// <param name="values">The header values.</param>
void AppendValues(string key, params string[] values);
void Append(string key, StringValues value);

/// <summary>
/// Quotes any values containing comas, and then coma joins all of the values with any existing values.
Expand All @@ -59,21 +52,6 @@ public interface IHeaderDictionary : IReadableStringCollection, IDictionary<stri
/// <param name="values">The header values.</param>
void AppendCommaSeparatedValues(string key, params string[] values);

/// <summary>
/// Sets a specific header value.
/// </summary>
/// <param name="key">The header name.</param>
/// <param name="value">The header value.</param>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Set", Justification = "Re-evaluate later.")]
void Set(string key, string value);

/// <summary>
/// Sets the specified header values without modification.
/// </summary>
/// <param name="key">The header name.</param>
/// <param name="values">The header values.</param>
void SetValues(string key, params string[] values);

/// <summary>
/// Quotes any values containing comas, and then coma joins all of the values.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.AspNet.Http
{
/// <summary>
/// Accessors for headers, query, forms, etc.
/// </summary>
public interface IReadableStringCollection : IEnumerable<KeyValuePair<string, string[]>>
public interface IReadableStringCollection : IEnumerable<KeyValuePair<string, StringValues>>
{
/// <summary>
/// Get the associated value from the collection. Multiple values will be merged.
/// Returns null if the key is not present.
/// Get the associated value from the collection.
/// Returns StringValues.Empty if the key is not present.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
string this[string key] { get; }
StringValues this[string key] { get; }

/// <summary>
/// Gets the number of elements contained in the collection.
Expand All @@ -35,22 +34,5 @@ public interface IReadableStringCollection : IEnumerable<KeyValuePair<string, st
/// <param name="key"></param>
/// <returns></returns>
bool ContainsKey(string key);

/// <summary>
/// Get the associated value from the collection. Multiple values will be merged.
/// Returns null if the key is not present.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get", Justification = "Re-evaluate later.")]
string Get(string key);

/// <summary>
/// Get the associated values from the collection in their original format.
/// Returns null if the key is not present.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
IList<string> GetValues(string key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal static void SetList<T>([NotNull] this IHeaderDictionary headers, [NotNu
}
else
{
headers.SetValues(name, values.Select(value => value.ToString()).ToArray());
headers[name] = values.Select(value => value.ToString()).ToArray();
}
}

Expand Down Expand Up @@ -98,7 +98,7 @@ internal static T Get<T>([NotNull] this IHeaderDictionary headers, string name)
}

var value = headers[name];
if (string.IsNullOrWhiteSpace(value))
if (StringValues.IsNullOrEmpty(value))
{
return default(T);
}
Expand All @@ -112,11 +112,11 @@ internal static IList<T> GetList<T>([NotNull] this IHeaderDictionary headers, st
if (KnownListParsers.TryGetValue(typeof(T), out temp))
{
var func = (Func<IList<string>, IList<T>>)temp;
return func(headers.GetValues(name));
return func(headers[name]);
}

var values = headers.GetValues(name);
if (values == null || !values.Any())
var values = headers[name];
if (StringValues.IsNullOrEmpty(values))
{
return null;
}
Expand Down Expand Up @@ -158,7 +158,7 @@ private static T GetViaReflection<T>(string value)
return default(T);
}

private static IList<T> GetListViaReflection<T>(IList<string> values)
private static IList<T> GetListViaReflection<T>(StringValues values)
{
// TODO: Cache the reflected type for later? Only if success?
var type = typeof(T);
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http.Extensions/RequestHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public void Append([NotNull] string name, [NotNull] object value)

public void AppendList<T>([NotNull] string name, [NotNull] IList<T> values)
{
Headers.AppendValues(name, values.Select(value => value.ToString()).ToArray());
Headers.Append(name, values.Select(value => value.ToString()).ToArray());
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http.Extensions/ResponseHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void Append([NotNull] string name, [NotNull] object value)

public void AppendList<T>([NotNull] string name, [NotNull] IList<T> values)
{
Headers.AppendValues(name, values.Select(value => value.ToString()).ToArray());
Headers.Append(name, values.Select(value => value.ToString()).ToArray());
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http.Features/IHttpRequestFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IHttpRequestFeature
string PathBase { get; set; }
string Path { get; set; }
string QueryString { get; set; }
IDictionary<string, string[]> Headers { get; set; }
IDictionary<string, StringValues> Headers { get; set; }
Stream Body { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Http.Features/IHttpResponseFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IHttpResponseFeature
{
int StatusCode { get; set; }
string ReasonPhrase { get; set; }
IDictionary<string, string[]> Headers { get; set; }
IDictionary<string, StringValues> Headers { get; set; }
Stream Body { get; set; }
bool HasStarted { get; }
void OnStarting(Func<object, Task> callback, object state);
Expand Down
Loading

0 comments on commit 087080a

Please sign in to comment.