Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move IServerVariablesFeature to Http.Features #10374

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(this Microsoft.AspNetCore.Http.IHeaderDictionary Headers, string name, System.Collections.Generic.IList<T> values) { }
Expand Down
31 changes: 31 additions & 0 deletions src/Http/Http.Extensions/src/ContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <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];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -19,7 +19,7 @@ public IISServerVariableSegment(string variableName, Func<PatternSegment> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Servers/IIS/IIS/samples/NativeIISSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/Servers/IIS/IIS/src/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]

20 changes: 5 additions & 15 deletions src/Servers/IIS/IIS/src/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,22 +18,11 @@ public static class HttpContextExtensions
/// <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(ContextExtensions.GetServerVariable) + " instead.")]
public static string GetIISServerVariable(this HttpContext context, string variableName) =>
context.GetServerVariable(variableName);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
Expand Down Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives;
using Xunit;
using HttpFeatures = Microsoft.AspNetCore.Http.Features;

namespace TestSite
{
Expand All @@ -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<IServerVariablesFeature>();
var feature = ctx.Features.Get<HttpFeatures.IServerVariablesFeature>();
if (newValue.Count != 0)
{
feature[varName] = newValue;
Expand Down Expand Up @@ -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<IServerVariablesFeature>();
var serverVariableFeature = ctx.Features.Get<HttpFeatures.IServerVariablesFeature>();
await ctx.Response.WriteAsync("Response Begin");
for (int i = 0; i < 1000; i++)
{
Expand Down