Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit dfaf37c

Browse files
committed
Implement config support #1290 #1879 #2016 #2166 #2167 #2188
1 parent 3a0a133 commit dfaf37c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1901
-397
lines changed

KestrelHttpServer.sln

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 15
3-
VisualStudioVersion = 15.0.27110.0
3+
VisualStudioVersion = 15.0.27130.2010
44
MinimumVisualStudioVersion = 15.0.26730.03
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7972A5D6-3385-4127-9277-428506DD44FF}"
66
ProjectSection(SolutionItems) = preProject
@@ -104,6 +104,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Protocols.Abstractions", "s
104104
EndProject
105105
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{C2910A13-B2C2-46D8-81D8-7E166F4F5981}"
106106
ProjectSection(SolutionItems) = preProject
107+
build\dependencies.props = build\dependencies.props
107108
build\repo.props = build\repo.props
108109
build\repo.targets = build\repo.targets
109110
EndProjectSection
@@ -119,7 +120,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kestrel.Transport.Libuv.Fun
119120
EndProject
120121
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kestrel.Transport.Sockets.FunctionalTests", "test\Kestrel.Transport.Sockets.FunctionalTests\Kestrel.Transport.Sockets.FunctionalTests.csproj", "{9C7B6B5F-088A-436E-834B-6373EA36DEEE}"
121122
EndProject
122-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Http2SampleApp", "samples\Http2SampleApp\Http2SampleApp.csproj", "{7BC22A4A-15D2-44C2-AB45-049F0FB562FA}"
123+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Http2SampleApp", "samples\Http2SampleApp\Http2SampleApp.csproj", "{7BC22A4A-15D2-44C2-AB45-049F0FB562FA}"
124+
EndProject
125+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SystemdTestApp", "samples\SystemdTestApp\SystemdTestApp.csproj", "{A7994A41-CAF8-47A7-8975-F101F75B5BC1}"
123126
EndProject
124127
Global
125128
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -335,6 +338,18 @@ Global
335338
{7BC22A4A-15D2-44C2-AB45-049F0FB562FA}.Release|x64.Build.0 = Release|Any CPU
336339
{7BC22A4A-15D2-44C2-AB45-049F0FB562FA}.Release|x86.ActiveCfg = Release|Any CPU
337340
{7BC22A4A-15D2-44C2-AB45-049F0FB562FA}.Release|x86.Build.0 = Release|Any CPU
341+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
342+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
343+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Debug|x64.ActiveCfg = Debug|Any CPU
344+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Debug|x64.Build.0 = Debug|Any CPU
345+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Debug|x86.ActiveCfg = Debug|Any CPU
346+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Debug|x86.Build.0 = Debug|Any CPU
347+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
348+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Release|Any CPU.Build.0 = Release|Any CPU
349+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Release|x64.ActiveCfg = Release|Any CPU
350+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Release|x64.Build.0 = Release|Any CPU
351+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Release|x86.ActiveCfg = Release|Any CPU
352+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1}.Release|x86.Build.0 = Release|Any CPU
338353
EndGlobalSection
339354
GlobalSection(SolutionProperties) = preSolution
340355
HideSolutionNode = FALSE
@@ -359,6 +374,7 @@ Global
359374
{74032D79-8EA7-4483-BD82-C38370420FFF} = {D3273454-EA07-41D2-BF0B-FCC3675C2483}
360375
{9C7B6B5F-088A-436E-834B-6373EA36DEEE} = {D3273454-EA07-41D2-BF0B-FCC3675C2483}
361376
{7BC22A4A-15D2-44C2-AB45-049F0FB562FA} = {8A3D00B8-1CCF-4BE6-A060-11104CE2D9CE}
377+
{A7994A41-CAF8-47A7-8975-F101F75B5BC1} = {8A3D00B8-1CCF-4BE6-A060-11104CE2D9CE}
362378
EndGlobalSection
363379
GlobalSection(ExtensibilityGlobals) = postSolution
364380
SolutionGuid = {2D10D020-6770-47CA-BB8D-2C23FE3AE071}

