#381 handle known headers with no value.

This commit is contained in:
Chris Ross (ASP.NET) 2017-10-11 15:31:00 -07:00
Родитель 6ee0186c36
Коммит 7f96eab775
2 изменённых файлов: 52 добавлений и 1 удалений

Просмотреть файл

@ -259,7 +259,7 @@ namespace Microsoft.AspNetCore.HttpSys.Internal
HttpApiTypes.HTTP_KNOWN_HEADER* pKnownHeader = (&request->Headers.KnownHeaders) + headerIndex;
// For known headers, when header value is empty, RawValueLength will be 0 and
// pRawValue will point to empty string ("\0")
if (pKnownHeader->pRawValue != null)
if (pKnownHeader->RawValueLength > 0)
{
value = HeaderEncoding.GetString(pKnownHeader->pRawValue + fixup, pKnownHeader->RawValueLength);
}

Просмотреть файл

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
@ -93,6 +94,56 @@ namespace Microsoft.AspNetCore.Server.HttpSys.Listener
}
}
[ConditionalFact]
public async Task RequestHeaders_ClientSendsKnownHeaderWithNoValue_Success()
{
string address;
using (var server = Utilities.CreateHttpServer(out address))
{
string[] customValues = new string[] { "" };
Task responseTask = SendRequestAsync(address, "If-None-Match", customValues);
var context = await server.AcceptAsync(Utilities.DefaultTimeout);
var requestHeaders = context.Request.Headers;
Assert.Equal(3, requestHeaders.Count);
Assert.Equal(new Uri(address).Authority, requestHeaders["Host"]);
Assert.Equal(new[] { new Uri(address).Authority }, requestHeaders.GetValues("Host"));
Assert.Equal("close", requestHeaders["Connection"]);
Assert.Equal(new[] { "close" }, requestHeaders.GetValues("Connection"));
Assert.Equal(StringValues.Empty, requestHeaders["If-None-Match"]);
Assert.Empty(requestHeaders.GetValues("If-None-Match"));
Assert.Equal("spacervalue", requestHeaders["Spacer-Header"]);
context.Dispose();
await responseTask;
}
}
[ConditionalFact]
public async Task RequestHeaders_ClientSendsUnknownHeaderWithNoValue_Success()
{
string address;
using (var server = Utilities.CreateHttpServer(out address))
{
string[] customValues = new string[] { "" };
Task responseTask = SendRequestAsync(address, "Custom-Header", customValues);
var context = await server.AcceptAsync(Utilities.DefaultTimeout);
var requestHeaders = context.Request.Headers;
Assert.Equal(4, requestHeaders.Count);
Assert.Equal(new Uri(address).Authority, requestHeaders["Host"]);
Assert.Equal(new[] { new Uri(address).Authority }, requestHeaders.GetValues("Host"));
Assert.Equal("close", requestHeaders["Connection"]);
Assert.Equal(new[] { "close" }, requestHeaders.GetValues("Connection"));
Assert.Equal("", requestHeaders["Custom-Header"]);
Assert.Empty(requestHeaders.GetValues("Custom-Header"));
Assert.Equal("spacervalue", requestHeaders["Spacer-Header"]);
context.Dispose();
await responseTask;
}
}
private async Task<string> SendRequestAsync(string uri)
{
using (HttpClient client = new HttpClient())