From d28468ca8f3327ae747b3377559ccfc4922d7baf Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 20 Sep 2018 09:21:34 -0700 Subject: [PATCH] Override IIS headers in inproc (#1409) --- .../Core/IISHttpContext.cs | 26 +++++++------------ .../Inprocess/ResponseHeaderTests.cs | 7 +++++ .../shared/SharedStartup/Startup.shared.cs | 6 +++++ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.IIS/Core/IISHttpContext.cs b/src/Microsoft.AspNetCore.Server.IIS/Core/IISHttpContext.cs index a9eecba00b..45848183ba 100644 --- a/src/Microsoft.AspNetCore.Server.IIS/Core/IISHttpContext.cs +++ b/src/Microsoft.AspNetCore.Server.IIS/Core/IISHttpContext.cs @@ -318,29 +318,23 @@ namespace Microsoft.AspNetCore.Server.IIS.Core { var headerValues = headerPair.Value; var knownHeaderIndex = HttpApiTypes.HTTP_RESPONSE_HEADER_ID.IndexOfKnownHeader(headerPair.Key); - if (knownHeaderIndex == -1) + for (var i = 0; i < headerValues.Count; i++) { - var headerNameBytes = Encoding.UTF8.GetBytes(headerPair.Key); - for (var i = 0; i < headerValues.Count; i++) + var isFirst = i == 0; + var headerValueBytes = Encoding.UTF8.GetBytes(headerValues[i]); + fixed (byte* pHeaderValue = headerValueBytes) { - var headerValueBytes = Encoding.UTF8.GetBytes(headerValues[i]); - fixed (byte* pHeaderName = headerNameBytes) + if (knownHeaderIndex == -1) { - fixed (byte* pHeaderValue = headerValueBytes) + var headerNameBytes = Encoding.UTF8.GetBytes(headerPair.Key); + fixed (byte* pHeaderName = headerNameBytes) { - NativeMethods.HttpResponseSetUnknownHeader(_pInProcessHandler, pHeaderName, pHeaderValue, (ushort)headerValueBytes.Length, fReplace: false); + NativeMethods.HttpResponseSetUnknownHeader(_pInProcessHandler, pHeaderName, pHeaderValue, (ushort)headerValueBytes.Length, fReplace: isFirst); } } - } - } - else - { - for (var i = 0; i < headerValues.Count; i++) - { - var headerValueBytes = Encoding.UTF8.GetBytes(headerValues[i]); - fixed (byte* pHeaderValue = headerValueBytes) + else { - NativeMethods.HttpResponseSetKnownHeader(_pInProcessHandler, knownHeaderIndex, pHeaderValue, (ushort)headerValueBytes.Length, fReplace: false); + NativeMethods.HttpResponseSetKnownHeader(_pInProcessHandler, knownHeaderIndex, pHeaderValue, (ushort)headerValueBytes.Length, fReplace: isFirst); } } } diff --git a/test/Common.FunctionalTests/Inprocess/ResponseHeaderTests.cs b/test/Common.FunctionalTests/Inprocess/ResponseHeaderTests.cs index 3f433b155f..fec5c227ec 100644 --- a/test/Common.FunctionalTests/Inprocess/ResponseHeaderTests.cs +++ b/test/Common.FunctionalTests/Inprocess/ResponseHeaderTests.cs @@ -74,5 +74,12 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests // ReadAsStringAsync returns empty string for empty results Assert.Equal(body ?? string.Empty, await response.Content.ReadAsStringAsync()); } + + [ConditionalFact] + public async Task ServerHeaderIsOverriden() + { + var response = await _fixture.Client.GetAsync("OverrideServer"); + Assert.Equal("MyServer/7.8", response.Headers.Server.Single().Product.ToString()); + } } } diff --git a/test/WebSites/shared/SharedStartup/Startup.shared.cs b/test/WebSites/shared/SharedStartup/Startup.shared.cs index bf9e3bb7fd..375902faea 100644 --- a/test/WebSites/shared/SharedStartup/Startup.shared.cs +++ b/test/WebSites/shared/SharedStartup/Startup.shared.cs @@ -82,5 +82,11 @@ namespace TestSite } await context.Response.WriteAsync(_applicationInitializationCalled.ToString()); } + + public Task OverrideServer(HttpContext context) + { + context.Response.Headers["Server"] = "MyServer/7.8"; + return Task.CompletedTask; + } } }