From 31eeab55f3bc33368901498cf8b7e2d1f5117e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Derriey?= Date: Sun, 19 May 2019 13:02:51 +1000 Subject: [PATCH 1/3] Move IServerVariablesFeature to Http.Features --- ...spNetCore.Http.Extensions.netcoreapp3.0.cs | 4 +++ .../Http.Extensions/src/ContextExtensions.cs | 31 +++++++++++++++++++ ...AspNetCore.Http.Features.netstandard2.0.cs | 4 +++ .../src/IServerVariablesFeature.cs} | 6 ---- .../ref/Microsoft.AspNetCore.Rewrite.csproj | 1 - .../IISServerVariableSegment.cs | 4 +-- .../src/Microsoft.AspNetCore.Rewrite.csproj | 3 +- .../Microsoft.AspNetCore.Server.IIS.csproj | 2 ++ ...oft.AspNetCore.Server.IIS.netcoreapp3.0.cs | 8 +---- src/Servers/IIS/IIS/src/AssemblyInfo.cs | 2 ++ .../IIS/IIS/src/HttpContextExtensions.cs | 20 +++--------- .../Microsoft.AspNetCore.Server.IIS.csproj | 4 ++- 12 files changed, 55 insertions(+), 34 deletions(-) create mode 100644 src/Http/Http.Extensions/src/ContextExtensions.cs rename src/{Servers/IIS/IIS/src/IServerVariableFeature.cs => Http/Http.Features/src/IServerVariablesFeature.cs} (72%) diff --git a/src/Http/Http.Extensions/ref/Microsoft.AspNetCore.Http.Extensions.netcoreapp3.0.cs b/src/Http/Http.Extensions/ref/Microsoft.AspNetCore.Http.Extensions.netcoreapp3.0.cs index 77740aa3f055..0698c34c49e8 100644 --- a/src/Http/Http.Extensions/ref/Microsoft.AspNetCore.Http.Extensions.netcoreapp3.0.cs +++ b/src/Http/Http.Extensions/ref/Microsoft.AspNetCore.Http.Extensions.netcoreapp3.0.cs @@ -3,6 +3,10 @@ namespace Microsoft.AspNetCore.Http { + public static partial class ContextExtensions + { + public static string GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext context, string variableName) { throw null; } + } public static partial class HeaderDictionaryTypeExtensions { public static void AppendList(this Microsoft.AspNetCore.Http.IHeaderDictionary Headers, string name, System.Collections.Generic.IList values) { } diff --git a/src/Http/Http.Extensions/src/ContextExtensions.cs b/src/Http/Http.Extensions/src/ContextExtensions.cs new file mode 100644 index 000000000000..9d8da05abd62 --- /dev/null +++ b/src/Http/Http.Extensions/src/ContextExtensions.cs @@ -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 ContextExtensions + { + /// + /// Gets the value of a server variable for the current request. + /// + /// The http context for the request. + /// The name of the variable. + /// + /// null if the server does not support the feature. + /// May return null or empty if the variable does not exist or is not set. + /// + public static string GetServerVariable(this HttpContext context, string variableName) + { + var feature = context.Features.Get(); + + if (feature == null) + { + return null; + } + + return feature[variableName]; + } + } +} diff --git a/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netstandard2.0.cs b/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netstandard2.0.cs index 48f381bb968e..2eccf0b5a700 100644 --- a/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netstandard2.0.cs +++ b/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netstandard2.0.cs @@ -255,6 +255,10 @@ public partial interface IResponseCookiesFeature { 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; } diff --git a/src/Servers/IIS/IIS/src/IServerVariableFeature.cs b/src/Http/Http.Features/src/IServerVariablesFeature.cs similarity index 72% rename from src/Servers/IIS/IIS/src/IServerVariableFeature.cs rename to src/Http/Http.Features/src/IServerVariablesFeature.cs index 44f667cb4103..2ff18f77c5c6 100644 --- a/src/Servers/IIS/IIS/src/IServerVariableFeature.cs +++ b/src/Http/Http.Features/src/IServerVariablesFeature.cs @@ -5,13 +5,7 @@ namespace Microsoft.AspNetCore.Http.Features { /// /// This feature provides access to request server variables set. - /// - /// This feature is only available when hosting ASP.NET Core in-process with IIS or IIS Express. - /// /// - /// - /// For a list of common server variables available in IIS, see http://go.microsoft.com/fwlink/?LinkId=52471. - /// public interface IServerVariablesFeature { /// diff --git a/src/Middleware/Rewrite/ref/Microsoft.AspNetCore.Rewrite.csproj b/src/Middleware/Rewrite/ref/Microsoft.AspNetCore.Rewrite.csproj index 587668329e38..a4014b3f9491 100644 --- a/src/Middleware/Rewrite/ref/Microsoft.AspNetCore.Rewrite.csproj +++ b/src/Middleware/Rewrite/ref/Microsoft.AspNetCore.Rewrite.csproj @@ -7,7 +7,6 @@ - diff --git a/src/Middleware/Rewrite/src/Internal/PatternSegments/IISServerVariableSegment.cs b/src/Middleware/Rewrite/src/Internal/PatternSegments/IISServerVariableSegment.cs index 30504d54a4f1..8bc3f96cf8a8 100644 --- a/src/Middleware/Rewrite/src/Internal/PatternSegments/IISServerVariableSegment.cs +++ b/src/Middleware/Rewrite/src/Internal/PatternSegments/IISServerVariableSegment.cs @@ -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 @@ public IISServerVariableSegment(string variableName, Func fallba 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); } } } diff --git a/src/Middleware/Rewrite/src/Microsoft.AspNetCore.Rewrite.csproj b/src/Middleware/Rewrite/src/Microsoft.AspNetCore.Rewrite.csproj index be6f19ceb739..b7f9f44efa67 100644 --- a/src/Middleware/Rewrite/src/Microsoft.AspNetCore.Rewrite.csproj +++ b/src/Middleware/Rewrite/src/Microsoft.AspNetCore.Rewrite.csproj @@ -1,4 +1,4 @@ - + ASP.NET Core basic middleware for rewriting URLs. Includes: @@ -15,7 +15,6 @@ - diff --git a/src/Servers/IIS/IIS/ref/Microsoft.AspNetCore.Server.IIS.csproj b/src/Servers/IIS/IIS/ref/Microsoft.AspNetCore.Server.IIS.csproj index ed6753b780b4..72d9c85999fa 100644 --- a/src/Servers/IIS/IIS/ref/Microsoft.AspNetCore.Server.IIS.csproj +++ b/src/Servers/IIS/IIS/ref/Microsoft.AspNetCore.Server.IIS.csproj @@ -8,6 +8,8 @@ + + diff --git a/src/Servers/IIS/IIS/ref/Microsoft.AspNetCore.Server.IIS.netcoreapp3.0.cs b/src/Servers/IIS/IIS/ref/Microsoft.AspNetCore.Server.IIS.netcoreapp3.0.cs index 7294851a439a..635097115dd5 100644 --- a/src/Servers/IIS/IIS/ref/Microsoft.AspNetCore.Server.IIS.netcoreapp3.0.cs +++ b/src/Servers/IIS/IIS/ref/Microsoft.AspNetCore.Server.IIS.netcoreapp3.0.cs @@ -19,13 +19,6 @@ public static partial class WebHostBuilderIISExtensions 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 @@ internal BadHttpRequestException() { } } 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 diff --git a/src/Servers/IIS/IIS/src/AssemblyInfo.cs b/src/Servers/IIS/IIS/src/AssemblyInfo.cs index 5f58cf6c8ed8..6f83e91b28ce 100644 --- a/src/Servers/IIS/IIS/src/AssemblyInfo.cs +++ b/src/Servers/IIS/IIS/src/AssemblyInfo.cs @@ -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))] diff --git a/src/Servers/IIS/IIS/src/HttpContextExtensions.cs b/src/Servers/IIS/IIS/src/HttpContextExtensions.cs index 9c0092d3fe5f..621366b42dd7 100644 --- a/src/Servers/IIS/IIS/src/HttpContextExtensions.cs +++ b/src/Servers/IIS/IIS/src/HttpContextExtensions.cs @@ -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 @@ public static class HttpContextExtensions /// The http context for the request. /// The name of the variable. /// - /// null if the feature does not support the feature. + /// null if the server does not support the feature. /// May return null or empty if the variable does not exist or is not set. /// - /// - /// For a list of common server variables available in IIS, see http://go.microsoft.com/fwlink/?LinkId=52471. - /// - public static string GetIISServerVariable(this HttpContext context, string variableName) - { - var feature = context.Features.Get(); - - if (feature == null) - { - return null; - } - - return feature[variableName]; - } + [Obsolete("This is obsolete and will be removed in a future version. Use " + nameof(ContextExtensions.GetServerVariable) + " instead.")] + public static string GetIISServerVariable(this HttpContext context, string variableName) => + context.GetServerVariable(variableName); } } diff --git a/src/Servers/IIS/IIS/src/Microsoft.AspNetCore.Server.IIS.csproj b/src/Servers/IIS/IIS/src/Microsoft.AspNetCore.Server.IIS.csproj index dd160c168a3b..e72a107035ab 100644 --- a/src/Servers/IIS/IIS/src/Microsoft.AspNetCore.Server.IIS.csproj +++ b/src/Servers/IIS/IIS/src/Microsoft.AspNetCore.Server.IIS.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.0 @@ -35,6 +35,8 @@ + + From 43a048b4014a27409893755fd5bff44684dec545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Derriey?= Date: Mon, 20 May 2019 12:02:07 +1000 Subject: [PATCH 2/3] Use GetServerVariable instead of GetIISServerVariable --- src/Servers/IIS/IIS/samples/NativeIISSample/Startup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Servers/IIS/IIS/samples/NativeIISSample/Startup.cs b/src/Servers/IIS/IIS/samples/NativeIISSample/Startup.cs index 216dcea3b89d..96c77e15cb4a 100644 --- a/src/Servers/IIS/IIS/samples/NativeIISSample/Startup.cs +++ b/src/Servers/IIS/IIS/samples/NativeIISSample/Startup.cs @@ -80,7 +80,7 @@ public void Configure(IApplicationBuilder app) 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); From b1a85e0b9a49b3a13aa9118b9d7c541a32a6074f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Derriey?= Date: Mon, 20 May 2019 12:13:02 +1000 Subject: [PATCH 3/3] Force resolution of IServerVariablesFeature in Http.Features --- .../IIS/IIS/test/testassets/InProcessWebSite/Startup.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Startup.cs b/src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Startup.cs index 110ac5e62523..1353ab622fe9 100644 --- a/src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Startup.cs +++ b/src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Startup.cs @@ -20,6 +20,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Primitives; using Xunit; +using HttpFeatures = Microsoft.AspNetCore.Http.Features; namespace TestSite { @@ -34,7 +35,7 @@ private async Task ServerVariable(HttpContext ctx) { var varName = ctx.Request.Query["q"]; var newValue = ctx.Request.Query["v"]; - var feature = ctx.Features.Get(); + var feature = ctx.Features.Get(); if (newValue.Count != 0) { feature[varName] = newValue; @@ -702,7 +703,7 @@ private async Task GetServerVariableStress(HttpContext ctx) // executed on background thread while request thread calls GetServerVariable // concurrent native calls may cause native object corruption - var serverVariableFeature = ctx.Features.Get(); + var serverVariableFeature = ctx.Features.Get(); await ctx.Response.WriteAsync("Response Begin"); for (int i = 0; i < 1000; i++) {