Skip to content
This repository was archived by the owner on Mar 19, 2019. It is now read-only.

Commit d858bf7

Browse files
committed
React to string[] -> StringValues changes.
1 parent b4ea8a5 commit d858bf7

File tree

15 files changed

+331
-364
lines changed

15 files changed

+331
-364
lines changed

src/Microsoft.AspNet.Server.WebListener/FeatureContext.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using System.Threading.Tasks;
2727
using Microsoft.AspNet.Http.Features;
2828
using Microsoft.AspNet.Http.Features.Authentication;
29+
using Microsoft.AspNet.Primitives;
2930
using Microsoft.Net.Http.Headers;
3031
using Microsoft.Net.Http.Server;
3132
using Microsoft.Net.WebSockets;
@@ -53,7 +54,7 @@ internal class FeatureContext :
5354
private bool _enableResponseCaching;
5455

5556
private Stream _requestBody;
56-
private IDictionary<string, string[]> _requestHeaders;
57+
private IDictionary<string, StringValues> _requestHeaders;
5758
private string _scheme;
5859
private string _httpMethod;
5960
private string _httpProtocolVersion;
@@ -71,7 +72,7 @@ internal class FeatureContext :
7172
private IAuthenticationHandler _authHandler;
7273
private CancellationToken? _disconnectToken;
7374
private Stream _responseStream;
74-
private IDictionary<string, string[]> _responseHeaders;
75+
private IDictionary<string, StringValues> _responseHeaders;
7576

7677
internal FeatureContext(RequestContext requestContext, bool enableResponseCaching)
7778
{
@@ -115,7 +116,7 @@ Stream IHttpRequestFeature.Body
115116
set { _requestBody = value; }
116117
}
117118

118-
IDictionary<string, string[]> IHttpRequestFeature.Headers
119+
IDictionary<string, StringValues> IHttpRequestFeature.Headers
119120
{
120121
get
121122
{
@@ -348,7 +349,7 @@ Stream IHttpResponseFeature.Body
348349
set { _responseStream = value; }
349350
}
350351

351-
IDictionary<string, string[]> IHttpResponseFeature.Headers
352+
IDictionary<string, StringValues> IHttpResponseFeature.Headers
352353
{
353354
get
354355
{

src/Microsoft.Net.Http.Server/AuthenticationManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using System.Runtime.InteropServices;
2828
using System.Security.Claims;
2929
using System.Security.Principal;
30+
using Microsoft.AspNet.Primitives;
3031

3132
namespace Microsoft.Net.Http.Server
3233
{
@@ -147,7 +148,8 @@ internal void SetAuthenticationChallenge(RequestContext context)
147148

148149
if (challenges.Count > 0)
149150
{
150-
context.Response.Headers.AppendValues(HttpKnownHeaderNames.WWWAuthenticate, challenges.ToArray());
151+
context.Response.Headers[HttpKnownHeaderNames.WWWAuthenticate]
152+
= StringValues.Concat(context.Response.Headers[HttpKnownHeaderNames.WWWAuthenticate], challenges.ToArray());
151153
}
152154
}
153155

src/Microsoft.Net.Http.Server/NativeInterop/UnsafeNativeMethods.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Collections.Generic;
2626
using System.Diagnostics.CodeAnalysis;
2727
using System.Runtime.InteropServices;
28+
using Microsoft.AspNet.Primitives;
2829

2930
namespace Microsoft.Net.Http.Server
3031
{
@@ -1048,7 +1049,7 @@ internal static bool Supported
10481049

10491050
// Server API
10501051

1051-
internal static void GetUnknownHeaders(IDictionary<string, string[]> unknownHeaders, byte[] memoryBlob, IntPtr originalAddress)
1052+
internal static void GetUnknownHeaders(IDictionary<string, StringValues> unknownHeaders, byte[] memoryBlob, IntPtr originalAddress)
10521053
{
10531054
// Return value.
10541055
fixed (byte* pMemoryBlob = memoryBlob)
@@ -1079,7 +1080,7 @@ internal static void GetUnknownHeaders(IDictionary<string, string[]> unknownHead
10791080
}
10801081
// Note that Http.Sys currently collapses all headers of the same name to a single coma seperated string,
10811082
// so we can just call Set.
1082-
unknownHeaders[headerName] = new[] { headerValue };
1083+
unknownHeaders[headerName] = headerValue;
10831084
}
10841085
pUnknownHeader++;
10851086
}

src/Microsoft.Net.Http.Server/RequestProcessing/HeaderCollection.cs

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,49 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using Microsoft.AspNet.Primitives;
78

89
namespace Microsoft.Net.Http.Server
910
{
10-
public class HeaderCollection : IDictionary<string, string[]>
11+
public class HeaderCollection : IDictionary<string, StringValues>
1112
{
1213
public HeaderCollection()
13-
: this(new Dictionary<string, string[]>(4, StringComparer.OrdinalIgnoreCase))
14+
: this(new Dictionary<string, StringValues>(4, StringComparer.OrdinalIgnoreCase))
1415
{
1516
}
1617

17-
public HeaderCollection(IDictionary<string, string[]> store)
18+
public HeaderCollection(IDictionary<string, StringValues> store)
1819
{
1920
Store = store;
2021
}
2122

22-
private IDictionary<string, string[]> Store { get; set; }
23+
private IDictionary<string, StringValues> Store { get; set; }
2324

2425
// Readonly after the response has been started.
2526
public bool IsReadOnly { get; internal set; }
2627

27-
public string this[string key]
28+
public StringValues this[string key]
2829
{
29-
get { return Get(key); }
30+
get
31+
{
32+
StringValues values;
33+
return TryGetValue(key, out values) ? values : StringValues.Empty;
34+
}
3035
set
3136
{
3237
ThrowIfReadOnly();
33-
if (string.IsNullOrEmpty(value))
38+
if (StringValues.IsNullOrEmpty(value))
3439
{
3540
Remove(key);
3641
}
3742
else
3843
{
39-
Set(key, value);
44+
Store[key] = value;
4045
}
4146
}
4247
}
4348

44-
string[] IDictionary<string, string[]>.this[string key]
49+
StringValues IDictionary<string, StringValues>.this[string key]
4550
{
4651
get { return Store[key]; }
4752
set
@@ -61,18 +66,18 @@ public ICollection<string> Keys
6166
get { return Store.Keys; }
6267
}
6368

64-
public ICollection<string[]> Values
69+
public ICollection<StringValues> Values
6570
{
6671
get { return Store.Values; }
6772
}
6873

69-
public void Add(KeyValuePair<string, string[]> item)
74+
public void Add(KeyValuePair<string, StringValues> item)
7075
{
7176
ThrowIfReadOnly();
7277
Store.Add(item);
7378
}
7479

75-
public void Add(string key, string[] value)
80+
public void Add(string key, StringValues value)
7681
{
7782
ThrowIfReadOnly();
7883
Store.Add(key, value);
@@ -81,35 +86,9 @@ public void Add(string key, string[] value)
8186
public void Append(string key, string value)
8287
{
8388
ThrowIfReadOnly();
84-
string[] values;
85-
if (Store.TryGetValue(key, out values))
86-
{
87-
var newValues = new string[values.Length + 1];
88-
Array.Copy(values, newValues, values.Length);
89-
newValues[values.Length] = value;
90-
Store[key] = newValues;
91-
}
92-
else
93-
{
94-
Set(key, value);
95-
}
96-
}
97-
98-
public void AppendValues(string key, params string[] values)
99-
{
100-
ThrowIfReadOnly();
101-
string[] oldValues;
102-
if (Store.TryGetValue(key, out oldValues))
103-
{
104-
var newValues = new string[oldValues.Length + values.Length];
105-
Array.Copy(oldValues, newValues, oldValues.Length);
106-
Array.Copy(values, 0, newValues, oldValues.Length, values.Length);
107-
Store[key] = newValues;
108-
}
109-
else
110-
{
111-
SetValues(key, values);
112-
}
89+
StringValues values;
90+
Store.TryGetValue(key, out values);
91+
Store[key] = StringValues.Concat(values, value);
11392
}
11493

11594
public void Clear()
@@ -118,7 +97,7 @@ public void Clear()
11897
Store.Clear();
11998
}
12099

121-
public bool Contains(KeyValuePair<string, string[]> item)
100+
public bool Contains(KeyValuePair<string, StringValues> item)
122101
{
123102
return Store.Contains(item);
124103
}
@@ -128,37 +107,27 @@ public bool ContainsKey(string key)
128107
return Store.ContainsKey(key);
129108
}
130109

131-
public void CopyTo(KeyValuePair<string, string[]>[] array, int arrayIndex)
110+
public void CopyTo(KeyValuePair<string, StringValues>[] array, int arrayIndex)
132111
{
133112
Store.CopyTo(array, arrayIndex);
134113
}
135114

136-
public string Get(string key)
137-
{
138-
string[] values;
139-
if (Store.TryGetValue(key, out values))
140-
{
141-
return string.Join(", ", values);
142-
}
143-
return null;
144-
}
145-
146-
public IEnumerator<KeyValuePair<string, string[]>> GetEnumerator()
115+
public IEnumerator<KeyValuePair<string, StringValues>> GetEnumerator()
147116
{
148117
return Store.GetEnumerator();
149118
}
150119

151120
public IEnumerable<string> GetValues(string key)
152121
{
153-
string[] values;
122+
StringValues values;
154123
if (Store.TryGetValue(key, out values))
155124
{
156125
return HeaderParser.SplitValues(values);
157126
}
158127
return HeaderParser.Empty;
159128
}
160129

161-
public bool Remove(KeyValuePair<string, string[]> item)
130+
public bool Remove(KeyValuePair<string, StringValues> item)
162131
{
163132
ThrowIfReadOnly();
164133
return Store.Remove(item);
@@ -170,19 +139,7 @@ public bool Remove(string key)
170139
return Store.Remove(key);
171140
}
172141

173-
public void Set(string key, string value)
174-
{
175-
ThrowIfReadOnly();
176-
Store[key] = new[] { value };
177-
}
178-
179-
public void SetValues(string key, params string[] values)
180-
{
181-
ThrowIfReadOnly();
182-
Store[key] = values;
183-
}
184-
185-
public bool TryGetValue(string key, out string[] value)
142+
public bool TryGetValue(string key, out StringValues value)
186143
{
187144
return Store.TryGetValue(key, out value);
188145
}

src/Microsoft.Net.Http.Server/RequestProcessing/HeaderParser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.AspNet.Primitives;
67

78
namespace Microsoft.Net.Http.Server
89
{
@@ -11,7 +12,7 @@ internal static class HeaderParser
1112
internal static IEnumerable<string> Empty = new string[0];
1213

1314
// Split on commas, except in quotes
14-
internal static IEnumerable<string> SplitValues(string[] values)
15+
internal static IEnumerable<string> SplitValues(StringValues values)
1516
{
1617
foreach (var value in values)
1718
{

src/Microsoft.Net.Http.Server/RequestProcessing/Request.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,16 @@ public long? ContentLength
231231
{
232232
if (_contentBoundaryType == BoundaryType.None)
233233
{
234-
string transferEncoding = Headers.Get(HttpKnownHeaderNames.TransferEncoding) ?? string.Empty;
235-
if (string.Equals("chunked", transferEncoding.Trim(), StringComparison.OrdinalIgnoreCase))
234+
string transferEncoding = Headers[HttpKnownHeaderNames.TransferEncoding];
235+
if (string.Equals("chunked", transferEncoding?.Trim(), StringComparison.OrdinalIgnoreCase))
236236
{
237237
_contentBoundaryType = BoundaryType.Chunked;
238238
}
239239
else
240240
{
241-
string length = Headers.Get(HttpKnownHeaderNames.ContentLength) ?? string.Empty;
241+
string length = Headers[HttpKnownHeaderNames.ContentLength];
242242
long value;
243-
if (long.TryParse(length.Trim(), NumberStyles.None,
243+
if (length != null && long.TryParse(length.Trim(), NumberStyles.None,
244244
CultureInfo.InvariantCulture.NumberFormat, out value))
245245
{
246246
_contentBoundaryType = BoundaryType.ContentLength;
@@ -422,7 +422,7 @@ public string ContentType
422422
{
423423
get
424424
{
425-
return Headers.Get(HttpKnownHeaderNames.ContentType);
425+
return Headers[HttpKnownHeaderNames.ContentType];
426426
}
427427
}
428428

src/Microsoft.Net.Http.Server/RequestProcessing/RequestContext.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ public bool IsWebSocketRequest
194194
}
195195

196196
// Connection: Upgrade (some odd clients send Upgrade,KeepAlive)
197-
string connection = Request.Headers[HttpKnownHeaderNames.Connection] ?? string.Empty;
198-
if (connection.IndexOf(HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase) < 0)
197+
string connection = Request.Headers[HttpKnownHeaderNames.Connection];
198+
if (connection == null || connection.IndexOf(HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase) < 0)
199199
{
200200
return false;
201201
}
@@ -244,8 +244,8 @@ private void ValidateWebSocketRequest()
244244
}
245245

246246
// Connection: Upgrade (some odd clients send Upgrade,KeepAlive)
247-
string connection = Request.Headers[HttpKnownHeaderNames.Connection] ?? string.Empty;
248-
if (connection.IndexOf(HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase) < 0)
247+
string connection = Request.Headers[HttpKnownHeaderNames.Connection];
248+
if (connection == null || connection.IndexOf(HttpKnownHeaderNames.Upgrade, StringComparison.OrdinalIgnoreCase) < 0)
249249
{
250250
throw new InvalidOperationException("The Connection header is invalid: " + connection);
251251
}
@@ -345,9 +345,9 @@ private async Task<WebSocket> AcceptWebSocketAsyncCore(
345345
string secWebSocketKey = Request.Headers[HttpKnownHeaderNames.SecWebSocketKey];
346346
string secWebSocketAccept = WebSocketHelpers.GetSecWebSocketAcceptString(secWebSocketKey);
347347

348-
Response.Headers.AppendValues(HttpKnownHeaderNames.Connection, HttpKnownHeaderNames.Upgrade);
349-
Response.Headers.AppendValues(HttpKnownHeaderNames.Upgrade, WebSocketHelpers.WebSocketUpgradeToken);
350-
Response.Headers.AppendValues(HttpKnownHeaderNames.SecWebSocketAccept, secWebSocketAccept);
348+
Response.Headers.Append(HttpKnownHeaderNames.Connection, HttpKnownHeaderNames.Upgrade);
349+
Response.Headers.Append(HttpKnownHeaderNames.Upgrade, WebSocketHelpers.WebSocketUpgradeToken);
350+
Response.Headers.Append(HttpKnownHeaderNames.SecWebSocketAccept, secWebSocketAccept);
351351

352352
Stream opaqueStream = await UpgradeAsync();
353353

0 commit comments

Comments
 (0)