-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AOT] Add expression free request filter pipeline for RequestDelegate (…
…#46020) Co-authored-by: Eric Erhardt <[email protected]>
- Loading branch information
Showing
11 changed files
with
400 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/Http/Routing/src/RequestDelegateFilterPipelineBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Text.Json.Serialization.Metadata; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Json; | ||
using Microsoft.AspNetCore.Internal; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.AspNetCore.Routing; | ||
|
||
internal static class RequestDelegateFilterPipelineBuilder | ||
{ | ||
// Due to https://github.com/dotnet/aspnetcore/issues/41330 we cannot reference the EmptyHttpResult type | ||
// but users still need to assert on it as in https://github.com/dotnet/aspnetcore/issues/45063 | ||
// so we temporarily work around this here by using reflection to get the actual type. | ||
private static readonly object? EmptyHttpResultInstance = Type.GetType("Microsoft.AspNetCore.Http.HttpResults.EmptyHttpResult, Microsoft.AspNetCore.Http.Results")?.GetProperty("Instance")?.GetValue(null, null); | ||
|
||
public static RequestDelegate Create(RequestDelegate requestDelegate, RequestDelegateFactoryOptions options) | ||
{ | ||
Debug.Assert(options.EndpointBuilder != null); | ||
|
||
var serviceProvider = options.ServiceProvider ?? options.EndpointBuilder.ApplicationServices; | ||
var jsonOptions = serviceProvider?.GetService<IOptions<JsonOptions>>()?.Value ?? new JsonOptions(); | ||
var jsonSerializerOptions = jsonOptions.SerializerOptions; | ||
|
||
var factoryContext = new EndpointFilterFactoryContext | ||
{ | ||
MethodInfo = requestDelegate.Method, | ||
ApplicationServices = options.EndpointBuilder.ApplicationServices | ||
}; | ||
var jsonTypeInfo = (JsonTypeInfo<object>)jsonSerializerOptions.GetReadOnlyTypeInfo(typeof(object)); | ||
|
||
EndpointFilterDelegate filteredInvocation = async (EndpointFilterInvocationContext context) => | ||
{ | ||
Debug.Assert(EmptyHttpResultInstance != null, "Unable to get EmptyHttpResult instance via reflection."); | ||
if (context.HttpContext.Response.StatusCode < 400) | ||
{ | ||
await requestDelegate(context.HttpContext); | ||
} | ||
return EmptyHttpResultInstance; | ||
}; | ||
|
||
var initialFilteredInvocation = filteredInvocation; | ||
for (var i = options.EndpointBuilder.FilterFactories.Count - 1; i >= 0; i--) | ||
{ | ||
var currentFilterFactory = options.EndpointBuilder.FilterFactories[i]; | ||
filteredInvocation = currentFilterFactory(factoryContext, filteredInvocation); | ||
} | ||
|
||
// The filter factories have run without modifying per-request behavior, we can skip running the pipeline. | ||
if (ReferenceEquals(initialFilteredInvocation, filteredInvocation)) | ||
{ | ||
return requestDelegate; | ||
} | ||
|
||
return async (HttpContext httpContext) => | ||
{ | ||
var obj = await filteredInvocation(new DefaultEndpointFilterInvocationContext(httpContext, new object[] { httpContext })); | ||
if (obj is not null) | ||
{ | ||
await ExecuteHandlerHelper.ExecuteReturnAsync(obj, httpContext, jsonSerializerOptions, jsonTypeInfo); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.