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

Commit 34fa7dc

Browse files
committed
Ignore null valued headers #158
1 parent f33ce78 commit 34fa7dc

File tree

2 files changed

+79
-20
lines changed

2 files changed

+79
-20
lines changed

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,6 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
575575
{
576576
if (headerPair.Value.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,11 +599,11 @@ 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
}
606+
608607
headerName = headerPair.Key;
609608
StringValues headerValues = headerPair.Value;
610609
lookup = HttpApi.HTTP_RESPONSE_HEADER_ID.IndexOfKnownHeader(headerName);
@@ -623,6 +622,13 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
623622

624623
for (int headerValueIndex = 0; headerValueIndex < headerValues.Count; headerValueIndex++)
625624
{
625+
headerValue = headerValues[headerValueIndex];
626+
627+
if (headerValue == null)
628+
{
629+
continue;
630+
}
631+
626632
// Add Name
627633
bytes = new byte[HeaderEncoding.GetByteCount(headerName)];
628634
unknownHeaders[_nativeResponse.Response_V1.Headers.UnknownHeaderCount].NameLength = (ushort)bytes.Length;
@@ -632,7 +638,6 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
632638
unknownHeaders[_nativeResponse.Response_V1.Headers.UnknownHeaderCount].pName = (sbyte*)gcHandle.AddrOfPinnedObject();
633639

634640
// Add Value
635-
headerValue = headerValues[headerValueIndex];
636641
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
637642
unknownHeaders[_nativeResponse.Response_V1.Headers.UnknownHeaderCount].RawValueLength = (ushort)bytes.Length;
638643
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
@@ -642,20 +647,17 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
642647
_nativeResponse.Response_V1.Headers.UnknownHeaderCount++;
643648
}
644649
}
645-
else if (headerPair.Value.Count == 1)
650+
else if (headerPair.Value.Where(s => !string.IsNullOrEmpty(s)).Count() == 1)
646651
{
647652
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-
}
653+
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
654+
pKnownHeaders[lookup].RawValueLength = (ushort)bytes.Length;
655+
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
656+
gcHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
657+
pinnedHeaders.Add(gcHandle);
658+
pKnownHeaders[lookup].pRawValue = (sbyte*)gcHandle.AddrOfPinnedObject();
657659
}
658-
else
660+
else if(headerPair.Value.Where(s => !string.IsNullOrEmpty(s)).Count() > 1)
659661
{
660662
if (knownHeaderInfo == null)
661663
{
@@ -673,15 +675,21 @@ private List<GCHandle> SerializeHeaders(bool isOpaqueUpgrade)
673675
header.HeaderId = (HttpApi.HTTP_RESPONSE_HEADER_ID.Enum)lookup;
674676
header.Flags = HttpApi.HTTP_RESPONSE_INFO_FLAGS.PreserveOrder; // TODO: The docs say this is for www-auth only.
675677

676-
HttpApi.HTTP_KNOWN_HEADER[] nativeHeaderValues = new HttpApi.HTTP_KNOWN_HEADER[headerValues.Count];
678+
HttpApi.HTTP_KNOWN_HEADER[] nativeHeaderValues = new HttpApi.HTTP_KNOWN_HEADER[headerValues.Where(s => s != null).Count()];
677679
gcHandle = GCHandle.Alloc(nativeHeaderValues, GCHandleType.Pinned);
678680
pinnedHeaders.Add(gcHandle);
679681
header.KnownHeaders = (HttpApi.HTTP_KNOWN_HEADER*)gcHandle.AddrOfPinnedObject();
680682

681683
for (int headerValueIndex = 0; headerValueIndex < headerValues.Count; headerValueIndex++)
682684
{
683-
// Add Value
684685
headerValue = headerValues[headerValueIndex];
686+
687+
if (string.IsNullOrEmpty(headerValue))
688+
{
689+
continue;
690+
}
691+
692+
// Add Valuegit
685693
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
686694
nativeHeaderValues[header.KnownHeaderCount].RawValueLength = (ushort)bytes.Length;
687695
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);

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

Lines changed: 52 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,57 @@ public async Task Headers_FlushAsyncSendsHeaders_Success()
259259
}
260260
}
261261

262+
[Theory, MemberData(nameof(NullHeaderData))]
263+
public async Task Headers_IgnoreNullHeaders(string headerName, StringValues headerValue, string 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 == null)
278+
{
279+
Assert.False(headers.Contains(headerName));
280+
}
281+
else
282+
{
283+
Assert.True(headers.Contains(headerName));
284+
Assert.Equal(headers.GetValues(headerName).Single(), expectedValue);
285+
}
286+
}
287+
}
288+
289+
public static TheoryData<string, StringValues, string> NullHeaderData
290+
{
291+
get
292+
{
293+
var dataset = new TheoryData<string, StringValues, string>();
294+
295+
// Unknown headers
296+
dataset.Add("NullString", (string)null, null);
297+
dataset.Add("EmptyString", "", "");
298+
dataset.Add("NullStringArray", new string[] { null }, null);
299+
dataset.Add("EmptyStringArray", new string[] { "" }, "");
300+
dataset.Add("MixedStringArray", new string[] { null, "" }, "");
301+
// Known headers
302+
dataset.Add("Location", (string)null, null);
303+
dataset.Add("Location", "", null);
304+
dataset.Add("Location", new string[] { null }, null);
305+
dataset.Add("Location", new string[] { "" }, null);
306+
dataset.Add("Location", new string[] { null, "" }, null);
307+
dataset.Add("Location", new string[] { null, "", "" }, null);
308+
309+
return dataset;
310+
}
311+
}
312+
262313
private async Task<HttpResponseMessage> SendRequestAsync(string uri)
263314
{
264315
using (HttpClient client = new HttpClient())

0 commit comments

Comments
 (0)