Skip to content

Add HostPolicyMatcher #6214

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

Merged
merged 1 commit into from
Jan 10, 2019
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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.Routing;

namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// Extension methods for adding routing metadata to endpoint instances using <see cref="IEndpointConventionBuilder"/>.
/// </summary>
public static class RoutingEndpointConventionBuilderExtensions
{
/// <summary>
/// Requires that endpoints match one of the specified hosts during routing.
/// </summary>
/// <param name="builder">The <see cref="IEndpointConventionBuilder"/> to add the metadata to.</param>
/// <param name="hosts">
/// The hosts used during routing.
/// Hosts should be Unicode rather than punycode, and may have a port.
/// An empty collection means any host will be accepted.
/// </param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IEndpointConventionBuilder RequireHost(this IEndpointConventionBuilder builder, params string[] hosts)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (hosts == null)
{
throw new ArgumentNullException(nameof(hosts));
}

builder.Add(endpointBuilder =>
{
endpointBuilder.Metadata.Add(new HostAttribute(hosts));
});
return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static IServiceCollection AddRouting(this IServiceCollection services)
//
services.TryAddSingleton<EndpointSelector, DefaultEndpointSelector>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<MatcherPolicy, HttpMethodMatcherPolicy>());
services.TryAddEnumerable(ServiceDescriptor.Singleton<MatcherPolicy, HostMatcherPolicy>());

//
// Misc infrastructure
Expand Down
67 changes: 67 additions & 0 deletions src/Http/Routing/src/HostAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Microsoft.AspNetCore.Routing
{
/// <summary>
/// Attribute for providing host metdata that is used during routing.
/// </summary>
[DebuggerDisplay("{DebuggerToString(),nq}")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class HostAttribute : Attribute, IHostMetadata
{
/// <summary>
/// Initializes a new instance of the <see cref="HostAttribute" /> class.
/// </summary>
/// <param name="host">
/// The host used during routing.
/// Host should be Unicode rather than punycode, and may have a port.
/// </param>
public HostAttribute(string host) : this(new[] { host })
{
if (host == null)
{
throw new ArgumentNullException(nameof(host));
}
}

/// <summary>
/// Initializes a new instance of the <see cref="HostAttribute" /> class.
/// </summary>
/// <param name="hosts">
/// The hosts used during routing.
/// Hosts should be Unicode rather than punycode, and may have a port.
/// An empty collection means any host will be accepted.
/// </param>
public HostAttribute(params string[] hosts)
{
if (hosts == null)
{
throw new ArgumentNullException(nameof(hosts));
}

Hosts = hosts.ToArray();
}

/// <summary>
/// Returns a read-only collection of hosts used during routing.
/// Hosts will be Unicode rather than punycode, and may have a port.
/// An empty collection means any host will be accepted.
/// </summary>
public IReadOnlyList<string> Hosts { get; }

private string DebuggerToString()
{
var hostsDisplay = (Hosts.Count == 0)
? "*:*"
: string.Join(",", Hosts.Select(h => h.Contains(':') ? h : h + ":*"));

return $"Hosts: {hostsDisplay}";
}
}
}
2 changes: 1 addition & 1 deletion src/Http/Routing/src/HttpMethodMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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;
Expand Down
20 changes: 20 additions & 0 deletions src/Http/Routing/src/IHostMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.Collections.Generic;

namespace Microsoft.AspNetCore.Routing
{
/// <summary>
/// Represents host metadata used during routing.
/// </summary>
public interface IHostMetadata
{
/// <summary>
/// Returns a read-only collection of hosts used during routing.
/// Hosts will be Unicode rather than punycode, and may have a port.
/// An empty collection means any host will be accepted.
/// </summary>
IReadOnlyList<string> Hosts { get; }
}
}
Loading