build/dependencies.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
<MicrosoftAspNetCoreHttpPackageVersion>2.1.0-preview1-27845</MicrosoftAspNetCoreHttpPackageVersion>
1717
<MicrosoftAspNetCoreTestingPackageVersion>2.1.0-preview1-27845</MicrosoftAspNetCoreTestingPackageVersion>
1818
<MicrosoftAspNetCoreWebUtilitiesPackageVersion>2.1.0-preview1-27845</MicrosoftAspNetCoreWebUtilitiesPackageVersion>
19+
<MicrosoftExtensionsConfigurationBinderPackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsConfigurationBinderPackageVersion>
20+
<MicrosoftExtensionsConfigurationJsonPackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsConfigurationJsonPackageVersion>
21+
<MicrosoftExtensionsDependencyInjectionPackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsDependencyInjectionPackageVersion>
1922
<MicrosoftExtensionsLoggingAbstractionsPackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsLoggingAbstractionsPackageVersion>
2023
<MicrosoftExtensionsLoggingConsolePackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsLoggingConsolePackageVersion>
2124
<MicrosoftExtensionsLoggingPackageVersion>2.1.0-preview1-27845</MicrosoftExtensionsLoggingPackageVersion>

samples/SampleApp/SampleApp.csproj

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsConfigurationJsonPackageVersion)" />
1516
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsolePackageVersion)" />
1617
</ItemGroup>
1718

1819
<ItemGroup>
19-
<Content Include="../../test/shared/TestCertificates/testCert.pfx" CopyToOutputDirectory="PreserveNewest" />
20+
<Content Include="testCert.pfx">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</Content>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<Content Update="appsettings.Development.json">
27+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
28+
</Content>
29+
<Content Update="appsettings.json">
30+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
31+
</Content>
32+
<Content Update="appsettings.Production.json">
33+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
34+
</Content>
2035
</ItemGroup>
2136

2237
</Project>

samples/SampleApp/Startup.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
using System;
55
using System.Diagnostics;
6-
using System.Globalization;
76
using System.IO;
87
using System.Net;
8+
using System.Security.Authentication;
9+
using System.Security.Cryptography.X509Certificates;
910
using System.Threading.Tasks;
1011
using Microsoft.AspNetCore.Builder;
1112
using Microsoft.AspNetCore.Hosting;
1213
using Microsoft.AspNetCore.Http;
14+
using Microsoft.AspNetCore.Server.Kestrel.Core;
1315
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal;
1416
using Microsoft.Extensions.Configuration;
1517
using Microsoft.Extensions.Logging;
@@ -48,10 +50,31 @@ public static Task Main(string[] args)
4850
{
4951
factory.AddConsole();
5052
})
53+
.ConfigureAppConfiguration((hostingContext, config) =>
54+
{
55+
var env = hostingContext.HostingEnvironment;
56+
config.AddJsonFile("appsettings.json", optional: true)
57+
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
58+
})
5159
.UseKestrel((context, options) =>
5260
{
61+
if (context.HostingEnvironment.IsDevelopment())
62+
{
63+
ShowConfig(context.Configuration);
64+
}
65+
5366
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
5467

68+
options.ConfigureEndpointDefaults(opt =>
69+
{
70+
opt.Protocols = HttpProtocols.Http1;
71+
});
72+
73+
options.ConfigureHttpsDefaults(httpsOptions =>
74+
{
75+
httpsOptions.SslProtocols = SslProtocols.Tls12;
76+
});
77+
5578
// Run callbacks on the transport thread
5679
options.ApplicationSchedulingMode = SchedulingMode.Inline;
5780

@@ -71,11 +94,34 @@ public static Task Main(string[] args)
7194

7295
options.ListenLocalhost(basePort + 2, listenOptions =>
7396
{
74-
listenOptions.UseHttps("testCert.pfx", "testPassword");
97+
// Use default dev cert
98+
listenOptions.UseHttps();
7599
});
76100

77101
options.ListenAnyIP(basePort + 3);
78102

103+
options.ListenAnyIP(basePort + 4, listenOptions =>
104+
{
105+
listenOptions.UseHttps(StoreName.My, "aspnet.test", allowInvalid: true);
106+
});
107+
108+
options
109+
.Configure()
110+
.Endpoint(IPAddress.Loopback, basePort + 5)
111+
.LocalhostEndpoint(basePort + 6)
112+
.Load();
113+
114+
options
115+
.Configure(context.Configuration.GetSection("Kestrel"))
116+
.Endpoint("NamedEndpoint", opt =>
117+
{
118+
opt.ListenOptions.Protocols = HttpProtocols.Http1;
119+
})
120+
.Endpoint("NamedHttpsEndpoint", opt =>
121+
{
122+
opt.HttpsOptions.SslProtocols = SslProtocols.Tls12;
123+
});
124+
79125
options.UseSystemd();
80126

81127
// The following section should be used to demo sockets
@@ -96,5 +142,14 @@ public static Task Main(string[] args)
96142

97143
return hostBuilder.Build().RunAsync();
98144
}
145+
146+
private static void ShowConfig(IConfiguration config)
147+
{
148+
foreach (var pair in config.GetChildren())
149+
{
150+
Console.WriteLine($"{pair.Path} - {pair.Value}");
151+
ShowConfig(pair);
152+
}
153+
}
99154
}
100155
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"Kestrel": {
3+
"Endpoints": {
4+
"NamedEndpoint": { "Url": "http://localhost:6000" },
5+
"NamedHttpsEndpoint": {
6+
"Url": "https://localhost:6443",
7+
"Certificate": {
8+
"Subject": "aspnet.test",
9+
"Store": "My",
10+
"AllowInvalid": true
11+
}
12+
}
13+
}
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Kestrel": {
3+
"Endpoints": {
4+
"NamedEndpoint": { "Url": "http://*:6000" },
5+
"NamedHttpsEndpoint": {
6+
"Url": "https://*:6443",
7+
"Certificate": {
8+
"Path": "testCert.pfx",
9+
"Password": "testPassword"
10+
}
11+
}
12+
}
13+
}
14+
}

