Allow null SeverAddressFeature in Redirection Middleware (#289)
This commit is contained in:
parent
7db86f0591
commit
726035e3fb
|
|
@ -29,11 +29,15 @@ namespace Microsoft.AspNetCore.Builder
|
|||
{
|
||||
throw new ArgumentNullException(nameof(app));
|
||||
}
|
||||
|
||||
var options = app.ApplicationServices.GetRequiredService<IOptions<HttpsRedirectionOptions>>().Value;
|
||||
|
||||
app.UseMiddleware<HttpsRedirectionMiddleware>(app.ServerFeatures.Get<IServerAddressesFeature>());
|
||||
|
||||
var serverAddressFeature = app.ServerFeatures.Get<IServerAddressesFeature>();
|
||||
if (serverAddressFeature != null)
|
||||
{
|
||||
app.UseMiddleware<HttpsRedirectionMiddleware>(serverAddressFeature);
|
||||
}
|
||||
else
|
||||
{
|
||||
app.UseMiddleware<HttpsRedirectionMiddleware>();
|
||||
}
|
||||
return app;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,24 +26,36 @@ namespace Microsoft.AspNetCore.HttpsPolicy
|
|||
/// Initializes the HttpsRedirectionMiddleware
|
||||
/// </summary>
|
||||
/// <param name="next"></param>
|
||||
/// <param name="serverAddressesFeature">The</param>
|
||||
/// <param name="options"></param>
|
||||
/// <param name="config"></param>
|
||||
public HttpsRedirectionMiddleware(RequestDelegate next, IServerAddressesFeature serverAddressesFeature, IOptions<HttpsRedirectionOptions> options, IConfiguration config)
|
||||
public HttpsRedirectionMiddleware(RequestDelegate next, IOptions<HttpsRedirectionOptions> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the HttpsRedirectionMiddleware
|
||||
/// </summary>
|
||||
/// <param name="next"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="serverAddressesFeature">The</param>
|
||||
public HttpsRedirectionMiddleware(RequestDelegate next, IOptions<HttpsRedirectionOptions> options, IConfiguration config, IServerAddressesFeature serverAddressesFeature)
|
||||
: this(next, options, config)
|
||||
{
|
||||
_serverAddressesFeature = serverAddressesFeature ?? throw new ArgumentNullException(nameof(serverAddressesFeature));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes the HttpsRedirectionMiddleware
|
||||
/// </summary>
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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<IServerAddressesFeature>(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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue