From 726035e3fb8c4ee31a32522c16ff73278b9b13e7 Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Fri, 12 Jan 2018 13:20:06 -0800 Subject: [PATCH] Allow null SeverAddressFeature in Redirection Middleware (#289) --- .../HttpsRedirectionBuilderExtensions.cs | 14 +++-- .../HttpsRedirectionMiddleware.cs | 32 +++++++--- .../HttpsRedirectionMiddlewareTests.cs | 59 ++++++++++++++++++- 3 files changed, 91 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.AspNetCore.HttpsPolicy/HttpsRedirectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.HttpsPolicy/HttpsRedirectionBuilderExtensions.cs index 600e29fead..429f0f0ee2 100644 --- a/src/Microsoft.AspNetCore.HttpsPolicy/HttpsRedirectionBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.HttpsPolicy/HttpsRedirectionBuilderExtensions.cs @@ -29,11 +29,15 @@ namespace Microsoft.AspNetCore.Builder { throw new ArgumentNullException(nameof(app)); } - - var options = app.ApplicationServices.GetRequiredService>().Value; - - app.UseMiddleware(app.ServerFeatures.Get()); - + var serverAddressFeature = app.ServerFeatures.Get(); + if (serverAddressFeature != null) + { + app.UseMiddleware(serverAddressFeature); + } + else + { + app.UseMiddleware(); + } return app; } } diff --git a/src/Microsoft.AspNetCore.HttpsPolicy/HttpsRedirectionMiddleware.cs b/src/Microsoft.AspNetCore.HttpsPolicy/HttpsRedirectionMiddleware.cs index f500bbb2bc..a4a9cc034d 100644 --- a/src/Microsoft.AspNetCore.HttpsPolicy/HttpsRedirectionMiddleware.cs +++ b/src/Microsoft.AspNetCore.HttpsPolicy/HttpsRedirectionMiddleware.cs @@ -26,24 +26,36 @@ namespace Microsoft.AspNetCore.HttpsPolicy /// Initializes the HttpsRedirectionMiddleware /// /// - /// The /// /// - public HttpsRedirectionMiddleware(RequestDelegate next, IServerAddressesFeature serverAddressesFeature, IOptions options, IConfiguration config) + public HttpsRedirectionMiddleware(RequestDelegate next, IOptions options, IConfiguration config) + { + _next = next ?? throw new ArgumentNullException(nameof(next)); + _config = config ?? throw new ArgumentNullException(nameof(config)); + if (options == null) { throw new ArgumentNullException(nameof(options)); } - _config = config ?? throw new ArgumentException(nameof(config)); - _next = next ?? throw new ArgumentNullException(nameof(next)); - _serverAddressesFeature = serverAddressesFeature ?? throw new ArgumentNullException(nameof(serverAddressesFeature)); - var httpsRedirectionOptions = options.Value; _httpsPort = httpsRedirectionOptions.HttpsPort; _statusCode = httpsRedirectionOptions.RedirectStatusCode; } + /// + /// Initializes the HttpsRedirectionMiddleware + /// + /// + /// + /// + /// The + public HttpsRedirectionMiddleware(RequestDelegate next, IOptions options, IConfiguration config, IServerAddressesFeature serverAddressesFeature) + : this(next, options, config) + { + _serverAddressesFeature = serverAddressesFeature ?? throw new ArgumentNullException(nameof(serverAddressesFeature)); + } + /// /// Invokes the HttpsRedirectionMiddleware /// @@ -98,7 +110,13 @@ namespace Microsoft.AspNetCore.HttpsPolicy if (_httpsPort.HasValue) { return; - } + } + + if (_serverAddressesFeature == null) + { + _httpsPort = 443; + return; + } int? httpsPort = null; foreach (var address in _serverAddressesFeature.Addresses) diff --git a/test/Microsoft.AspNetCore.HttpsPolicy.Tests/HttpsRedirectionMiddlewareTests.cs b/test/Microsoft.AspNetCore.HttpsPolicy.Tests/HttpsRedirectionMiddlewareTests.cs index 95888d614f..9d482151ca 100644 --- a/test/Microsoft.AspNetCore.HttpsPolicy.Tests/HttpsRedirectionMiddlewareTests.cs +++ b/test/Microsoft.AspNetCore.HttpsPolicy.Tests/HttpsRedirectionMiddlewareTests.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; @@ -13,7 +12,6 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Net.Http.Headers; using Xunit; namespace Microsoft.AspNetCore.HttpsPolicy.Tests @@ -280,5 +278,62 @@ namespace Microsoft.AspNetCore.HttpsPolicy.Tests Assert.Equal("https://localhost:5050/", response.Headers.Location.ToString()); } + + [Fact] + public async Task NoServerAddressFeature_DoesNotThrow_DefaultsTo443() + { + var builder = new WebHostBuilder() + .ConfigureServices(services => + { + services.AddHttpsRedirection(options => + { + }); + }) + .Configure(app => + { + app.UseHttpsRedirection(); + app.Run(context => + { + return context.Response.WriteAsync("Hello world"); + }); + }); + + var server = new TestServer(builder); + var client = server.CreateClient(); + var request = new HttpRequestMessage(HttpMethod.Get, ""); + var response = await client.SendAsync(request); + + Assert.Equal("https://localhost/", response.Headers.Location.ToString()); + } + + [Fact] + public async Task SetNullAddressFeature_DoesNotThrow() + { + var builder = new WebHostBuilder() + .ConfigureServices(services => + { + services.AddHttpsRedirection(options => + { + }); + }) + .Configure(app => + { + app.UseHttpsRedirection(); + app.Run(context => + { + return context.Response.WriteAsync("Hello world"); + }); + }); + + var featureCollection = new FeatureCollection(); + featureCollection.Set(null); + var server = new TestServer(builder, featureCollection); + + var client = server.CreateClient(); + var request = new HttpRequestMessage(HttpMethod.Get, ""); + var response = await client.SendAsync(request); + + Assert.Equal("https://localhost/", response.Headers.Location.ToString()); + } } }