samples/SampleApp/appsettings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"Kestrel": {
3+
"Endpoints": {
4+
}
5+
}
6+
}

samples/SystemdTestApp/Startup.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.Diagnostics;
6+
using System.IO;
7+
using System.Net;
8+
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Builder;
10+
using Microsoft.AspNetCore.Hosting;
11+
using Microsoft.AspNetCore.Http;
12+
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal;
13+
using Microsoft.Extensions.Configuration;
14+
using Microsoft.Extensions.Logging;
15+
16+
namespace SystemdTestApp
17+
{
18+
public class Startup
19+
{
20+
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
21+
{
22+
var logger = loggerFactory.CreateLogger("Default");
23+
24+
app.Run(async context =>
25+
{
26+
var connectionFeature = context.Connection;
27+
logger.LogDebug($"Peer: {connectionFeature.RemoteIpAddress?.ToString()}:{connectionFeature.RemotePort}"
28+
+ $"{Environment.NewLine}"
29+
+ $"Sock: {connectionFeature.LocalIpAddress?.ToString()}:{connectionFeature.LocalPort}");
30+
31+
var response = $"hello, world{Environment.NewLine}";
32+
context.Response.ContentLength = response.Length;
33+
context.Response.ContentType = "text/plain";
34+
await context.Response.WriteAsync(response);
35+
});
36+
}
37+
38+
public static Task Main(string[] args)
39+
{
40+
TaskScheduler.UnobservedTaskException += (sender, e) =>
41+
{
42+
Console.WriteLine("Unobserved exception: {0}", e.Exception);
43+
};
44+
45+
var hostBuilder = new WebHostBuilder()
46+
.ConfigureLogging((_, factory) =>
47+
{
48+
factory.AddConsole();
49+
})
50+
.UseKestrel((context, options) =>
51+
{
52+
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
53+
54+
// Run callbacks on the transport thread
55+
options.ApplicationSchedulingMode = SchedulingMode.Inline;
56+
57+
options.Listen(IPAddress.Loopback, basePort, listenOptions =>
58+
{
59+
// Uncomment the following to enable Nagle's algorithm for this endpoint.
60+
//listenOptions.NoDelay = false;
61+
62+
listenOptions.UseConnectionLogging();
63+
});
64+
65+
options.Listen(IPAddress.Loopback, basePort + 1, listenOptions =>
66+
{
67+
listenOptions.UseHttps("testCert.pfx", "testPassword");
68+
listenOptions.UseConnectionLogging();
69+
});
70+
71+
options.UseSystemd();
72+
73+
// The following section should be used to demo sockets
74+
//options.ListenUnixSocket("/tmp/kestrel-test.sock");
75+
})
76+
.UseContentRoot(Directory.GetCurrentDirectory())
77+
.UseStartup<Startup>();
78+
79+
if (string.Equals(Process.GetCurrentProcess().Id.ToString(), Environment.GetEnvironmentVariable("LISTEN_PID")))
80+
{
81+
// Use libuv if activated by systemd, since that's currently the only transport that supports being passed a socket handle.
82+
hostBuilder.UseLibuv(options =>
83+
{
84+
// Uncomment the following line to change the default number of libuv threads for all endpoints.
85+
// options.ThreadCount = 4;
86+
});
87+
}
88+
89+
return hostBuilder.Build().RunAsync();
90+
}
91+
}
92+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp2.1;netcoreapp2.0;net461</TargetFrameworks>
5+
<IsPackable>false</IsPackable>
6+
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\src\Kestrel\Kestrel.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsolePackageVersion)" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Content Include="testCert.pfx">
19+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
20+
</Content>
21+
</ItemGroup>
22+
23+
</Project>

