Skip to content

Commit dcd1250

Browse files
jkotalikTratcher
andauthored
[3.1] Add latin1 support to IIS (#22798)
Co-authored-by: Chris Ross <[email protected]>
1 parent 0ec79c5 commit dcd1250

File tree

18 files changed

+220
-92
lines changed

18 files changed

+220
-92
lines changed

src/Servers/HttpSys/src/Microsoft.AspNetCore.Server.HttpSys.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<ItemGroup>
1515
<Compile Include="$(SharedSourceRoot)HttpSys\**\*.cs" />
16+
<Compile Include="$(SharedSourceRoot)ServerInfrastructure\*.cs" LinkBase="ServerInfrastructure" />
1617
</ItemGroup>
1718

1819
<ItemGroup>

src/Servers/IIS/IIS/src/Core/IISHttpContext.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ internal unsafe IISHttpContext(
7474
IntPtr pInProcessHandler,
7575
IISServerOptions options,
7676
IISHttpServer server,
77-
ILogger logger)
78-
: base((HttpApiTypes.HTTP_REQUEST*)NativeMethods.HttpGetRawRequest(pInProcessHandler))
77+
ILogger logger,
78+
bool useLatin1)
79+
: base((HttpApiTypes.HTTP_REQUEST*)NativeMethods.HttpGetRawRequest(pInProcessHandler), useLatin1: useLatin1)
7980
{
8081
_memoryPool = memoryPool;
8182
_pInProcessHandler = pInProcessHandler;

src/Servers/IIS/IIS/src/Core/IISHttpContextOfT.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Buffers;
6+
using System.Text;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.AspNetCore.Builder;
@@ -15,8 +16,8 @@ internal class IISHttpContextOfT<TContext> : IISHttpContext
1516
{
1617
private readonly IHttpApplication<TContext> _application;
1718

18-
public IISHttpContextOfT(MemoryPool<byte> memoryPool, IHttpApplication<TContext> application, IntPtr pInProcessHandler, IISServerOptions options, IISHttpServer server, ILogger logger)
19-
: base(memoryPool, pInProcessHandler, options, server, logger)
19+
public IISHttpContextOfT(MemoryPool<byte> memoryPool, IHttpApplication<TContext> application, IntPtr pInProcessHandler, IISServerOptions options, IISHttpServer server, ILogger logger, bool useLatin1)
20+
: base(memoryPool, pInProcessHandler, options, server, logger, useLatin1)
2021
{
2122
_application = application;
2223
}

src/Servers/IIS/IIS/src/Core/IISHttpServer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,14 @@ private static void OnRequestsDrained(IntPtr serverContext)
214214

215215
private class IISContextFactory<T> : IISContextFactory
216216
{
217+
private const string Latin1Suppport = "Microsoft.AspNetCore.Server.IIS.Latin1RequestHeaders";
218+
217219
private readonly IHttpApplication<T> _application;
218220
private readonly MemoryPool<byte> _memoryPool;
219221
private readonly IISServerOptions _options;
220222
private readonly IISHttpServer _server;
221223
private readonly ILogger _logger;
224+
private readonly bool _useLatin1;
222225

223226
public IISContextFactory(MemoryPool<byte> memoryPool, IHttpApplication<T> application, IISServerOptions options, IISHttpServer server, ILogger logger)
224227
{
@@ -227,11 +230,12 @@ public IISContextFactory(MemoryPool<byte> memoryPool, IHttpApplication<T> applic
227230
_options = options;
228231
_server = server;
229232
_logger = logger;
233+
AppContext.TryGetSwitch(Latin1Suppport, out _useLatin1);
230234
}
231235

232236
public IISHttpContext CreateHttpContext(IntPtr pInProcessHandler)
233237
{
234-
return new IISHttpContextOfT<T>(_memoryPool, _application, pInProcessHandler, _options, _server, _logger);
238+
return new IISHttpContextOfT<T>(_memoryPool, _application, pInProcessHandler, _options, _server, _logger, _useLatin1);
235239
}
236240
}
237241
}

src/Servers/IIS/IIS/src/Microsoft.AspNetCore.Server.IIS.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<Compile Include="$(SharedSourceRoot)StackTrace\**\*.cs" LinkBase="Shared\" />
2020
<Compile Include="$(SharedSourceRoot)RazorViews\*.cs" LinkBase="Shared\" />
2121
<Compile Include="$(SharedSourceRoot)ErrorPage\*.cs" LinkBase="Shared\" />
22+
<Compile Include="$(SharedSourceRoot)ServerInfrastructure\*.cs" LinkBase="Shared\" />
2223
</ItemGroup>
2324

2425
<Target Name="ValidateNativeComponentsBuilt" AfterTargets="Build" >
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Server.IIS.FunctionalTests.Utilities;
9+
using Microsoft.AspNetCore.Server.IntegrationTesting;
10+
using Microsoft.AspNetCore.Server.IntegrationTesting.IIS;
11+
using Microsoft.AspNetCore.Testing;
12+
using Xunit;
13+
14+
namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests.InProcess
15+
{
16+
[Collection(PublishedSitesCollection.Name)]
17+
public class Latin1Tests : IISFunctionalTestBase
18+
{
19+
public Latin1Tests(PublishedSitesFixture fixture) : base(fixture)
20+
{
21+
}
22+
23+
[ConditionalFact]
24+
[RequiresNewHandler]
25+
public async Task Latin1Works()
26+
{
27+
var deploymentParameters = Fixture.GetBaseDeploymentParameters();
28+
deploymentParameters.TransformArguments((a, _) => $"{a} AddLatin1");
29+
30+
var deploymentResult = await DeployAsync(deploymentParameters);
31+
32+
var client = new HttpClient(new LoggingHandler(new WinHttpHandler() { SendTimeout = TimeSpan.FromMinutes(3) }, deploymentResult.Logger));
33+
34+
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{deploymentResult.ApplicationBaseUri}Latin1");
35+
requestMessage.Headers.Add("foo", "£");
36+
37+
var result = await client.SendAsync(requestMessage);
38+
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
39+
}
40+
41+
[ConditionalFact]
42+
[RequiresNewHandler]
43+
public async Task Latin1ReplacedWithoutAppContextSwitch()
44+
{
45+
var deploymentParameters = Fixture.GetBaseDeploymentParameters();
46+
deploymentParameters.TransformArguments((a, _) => $"{a}");
47+
48+
var deploymentResult = await DeployAsync(deploymentParameters);
49+
50+
var client = new HttpClient(new LoggingHandler(new WinHttpHandler() { SendTimeout = TimeSpan.FromMinutes(3) }, deploymentResult.Logger));
51+
52+
var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"{deploymentResult.ApplicationBaseUri}InvalidCharacter");
53+
requestMessage.Headers.Add("foo", "£");
54+
55+
var result = await client.SendAsync(requestMessage);
56+
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
57+
}
58+
59+
[ConditionalFact]
60+
[RequiresNewHandler]
61+
public async Task Latin1InvalidCharacters_HttpSysRejects()
62+
{
63+
var deploymentParameters = Fixture.GetBaseDeploymentParameters();
64+
deploymentParameters.TransformArguments((a, _) => $"{a} AddLatin1");
65+
66+
var deploymentResult = await DeployAsync(deploymentParameters);
67+
68+
using (var connection = new TestConnection(deploymentResult.HttpClient.BaseAddress.Port))
69+
{
70+
await connection.Send(
71+
"GET /ReadAndFlushEcho HTTP/1.1",
72+
"Host: localhost",
73+
"Connection: close",
74+
"foo: £\0a",
75+
"",
76+
"");
77+
78+
await connection.ReceiveStartsWith("HTTP/1.1 400 Bad Request");
79+
}
80+
}
81+
}
82+
}

src/Servers/IIS/IIS/test/IIS.FunctionalTests/IIS.FunctionalTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33

44
<PropertyGroup>
@@ -27,6 +27,7 @@
2727
<Reference Include="Microsoft.Extensions.Logging.Testing" />
2828
<Reference Include="Microsoft.Extensions.Logging" />
2929
<Reference Include="System.Diagnostics.EventLog" />
30+
<Reference Include="System.Net.Http.WinHttpHandler" />
3031
</ItemGroup>
3132

3233
</Project>

src/Servers/IIS/IIS/test/IIS.NewHandler.FunctionalTests/IIS.NewHandler.FunctionalTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
@@ -34,6 +34,7 @@
3434
<Reference Include="Microsoft.Extensions.Logging" />
3535
<Reference Include="System.Diagnostics.EventLog" />
3636
<Reference Include="System.Net.WebSockets.WebSocketProtocol" />
37+
<Reference Include="System.Net.Http.WinHttpHandler" />
3738
</ItemGroup>
3839

3940
</Project>

src/Servers/IIS/IIS/test/IIS.NewShim.FunctionalTests/IIS.NewShim.FunctionalTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
@@ -28,6 +28,7 @@
2828
<Reference Include="Microsoft.Extensions.Logging" />
2929
<Reference Include="Microsoft.Extensions.Logging.Testing" />
3030
<Reference Include="System.Diagnostics.EventLog" />
31+
<Reference Include="System.Net.Http.WinHttpHandler" />
3132
</ItemGroup>
3233

3334
</Project>

src/Servers/IIS/IIS/test/IISExpress.FunctionalTests/IISExpress.FunctionalTests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<Import Project="../FunctionalTest.props" />
44

@@ -30,6 +30,7 @@
3030
<Reference Include="Microsoft.Extensions.Logging" />
3131
<Reference Include="Microsoft.Extensions.Logging.Testing" />
3232
<Reference Include="System.Diagnostics.EventLog" />
33+
<Reference Include="System.Net.Http.WinHttpHandler" />
3334
</ItemGroup>
3435

3536
</Project>

0 commit comments

Comments
 (0)