Skip to content

Commit d7fc06a

Browse files
authored
Move IServerVariablesFeature to Http.Features (#11007)
1 parent 5561338 commit d7fc06a

16 files changed

+104
-46
lines changed

src/Http/Http.Extensions/ref/Microsoft.AspNetCore.Http.Extensions.netcoreapp3.0.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public static void AppendList<T>(this Microsoft.AspNetCore.Http.IHeaderDictionar
99
public static Microsoft.AspNetCore.Http.Headers.RequestHeaders GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpRequest request) { throw null; }
1010
public static Microsoft.AspNetCore.Http.Headers.ResponseHeaders GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpResponse response) { throw null; }
1111
}
12+
public static partial class HttpContextServerVariableExtensions
13+
{
14+
public static string GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext context, string variableName) { throw null; }
15+
}
1216
public static partial class ResponseExtensions
1317
{
1418
public static void Clear(this Microsoft.AspNetCore.Http.HttpResponse response) { }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 Microsoft.AspNetCore.Http.Features;
5+
6+
namespace Microsoft.AspNetCore.Http
7+
{
8+
public static class HttpContextServerVariableExtensions
9+
{
10+
/// <summary>
11+
/// Gets the value of a server variable for the current request.
12+
/// </summary>
13+
/// <param name="context">The http context for the request.</param>
14+
/// <param name="variableName">The name of the variable.</param>
15+
/// <returns>
16+
/// <c>null</c> if the server does not support the <see cref="IServerVariablesFeature"/> feature.
17+
/// May return null or empty if the variable does not exist or is not set.
18+
/// </returns>
19+
public static string GetServerVariable(this HttpContext context, string variableName)
20+
{
21+
var feature = context.Features.Get<IServerVariablesFeature>();
22+
23+
if (feature == null)
24+
{
25+
return null;
26+
}
27+
28+
return feature[variableName];
29+
}
30+
}
31+
}

src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netstandard2.0.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ public partial interface IResponseCookiesFeature
260260
{
261261
Microsoft.AspNetCore.Http.IResponseCookies Cookies { get; }
262262
}
263+
public partial interface IServerVariablesFeature
264+
{
265+
string this[string variableName] { get; set; }
266+
}
263267
public partial interface IServiceProvidersFeature
264268
{
265269
System.IServiceProvider RequestServices { get; set; }

src/Servers/IIS/IIS/src/IServerVariableFeature.cs renamed to src/Http/Http.Features/src/IServerVariablesFeature.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ namespace Microsoft.AspNetCore.Http.Features
55
{
66
/// <summary>
77
/// This feature provides access to request server variables set.
8-
/// <para>
9-
/// This feature is only available when hosting ASP.NET Core in-process with IIS or IIS Express.
10-
/// </para>
118
/// </summary>
12-
/// <remarks>
13-
/// For a list of common server variables available in IIS, see http://go.microsoft.com/fwlink/?LinkId=52471.
14-
/// </remarks>
159
public interface IServerVariablesFeature
1610
{
1711
/// <summary>

src/Middleware/Rewrite/ref/Microsoft.AspNetCore.Rewrite.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<Compile Include="Microsoft.AspNetCore.Rewrite.netcoreapp3.0.cs" />
88
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
99
<Reference Include="Microsoft.AspNetCore.Http.Extensions" />
10-
<Reference Include="Microsoft.AspNetCore.Server.IIS" />
1110
<Reference Include="Microsoft.Extensions.Configuration.Abstractions" />
1211
<Reference Include="Microsoft.Extensions.FileProviders.Abstractions" />
1312
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />

src/Middleware/Rewrite/src/Internal/PatternSegments/IISServerVariableSegment.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using Microsoft.AspNetCore.Server.IIS;
5+
using Microsoft.AspNetCore.Http;
66

77
namespace Microsoft.AspNetCore.Rewrite.Internal.PatternSegments
88
{
@@ -19,7 +19,7 @@ public IISServerVariableSegment(string variableName, Func<PatternSegment> fallba
1919

2020
public override string Evaluate(RewriteContext context, BackReferenceCollection ruleBackReferences, BackReferenceCollection conditionBackReferences)
2121
{
22-
return context.HttpContext.GetIISServerVariable(_variableName) ?? _fallbackThunk().Evaluate(context, ruleBackReferences, conditionBackReferences);
22+
return context.HttpContext.GetServerVariable(_variableName) ?? _fallbackThunk().Evaluate(context, ruleBackReferences, conditionBackReferences);
2323
}
2424
}
2525
}

src/Middleware/Rewrite/src/Microsoft.AspNetCore.Rewrite.csproj

Lines changed: 1 addition & 2 deletions
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
<Description>ASP.NET Core basic middleware for rewriting URLs. Includes:
@@ -15,7 +15,6 @@
1515
<ItemGroup>
1616
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
1717
<Reference Include="Microsoft.AspNetCore.Http.Extensions" />
18-
<Reference Include="Microsoft.AspNetCore.Server.IIS" />
1918
<Reference Include="Microsoft.Extensions.Configuration.Abstractions" />
2019
<Reference Include="Microsoft.Extensions.FileProviders.Abstractions" />
2120
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<Reference Include="Microsoft.AspNetCore.Authentication.Core" />
99
<Reference Include="Microsoft.AspNetCore.Connections.Abstractions" />
1010
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
11+
<Reference Include="Microsoft.AspNetCore.Http.Extensions" />
12+
<Reference Include="Microsoft.AspNetCore.Http.Features" />
1113
<Reference Include="Microsoft.Extensions.FileProviders.Physical" />
1214
<Reference Include="Microsoft.Extensions.TypeNameHelper.Sources" />
1315
<Reference Include="System.IO.Pipelines" />

src/Servers/IIS/IIS/ref/Microsoft.AspNetCore.Server.IIS.netcoreapp3.0.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ public static partial class WebHostBuilderIISExtensions
1919
public static Microsoft.AspNetCore.Hosting.IWebHostBuilder UseIIS(this Microsoft.AspNetCore.Hosting.IWebHostBuilder hostBuilder) { throw null; }
2020
}
2121
}
22-
namespace Microsoft.AspNetCore.Http.Features
23-
{
24-
public partial interface IServerVariablesFeature
25-
{
26-
string this[string variableName] { get; set; }
27-
}
28-
}
2922
namespace Microsoft.AspNetCore.Server.IIS
3023
{
3124
public sealed partial class BadHttpRequestException : System.IO.IOException
@@ -35,6 +28,7 @@ internal BadHttpRequestException() { }
3528
}
3629
public static partial class HttpContextExtensions
3730
{
31+
[System.ObsoleteAttribute("This is obsolete and will be removed in a future version. Use GetServerVariable instead.")]
3832
public static string GetIISServerVariable(this Microsoft.AspNetCore.Http.HttpContext context, string variableName) { throw null; }
3933
}
4034
public partial class IISServerDefaults

src/Servers/IIS/IIS/samples/NativeIISSample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void Configure(IApplicationBuilder app)
8080

8181
foreach (var varName in IISServerVarNames)
8282
{
83-
await context.Response.WriteAsync(varName + ": " + context.GetIISServerVariable(varName) + Environment.NewLine);
83+
await context.Response.WriteAsync(varName + ": " + context.GetServerVariable(varName) + Environment.NewLine);
8484
}
8585

8686
await context.Response.WriteAsync(Environment.NewLine);

src/Servers/IIS/IIS/src/AssemblyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Runtime.CompilerServices;
5+
using Microsoft.AspNetCore.Http.Features;
56

67
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.IISIntegration.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
78
[assembly: InternalsVisibleTo("IIS.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
9+
[assembly: TypeForwardedTo(typeof(IServerVariablesFeature))]
810

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using Microsoft.AspNetCore.Http;
56
using Microsoft.AspNetCore.Http.Features;
67

@@ -17,22 +18,11 @@ public static class HttpContextExtensions
1718
/// <param name="context">The http context for the request.</param>
1819
/// <param name="variableName">The name of the variable.</param>
1920
/// <returns>
20-
/// <c>null</c> if the feature does not support the <see cref="IServerVariablesFeature"/> feature.
21+
/// <c>null</c> if the server does not support the <see cref="IServerVariablesFeature"/> feature.
2122
/// May return null or empty if the variable does not exist or is not set.
2223
/// </returns>
23-
/// <remarks>
24-
/// For a list of common server variables available in IIS, see http://go.microsoft.com/fwlink/?LinkId=52471.
25-
/// </remarks>
26-
public static string GetIISServerVariable(this HttpContext context, string variableName)
27-
{
28-
var feature = context.Features.Get<IServerVariablesFeature>();
29-
30-
if (feature == null)
31-
{
32-
return null;
33-
}
34-
35-
return feature[variableName];
36-
}
24+
[Obsolete("This is obsolete and will be removed in a future version. Use " + nameof(HttpContextServerVariableExtensions.GetServerVariable) + " instead.")]
25+
public static string GetIISServerVariable(this HttpContext context, string variableName) =>
26+
context.GetServerVariable(variableName);
3727
}
3828
}

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

Lines changed: 3 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>netcoreapp3.0</TargetFramework>
@@ -35,6 +35,8 @@
3535
<Reference Include="Microsoft.AspNetCore.Authentication.Core" />
3636
<Reference Include="Microsoft.AspNetCore.Connections.Abstractions" />
3737
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
38+
<Reference Include="Microsoft.AspNetCore.Http.Extensions" />
39+
<Reference Include="Microsoft.AspNetCore.Http.Features" />
3840
<Reference Include="Microsoft.Extensions.FileProviders.Physical" />
3941
<Reference Include="Microsoft.Extensions.TypeNameHelper.Sources" />
4042
<Reference Include="System.IO.Pipelines" />

src/Servers/IIS/IIS/test/testassets/InProcessNewShimWebSite/InProcessNewShimWebSite.csproj

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,30 @@
3030
</ItemGroup>
3131

3232
<ItemGroup>
33-
<Reference Include="Microsoft.AspNetCore.Hosting" />
34-
<Reference Include="Microsoft.AspNetCore.ResponseCompression" />
35-
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
36-
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
37-
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
38-
<Reference Include="Microsoft.Extensions.Configuration.Json" />
39-
<Reference Include="Microsoft.Extensions.Logging.Console" />
40-
<Reference Include="System.Net.WebSockets.WebSocketProtocol" />
33+
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" >
34+
<AllowExplicitReference>true</AllowExplicitReference>
35+
</PackageReference>
36+
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.2.0" >
37+
<AllowExplicitReference>true</AllowExplicitReference>
38+
</PackageReference>
39+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.2.0" >
40+
<AllowExplicitReference>true</AllowExplicitReference>
41+
</PackageReference>
42+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" >
43+
<AllowExplicitReference>true</AllowExplicitReference>
44+
</PackageReference>
45+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0" >
46+
<AllowExplicitReference>true</AllowExplicitReference>
47+
</PackageReference>
48+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" >
49+
<AllowExplicitReference>true</AllowExplicitReference>
50+
</PackageReference>
51+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" >
52+
<AllowExplicitReference>true</AllowExplicitReference>
53+
</PackageReference>
54+
<PackageReference Include="System.Net.WebSockets.WebSocketProtocol" Version="4.5.1" >
55+
<AllowExplicitReference>true</AllowExplicitReference>
56+
</PackageReference>
4157
<Reference Include="xunit.assert" />
4258
</ItemGroup>
4359

src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Startup.WebSockets.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ private void WebSocketEcho(IApplicationBuilder app)
4747
app.Run(async context =>
4848
{
4949
var ws = await Upgrade(context);
50-
50+
#if FORWARDCOMPAT
51+
var appLifetime = app.ApplicationServices.GetRequiredService<Microsoft.AspNetCore.Hosting.IApplicationLifetime>();
52+
#else
5153
var appLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
54+
#endif
5255

5356
await Echo(ws, appLifetime.ApplicationStopping);
5457
});

src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Startup.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Microsoft.Extensions.Hosting;
2727
using Microsoft.Extensions.Primitives;
2828
using Xunit;
29+
using HttpFeatures = Microsoft.AspNetCore.Http.Features;
2930

3031
namespace TestSite
3132
{
@@ -35,15 +36,20 @@ public void Configure(IApplicationBuilder app)
3536
{
3637
TestStartup.Register(app, this);
3738
}
38-
39+
3940
public void ConfigureServices(IServiceCollection serviceCollection)
4041
{
4142
serviceCollection.AddResponseCompression();
4243
}
44+
#if FORWARDCOMPAT
45+
private async Task ContentRootPath(HttpContext ctx) => await ctx.Response.WriteAsync(ctx.RequestServices.GetService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>().ContentRootPath);
4346

47+
private async Task WebRootPath(HttpContext ctx) => await ctx.Response.WriteAsync(ctx.RequestServices.GetService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>().WebRootPath);
48+
#else
4449
private async Task ContentRootPath(HttpContext ctx) => await ctx.Response.WriteAsync(ctx.RequestServices.GetService<IWebHostEnvironment>().ContentRootPath);
4550

4651
private async Task WebRootPath(HttpContext ctx) => await ctx.Response.WriteAsync(ctx.RequestServices.GetService<IWebHostEnvironment>().WebRootPath);
52+
#endif
4753

4854
private async Task CurrentDirectory(HttpContext ctx) => await ctx.Response.WriteAsync(Environment.CurrentDirectory);
4955

@@ -118,7 +124,11 @@ public async Task WaitingRequestCount(HttpContext context)
118124

119125
public Task CreateFile(HttpContext context)
120126
{
127+
#if FORWARDCOMPAT
128+
var hostingEnv = context.RequestServices.GetService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>();
129+
#else
121130
var hostingEnv = context.RequestServices.GetService<IWebHostEnvironment>();
131+
#endif
122132

123133
if (context.Connection.LocalIpAddress == null || context.Connection.RemoteIpAddress == null)
124134
{
@@ -434,7 +444,11 @@ private async Task ReadAndCountRequestBody(HttpContext ctx)
434444
private async Task WaitForAppToStartShuttingDown(HttpContext ctx)
435445
{
436446
await ctx.Response.WriteAsync("test1");
447+
#if FORWARDCOMPAT
448+
var lifetime = ctx.RequestServices.GetService<Microsoft.AspNetCore.Hosting.IApplicationLifetime>();
449+
#else
437450
var lifetime = ctx.RequestServices.GetService<IHostApplicationLifetime>();
451+
#endif
438452
lifetime.ApplicationStopping.WaitHandle.WaitOne();
439453
await ctx.Response.WriteAsync("test2");
440454
}
@@ -797,7 +811,11 @@ private async Task BasePath(HttpContext ctx)
797811
private async Task Shutdown(HttpContext ctx)
798812
{
799813
await ctx.Response.WriteAsync("Shutting down");
814+
#if FORWARDCOMPAT
815+
ctx.RequestServices.GetService<Microsoft.AspNetCore.Hosting.IApplicationLifetime>().StopApplication();
816+
#else
800817
ctx.RequestServices.GetService<IHostApplicationLifetime>().StopApplication();
818+
#endif
801819
}
802820

803821
private async Task ShutdownStopAsync(HttpContext ctx)
@@ -844,8 +862,8 @@ private async Task GetServerVariableStress(HttpContext ctx)
844862
// This test simulates the scenario where native Flush call is being
845863
// executed on background thread while request thread calls GetServerVariable
846864
// concurrent native calls may cause native object corruption
847-
848865
var serverVariableFeature = ctx.Features.Get<IServerVariablesFeature>();
866+
849867
await ctx.Response.WriteAsync("Response Begin");
850868
for (int i = 0; i < 1000; i++)
851869
{

0 commit comments

Comments
 (0)