samples/SystemdTestApp/testCert.pfx

2.42 KB
Binary file not shown.

src/Kestrel.Core/CoreStrings.resx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,19 @@
480480
<data name="ServiceCertificateRequired" xml:space="preserve">
481481
<value>The server certificate parameter is required.</value>
482482
</data>
483+
<data name="BindingToDefaultAddresses" xml:space="preserve">
484+
<value>No listening endpoints were configured. Binding to {address0} and {address1} by default.</value>
485+
</data>
486+
<data name="CertNotFoundInStore" xml:space="preserve">
487+
<value>The requested certificate {subject} could not be found in {storeLocation}/{storeName} with AllowInvalid setting: {allowInvalid}.</value>
488+
</data>
489+
<data name="EndpointMissingUrl" xml:space="preserve">
490+
<value>The endpoint {endpointName} is missing the required 'Url' parameter.</value>
491+
</data>
492+
<data name="NoCertSpecifiedNoDevelopmentCertificateFound" xml:space="preserve">
493+
<value>Unable to configure HTTPS endpoint. No server certificate was specified and the default developer certificate could not be found. Try running 'dotnet developercertificates https -t' to setup a developer certificate for use with localhost. For information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054</value>
494+
</data>
495+
<data name="MultipleCertificateSources" xml:space="preserve">
496+
<value>The endpoint {endpointName} specified multiple certificate sources.</value>
497+
</data>
483498
</root>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.Server.Kestrel.Core;
6+
using Microsoft.AspNetCore.Server.Kestrel.Https;
7+
using Microsoft.Extensions.Configuration;
8+
9+
namespace Microsoft.AspNetCore.Server.Kestrel
10+
{
11+
public class EndpointConfiguration
12+
{
13+
internal EndpointConfiguration(bool isHttps, ListenOptions listenOptions, HttpsConnectionAdapterOptions httpsOptions, IConfigurationSection configSection)
14+
{
15+
IsHttps = isHttps;
16+
ListenOptions = listenOptions ?? throw new ArgumentNullException(nameof(listenOptions));
17+
HttpsOptions = httpsOptions ?? throw new ArgumentNullException(nameof(httpsOptions));
18+
ConfigSection = configSection ?? throw new ArgumentNullException(nameof(configSection));
19+
}
20+
21+
public bool IsHttps { get; }
22+
public ListenOptions ListenOptions { get; }
23+
public HttpsConnectionAdapterOptions HttpsOptions { get; }
24+
public IConfigurationSection ConfigSection { get; }
25+
}
26+
}

0 commit comments

Comments
 (0)