Move IServerVariablesFeature to Http.Features (#11007)

This commit is contained in:
Justin Kotalik 2019-06-10 21:16:36 -07:00 committed by GitHub
parent 5561338cfe
commit d7fc06af5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 104 additions and 46 deletions

View File

@ -9,6 +9,10 @@ namespace Microsoft.AspNetCore.Http
public static Microsoft.AspNetCore.Http.Headers.RequestHeaders GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpRequest request) { throw null; }
public static Microsoft.AspNetCore.Http.Headers.ResponseHeaders GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpResponse response) { throw null; }
}
public static partial class HttpContextServerVariableExtensions
{
public static string GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext context, string variableName) { throw null; }
}
public static partial class ResponseExtensions
{
public static void Clear(this Microsoft.AspNetCore.Http.HttpResponse response) { }

View File

@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Http.Features;
namespace Microsoft.AspNetCore.Http
{
public static class HttpContextServerVariableExtensions
{
/// <summary>
/// Gets the value of a server variable for the current request.
/// </summary>
/// <param name="context">The http context for the request.</param>
/// <param name="variableName">The name of the variable.</param>
/// <returns>
/// <c>null</c> if the server does not support the <see cref="IServerVariablesFeature"/> feature.
/// May return null or empty if the variable does not exist or is not set.
/// </returns>
public static string GetServerVariable(this HttpContext context, string variableName)
{
var feature = context.Features.Get<IServerVariablesFeature>();
if (feature == null)
{
return null;
}
return feature[variableName];
}
}
}

View File

@ -260,6 +260,10 @@ namespace Microsoft.AspNetCore.Http.Features
{
Microsoft.AspNetCore.Http.IResponseCookies Cookies { get; }
}
public partial interface IServerVariablesFeature
{
string this[string variableName] { get; set; }
}
public partial interface IServiceProvidersFeature
{
System.IServiceProvider RequestServices { get; set; }

View File

@ -5,13 +5,7 @@ namespace Microsoft.AspNetCore.Http.Features
{
/// <summary>
/// This feature provides access to request server variables set.
/// <para>
/// This feature is only available when hosting ASP.NET Core in-process with IIS or IIS Express.
/// </para>
/// </summary>
/// <remarks>
/// For a list of common server variables available in IIS, see http://go.microsoft.com/fwlink/?LinkId=52471.
/// </remarks>
public interface IServerVariablesFeature
{
/// <summary>

View File

@ -7,7 +7,6 @@
<Compile Include="Microsoft.AspNetCore.Rewrite.netcoreapp3.0.cs" />
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Http.Extensions" />
<Reference Include="Microsoft.AspNetCore.Server.IIS" />
<Reference Include="Microsoft.Extensions.Configuration.Abstractions" />
<Reference Include="Microsoft.Extensions.FileProviders.Abstractions" />
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />

View File

@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Server.IIS;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Rewrite.Internal.PatternSegments
{
@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.PatternSegments
public override string Evaluate(RewriteContext context, BackReferenceCollection ruleBackReferences, BackReferenceCollection conditionBackReferences)
{
return context.HttpContext.GetIISServerVariable(_variableName) ?? _fallbackThunk().Evaluate(context, ruleBackReferences, conditionBackReferences);
return context.HttpContext.GetServerVariable(_variableName) ?? _fallbackThunk().Evaluate(context, ruleBackReferences, conditionBackReferences);
}
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>ASP.NET Core basic middleware for rewriting URLs. Includes:
@ -15,7 +15,6 @@
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Http.Extensions" />
<Reference Include="Microsoft.AspNetCore.Server.IIS" />
<Reference Include="Microsoft.Extensions.Configuration.Abstractions" />
<Reference Include="Microsoft.Extensions.FileProviders.Abstractions" />
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />

View File

@ -8,6 +8,8 @@
<Reference Include="Microsoft.AspNetCore.Authentication.Core" />
<Reference Include="Microsoft.AspNetCore.Connections.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Http.Extensions" />
<Reference Include="Microsoft.AspNetCore.Http.Features" />
<Reference Include="Microsoft.Extensions.FileProviders.Physical" />
<Reference Include="Microsoft.Extensions.TypeNameHelper.Sources" />
<Reference Include="System.IO.Pipelines" />

View File

@ -19,13 +19,6 @@ namespace Microsoft.AspNetCore.Hosting
public static Microsoft.AspNetCore.Hosting.IWebHostBuilder UseIIS(this Microsoft.AspNetCore.Hosting.IWebHostBuilder hostBuilder) { throw null; }
}
}
namespace Microsoft.AspNetCore.Http.Features
{
public partial interface IServerVariablesFeature
{
string this[string variableName] { get; set; }
}
}
namespace Microsoft.AspNetCore.Server.IIS
{
public sealed partial class BadHttpRequestException : System.IO.IOException
@ -35,6 +28,7 @@ namespace Microsoft.AspNetCore.Server.IIS
}
public static partial class HttpContextExtensions
{
[System.ObsoleteAttribute("This is obsolete and will be removed in a future version. Use GetServerVariable instead.")]
public static string GetIISServerVariable(this Microsoft.AspNetCore.Http.HttpContext context, string variableName) { throw null; }
}
public partial class IISServerDefaults

View File

@ -80,7 +80,7 @@ namespace NativeIISSample
foreach (var varName in IISServerVarNames)
{
await context.Response.WriteAsync(varName + ": " + context.GetIISServerVariable(varName) + Environment.NewLine);
await context.Response.WriteAsync(varName + ": " + context.GetServerVariable(varName) + Environment.NewLine);
}
await context.Response.WriteAsync(Environment.NewLine);

View File

@ -2,7 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Http.Features;
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.IISIntegration.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("IIS.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: TypeForwardedTo(typeof(IServerVariablesFeature))]

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
@ -17,22 +18,11 @@ namespace Microsoft.AspNetCore.Server.IIS
/// <param name="context">The http context for the request.</param>
/// <param name="variableName">The name of the variable.</param>
/// <returns>
/// <c>null</c> if the feature does not support the <see cref="IServerVariablesFeature"/> feature.
/// <c>null</c> if the server does not support the <see cref="IServerVariablesFeature"/> feature.
/// May return null or empty if the variable does not exist or is not set.
/// </returns>
/// <remarks>
/// For a list of common server variables available in IIS, see http://go.microsoft.com/fwlink/?LinkId=52471.
/// </remarks>
public static string GetIISServerVariable(this HttpContext context, string variableName)
{
var feature = context.Features.Get<IServerVariablesFeature>();
if (feature == null)
{
return null;
}
return feature[variableName];
}
[Obsolete("This is obsolete and will be removed in a future version. Use " + nameof(HttpContextServerVariableExtensions.GetServerVariable) + " instead.")]
public static string GetIISServerVariable(this HttpContext context, string variableName) =>
context.GetServerVariable(variableName);
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
@ -35,6 +35,8 @@
<Reference Include="Microsoft.AspNetCore.Authentication.Core" />
<Reference Include="Microsoft.AspNetCore.Connections.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Http.Extensions" />
<Reference Include="Microsoft.AspNetCore.Http.Features" />
<Reference Include="Microsoft.Extensions.FileProviders.Physical" />
<Reference Include="Microsoft.Extensions.TypeNameHelper.Sources" />
<Reference Include="System.IO.Pipelines" />

View File

@ -30,14 +30,30 @@
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Hosting" />
<Reference Include="Microsoft.AspNetCore.ResponseCompression" />
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
<Reference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<Reference Include="Microsoft.Extensions.Configuration.Json" />
<Reference Include="Microsoft.Extensions.Logging.Console" />
<Reference Include="System.Net.WebSockets.WebSocketProtocol" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" >
<AllowExplicitReference>true</AllowExplicitReference>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.2.0" >
<AllowExplicitReference>true</AllowExplicitReference>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.2.0" >
<AllowExplicitReference>true</AllowExplicitReference>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" >
<AllowExplicitReference>true</AllowExplicitReference>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0" >
<AllowExplicitReference>true</AllowExplicitReference>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" >
<AllowExplicitReference>true</AllowExplicitReference>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" >
<AllowExplicitReference>true</AllowExplicitReference>
</PackageReference>
<PackageReference Include="System.Net.WebSockets.WebSocketProtocol" Version="4.5.1" >
<AllowExplicitReference>true</AllowExplicitReference>
</PackageReference>
<Reference Include="xunit.assert" />
</ItemGroup>

View File

@ -47,8 +47,11 @@ namespace TestSite
app.Run(async context =>
{
var ws = await Upgrade(context);
#if FORWARDCOMPAT
var appLifetime = app.ApplicationServices.GetRequiredService<Microsoft.AspNetCore.Hosting.IApplicationLifetime>();
#else
var appLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
#endif
await Echo(ws, appLifetime.ApplicationStopping);
});

View File

@ -26,6 +26,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives;
using Xunit;
using HttpFeatures = Microsoft.AspNetCore.Http.Features;
namespace TestSite
{
@ -35,15 +36,20 @@ namespace TestSite
{
TestStartup.Register(app, this);
}
public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddResponseCompression();
}
#if FORWARDCOMPAT
private async Task ContentRootPath(HttpContext ctx) => await ctx.Response.WriteAsync(ctx.RequestServices.GetService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>().ContentRootPath);
private async Task WebRootPath(HttpContext ctx) => await ctx.Response.WriteAsync(ctx.RequestServices.GetService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>().WebRootPath);
#else
private async Task ContentRootPath(HttpContext ctx) => await ctx.Response.WriteAsync(ctx.RequestServices.GetService<IWebHostEnvironment>().ContentRootPath);
private async Task WebRootPath(HttpContext ctx) => await ctx.Response.WriteAsync(ctx.RequestServices.GetService<IWebHostEnvironment>().WebRootPath);
#endif
private async Task CurrentDirectory(HttpContext ctx) => await ctx.Response.WriteAsync(Environment.CurrentDirectory);
@ -118,7 +124,11 @@ namespace TestSite
public Task CreateFile(HttpContext context)
{
#if FORWARDCOMPAT
var hostingEnv = context.RequestServices.GetService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>();
#else
var hostingEnv = context.RequestServices.GetService<IWebHostEnvironment>();
#endif
if (context.Connection.LocalIpAddress == null || context.Connection.RemoteIpAddress == null)
{
@ -434,7 +444,11 @@ namespace TestSite
private async Task WaitForAppToStartShuttingDown(HttpContext ctx)
{
await ctx.Response.WriteAsync("test1");
#if FORWARDCOMPAT
var lifetime = ctx.RequestServices.GetService<Microsoft.AspNetCore.Hosting.IApplicationLifetime>();
#else
var lifetime = ctx.RequestServices.GetService<IHostApplicationLifetime>();
#endif
lifetime.ApplicationStopping.WaitHandle.WaitOne();
await ctx.Response.WriteAsync("test2");
}
@ -797,7 +811,11 @@ namespace TestSite
private async Task Shutdown(HttpContext ctx)
{
await ctx.Response.WriteAsync("Shutting down");
#if FORWARDCOMPAT
ctx.RequestServices.GetService<Microsoft.AspNetCore.Hosting.IApplicationLifetime>().StopApplication();
#else
ctx.RequestServices.GetService<IHostApplicationLifetime>().StopApplication();
#endif
}
private async Task ShutdownStopAsync(HttpContext ctx)
@ -844,8 +862,8 @@ namespace TestSite
// This test simulates the scenario where native Flush call is being
// executed on background thread while request thread calls GetServerVariable
// concurrent native calls may cause native object corruption
var serverVariableFeature = ctx.Features.Get<IServerVariablesFeature>();
await ctx.Response.WriteAsync("Response Begin");
for (int i = 0; i < 1000; i++)
{