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

Support HttpApplication vary-by #326

Merged
merged 2 commits into from
Jun 2, 2023
Merged
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
20 changes: 20 additions & 0 deletions samples/Modules/ModulesCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Web;
using Microsoft.AspNetCore.OutputCaching;
using ModulesLibrary;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -13,9 +14,18 @@
options.RegisterModule<EventsModule>("Events");
});

builder.Services.AddOutputCache(options =>
{
options.AddHttpApplicationBasePolicy(_ => new[] { "browser" });
});

var app = builder.Build();

app.UseSystemWebAdapters();
app.UseOutputCache();

app.MapGet("/", () => "Hello")
.CacheOutput();

app.Run();

Expand All @@ -24,4 +34,14 @@ class MyApp : HttpApplication
protected void Application_Start()
{
}

public override string? GetVaryByCustomString(System.Web.HttpContext context, string custom)
{
if (custom == "test")
{
return "blah";
}

return base.GetVaryByCustomString(context, custom);
}
}
5 changes: 4 additions & 1 deletion samples/Modules/ModulesLibrary/EventsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ protected override void InvokeEvent(HttpContext context, string name)
throw new ArgumentNullException(nameof(context));
}

context.Response.ContentType = "text/plain";
if (context.CurrentNotification == RequestNotification.BeginRequest)
{
context.Response.ContentType = "text/plain";
}

