Skip to content

Commit 487b81b

Browse files
committed
Add HostPolicyMatcher
1 parent 55ec35b commit 487b81b

14 files changed

+1221
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.AspNetCore.Routing;
6+
7+
namespace Microsoft.AspNetCore.Builder
8+
{
9+
/// <summary>
10+
/// Extension methods for adding routing metadata to endpoint instances using <see cref="IEndpointConventionBuilder"/>.
11+
/// </summary>
12+
public static class RoutingEndpointConventionBuilderExtensions
13+
{
14+
/// <summary>
15+
/// Requires that endpoints match one of the specified hosts during routing.
16+
/// </summary>
17+
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/> to add the metadata to.</param>
18+
/// <param name="hosts">
19+
/// The hosts used during routing.
20+
/// Hosts should be Unicode rather than punycode, and may have a port.
21+
/// An empty collection means any host will be accepted.
22+
/// </param>
23+
/// <returns>A reference to this instance after the operation has completed.</returns>
24+
public static IEndpointConventionBuilder RequireHost(this IEndpointConventionBuilder builder, params string[] hosts)
25+
{
26+
if (builder == null)
27+
{
28+
throw new ArgumentNullException(nameof(builder));
29+
}
30+
31+
if (hosts == null)
32+
{
33+
throw new ArgumentNullException(nameof(hosts));
34+
}
35+
36+
builder.Add(endpointBuilder =>
37+
{
38+
endpointBuilder.Metadata.Add(new HostAttribute(hosts));
39+
});
40+
return builder;
41+
}
42+
}
43+
}

src/Http/Routing/src/DependencyInjection/RoutingServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public static IServiceCollection AddRouting(this IServiceCollection services)
8787
//
8888
services.TryAddSingleton<EndpointSelector, DefaultEndpointSelector>();
8989
services.TryAddEnumerable(ServiceDescriptor.Singleton<MatcherPolicy, HttpMethodMatcherPolicy>());
90+
services.TryAddEnumerable(ServiceDescriptor.Singleton<MatcherPolicy, HostMatcherPolicy>());
9091

9192
//
9293
// Misc infrastructure

src/Http/Routing/src/HostAttribute.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Linq;
8+
9+
namespace Microsoft.AspNetCore.Routing
10+
{
11+
/// <summary>
12+
/// Attribute for providing host metdata that is used during routing.
13+
/// </summary>
14+
[DebuggerDisplay("{DebuggerToString(),nq}")]
15+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
16+
public sealed class HostAttribute : Attribute, IHostMetadata
17+
{
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="HostAttribute" /> class.
20+
/// </summary>
21+
/// <param name="host">
22+
/// The host used during routing.
23+
/// Host should be Unicode rather than punycode, and may have a port.
24+
/// </param>
25+
public HostAttribute(string host) : this(new[] { host })
26+
{
27+
if (host == null)
28+
{
29+
throw new ArgumentNullException(nameof(host));
30+
}
31+
}
32+
33+
/// <summary>
34+
/// Initializes a new instance of the <see cref="HostAttribute" /> class.
35+
/// </summary>
36+
/// <param name="hosts">
37+
/// The hosts used during routing.
38+
/// Hosts should be Unicode rather than punycode, and may have a port.
39+
/// An empty collection means any host will be accepted.
40+
/// </param>
41+
public HostAttribute(params string[] hosts)
42+
{
43+
if (hosts == null)
44+
{
45+
throw new ArgumentNullException(nameof(hosts));
46+
}
47+
48+
Hosts = hosts.ToArray();
49+
}
50+
51+
/// <summary>
52+
/// Returns a read-only collection of hosts used during routing.
53+
/// Hosts will be Unicode rather than punycode, and may have a port.
54+
/// An empty collection means any host will be accepted.
55+
/// </summary>
56+
public IReadOnlyList<string> Hosts { get; }
57+
58+
private string DebuggerToString()
59+
{
60+
var hostsDisplay = (Hosts.Count == 0)
61+
? "*:*"
62+
: string.Join(",", Hosts.Select(h => h.Contains(':') ? h : h + ":*"));
63+
64+
return $"Hosts: {hostsDisplay}";
65+
}
66+
}
67+
}

src/Http/Routing/src/HttpMethodMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;

src/Http/Routing/src/IHostMetadata.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information
3+
4+
using System.Collections.Generic;
5+
6+
namespace Microsoft.AspNetCore.Routing
7+
{
8+
/// <summary>
9+
/// Represents host metadata used during routing.
10+
/// </summary>
11+
public interface IHostMetadata
12+
{
13+
/// <summary>
14+
/// Returns a read-only collection of hosts used during routing.
15+
/// Hosts will be Unicode rather than punycode, and may have a port.
16+
/// An empty collection means any host will be accepted.
17+
/// </summary>
18+
IReadOnlyList<string> Hosts { get; }
19+
}
20+
}

0 commit comments

Comments
 (0)