Skip to content

Commit 774dc4b

Browse files
committed
React to UseHttps change
1 parent b0c0c7a commit 774dc4b

File tree

2 files changed

+52
-37
lines changed

2 files changed

+52
-37
lines changed

src/Benchmarks/PassthroughConnectionFilter.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
1-
using Microsoft.AspNetCore.Server.Kestrel.Filter;
2-
using System;
3-
using System.Collections.Generic;
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+
44
using System.IO;
5-
using System.Linq;
65
using System.Threading.Tasks;
76
using System.Threading;
7+
using Microsoft.AspNetCore.Server.Kestrel.Adapter;
8+
using Microsoft.AspNetCore.Http.Features;
89

910
namespace Benchmarks
1011
{
11-
public class PassthroughConnectionFilter : IConnectionFilter
12+
public class PassthroughConnectionFilter : IConnectionAdapter
1213
{
13-
private readonly IConnectionFilter _previous;
14-
15-
public PassthroughConnectionFilter(IConnectionFilter previous)
14+
public Task<IAdaptedConnection> OnConnectionAsync(ConnectionAdapterContext context)
1615
{
17-
_previous = previous;
16+
var adapted = new AdaptedConnection(new PassthroughStream(context.ConnectionStream));
17+
return Task.FromResult<IAdaptedConnection>(adapted);
1818
}
1919

20-
public async Task OnConnectionAsync(ConnectionFilterContext context)
20+
private class AdaptedConnection : IAdaptedConnection
2121
{
22-
await _previous.OnConnectionAsync(context);
22+
public AdaptedConnection(Stream stream)
23+
{
24+
ConnectionStream = stream;
25+
}
2326

24-
context.Connection = new PassthroughStream(context.Connection);
27+
public Stream ConnectionStream { get; }
28+
29+
public void PrepareRequest(IFeatureCollection requestFeatures)
30+
{
31+
}
2532
}
2633

2734
private class PassthroughStream : Stream

src/Benchmarks/Program.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
using System;
55
using System.IO;
6+
using System.Net;
67
using System.Runtime;
78
using System.Threading;
89
using Benchmarks.Configuration;
910
using Microsoft.AspNetCore.Hosting;
10-
using Microsoft.AspNetCore.Server.Kestrel.Filter;
11+
using Microsoft.AspNetCore.Server.Kestrel;
12+
using Microsoft.AspNetCore.Server.Kestrel.Adapter;
1113
using Microsoft.Extensions.Configuration;
1214
using Microsoft.Extensions.DependencyInjection;
1315

@@ -48,25 +50,31 @@ public static void Main(string[] args)
4850

4951
if (String.Equals(Server, "Kestrel", StringComparison.OrdinalIgnoreCase))
5052
{
51-
var threads = GetThreadCount(config);
52-
webHostBuilder = webHostBuilder.UseKestrel((options) =>
53+
webHostBuilder = webHostBuilder.UseKestrel(options =>
5354
{
54-
var connectionFilter = GetConnectionFilter(config, options.ConnectionFilter);
55-
if (connectionFilter != null)
55+
var urls = config["urls"] ?? config["server.urls"];
56+
57+
if (!string.IsNullOrEmpty(urls))
5658
{
57-
options.ConnectionFilter = connectionFilter;
59+
foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
60+
{
61+
Listen(options, config, value);
62+
}
5863
}
59-
60-
if (threads > 0)
64+
else
6165
{
62-
options.ThreadCount = threads;
66+
Listen(options, config, "http://localhost:5000/");
6367
}
6468

65-
if (UrlContainsHttps(config))
69+
var threads = GetThreadCount(config);
70+
71+
if (threads > 0)
6672
{
67-
options.UseHttps("testCert.pfx", "testPassword");
73+
options.ThreadCount = threads;
6874
}
6975
});
76+
77+
webHostBuilder.UseSetting(WebHostDefaults.ServerUrlsKey, string.Empty);
7078
}
7179
else if (String.Equals(Server, "WebListener", StringComparison.OrdinalIgnoreCase))
7280
{
@@ -146,7 +154,7 @@ private static int GetThreadCount(IConfigurationRoot config)
146154
return threadCountValue == null ? -1 : int.Parse(threadCountValue);
147155
}
148156

149-
private static IConnectionFilter GetConnectionFilter(IConfigurationRoot config, IConnectionFilter prevFilter)
157+
private static IConnectionAdapter GetConnectionFilter(IConfigurationRoot config)
150158
{
151159
var connectionFilterValue = config["connectionFilter"];
152160
if (string.IsNullOrEmpty(connectionFilterValue))
@@ -156,27 +164,27 @@ private static IConnectionFilter GetConnectionFilter(IConfigurationRoot config,
156164
else
157165
{
158166
var connectionFilterType = Type.GetType(connectionFilterValue, throwOnError: true);
159-
return (IConnectionFilter)Activator.CreateInstance(connectionFilterType, prevFilter ?? new NoOpConnectionFilter());
167+
return (IConnectionAdapter)Activator.CreateInstance(connectionFilterType);
160168
}
161169
}
162170

163-
// Copied from https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs
164-
private static bool UrlContainsHttps(IConfiguration config)
171+
private static void Listen(KestrelServerOptions options, IConfigurationRoot config, string url)
165172
{
166-
var urls = config["urls"] ?? config["server.urls"];
167-
168-
if (!string.IsNullOrEmpty(urls))
173+
var uri = new Uri(url);
174+
175+
options.Listen(IPAddress.Loopback, uri.Port, listenOptions =>
169176
{
170-
foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
177+
var connectionFilter = GetConnectionFilter(config);
178+
if (connectionFilter != null)
171179
{
172-
if (value.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) {
173-
return true;
174-
}
180+
listenOptions.ConnectionAdapters.Add(connectionFilter);
175181
}
176-
}
177182

178-
return false;
183+
if (uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
184+
{
185+
listenOptions.UseHttps("testCert.pfx", "testPassword");
186+
}
187+
});
179188
}
180189
}
181190
}
182-

0 commit comments

Comments
 (0)