context.Response.Output.WriteLine(name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ public static ISystemWebAdapterBuilder AddHttpApplication<TApp>(this ISystemWebA
return builder;
}

internal static void UseHttpApplication(this IApplicationBuilder app)
{
if (app.AreHttpApplicationEventsRequired())
{
app.UseMiddleware<HttpApplicationMiddleware>();
app.UseHttpApplicationEvent(ApplicationEvent.BeginRequest);
}
}

internal static void UseHttpApplicationEvent(this IApplicationBuilder app, params ApplicationEvent[] preEvents)
=> app.UseHttpApplicationEvent(preEvents, Array.Empty<ApplicationEvent>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Web;

using static System.FormattableString;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NET7_0_OR_GREATER

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SystemWebAdapters;

namespace Microsoft.AspNetCore.OutputCaching;

public static class HttpApplicationVaryByExtensions
{
/// <summary>
/// Adds an output cache policy to the base policy that will query <see cref="System.Web.HttpApplication.GetVaryByCustomString(System.Web.HttpContext, string)"/>
/// for values to vary by with the keys supplied by the the <paramref name="keySelector"/>.
/// </summary>
/// <param name="options">The <see cref="OutputCacheOptions"/> to add the base policy to.</param>
/// <param name="keySelector">The selector for keys to query <see cref="System.Web.HttpApplication"/> given the current <see cref="HttpContextCore"/>.</param>
public static void AddHttpApplicationBasePolicy(this OutputCacheOptions options, Func<HttpContextCore, IEnumerable<string>> keySelector)
{
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(keySelector);

options.AddBasePolicy(new HttpApplicationVaryByPolicy(keySelector));
}

/// <summary>
/// Adds a collection of custom keys to query <see cref="System.Web.HttpApplication.GetVaryByCustomString(System.Web.HttpContext, string)"/> for values to vary by.
/// </summary>
/// <param name="builder">The <see cref="OutputCachePolicyBuilder"/> to add the values to.</param>
/// <param name="customKeys">The custom keys to vary by value.</param>
public static void AddHttpApplicationVaryByCustom(this OutputCachePolicyBuilder builder, params string[] customKeys)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(customKeys);

foreach (var custom in customKeys)
{
builder.VaryByValue(context =>
{
var value = context.Features.Get<IHttpApplicationFeature>()?.Application.GetVaryByCustomString(context, custom);

return new(custom, value ?? string.Empty);
});
}
}

/// <summary>
/// Adds a named output cache policy that will query <see cref="System.Web.HttpApplication.GetVaryByCustomString(System.Web.HttpContext, string)"/>
/// for values to vary by with the keys supplied by the the <paramref name="keySelector"/>.
/// </summary>
/// <param name="options">The <see cref="OutputCacheOptions"/> to add the named policy to.</param>
/// <param name="keySelector">The selector for keys to query <see cref="System.Web.HttpApplication"/> given the current <see cref="HttpContextCore"/>.</param>
public static void AddHttpApplicationPolicy(this OutputCacheOptions options, string name, Func<HttpContextCore, IEnumerable<string>> keySelector)
{
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(keySelector);

options.AddPolicy(name, new HttpApplicationVaryByPolicy(keySelector));
}

private sealed class HttpApplicationVaryByPolicy : IOutputCachePolicy
{
private readonly Func<HttpContextCore, IEnumerable<string>> _keySelector;

public HttpApplicationVaryByPolicy(Func<HttpContextCore, IEnumerable<string>> keySelector)
{
_keySelector = keySelector;
}

ValueTask IOutputCachePolicy.CacheRequestAsync(OutputCacheContext context, CancellationToken cancellation)
{
if (context.HttpContext.Features.Get<IHttpApplicationFeature>() is { Application: { } app })
{
foreach (var key in _keySelector(context.HttpContext))
{
context.CacheVaryByRules.VaryByValues[key] = app.GetVaryByCustomString(context.HttpContext, key) ?? string.Empty;
}
}

return ValueTask.CompletedTask;
}

ValueTask IOutputCachePolicy.ServeFromCacheAsync(OutputCacheContext context, CancellationToken cancellation)
=> ValueTask.CompletedTask;

ValueTask IOutputCachePolicy.ServeResponseAsync(OutputCacheContext context, CancellationToken cancellation)
=> ValueTask.CompletedTask;
}
}
#endif

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<IsPackable>true</IsPackable>
<EnablePackageValidation>true</EnablePackageValidation>
<RootNamespace>Microsoft.AspNetCore.SystemWebAdapters</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Extensions.DependencyInjection;

internal class RemoteAppClientPostConfigureOptions : IPostConfigureOptions<RemoteAppClientOptions>
{
public void PostConfigure(string name, RemoteAppClientOptions options)
public void PostConfigure(string? name, RemoteAppClientOptions options)
{
if (options.BackchannelClient is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,11 @@ internal static void UseSystemWebAdapterFeatures(this IApplicationBuilder app)
return;
}

app.UseMiddleware<RegisterAdapterFeaturesMiddleware>();
app.UseMiddleware<PreBufferRequestStreamMiddleware>();
app.UseMiddleware<BufferResponseStreamMiddleware>();

app.UseMiddleware<SetDefaultResponseHeadersMiddleware>();
app.UseMiddleware<SingleThreadedRequestMiddleware>();
app.UseMiddleware<CurrentPrincipalMiddleware>();

app.UseHttpApplication();
}

public static void UseSystemWebAdapters(this IApplicationBuilder app)
Expand Down Expand Up @@ -122,6 +118,13 @@ public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
=> builder =>
{
builder.UseMiddleware<SetHttpContextTimestampMiddleware>();
builder.UseMiddleware<RegisterAdapterFeaturesMiddleware>();

if (builder.AreHttpApplicationEventsRequired())
{
builder.UseMiddleware<HttpApplicationMiddleware>();
builder.UseHttpApplicationEvent(ApplicationEvent.BeginRequest);
}

next(builder);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public event System.EventHandler ResolveRequestCache { add { } remove { } }
public event System.EventHandler UpdateRequestCache { add { } remove { } }
public void CompleteRequest() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public void Dispose() { }
public virtual string GetVaryByCustomString(System.Web.HttpContext context, string custom) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
}
public sealed partial class HttpApplicationState : System.Collections.Specialized.NameObjectCollectionBase
{
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public HttpContext Context

public void CompleteRequest() => Context.Response.End();

public virtual string? GetVaryByCustomString(HttpContext context, string custom)
{
if (string.Equals(custom, "browser", StringComparison.OrdinalIgnoreCase))
{
return context?.Request.Browser.Type;
}

return null;
}

public event EventHandler? BeginRequest
{
add => AddEvent(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private static async Task<List<string>> RunAsync(string action, string eventName
})
.ConfigureServices(services =>
{
services.AddSingleton<IStartupFilter>(new ModuleTestStartup(notifier, action));
services.AddRouting();
services.AddSystemWebAdapters()
.AddHttpApplication(options =>
Expand All @@ -137,17 +138,6 @@ private static async Task<List<string>> RunAsync(string action, string eventName
{
app.UseRouting();

app.Use(async (ctx, next) =>
{
ctx.Features.Set(notifier);
try
{
await next(ctx);
}
catch (InvalidOperationException) when (action == ModuleTestModule.Throw)
{
}
});
app.UseAuthenticationEvents();
app.UseAuthorizationEvents();
app.UseSystemWebAdapters();
Expand All @@ -168,6 +158,37 @@ private sealed class NotificationCollection : List<string>
{
}

private sealed class ModuleTestStartup : IStartupFilter
{
private readonly NotificationCollection _collection;
private readonly string _action;

public ModuleTestStartup(NotificationCollection collection, string action)
{
_collection = collection;
_action = action;
}

public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
=> builder =>
{
builder.Use(async (ctx, next) =>
{
ctx.Features.Set(_collection);

try
{
await next(ctx);
}
catch (InvalidOperationException) when (_action == ModuleTestModule.Throw)
{
}
});

next(builder);
};
}

private sealed class ModuleTestModule : EventsModule
{
protected override void InvokeEvent(HttpContext context, string name)
Expand Down