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

Commit 6ad21c2

Browse files
committed
Ignore null valued headers #158
1 parent f33ce78 commit 6ad21c2

File tree

2 files changed

+80
-21
lines changed

2 files changed

+80
-21
lines changed

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,8 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
573573
int numKnownMultiHeaders = 0;
574574
foreach (var headerPair in Headers)
575575
{
576-
if (headerPair.Value.Count == 0)
576+
if (headerPair.Value.Where(s => s != null).Count() == 0)
577577
{
578-
// TODO: Have the collection exclude empty headers.
579578
continue;
580579
}
581580
// See if this is an unknown header
@@ -585,9 +584,9 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
585584
if (lookup == -1 ||
586585
(isOpaqueUpgrade && lookup == (int)HttpApi.HTTP_RESPONSE_HEADER_ID.Enum.HttpHeaderConnection))
587586
{
588-
numUnknownHeaders += headerPair.Value.Count;
587+
numUnknownHeaders += headerPair.Value.Where(s => s != null).Count();
589588
}
590-
else if (headerPair.Value.Count > 1)
589+
else if (headerPair.Value.Where(s => !string.IsNullOrEmpty(s)).Count() > 1)
591590
{
592591
numKnownMultiHeaders++;
593592
}
@@ -600,9 +599,8 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
600599
{
601600
foreach (var headerPair in Headers)
602601
{
603-
if (headerPair.Value.Count == 0)
602+
if (headerPair.Value.Where(s => s != null).Count() == 0)
604603
{
605-
// TODO: Have the collection exclude empty headers.
606604
continue;
607605
}
608606
headerName = headerPair.Key;
@@ -623,6 +621,13 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
623621

624622
for (int headerValueIndex = 0; headerValueIndex < headerValues.Count; headerValueIndex++)
625623
{
624+
headerValue = headerValues[headerValueIndex];
625+
626+
if (headerValue == null)
627+
{
628+
continue;
629+
}
630+
626631
// Add Name
627632
bytes = new byte[HeaderEncoding.GetByteCount(headerName)];
628633
unknownHeaders[_nativeResponse.Response_V1.Headers.UnknownHeaderCount].NameLength = (ushort)bytes.Length;
@@ -632,7 +637,6 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
632637
unknownHeaders[_nativeResponse.Response_V1.Headers.UnknownHeaderCount].pName = (sbyte*)gcHandle.AddrOfPinnedObject();
633638

634639
// Add Value
635-
headerValue = headerValues[headerValueIndex];
636640
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
637641
unknownHeaders[_nativeResponse.Response_V1.Headers.UnknownHeaderCount].RawValueLength = (ushort)bytes.Length;
638642
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
@@ -642,20 +646,17 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
642646
_nativeResponse.Response_V1.Headers.UnknownHeaderCount++;
643647
}
644648
}
645-
else if (headerPair.Value.Count == 1)
649+
else if (headerPair.Value.Where(s => !string.IsNullOrEmpty(s)).Count() == 1)
646650
{
647651
headerValue = headerValues[0];
648-
if (headerValue != null)
649-
{
650-
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
651-
pKnownHeaders[lookup].RawValueLength = (ushort)bytes.Length;
652-
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
653-
gcHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
654-
pinnedHeaders.Add(gcHandle);
655-
pKnownHeaders[lookup].pRawValue = (sbyte*)gcHandle.AddrOfPinnedObject();
656-
}
652+
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
653+
pKnownHeaders[lookup].RawValueLength = (ushort)bytes.Length;
654+
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
655+
gcHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
656+
pinnedHeaders.Add(gcHandle);
657+
pKnownHeaders[lookup].pRawValue = (sbyte*)gcHandle.AddrOfPinnedObject();
657658
}
658-
else
659+
else if(headerPair.Value.Where(s => !string.IsNullOrEmpty(s)).Count() > 1)
659660
{
660661
if (knownHeaderInfo == null)
661662
{
@@ -673,15 +674,21 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
673674
header.HeaderId = (HttpApi.HTTP_RESPONSE_HEADER_ID.Enum)lookup;
674675
header.Flags = HttpApi.HTTP_RESPONSE_INFO_FLAGS.PreserveOrder; // TODO: The docs say this is for www-auth only.
675676

676-
HttpApi.HTTP_KNOWN_HEADER[] nativeHeaderValues = new HttpApi.HTTP_KNOWN_HEADER[headerValues.Count];
677+
HttpApi.HTTP_KNOWN_HEADER[] nativeHeaderValues = new HttpApi.HTTP_KNOWN_HEADER[headerValues.Where(s => !string.IsNullOrEmpty(s)).Count()];
677678
gcHandle = GCHandle.Alloc(nativeHeaderValues, GCHandleType.Pinned);
678679
pinnedHeaders.Add(gcHandle);
679680
header.KnownHeaders = (HttpApi.HTTP_KNOWN_HEADER*)gcHandle.AddrOfPinnedObject();
680681

681682
for (int headerValueIndex = 0; headerValueIndex < headerValues.Count; headerValueIndex++)
682683
{
683-
// Add Value
684684
headerValue = headerValues[headerValueIndex];
685+
686+
if (string.IsNullOrEmpty(headerValue))
687+
{
688+
continue;
689+
}
690+
691+
// Add Valuegit
685692
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
686693
nativeHeaderValues[header.KnownHeaderCount].RawValueLength = (ushort)bytes.Length;
687694
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);

test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseHeaderTests.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
using System.Text;
2424
using System.Threading.Tasks;
2525
using Microsoft.AspNet.Http.Features;
26-
using Microsoft.AspNet.Http.Internal;
26+
using Microsoft.Extensions.Primitives;
2727
using Xunit;
2828

2929
namespace Microsoft.AspNet.Server.WebListener
@@ -259,6 +259,58 @@ public async Task Headers_FlushAsyncSendsHeaders_Success()
259259
}
260260
}
261261

262+
[Theory, MemberData(nameof(NullHeaderData))]
263+
public async Task Headers_IgnoreNullHeaders(string headerName, StringValues headerValue, StringValues expectedValue)
264+
{
265+
string address;
266+
using (Utilities.CreateHttpServer(out address, httpContext =>
267+
{
268+
var responseHeaders = httpContext.Response.Headers;
269+
responseHeaders.Add(headerName, headerValue);
270+
return Task.FromResult(0);
271+
}))
272+
{
273+
HttpResponseMessage response = await SendRequestAsync(address);
274+
response.EnsureSuccessStatusCode();
275+
var headers = response.Headers;
276+
277+
if (expectedValue.Count == 0)
278+
{
279+
Assert.False(headers.Contains(headerName));
280+
}
281+
else
282+
{
283+
Assert.True(headers.Contains(headerName));
284+
Assert.Equal(headers.GetValues(headerName), expectedValue);
285+
}
286+
}
287+
}
288+
289+
public static TheoryData<string, StringValues, StringValues> NullHeaderData
290+
{
291+
get
292+
{
293+
var dataset = new TheoryData<string, StringValues, StringValues>();
294+
295+
// Unknown headers
296+
dataset.Add("NullString", (string)null, (string)null);
297+
dataset.Add("EmptyString", "", "");
298+
dataset.Add("NullStringArray", new string[] { null }, (string)null);
299+
dataset.Add("EmptyStringArray", new string[] { "" }, "");
300+
dataset.Add("MixedStringArray", new string[] { null, "" }, "");
301+
// Known headers
302+
dataset.Add("Location", (string)null, (string)null);
303+
dataset.Add("Location", "", (string)null);
304+
dataset.Add("Location", new string[] { null }, (string)null);
305+
dataset.Add("Location", new string[] { "" }, (string)null);
306+
dataset.Add("Location", new string[] { "a" }, "a");
307+
dataset.Add("Location", new string[] { null, "" }, (string)null);
308+
dataset.Add("Location", new string[] { null, "", "a", "b" }, new string[] { "a", "b" });
309+
310+
return dataset;
311+
}
312+
}
313+
262314
private async Task<HttpResponseMessage> SendRequestAsync(string uri)
263315
{
264316
using (HttpClient client = new HttpClient())

0 commit comments

Comments
 (0)