Skip to content
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

Enable nullable in Kestrel.Core #28050

Merged
merged 3 commits into from Nov 30, 2020
Merged

Enable nullable in Kestrel.Core #28050

merged 3 commits into from Nov 30, 2020

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented Nov 21, 2020

Addresses #5680
Fixes #28195

@@ -418,7 +421,7 @@ internal void ExportCertificate(X509Certificate2 certificate, string path, bool
bytes = certificate.Export(X509ContentType.Pkcs12, password);
break;
case CertificateKeyExportFormat.Pem:
key = certificate.GetRSAPrivateKey();
key = certificate.GetRSAPrivateKey()!; // TODO - what if PEM doesn't have a private key?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any tests with a PEM file that doesn't have a private key?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A private key is required to function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know. At the moment I think that no private key will NRE instead of giving a useful error message.

Copy link
Member

@halter73 halter73 Nov 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiercn

We should probably throw a useful error and add a test for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -13,6 +13,6 @@ public interface IHostContextContainer<TContext> where TContext : notnull
/// <summary>
/// Represents the <typeparamref name="TContext"/> of the host.
/// </summary>
TContext HostContext { get; set; }
TContext? HostContext { get; set; }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a little weird that TContext has generic constraint of notnull, but the only place it is used is nullable.

Is notnull a way of saying "this type can be anything but Nullable<Struct>"

Copy link
Member

@halter73 halter73 Nov 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the way it is because if flows from IHttpApplication<TContext> which has the same constraint. You can use your own implementation of that if you call IServer.StartAsync directly, and we probably don't want to have to reason about IHttpApplication<HttpContext?> and the like.

@@ -1171,7 +1173,7 @@ private HttpResponseHeaders CreateResponseHeaders(bool appCompleted)
{
if ((option.Protocols & HttpProtocols.Http3) == HttpProtocols.Http3)
{
responseHeaders.HeaderAltSvc = $"h3-25=\":{option.IPEndPoint.Port}\"; ma=84600";
responseHeaders.HeaderAltSvc = $"h3-25=\":{option.IPEndPoint!.Port}\"; ma=84600";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know that option.IPEndPoint isn't null here @jkotalik? Now that IMultiplexedConnectionListenerFactory is experimentally public, I figure it's theoretically possible. I might just add a null check on option.IPEndPoint in the above if condition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to leave this as it. This is experimental, and at the moment I think an exception to say that something is wrong is preferable to silently doing the wrong thing.

reloadToken = Options.ConfigurationLoader.Configuration.GetReloadToken();
var (endpointsToStop, endpointsToStart) = Options.ConfigurationLoader.Reload();

Trace.LogDebug("Config reload token fired. Checking for changes...");

if (endpointsToStop.Count > 0)
{
var urlsToStop = endpointsToStop.Select(lo => lo.EndpointConfig.Url ?? "<unknown>");
var urlsToStop = endpointsToStop.Select(lo => lo.EndpointConfig?.Url ?? "<unknown>");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: ListenOptions in endpointsToStop must have a non-null EndpointConfig since it comes from the KestrelConfigurationLoader.

Suggested change
var urlsToStop = endpointsToStop.Select(lo => lo.EndpointConfig?.Url ?? "<unknown>");
var urlsToStop = endpointsToStop.Select(lo => lo.EndpointConfig!.Url ?? "<unknown>");

Copy link
Member Author

@JamesNK JamesNK Nov 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Url can't be null because ConfigurationReader throws an error when it is null or empty.

Can all the "<unknown>" be removed from this file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can all the "" be removed from this file?

I believe so. If EndpointConfig isn't null, Url shouldn't be null either.

@JamesNK JamesNK merged commit 6ea0926 into master Nov 30, 2020
@JamesNK JamesNK deleted the jamesnk/kestrel-nullable branch November 30, 2020 23:03
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Obsolete BindingAddress ctor
4 participants