Skip to content

Commit 54e5370

Browse files
committed
Add CORS support to HttpMethodMatcherPolicy
Also removes HttpMethodEndpointConstraint, since it's been fully replaced.
1 parent c68c5be commit 54e5370

File tree

14 files changed

+561
-221
lines changed

14 files changed

+561
-221
lines changed

benchmarks/Microsoft.AspNetCore.Routing.Performance/Matchers/MatcherAzureBenchmark.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public partial class MatcherAzureBenchmark : MatcherBenchmarkBase
1313

1414
private BarebonesMatcher _baseline;
1515
private Matcher _dfa;
16-
private Matcher _tree;
1716

1817
private int[] _samples;
1918
private EndpointFeature _feature;
@@ -31,7 +30,6 @@ public void Setup()
3130

3231
_baseline = (BarebonesMatcher)SetupMatcher(new BarebonesMatcherBuilder());
3332
_dfa = SetupMatcher(CreateDfaMatcherBuilder());
34-
_tree = SetupMatcher(new TreeRouterMatcherBuilder());
3533

3634
_feature = new EndpointFeature();
3735
}
@@ -61,22 +59,5 @@ public async Task Dfa()
6159
Validate(httpContext, Endpoints[sample], feature.Endpoint);
6260
}
6361
}
64-
65-
[Benchmark(OperationsPerInvoke = SampleCount)]
66-
public async Task LegacyTreeRouter()
67-
{
68-
var feature = _feature;
69-
for (var i = 0; i < SampleCount; i++)
70-
{
71-
var sample = _samples[i];
72-
var httpContext = Requests[sample];
73-
74-
// This is required to make the legacy router implementation work with global routing.
75-
httpContext.Features.Set<IEndpointFeature>(feature);
76-
77-
await _tree.MatchAsync(httpContext, feature);
78-
Validate(httpContext, Endpoints[sample], feature.Endpoint);
79-
}
80-
}
8162
}
8263
}

benchmarks/Microsoft.AspNetCore.Routing.Performance/Matchers/MatcherBenchmarkBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private protected static MatcherEndpoint CreateEndpoint(string template, string
3939
var metadata = new List<object>();
4040
if (httpMethod != null)
4141
{
42-
metadata.Add(new HttpMethodEndpointConstraint(new string[] { httpMethod, }));
42+
metadata.Add(new HttpMethodMetadata(new string[] { httpMethod, }));
4343
}
4444

4545
return new MatcherEndpoint(

benchmarks/Microsoft.AspNetCore.Routing.Performance/Matchers/MatcherGithubBenchmark.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public partial class MatcherGithubBenchmark : MatcherBenchmarkBase
1212
{
1313
private BarebonesMatcher _baseline;
1414
private Matcher _dfa;
15-
private Matcher _tree;
1615

1716
private EndpointFeature _feature;
1817

@@ -25,7 +24,6 @@ public void Setup()
2524

2625
_baseline = (BarebonesMatcher)SetupMatcher(new BarebonesMatcherBuilder());
2726
_dfa = SetupMatcher(CreateDfaMatcherBuilder());
28-
_tree = SetupMatcher(new TreeRouterMatcherBuilder());
2927

3028
_feature = new EndpointFeature();
3129
}
@@ -53,21 +51,5 @@ public async Task Dfa()
5351
Validate(httpContext, Endpoints[i], feature.Endpoint);
5452
}
5553
}
56-
57-
[Benchmark(OperationsPerInvoke = EndpointCount)]
58-
public async Task LegacyTreeRouter()
59-
{
60-
var feature = _feature;
61-
for (var i = 0; i < EndpointCount; i++)
62-
{
63-
var httpContext = Requests[i];
64-
65-
// This is required to make the legacy router implementation work with global routing.
66-
httpContext.Features.Set<IEndpointFeature>(feature);
67-
68-
await _tree.MatchAsync(httpContext, feature);
69-
Validate(httpContext, Endpoints[i], feature.Endpoint);
70-
}
71-
}
7254
}
7355
}

src/Microsoft.AspNetCore.Routing/CompositeEndpointDataSource.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading;
1010
using Microsoft.AspNetCore.Routing.EndpointConstraints;
1111
using Microsoft.AspNetCore.Routing.Matchers;
12+
using Microsoft.AspNetCore.Routing.Metadata;
1213
using Microsoft.Extensions.Primitives;
1314

1415
namespace Microsoft.AspNetCore.Routing
@@ -134,15 +135,16 @@ private string DebuggerDisplayString
134135
sb.Append(", Order:");
135136
sb.Append(matcherEndpoint.Order);
136137

137-
var httpEndpointConstraints = matcherEndpoint.Metadata.GetOrderedMetadata<IEndpointConstraintMetadata>()
138-
.OfType<HttpMethodEndpointConstraint>();
139-
foreach (var constraint in httpEndpointConstraints)
138+
var httpMethodMetadata = matcherEndpoint.Metadata.GetMetadata<IHttpMethodMetadata>();
139+
if (httpMethodMetadata != null)
140140
{
141-
sb.Append(", Http Methods: ");
142-
sb.Append(string.Join(", ", constraint.HttpMethods));
143-
sb.Append(", Constraint Order:");
144-
sb.Append(constraint.Order);
141+
foreach (var httpMethod in httpMethodMetadata.HttpMethods)
142+
{
143+
sb.Append(", Http Methods: ");
144+
sb.Append(string.Join(", ", httpMethod));
145+
}
145146
}
147+
146148
sb.AppendLine();
147149
}
148150
else

src/Microsoft.AspNetCore.Routing/DependencyInjection/RoutingServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static IServiceCollection AddRouting(this IServiceCollection services)
7979
//
8080
services.TryAddSingleton<EndpointSelector, EndpointConstraintEndpointSelector>();
8181
services.TryAddSingleton<EndpointConstraintCache>();
82-
services.TryAddEnumerable(ServiceDescriptor.Singleton<MatcherPolicy, HttpMethodEndpointSelectorPolicy>());
82+
services.TryAddEnumerable(ServiceDescriptor.Singleton<MatcherPolicy, HttpMethodMatcherPolicy>());
8383

8484
// Will be cached by the EndpointSelector
8585
services.TryAddEnumerable(

src/Microsoft.AspNetCore.Routing/EndpointConstraints/HttpMethodEndpointConstraint.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public HttpMethodEndpointConstraint(IEnumerable<string> httpMethods)
4343

4444
IReadOnlyList<string> IHttpMethodMetadata.HttpMethods => _httpMethods;
4545

46+
bool IHttpMethodMetadata.AcceptCorsPreflight => false;
47+
4648
public virtual bool Accept(EndpointConstraintContext context)
4749
{
4850
if (context == null)

0 commit comments

Comments
 (0)