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

Commit b7bf77d

Browse files
committed
#361 Introduce StringValues to replace string[] usage.
1 parent 8487e42 commit b7bf77d

29 files changed

+757
-369
lines changed

src/Microsoft.AspNet.Http.Abstractions/IHeaderDictionary.cs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
5-
using System.Diagnostics.CodeAnalysis;
65

76
namespace Microsoft.AspNet.Http
87
{
98
/// <summary>
109
/// Represents request and response headers
1110
/// </summary>
12-
public interface IHeaderDictionary : IReadableStringCollection, IDictionary<string, string[]>
11+
public interface IHeaderDictionary : IReadableStringCollection, IDictionary<string, StringValues>
1312
{
13+
// This property is duplicated to resolve an ambiguity between IReadableStringCollection and IDictionary<string, string[]>
1414
/// <summary>
15-
/// Get or sets the associated value from the collection as a single string.
15+
///
1616
/// </summary>
17-
/// <param name="key">The header name.</param>
18-
/// <returns>the associated value from the collection as a single string or null if the key is not present.</returns>
19-
new string this[string key] { get; set; }
17+
/// <param name="key"></param>
18+
/// <returns>The stored value, or StringValues.Empty if the key is not present.</returns>
19+
new StringValues this[string key] { get; set; }
2020

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

4141
/// <summary>
42-
/// Add a new value. Appends to the header if already present
42+
/// Add a new value. Appends to the header list if already present
4343
/// </summary>
4444
/// <param name="key">The header name.</param>
4545
/// <param name="value">The header value.</param>
46-
void Append(string key, string value);
47-
48-
/// <summary>
49-
/// Add new values. Each item remains a separate array entry.
50-
/// </summary>
51-
/// <param name="key">The header name.</param>
52-
/// <param name="values">The header values.</param>
53-
void AppendValues(string key, params string[] values);
46+
void Append(string key, StringValues value);
5447

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

62-
/// <summary>
63-
/// Sets a specific header value.
64-
/// </summary>
65-
/// <param name="key">The header name.</param>
66-
/// <param name="value">The header value.</param>
67-
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Set", Justification = "Re-evaluate later.")]
68-
void Set(string key, string value);
69-
70-
/// <summary>
71-
/// Sets the specified header values without modification.
72-
/// </summary>
73-
/// <param name="key">The header name.</param>
74-
/// <param name="values">The header values.</param>
75-
void SetValues(string key, params string[] values);
76-
7755
/// <summary>
7856
/// Quotes any values containing comas, and then coma joins all of the values.
7957
/// </summary>

src/Microsoft.AspNet.Http.Abstractions/IReadableStringCollection.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
5-
using System.Diagnostics.CodeAnalysis;
65

76
namespace Microsoft.AspNet.Http
87
{
98
/// <summary>
109
/// Accessors for headers, query, forms, etc.
1110
/// </summary>
12-
public interface IReadableStringCollection : IEnumerable<KeyValuePair<string, string[]>>
11+
public interface IReadableStringCollection : IEnumerable<KeyValuePair<string, StringValues>>
1312
{
1413
/// <summary>
15-
/// Get the associated value from the collection. Multiple values will be merged.
16-
/// Returns null if the key is not present.
14+
/// Get the associated value from the collection.
15+
/// Returns StringValues.Empty if the key is not present.
1716
/// </summary>
1817
/// <param name="key"></param>
1918
/// <returns></returns>
20-
string this[string key] { get; }
19+
StringValues this[string key] { get; }
2120

2221
/// <summary>
2322
/// Gets the number of elements contained in the collection.
@@ -35,22 +34,5 @@ public interface IReadableStringCollection : IEnumerable<KeyValuePair<string, st
3534
/// <param name="key"></param>
3635
/// <returns></returns>
3736
bool ContainsKey(string key);
38-
39-
/// <summary>
40-
/// Get the associated value from the collection. Multiple values will be merged.
41-
/// Returns null if the key is not present.
42-
/// </summary>
43-
/// <param name="key"></param>
44-
/// <returns></returns>
45-
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get", Justification = "Re-evaluate later.")]
46-
string Get(string key);
47-
48-
/// <summary>
49-
/// Get the associated values from the collection in their original format.
50-
/// Returns null if the key is not present.
51-
/// </summary>
52-
/// <param name="key"></param>
53-
/// <returns></returns>
54-
IList<string> GetValues(string key);
5537
}
5638
}

src/Microsoft.AspNet.Http.Extensions/HeaderDictionaryTypeExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ internal static void SetList<T>([NotNull] this IHeaderDictionary headers, [NotNu
5050
}
5151
else
5252
{
53-
headers.SetValues(name, values.Select(value => value.ToString()).ToArray());
53+
headers[name] = values.Select(value => value.ToString()).ToArray();
5454
}
5555
}
5656

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

100100
var value = headers[name];
101-
if (string.IsNullOrWhiteSpace(value))
101+
if (StringValues.IsNullOrEmpty(value))
102102
{
103103
return default(T);
104104
}
@@ -112,11 +112,11 @@ internal static IList<T> GetList<T>([NotNull] this IHeaderDictionary headers, st
112112
if (KnownListParsers.TryGetValue(typeof(T), out temp))
113113
{
114114
var func = (Func<IList<string>, IList<T>>)temp;
115-
return func(headers.GetValues(name));
115+
return func(headers[name]);
116116
}
117117

118-
var values = headers.GetValues(name);
119-
if (values == null || !values.Any())
118+
var values = headers[name];
119+
if (StringValues.IsNullOrEmpty(values))
120120
{
121121
return null;
122122
}
@@ -158,7 +158,7 @@ private static T GetViaReflection<T>(string value)
158158
return default(T);
159159
}
160160

161-
private static IList<T> GetListViaReflection<T>(IList<string> values)
161+
private static IList<T> GetListViaReflection<T>(StringValues values)
162162
{
163163
// TODO: Cache the reflected type for later? Only if success?
164164
var type = typeof(T);

src/Microsoft.AspNet.Http.Extensions/RequestHeaders.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public void Append([NotNull] string name, [NotNull] object value)
285285

286286
public void AppendList<T>([NotNull] string name, [NotNull] IList<T> values)
287287
{
288-
Headers.AppendValues(name, values.Select(value => value.ToString()).ToArray());
288+
Headers.Append(name, values.Select(value => value.ToString()).ToArray());
289289
}
290290
}
291291
}

src/Microsoft.AspNet.Http.Extensions/ResponseHeaders.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void Append([NotNull] string name, [NotNull] object value)
182182

183183
public void AppendList<T>([NotNull] string name, [NotNull] IList<T> values)
184184
{
185-
Headers.AppendValues(name, values.Select(value => value.ToString()).ToArray());
185+
Headers.Append(name, values.Select(value => value.ToString()).ToArray());
186186
}
187187
}
188188
}

src/Microsoft.AspNet.Http.Features/IHttpRequestFeature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface IHttpRequestFeature
1414
string PathBase { get; set; }
1515
string Path { get; set; }
1616
string QueryString { get; set; }
17-
IDictionary<string, string[]> Headers { get; set; }
17+
IDictionary<string, StringValues> Headers { get; set; }
1818
Stream Body { get; set; }
1919
}
2020
}

src/Microsoft.AspNet.Http.Features/IHttpResponseFeature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface IHttpResponseFeature
1212
{
1313
int StatusCode { get; set; }
1414
string ReasonPhrase { get; set; }
15-
IDictionary<string, string[]> Headers { get; set; }
15+
IDictionary<string, StringValues> Headers { get; set; }
1616
Stream Body { get; set; }
1717
bool HasStarted { get; }
1818
void OnStarting(Func<object, Task> callback, object state);

0 commit comments

Comments
 (0)