Handle null valued headers #158
This commit is contained in:
parent
94dd583a15
commit
3b42433f07
|
|
@ -575,7 +575,6 @@ namespace Microsoft.Net.Http.Server
|
||||||
{
|
{
|
||||||
if (headerPair.Value.Count == 0)
|
if (headerPair.Value.Count == 0)
|
||||||
{
|
{
|
||||||
// TODO: Have the collection exclude empty headers.
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// See if this is an unknown header
|
// See if this is an unknown header
|
||||||
|
|
@ -602,7 +601,6 @@ namespace Microsoft.Net.Http.Server
|
||||||
{
|
{
|
||||||
if (headerPair.Value.Count == 0)
|
if (headerPair.Value.Count == 0)
|
||||||
{
|
{
|
||||||
// TODO: Have the collection exclude empty headers.
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
headerName = headerPair.Key;
|
headerName = headerPair.Key;
|
||||||
|
|
@ -632,7 +630,7 @@ namespace Microsoft.Net.Http.Server
|
||||||
unknownHeaders[_nativeResponse.Response_V1.Headers.UnknownHeaderCount].pName = (sbyte*)gcHandle.AddrOfPinnedObject();
|
unknownHeaders[_nativeResponse.Response_V1.Headers.UnknownHeaderCount].pName = (sbyte*)gcHandle.AddrOfPinnedObject();
|
||||||
|
|
||||||
// Add Value
|
// Add Value
|
||||||
headerValue = headerValues[headerValueIndex];
|
headerValue = headerValues[headerValueIndex] ?? string.Empty;
|
||||||
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
|
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
|
||||||
unknownHeaders[_nativeResponse.Response_V1.Headers.UnknownHeaderCount].RawValueLength = (ushort)bytes.Length;
|
unknownHeaders[_nativeResponse.Response_V1.Headers.UnknownHeaderCount].RawValueLength = (ushort)bytes.Length;
|
||||||
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
|
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
|
||||||
|
|
@ -644,16 +642,13 @@ namespace Microsoft.Net.Http.Server
|
||||||
}
|
}
|
||||||
else if (headerPair.Value.Count == 1)
|
else if (headerPair.Value.Count == 1)
|
||||||
{
|
{
|
||||||
headerValue = headerValues[0];
|
headerValue = headerValues[0] ?? string.Empty;
|
||||||
if (headerValue != null)
|
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
|
||||||
{
|
pKnownHeaders[lookup].RawValueLength = (ushort)bytes.Length;
|
||||||
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
|
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
|
||||||
pKnownHeaders[lookup].RawValueLength = (ushort)bytes.Length;
|
gcHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
|
||||||
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
|
pinnedHeaders.Add(gcHandle);
|
||||||
gcHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
|
pKnownHeaders[lookup].pRawValue = (sbyte*)gcHandle.AddrOfPinnedObject();
|
||||||
pinnedHeaders.Add(gcHandle);
|
|
||||||
pKnownHeaders[lookup].pRawValue = (sbyte*)gcHandle.AddrOfPinnedObject();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -681,7 +676,7 @@ namespace Microsoft.Net.Http.Server
|
||||||
for (int headerValueIndex = 0; headerValueIndex < headerValues.Count; headerValueIndex++)
|
for (int headerValueIndex = 0; headerValueIndex < headerValues.Count; headerValueIndex++)
|
||||||
{
|
{
|
||||||
// Add Value
|
// Add Value
|
||||||
headerValue = headerValues[headerValueIndex];
|
headerValue = headerValues[headerValueIndex] ?? string.Empty;
|
||||||
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
|
bytes = new byte[HeaderEncoding.GetByteCount(headerValue)];
|
||||||
nativeHeaderValues[header.KnownHeaderCount].RawValueLength = (ushort)bytes.Length;
|
nativeHeaderValues[header.KnownHeaderCount].RawValueLength = (ushort)bytes.Length;
|
||||||
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
|
HeaderEncoding.GetBytes(headerValue, 0, bytes.Length, bytes, 0);
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Http.Features;
|
using Microsoft.AspNet.Http.Features;
|
||||||
using Microsoft.AspNet.Http.Internal;
|
using Microsoft.Extensions.Primitives;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Server.WebListener
|
namespace Microsoft.AspNet.Server.WebListener
|
||||||
|
|
@ -259,6 +259,58 @@ namespace Microsoft.AspNet.Server.WebListener
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory, MemberData(nameof(NullHeaderData))]
|
||||||
|
public async Task Headers_IgnoreNullHeaders(string headerName, StringValues headerValue, StringValues expectedValue)
|
||||||
|
{
|
||||||
|
string address;
|
||||||
|
using (Utilities.CreateHttpServer(out address, httpContext =>
|
||||||
|
{
|
||||||
|
var responseHeaders = httpContext.Response.Headers;
|
||||||
|
responseHeaders.Add(headerName, headerValue);
|
||||||
|
return Task.FromResult(0);
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
HttpResponseMessage response = await SendRequestAsync(address);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
var headers = response.Headers;
|
||||||
|
|
||||||
|
if (expectedValue.Count == 0)
|
||||||
|
{
|
||||||
|
Assert.False(headers.Contains(headerName));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Assert.True(headers.Contains(headerName));
|
||||||
|
Assert.Equal(headers.GetValues(headerName), expectedValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TheoryData<string, StringValues, StringValues> NullHeaderData
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var dataset = new TheoryData<string, StringValues, StringValues>();
|
||||||
|
|
||||||
|
// Unknown headers
|
||||||
|
dataset.Add("NullString", (string)null, (string)null);
|
||||||
|
dataset.Add("EmptyString", "", "");
|
||||||
|
dataset.Add("NullStringArray", new string[] { null }, "");
|
||||||
|
dataset.Add("EmptyStringArray", new string[] { "" }, "");
|
||||||
|
dataset.Add("MixedStringArray", new string[] { null, "" }, new string[] { "", "" });
|
||||||
|
// Known headers
|
||||||
|
dataset.Add("Location", (string)null, (string)null);
|
||||||
|
dataset.Add("Location", "", (string)null);
|
||||||
|
dataset.Add("Location", new string[] { null }, (string)null);
|
||||||
|
dataset.Add("Location", new string[] { "" }, (string)null);
|
||||||
|
dataset.Add("Location", new string[] { "a" }, "a");
|
||||||
|
dataset.Add("Location", new string[] { null, "" }, (string)null);
|
||||||
|
dataset.Add("Location", new string[] { null, "", "a", "b" }, new string[] { "a", "b" });
|
||||||
|
|
||||||
|
return dataset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<HttpResponseMessage> SendRequestAsync(string uri)
|
private async Task<HttpResponseMessage> SendRequestAsync(string uri)
|
||||||
{
|
{
|
||||||
using (HttpClient client = new HttpClient())
|
using (HttpClient client = new HttpClient())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue