From 628edbad28ee944a6783945a3638512c71c82c9f Mon Sep 17 00:00:00 2001 From: Pranav K Date: Sat, 13 Jun 2020 18:12:13 -0700 Subject: [PATCH 1/2] Annotate Microsoft.AspNetCore.Http with nullable attributes --- ...AspNetCore.Http.Abstractions.netcoreapp.cs | 14 ++--- .../Http.Abstractions/src/ConnectionInfo.cs | 8 +-- src/Http/Http.Abstractions/src/HttpContext.cs | 2 +- .../src/IApplicationBuilder.cs | 2 +- .../src/IHttpContextAccessor.cs | 2 +- ...oft.AspNetCore.Http.Features.netcoreapp.cs | 28 +++++----- ...AspNetCore.Http.Features.netstandard2.0.cs | 28 +++++----- .../IHttpAuthenticationFeature.cs | 2 +- .../Http.Features/src/FeatureReferences.cs | 10 ++-- src/Http/Http.Features/src/IFormFeature.cs | 4 +- .../Http.Features/src/IFormFileCollection.cs | 4 +- .../src/IHttpConnectionFeature.cs | 6 +-- .../Http.Features/src/IHttpResponseFeature.cs | 2 +- src/Http/Http.Features/src/IItemsFeature.cs | 4 +- .../src/IRequestCookieCollection.cs | 5 +- .../src/ITlsConnectionFeature.cs | 4 +- .../Http/ref/Microsoft.AspNetCore.Http.csproj | 1 + .../Microsoft.AspNetCore.Http.netcoreapp.cs | 44 +++++++-------- src/Http/Http/src/BindingAddress.cs | 8 +-- .../Http/src/Builder/ApplicationBuilder.cs | 17 +++--- src/Http/Http/src/DefaultHttpContext.cs | 41 +++++++------- .../HttpAuthenticationFeature.cs | 6 +-- .../src/Features/DefaultSessionFeature.cs | 2 +- src/Http/Http/src/Features/FormFeature.cs | 27 +++++----- .../src/Features/HttpConnectionFeature.cs | 8 +-- .../Features/HttpRequestIdentifierFeature.cs | 2 +- .../Http/src/Features/HttpResponseFeature.cs | 2 +- src/Http/Http/src/Features/ItemsFeature.cs | 2 +- src/Http/Http/src/Features/QueryFeature.cs | 8 +-- .../src/Features/RequestBodyPipeFeature.cs | 4 +- .../src/Features/RequestCookiesFeature.cs | 6 +-- .../src/Features/RequestServicesFeature.cs | 10 ++-- .../src/Features/ResponseCookiesFeature.cs | 10 ++-- .../Http/src/Features/RouteValuesFeature.cs | 6 +-- .../src/Features/ServiceProvidersFeature.cs | 4 +- .../Http/src/Features/TlsConnectionFeature.cs | 6 +-- src/Http/Http/src/FormCollection.cs | 6 +-- src/Http/Http/src/FormFile.cs | 2 +- src/Http/Http/src/FormFileCollection.cs | 4 +- src/Http/Http/src/HeaderDictionary.cs | 8 +-- src/Http/Http/src/HttpContextAccessor.cs | 4 +- src/Http/Http/src/HttpContextFactory.cs | 10 ++-- .../src/Internal/DefaultConnectionInfo.cs | 16 +++--- .../Http/src/Internal/DefaultHttpRequest.cs | 34 ++++++------ .../Http/src/Internal/DefaultHttpResponse.cs | 18 +++---- .../src/Internal/DefaultWebSocketManager.cs | 14 ++--- src/Http/Http/src/Internal/ItemsDictionary.cs | 54 ++++++++++--------- .../src/Internal/RequestCookieCollection.cs | 15 +++--- src/Http/Http/src/Internal/ResponseCookies.cs | 5 +- .../Http/src/Microsoft.AspNetCore.Http.csproj | 4 +- src/Http/Http/src/MiddlewareFactory.cs | 8 +-- src/Http/Http/src/QueryCollection.cs | 2 +- .../Http/src/StreamResponseBodyFeature.cs | 4 +- src/Http/Http/test/ResponseCookiesTest.cs | 10 ++-- ...soft.AspNetCore.WebUtilities.netcoreapp.cs | 2 +- src/Http/WebUtilities/src/MultipartSection.cs | 5 +- .../CopyOnWriteDictionary.cs | 10 ++-- .../CopyOnWriteDictionaryHolder.cs | 11 ++-- 58 files changed, 297 insertions(+), 288 deletions(-) diff --git a/src/Http/Http.Abstractions/ref/Microsoft.AspNetCore.Http.Abstractions.netcoreapp.cs b/src/Http/Http.Abstractions/ref/Microsoft.AspNetCore.Http.Abstractions.netcoreapp.cs index df35a394696e..03f8eec18121 100644 --- a/src/Http/Http.Abstractions/ref/Microsoft.AspNetCore.Http.Abstractions.netcoreapp.cs +++ b/src/Http/Http.Abstractions/ref/Microsoft.AspNetCore.Http.Abstractions.netcoreapp.cs @@ -14,7 +14,7 @@ protected EndpointBuilder() { } public partial interface IApplicationBuilder { System.IServiceProvider ApplicationServices { get; set; } - System.Collections.Generic.IDictionary Properties { get; } + System.Collections.Generic.IDictionary Properties { get; } Microsoft.AspNetCore.Http.Features.IFeatureCollection ServerFeatures { get; } Microsoft.AspNetCore.Http.RequestDelegate Build(); Microsoft.AspNetCore.Builder.IApplicationBuilder New(); @@ -108,13 +108,13 @@ public BadHttpRequestException(string message, int statusCode, System.Exception public abstract partial class ConnectionInfo { protected ConnectionInfo() { } - public abstract System.Security.Cryptography.X509Certificates.X509Certificate2 ClientCertificate { get; set; } + public abstract System.Security.Cryptography.X509Certificates.X509Certificate2? ClientCertificate { get; set; } public abstract string Id { get; set; } - public abstract System.Net.IPAddress LocalIpAddress { get; set; } + public abstract System.Net.IPAddress? LocalIpAddress { get; set; } public abstract int LocalPort { get; set; } - public abstract System.Net.IPAddress RemoteIpAddress { get; set; } + public abstract System.Net.IPAddress? RemoteIpAddress { get; set; } public abstract int RemotePort { get; set; } - public abstract System.Threading.Tasks.Task GetClientCertificateAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)); + public abstract System.Threading.Tasks.Task GetClientCertificateAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)); } public partial class CookieBuilder { @@ -231,7 +231,7 @@ protected HttpContext() { } public abstract System.Threading.CancellationToken RequestAborted { get; set; } public abstract System.IServiceProvider RequestServices { get; set; } public abstract Microsoft.AspNetCore.Http.HttpResponse Response { get; } - public abstract Microsoft.AspNetCore.Http.ISession? Session { get; set; } + public abstract Microsoft.AspNetCore.Http.ISession Session { get; set; } public abstract string TraceIdentifier { get; set; } public abstract System.Security.Claims.ClaimsPrincipal User { get; set; } public abstract Microsoft.AspNetCore.Http.WebSocketManager WebSockets { get; } @@ -326,7 +326,7 @@ public static partial class HttpResponseWritingExtensions } public partial interface IHttpContextAccessor { - Microsoft.AspNetCore.Http.HttpContext HttpContext { get; set; } + Microsoft.AspNetCore.Http.HttpContext? HttpContext { get; set; } } public partial interface IHttpContextFactory { diff --git a/src/Http/Http.Abstractions/src/ConnectionInfo.cs b/src/Http/Http.Abstractions/src/ConnectionInfo.cs index d4cab49afea2..da26205faad5 100644 --- a/src/Http/Http.Abstractions/src/ConnectionInfo.cs +++ b/src/Http/Http.Abstractions/src/ConnectionInfo.cs @@ -15,16 +15,16 @@ public abstract class ConnectionInfo /// public abstract string Id { get; set; } - public abstract IPAddress RemoteIpAddress { get; set; } + public abstract IPAddress? RemoteIpAddress { get; set; } public abstract int RemotePort { get; set; } - public abstract IPAddress LocalIpAddress { get; set; } + public abstract IPAddress? LocalIpAddress { get; set; } public abstract int LocalPort { get; set; } - public abstract X509Certificate2 ClientCertificate { get; set; } + public abstract X509Certificate2? ClientCertificate { get; set; } - public abstract Task GetClientCertificateAsync(CancellationToken cancellationToken = new CancellationToken()); + public abstract Task GetClientCertificateAsync(CancellationToken cancellationToken = new CancellationToken()); } } diff --git a/src/Http/Http.Abstractions/src/HttpContext.cs b/src/Http/Http.Abstractions/src/HttpContext.cs index a39b208e18e7..17448e07012b 100644 --- a/src/Http/Http.Abstractions/src/HttpContext.cs +++ b/src/Http/Http.Abstractions/src/HttpContext.cs @@ -68,7 +68,7 @@ public abstract class HttpContext /// /// Gets or sets the object used to manage user session data for this request. /// - public abstract ISession? Session { get; set; } + public abstract ISession Session { get; set; } /// /// Aborts the connection underlying this request. diff --git a/src/Http/Http.Abstractions/src/IApplicationBuilder.cs b/src/Http/Http.Abstractions/src/IApplicationBuilder.cs index 6110d7f3db6e..be3a06c2b3ea 100644 --- a/src/Http/Http.Abstractions/src/IApplicationBuilder.cs +++ b/src/Http/Http.Abstractions/src/IApplicationBuilder.cs @@ -26,7 +26,7 @@ public interface IApplicationBuilder /// /// Gets a key/value collection that can be used to share data between middleware. /// - IDictionary Properties { get; } + IDictionary Properties { get; } /// /// Adds a middleware delegate to the application's request pipeline. diff --git a/src/Http/Http.Abstractions/src/IHttpContextAccessor.cs b/src/Http/Http.Abstractions/src/IHttpContextAccessor.cs index b02c78d14476..9171cc003f29 100644 --- a/src/Http/Http.Abstractions/src/IHttpContextAccessor.cs +++ b/src/Http/Http.Abstractions/src/IHttpContextAccessor.cs @@ -15,6 +15,6 @@ public interface IHttpContextAccessor /// /// Gets or sets the current . Returns if there is no active . /// - HttpContext HttpContext { get; set; } + HttpContext? HttpContext { get; set; } } } diff --git a/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netcoreapp.cs b/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netcoreapp.cs index 76eaa034473c..28db424af538 100644 --- a/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netcoreapp.cs +++ b/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netcoreapp.cs @@ -38,8 +38,8 @@ public partial interface IFormFile } public partial interface IFormFileCollection : System.Collections.Generic.IEnumerable, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.IEnumerable { - Microsoft.AspNetCore.Http.IFormFile this[string name] { get; } - Microsoft.AspNetCore.Http.IFormFile GetFile(string name); + Microsoft.AspNetCore.Http.IFormFile? this[string name] { get; } + Microsoft.AspNetCore.Http.IFormFile? GetFile(string name); System.Collections.Generic.IReadOnlyList GetFiles(string name); } public partial interface IHeaderDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable @@ -58,10 +58,10 @@ public partial interface IQueryCollection : System.Collections.Generic.IEnumerab public partial interface IRequestCookieCollection : System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable { int Count { get; } - string this[string key] { get; } + string? this[string key] { get; } System.Collections.Generic.ICollection Keys { get; } bool ContainsKey(string key); - bool TryGetValue(string key, out string value); + bool TryGetValue(string key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out string? value); } public partial interface IResponseCookies { @@ -121,8 +121,8 @@ public partial struct FeatureReferences public FeatureReferences(Microsoft.AspNetCore.Http.Features.IFeatureCollection collection) { throw null; } public Microsoft.AspNetCore.Http.Features.IFeatureCollection Collection { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } public int Revision { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } - public TFeature Fetch([System.Diagnostics.CodeAnalysis.AllowNullAttribute, System.Diagnostics.CodeAnalysis.MaybeNullAttribute] ref TFeature cached, System.Func factory) where TFeature : class { throw null; } - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public TFeature Fetch([System.Diagnostics.CodeAnalysis.AllowNullAttribute, System.Diagnostics.CodeAnalysis.MaybeNullAttribute] ref TFeature cached, TState state, System.Func factory) where TFeature : class { throw null; } + public TFeature Fetch([System.Diagnostics.CodeAnalysis.AllowNullAttribute, System.Diagnostics.CodeAnalysis.MaybeNullAttribute] ref TFeature cached, System.Func factory) where TFeature : class? { throw null; } + [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public TFeature Fetch([System.Diagnostics.CodeAnalysis.AllowNullAttribute, System.Diagnostics.CodeAnalysis.MaybeNullAttribute] ref TFeature cached, TState state, System.Func factory) where TFeature : class? { throw null; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public void Initalize(Microsoft.AspNetCore.Http.Features.IFeatureCollection collection) { } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public void Initalize(Microsoft.AspNetCore.Http.Features.IFeatureCollection collection, int revision) { } } @@ -152,7 +152,7 @@ public partial interface IFeatureCollection : System.Collections.Generic.IEnumer } public partial interface IFormFeature { - Microsoft.AspNetCore.Http.IFormCollection Form { get; set; } + Microsoft.AspNetCore.Http.IFormCollection? Form { get; set; } bool HasFormContentType { get; } Microsoft.AspNetCore.Http.IFormCollection ReadForm(); System.Threading.Tasks.Task ReadFormAsync(System.Threading.CancellationToken cancellationToken); @@ -170,9 +170,9 @@ public partial interface IHttpBufferingFeature public partial interface IHttpConnectionFeature { string ConnectionId { get; set; } - System.Net.IPAddress LocalIpAddress { get; set; } + System.Net.IPAddress? LocalIpAddress { get; set; } int LocalPort { get; set; } - System.Net.IPAddress RemoteIpAddress { get; set; } + System.Net.IPAddress? RemoteIpAddress { get; set; } int RemotePort { get; set; } } public partial interface IHttpMaxRequestBodySizeFeature @@ -225,7 +225,7 @@ public partial interface IHttpResponseFeature System.IO.Stream Body { get; set; } bool HasStarted { get; } Microsoft.AspNetCore.Http.IHeaderDictionary Headers { get; set; } - string ReasonPhrase { get; set; } + string? ReasonPhrase { get; set; } int StatusCode { get; set; } void OnCompleted(System.Func callback, object state); void OnStarting(System.Func callback, object state); @@ -255,7 +255,7 @@ public partial interface IHttpWebSocketFeature } public partial interface IItemsFeature { - System.Collections.Generic.IDictionary Items { get; set; } + System.Collections.Generic.IDictionary Items { get; set; } } public partial interface IQueryFeature { @@ -287,8 +287,8 @@ public partial interface ISessionFeature } public partial interface ITlsConnectionFeature { - System.Security.Cryptography.X509Certificates.X509Certificate2 ClientCertificate { get; set; } - System.Threading.Tasks.Task GetClientCertificateAsync(System.Threading.CancellationToken cancellationToken); + System.Security.Cryptography.X509Certificates.X509Certificate2? ClientCertificate { get; set; } + System.Threading.Tasks.Task GetClientCertificateAsync(System.Threading.CancellationToken cancellationToken); } public partial interface ITlsTokenBindingFeature { @@ -309,6 +309,6 @@ namespace Microsoft.AspNetCore.Http.Features.Authentication { public partial interface IHttpAuthenticationFeature { - System.Security.Claims.ClaimsPrincipal User { get; set; } + System.Security.Claims.ClaimsPrincipal? User { get; set; } } } diff --git a/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netstandard2.0.cs b/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netstandard2.0.cs index 802000e099aa..4ed7226c08dd 100644 --- a/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netstandard2.0.cs +++ b/src/Http/Http.Features/ref/Microsoft.AspNetCore.Http.Features.netstandard2.0.cs @@ -38,8 +38,8 @@ public partial interface IFormFile } public partial interface IFormFileCollection : System.Collections.Generic.IEnumerable, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.IEnumerable { - Microsoft.AspNetCore.Http.IFormFile this[string name] { get; } - Microsoft.AspNetCore.Http.IFormFile GetFile(string name); + Microsoft.AspNetCore.Http.IFormFile? this[string name] { get; } + Microsoft.AspNetCore.Http.IFormFile? GetFile(string name); System.Collections.Generic.IReadOnlyList GetFiles(string name); } public partial interface IHeaderDictionary : System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable @@ -58,10 +58,10 @@ public partial interface IQueryCollection : System.Collections.Generic.IEnumerab public partial interface IRequestCookieCollection : System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable { int Count { get; } - string this[string key] { get; } + string? this[string key] { get; } System.Collections.Generic.ICollection Keys { get; } bool ContainsKey(string key); - bool TryGetValue(string key, out string value); + bool TryGetValue(string key, out string? value); } public partial interface IResponseCookies { @@ -118,8 +118,8 @@ public partial struct FeatureReferences public FeatureReferences(Microsoft.AspNetCore.Http.Features.IFeatureCollection collection) { throw null; } public Microsoft.AspNetCore.Http.Features.IFeatureCollection Collection { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } public int Revision { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } - public TFeature Fetch(ref TFeature cached, System.Func factory) where TFeature : class { throw null; } - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public TFeature Fetch(ref TFeature cached, TState state, System.Func factory) where TFeature : class { throw null; } + public TFeature Fetch(ref TFeature cached, System.Func factory) where TFeature : class? { throw null; } + [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public TFeature Fetch(ref TFeature cached, TState state, System.Func factory) where TFeature : class? { throw null; } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public void Initalize(Microsoft.AspNetCore.Http.Features.IFeatureCollection collection) { } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]public void Initalize(Microsoft.AspNetCore.Http.Features.IFeatureCollection collection, int revision) { } } @@ -148,7 +148,7 @@ public partial interface IFeatureCollection : System.Collections.Generic.IEnumer } public partial interface IFormFeature { - Microsoft.AspNetCore.Http.IFormCollection Form { get; set; } + Microsoft.AspNetCore.Http.IFormCollection? Form { get; set; } bool HasFormContentType { get; } Microsoft.AspNetCore.Http.IFormCollection ReadForm(); System.Threading.Tasks.Task ReadFormAsync(System.Threading.CancellationToken cancellationToken); @@ -166,9 +166,9 @@ public partial interface IHttpBufferingFeature public partial interface IHttpConnectionFeature { string ConnectionId { get; set; } - System.Net.IPAddress LocalIpAddress { get; set; } + System.Net.IPAddress? LocalIpAddress { get; set; } int LocalPort { get; set; } - System.Net.IPAddress RemoteIpAddress { get; set; } + System.Net.IPAddress? RemoteIpAddress { get; set; } int RemotePort { get; set; } } public partial interface IHttpMaxRequestBodySizeFeature @@ -221,7 +221,7 @@ public partial interface IHttpResponseFeature System.IO.Stream Body { get; set; } bool HasStarted { get; } Microsoft.AspNetCore.Http.IHeaderDictionary Headers { get; set; } - string ReasonPhrase { get; set; } + string? ReasonPhrase { get; set; } int StatusCode { get; set; } void OnCompleted(System.Func callback, object state); void OnStarting(System.Func callback, object state); @@ -251,7 +251,7 @@ public partial interface IHttpWebSocketFeature } public partial interface IItemsFeature { - System.Collections.Generic.IDictionary Items { get; set; } + System.Collections.Generic.IDictionary Items { get; set; } } public partial interface IQueryFeature { @@ -283,8 +283,8 @@ public partial interface ISessionFeature } public partial interface ITlsConnectionFeature { - System.Security.Cryptography.X509Certificates.X509Certificate2 ClientCertificate { get; set; } - System.Threading.Tasks.Task GetClientCertificateAsync(System.Threading.CancellationToken cancellationToken); + System.Security.Cryptography.X509Certificates.X509Certificate2? ClientCertificate { get; set; } + System.Threading.Tasks.Task GetClientCertificateAsync(System.Threading.CancellationToken cancellationToken); } public partial interface ITlsTokenBindingFeature { @@ -305,6 +305,6 @@ namespace Microsoft.AspNetCore.Http.Features.Authentication { public partial interface IHttpAuthenticationFeature { - System.Security.Claims.ClaimsPrincipal User { get; set; } + System.Security.Claims.ClaimsPrincipal? User { get; set; } } } diff --git a/src/Http/Http.Features/src/Authentication/IHttpAuthenticationFeature.cs b/src/Http/Http.Features/src/Authentication/IHttpAuthenticationFeature.cs index 49684c0df7c9..a82d4e2d0344 100644 --- a/src/Http/Http.Features/src/Authentication/IHttpAuthenticationFeature.cs +++ b/src/Http/Http.Features/src/Authentication/IHttpAuthenticationFeature.cs @@ -8,6 +8,6 @@ namespace Microsoft.AspNetCore.Http.Features.Authentication { public interface IHttpAuthenticationFeature { - ClaimsPrincipal User { get; set; } + ClaimsPrincipal? User { get; set; } } } diff --git a/src/Http/Http.Features/src/FeatureReferences.cs b/src/Http/Http.Features/src/FeatureReferences.cs index fa32c413471e..84bf46974c39 100644 --- a/src/Http/Http.Features/src/FeatureReferences.cs +++ b/src/Http/Http.Features/src/FeatureReferences.cs @@ -62,9 +62,9 @@ public void Initalize(IFeatureCollection collection, int revision) // Generally Fetch is called at a ratio > x4 of UpdateCached so this is a large gain [MethodImpl(MethodImplOptions.AggressiveInlining)] public TFeature Fetch( - [AllowNull, MaybeNull]ref TFeature cached, + [AllowNull, MaybeNull] ref TFeature cached, TState state, - Func factory) where TFeature : class + Func factory) where TFeature : class? { var flush = false; var revision = Collection?.Revision ?? ContextDisposed(); @@ -80,7 +80,7 @@ public TFeature Fetch( } // Update and cache clearing logic, when the fast-path in Fetch isn't applicable - private TFeature UpdateCached(ref TFeature cached, TState state, Func factory, int revision, bool flush) where TFeature : class + private TFeature UpdateCached(ref TFeature cached, TState state, Func factory, int revision, bool flush) where TFeature : class? { if (flush) { @@ -108,8 +108,8 @@ private TFeature UpdateCached(ref TFeature cached, TState stat return cached; } - public TFeature Fetch([AllowNull, MaybeNull]ref TFeature cached, Func factory) - where TFeature : class => Fetch(ref cached!, Collection, factory); + public TFeature Fetch([AllowNull, MaybeNull] ref TFeature cached, Func factory) + where TFeature : class? => Fetch(ref cached!, Collection, factory); private static int ContextDisposed() { diff --git a/src/Http/Http.Features/src/IFormFeature.cs b/src/Http/Http.Features/src/IFormFeature.cs index f10ed47b806e..9ff770108878 100644 --- a/src/Http/Http.Features/src/IFormFeature.cs +++ b/src/Http/Http.Features/src/IFormFeature.cs @@ -16,12 +16,12 @@ public interface IFormFeature /// /// The parsed form, if any. /// - IFormCollection Form { get; set; } + IFormCollection? Form { get; set; } /// /// Parses the request body as a form. /// - /// + /// The . IFormCollection ReadForm(); /// diff --git a/src/Http/Http.Features/src/IFormFileCollection.cs b/src/Http/Http.Features/src/IFormFileCollection.cs index ab862c917b7e..b3501768366a 100644 --- a/src/Http/Http.Features/src/IFormFileCollection.cs +++ b/src/Http/Http.Features/src/IFormFileCollection.cs @@ -17,7 +17,7 @@ public interface IFormFileCollection : IReadOnlyList /// /// The requested file, or null if it is not present. /// - IFormFile this[string name] { get; } + IFormFile? this[string name] { get; } /// /// Gets the first file with the specified name. @@ -26,7 +26,7 @@ public interface IFormFileCollection : IReadOnlyList /// /// The requested file, or null if it is not present. /// - IFormFile GetFile(string name); + IFormFile? GetFile(string name); /// /// Gets an containing the files of the diff --git a/src/Http/Http.Features/src/IHttpConnectionFeature.cs b/src/Http/Http.Features/src/IHttpConnectionFeature.cs index 932e9bfe2c1c..ee5323341c2a 100644 --- a/src/Http/Http.Features/src/IHttpConnectionFeature.cs +++ b/src/Http/Http.Features/src/IHttpConnectionFeature.cs @@ -18,12 +18,12 @@ public interface IHttpConnectionFeature /// /// The IPAddress of the client making the request. Note this may be for a proxy rather than the end user. /// - IPAddress RemoteIpAddress { get; set; } + IPAddress? RemoteIpAddress { get; set; } /// /// The local IPAddress on which the request was received. /// - IPAddress LocalIpAddress { get; set; } + IPAddress? LocalIpAddress { get; set; } /// /// The remote port of the client making the request. @@ -35,4 +35,4 @@ public interface IHttpConnectionFeature /// int LocalPort { get; set; } } -} \ No newline at end of file +} diff --git a/src/Http/Http.Features/src/IHttpResponseFeature.cs b/src/Http/Http.Features/src/IHttpResponseFeature.cs index a1f3819a06fe..4f6254586668 100644 --- a/src/Http/Http.Features/src/IHttpResponseFeature.cs +++ b/src/Http/Http.Features/src/IHttpResponseFeature.cs @@ -20,7 +20,7 @@ public interface IHttpResponseFeature /// /// The reason-phrase as defined in RFC 7230. Note this field is no longer supported by HTTP/2. /// - string ReasonPhrase { get; set; } + string? ReasonPhrase { get; set; } /// /// The response headers to send. Headers with multiple values will be emitted as multiple headers. diff --git a/src/Http/Http.Features/src/IItemsFeature.cs b/src/Http/Http.Features/src/IItemsFeature.cs index bea03e466c1b..26d2c6f544de 100644 --- a/src/Http/Http.Features/src/IItemsFeature.cs +++ b/src/Http/Http.Features/src/IItemsFeature.cs @@ -7,6 +7,6 @@ namespace Microsoft.AspNetCore.Http.Features { public interface IItemsFeature { - IDictionary Items { get; set; } + IDictionary Items { get; set; } } -} \ No newline at end of file +} diff --git a/src/Http/Http.Features/src/IRequestCookieCollection.cs b/src/Http/Http.Features/src/IRequestCookieCollection.cs index 6e9444ac8f0d..1f7c8e06a6c9 100644 --- a/src/Http/Http.Features/src/IRequestCookieCollection.cs +++ b/src/Http/Http.Features/src/IRequestCookieCollection.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.AspNetCore.Http { @@ -63,7 +64,7 @@ public interface IRequestCookieCollection : IEnumerable /// key is null. /// - bool TryGetValue(string key, out string value); + bool TryGetValue(string key, [MaybeNullWhen(false)] out string? value); /// /// Gets the value with the specified key. @@ -82,6 +83,6 @@ public interface IRequestCookieCollection : IEnumerable, as it will return string.Empty for missing entries /// rather than throwing an Exception. /// - string this[string key] { get; } + string? this[string key] { get; } } } diff --git a/src/Http/Http.Features/src/ITlsConnectionFeature.cs b/src/Http/Http.Features/src/ITlsConnectionFeature.cs index c34a3339d529..117f450d8f73 100644 --- a/src/Http/Http.Features/src/ITlsConnectionFeature.cs +++ b/src/Http/Http.Features/src/ITlsConnectionFeature.cs @@ -12,12 +12,12 @@ public interface ITlsConnectionFeature /// /// Synchronously retrieves the client certificate, if any. /// - X509Certificate2 ClientCertificate { get; set; } + X509Certificate2? ClientCertificate { get; set; } /// /// Asynchronously retrieves the client certificate, if any. /// /// - Task GetClientCertificateAsync(CancellationToken cancellationToken); + Task GetClientCertificateAsync(CancellationToken cancellationToken); } } diff --git a/src/Http/Http/ref/Microsoft.AspNetCore.Http.csproj b/src/Http/Http/ref/Microsoft.AspNetCore.Http.csproj index 5f804905ea03..0c2a70b72ae5 100644 --- a/src/Http/Http/ref/Microsoft.AspNetCore.Http.csproj +++ b/src/Http/Http/ref/Microsoft.AspNetCore.Http.csproj @@ -2,6 +2,7 @@ $(DefaultNetCoreTargetFramework) + annotations diff --git a/src/Http/Http/ref/Microsoft.AspNetCore.Http.netcoreapp.cs b/src/Http/Http/ref/Microsoft.AspNetCore.Http.netcoreapp.cs index 4cadeb67894d..721b0ca9c8df 100644 --- a/src/Http/Http/ref/Microsoft.AspNetCore.Http.netcoreapp.cs +++ b/src/Http/Http/ref/Microsoft.AspNetCore.Http.netcoreapp.cs @@ -8,7 +8,7 @@ public partial class ApplicationBuilder : Microsoft.AspNetCore.Builder.IApplicat public ApplicationBuilder(System.IServiceProvider serviceProvider) { } public ApplicationBuilder(System.IServiceProvider serviceProvider, object server) { } public System.IServiceProvider ApplicationServices { get { throw null; } set { } } - public System.Collections.Generic.IDictionary Properties { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } + public System.Collections.Generic.IDictionary Properties { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } public Microsoft.AspNetCore.Http.Features.IFeatureCollection ServerFeatures { get { throw null; } } public Microsoft.AspNetCore.Http.RequestDelegate Build() { throw null; } public Microsoft.AspNetCore.Builder.IApplicationBuilder New() { throw null; } @@ -26,7 +26,7 @@ public BindingAddress() { } public int Port { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } public string Scheme { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } public string UnixPipePath { get { throw null; } } - public override bool Equals(object obj) { throw null; } + public override bool Equals(object? obj) { throw null; } public override int GetHashCode() { throw null; } public static Microsoft.AspNetCore.Http.BindingAddress Parse(string address) { throw null; } public override string ToString() { throw null; } @@ -40,7 +40,7 @@ public DefaultHttpContext(Microsoft.AspNetCore.Http.Features.IFeatureCollection public Microsoft.AspNetCore.Http.Features.FormOptions FormOptions { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] public Microsoft.AspNetCore.Http.HttpContext HttpContext { get { throw null; } } - public override System.Collections.Generic.IDictionary Items { get { throw null; } set { } } + public override System.Collections.Generic.IDictionary Items { get { throw null; } set { } } public override Microsoft.AspNetCore.Http.HttpRequest Request { get { throw null; } } public override System.Threading.CancellationToken RequestAborted { get { throw null; } set { } } public override System.IServiceProvider RequestServices { get { throw null; } set { } } @@ -57,7 +57,7 @@ public void Uninitialize() { } public partial class FormCollection : Microsoft.AspNetCore.Http.IFormCollection, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable { public static readonly Microsoft.AspNetCore.Http.FormCollection Empty; - public FormCollection(System.Collections.Generic.Dictionary fields, Microsoft.AspNetCore.Http.IFormFileCollection files = null) { } + public FormCollection(System.Collections.Generic.Dictionary? fields, Microsoft.AspNetCore.Http.IFormFileCollection? files = null) { } public int Count { get { throw null; } } public Microsoft.AspNetCore.Http.IFormFileCollection Files { get { throw null; } } public Microsoft.Extensions.Primitives.StringValues this[string key] { get { throw null; } } @@ -96,14 +96,14 @@ public void CopyTo(System.IO.Stream target) { } public partial class FormFileCollection : System.Collections.Generic.List, Microsoft.AspNetCore.Http.IFormFileCollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.IEnumerable { public FormFileCollection() { } - public Microsoft.AspNetCore.Http.IFormFile this[string name] { get { throw null; } } - public Microsoft.AspNetCore.Http.IFormFile GetFile(string name) { throw null; } + public Microsoft.AspNetCore.Http.IFormFile? this[string name] { get { throw null; } } + public Microsoft.AspNetCore.Http.IFormFile? GetFile(string name) { throw null; } public System.Collections.Generic.IReadOnlyList GetFiles(string name) { throw null; } } public partial class HeaderDictionary : Microsoft.AspNetCore.Http.IHeaderDictionary, System.Collections.Generic.ICollection>, System.Collections.Generic.IDictionary, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable { public HeaderDictionary() { } - public HeaderDictionary(System.Collections.Generic.Dictionary store) { } + public HeaderDictionary(System.Collections.Generic.Dictionary? store) { } public HeaderDictionary(int capacity) { } public long? ContentLength { get { throw null; } set { } } public int Count { get { throw null; } } @@ -139,15 +139,15 @@ void System.Collections.IEnumerator.Reset() { } public partial class HttpContextAccessor : Microsoft.AspNetCore.Http.IHttpContextAccessor { public HttpContextAccessor() { } - public Microsoft.AspNetCore.Http.HttpContext HttpContext { get { throw null; } set { } } + public Microsoft.AspNetCore.Http.HttpContext? HttpContext { get { throw null; } set { } } } [System.ObsoleteAttribute("This is obsolete and will be removed in a future version. Use DefaultHttpContextFactory instead.")] public partial class HttpContextFactory : Microsoft.AspNetCore.Http.IHttpContextFactory { public HttpContextFactory(Microsoft.Extensions.Options.IOptions formOptions) { } - public HttpContextFactory(Microsoft.Extensions.Options.IOptions formOptions, Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor) { } + public HttpContextFactory(Microsoft.Extensions.Options.IOptions formOptions, Microsoft.AspNetCore.Http.IHttpContextAccessor? httpContextAccessor) { } public HttpContextFactory(Microsoft.Extensions.Options.IOptions formOptions, Microsoft.Extensions.DependencyInjection.IServiceScopeFactory serviceScopeFactory) { } - public HttpContextFactory(Microsoft.Extensions.Options.IOptions formOptions, Microsoft.Extensions.DependencyInjection.IServiceScopeFactory serviceScopeFactory, Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor) { } + public HttpContextFactory(Microsoft.Extensions.Options.IOptions formOptions, Microsoft.Extensions.DependencyInjection.IServiceScopeFactory serviceScopeFactory, Microsoft.AspNetCore.Http.IHttpContextAccessor? httpContextAccessor) { } public Microsoft.AspNetCore.Http.HttpContext Create(Microsoft.AspNetCore.Http.Features.IFeatureCollection featureCollection) { throw null; } public void Dispose(Microsoft.AspNetCore.Http.HttpContext httpContext) { } } @@ -161,7 +161,7 @@ public static void EnableBuffering(this Microsoft.AspNetCore.Http.HttpRequest re public partial class MiddlewareFactory : Microsoft.AspNetCore.Http.IMiddlewareFactory { public MiddlewareFactory(System.IServiceProvider serviceProvider) { } - public Microsoft.AspNetCore.Http.IMiddleware Create(System.Type middlewareType) { throw null; } + public Microsoft.AspNetCore.Http.IMiddleware? Create(System.Type middlewareType) { throw null; } public void Release(Microsoft.AspNetCore.Http.IMiddleware middleware) { } } public partial class QueryCollection : Microsoft.AspNetCore.Http.IQueryCollection, System.Collections.Generic.IEnumerable>, System.Collections.IEnumerable @@ -204,7 +204,7 @@ public partial class StreamResponseBodyFeature : Microsoft.AspNetCore.Http.Featu { public StreamResponseBodyFeature(System.IO.Stream stream) { } public StreamResponseBodyFeature(System.IO.Stream stream, Microsoft.AspNetCore.Http.Features.IHttpResponseBodyFeature priorFeature) { } - public Microsoft.AspNetCore.Http.Features.IHttpResponseBodyFeature PriorFeature { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } + public Microsoft.AspNetCore.Http.Features.IHttpResponseBodyFeature? PriorFeature { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } public System.IO.Stream Stream { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } } public System.IO.Pipelines.PipeWriter Writer { get { throw null; } } [System.Diagnostics.DebuggerStepThroughAttribute] @@ -228,7 +228,7 @@ public partial class FormFeature : Microsoft.AspNetCore.Http.Features.IFormFeatu public FormFeature(Microsoft.AspNetCore.Http.HttpRequest request) { } public FormFeature(Microsoft.AspNetCore.Http.HttpRequest request, Microsoft.AspNetCore.Http.Features.FormOptions options) { } public FormFeature(Microsoft.AspNetCore.Http.IFormCollection form) { } - public Microsoft.AspNetCore.Http.IFormCollection Form { get { throw null; } set { } } + public Microsoft.AspNetCore.Http.IFormCollection? Form { get { throw null; } set { } } public bool HasFormContentType { get { throw null; } } public Microsoft.AspNetCore.Http.IFormCollection ReadForm() { throw null; } public System.Threading.Tasks.Task ReadFormAsync() { throw null; } @@ -256,9 +256,9 @@ public partial class HttpConnectionFeature : Microsoft.AspNetCore.Http.Features. { public HttpConnectionFeature() { } public string ConnectionId { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } - public System.Net.IPAddress LocalIpAddress { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } + public System.Net.IPAddress? LocalIpAddress { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public int LocalPort { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } - public System.Net.IPAddress RemoteIpAddress { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } + public System.Net.IPAddress? RemoteIpAddress { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public int RemotePort { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } } public partial class HttpRequestFeature : Microsoft.AspNetCore.Http.Features.IHttpRequestFeature @@ -291,7 +291,7 @@ public HttpResponseFeature() { } public System.IO.Stream Body { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public virtual bool HasStarted { get { throw null; } } public Microsoft.AspNetCore.Http.IHeaderDictionary Headers { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } - public string ReasonPhrase { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } + public string? ReasonPhrase { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public int StatusCode { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public virtual void OnCompleted(System.Func callback, object state) { } public virtual void OnStarting(System.Func callback, object state) { } @@ -299,7 +299,7 @@ public virtual void OnStarting(System.Func public partial class ItemsFeature : Microsoft.AspNetCore.Http.Features.IItemsFeature { public ItemsFeature() { } - public System.Collections.Generic.IDictionary Items { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } + public System.Collections.Generic.IDictionary Items { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } } public partial class QueryFeature : Microsoft.AspNetCore.Http.Features.IQueryFeature { @@ -320,7 +320,7 @@ public RequestCookiesFeature(Microsoft.AspNetCore.Http.IRequestCookieCollection } public partial class RequestServicesFeature : Microsoft.AspNetCore.Http.Features.IServiceProvidersFeature, System.IAsyncDisposable, System.IDisposable { - public RequestServicesFeature(Microsoft.AspNetCore.Http.HttpContext context, Microsoft.Extensions.DependencyInjection.IServiceScopeFactory scopeFactory) { } + public RequestServicesFeature(Microsoft.AspNetCore.Http.HttpContext context, Microsoft.Extensions.DependencyInjection.IServiceScopeFactory? scopeFactory) { } public System.IServiceProvider RequestServices { get { throw null; } set { } } public void Dispose() { } public System.Threading.Tasks.ValueTask DisposeAsync() { throw null; } @@ -328,7 +328,7 @@ public void Dispose() { } public partial class ResponseCookiesFeature : Microsoft.AspNetCore.Http.Features.IResponseCookiesFeature { public ResponseCookiesFeature(Microsoft.AspNetCore.Http.Features.IFeatureCollection features) { } - public ResponseCookiesFeature(Microsoft.AspNetCore.Http.Features.IFeatureCollection features, Microsoft.Extensions.ObjectPool.ObjectPool builderPool) { } + public ResponseCookiesFeature(Microsoft.AspNetCore.Http.Features.IFeatureCollection features, Microsoft.Extensions.ObjectPool.ObjectPool? builderPool) { } public Microsoft.AspNetCore.Http.IResponseCookies Cookies { get { throw null; } } } public partial class RouteValuesFeature : Microsoft.AspNetCore.Http.Features.IRouteValuesFeature @@ -344,8 +344,8 @@ public ServiceProvidersFeature() { } public partial class TlsConnectionFeature : Microsoft.AspNetCore.Http.Features.ITlsConnectionFeature { public TlsConnectionFeature() { } - public System.Security.Cryptography.X509Certificates.X509Certificate2 ClientCertificate { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } - public System.Threading.Tasks.Task GetClientCertificateAsync(System.Threading.CancellationToken cancellationToken) { throw null; } + public System.Security.Cryptography.X509Certificates.X509Certificate2? ClientCertificate { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } + public System.Threading.Tasks.Task GetClientCertificateAsync(System.Threading.CancellationToken cancellationToken) { throw null; } } } namespace Microsoft.AspNetCore.Http.Features.Authentication @@ -353,7 +353,7 @@ namespace Microsoft.AspNetCore.Http.Features.Authentication public partial class HttpAuthenticationFeature : Microsoft.AspNetCore.Http.Features.Authentication.IHttpAuthenticationFeature { public HttpAuthenticationFeature() { } - public System.Security.Claims.ClaimsPrincipal User { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } + public System.Security.Claims.ClaimsPrincipal? User { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } } } namespace Microsoft.Extensions.DependencyInjection diff --git a/src/Http/Http/src/BindingAddress.cs b/src/Http/Http/src/BindingAddress.cs index 38e552b3819d..d39a7be54f55 100644 --- a/src/Http/Http/src/BindingAddress.cs +++ b/src/Http/Http/src/BindingAddress.cs @@ -12,10 +12,10 @@ public class BindingAddress { private const string UnixPipeHostPrefix = "unix:/"; - public string Host { get; private set; } - public string PathBase { get; private set; } + public string Host { get; private set; } = default!; + public string PathBase { get; private set; } = default!; public int Port { get; internal set; } - public string Scheme { get; private set; } + public string Scheme { get; private set; } = default!; public bool IsUnixPipe { @@ -62,7 +62,7 @@ public override int GetHashCode() return ToString().GetHashCode(); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { var other = obj as BindingAddress; if (other == null) diff --git a/src/Http/Http/src/Builder/ApplicationBuilder.cs b/src/Http/Http/src/Builder/ApplicationBuilder.cs index 071e04dc8c70..f7cc65897c8a 100644 --- a/src/Http/Http/src/Builder/ApplicationBuilder.cs +++ b/src/Http/Http/src/Builder/ApplicationBuilder.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; @@ -20,7 +21,7 @@ public class ApplicationBuilder : IApplicationBuilder public ApplicationBuilder(IServiceProvider serviceProvider) { - Properties = new Dictionary(StringComparer.Ordinal); + Properties = new Dictionary(StringComparer.Ordinal); ApplicationServices = serviceProvider; } @@ -32,14 +33,14 @@ public ApplicationBuilder(IServiceProvider serviceProvider, object server) private ApplicationBuilder(ApplicationBuilder builder) { - Properties = new CopyOnWriteDictionary(builder.Properties, StringComparer.Ordinal); + Properties = new CopyOnWriteDictionary(builder.Properties, StringComparer.Ordinal); } public IServiceProvider ApplicationServices { get { - return GetProperty(ApplicationServicesKey); + return GetProperty(ApplicationServicesKey)!; } set { @@ -51,16 +52,16 @@ public IFeatureCollection ServerFeatures { get { - return GetProperty(ServerFeaturesKey); + return GetProperty(ServerFeaturesKey)!; } } - public IDictionary Properties { get; } + public IDictionary Properties { get; } + [return: MaybeNull] private T GetProperty(string key) { - object value; - return Properties.TryGetValue(key, out value) ? (T)value : default(T); + return Properties.TryGetValue(key, out var value) ? (T)value : default(T); } private void SetProperty(string key, T value) @@ -90,7 +91,7 @@ public RequestDelegate Build() if (endpointRequestDelegate != null) { var message = - $"The request reached the end of the pipeline without executing the endpoint: '{endpoint.DisplayName}'. " + + $"The request reached the end of the pipeline without executing the endpoint: '{endpoint!.DisplayName}'. " + $"Please register the EndpointMiddleware using '{nameof(IApplicationBuilder)}.UseEndpoints(...)' if using " + $"routing."; throw new InvalidOperationException(message); diff --git a/src/Http/Http/src/DefaultHttpContext.cs b/src/Http/Http/src/DefaultHttpContext.cs index 59c7739e869b..d965869bbc58 100644 --- a/src/Http/Http/src/DefaultHttpContext.cs +++ b/src/Http/Http/src/DefaultHttpContext.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Security.Claims; using System.Threading; @@ -24,7 +25,7 @@ public sealed class DefaultHttpContext : HttpContext private readonly static Func _newHttpAuthenticationFeature = f => new HttpAuthenticationFeature(); private readonly static Func _newHttpRequestLifetimeFeature = f => new HttpRequestLifetimeFeature(); private readonly static Func _newSessionFeature = f => new DefaultSessionFeature(); - private readonly static Func _nullSessionFeature = f => null; + private readonly static Func _nullSessionFeature = f => null; private readonly static Func _newHttpRequestIdentifierFeature = f => new HttpRequestIdentifierFeature(); private FeatureReferences _features; @@ -32,8 +33,8 @@ public sealed class DefaultHttpContext : HttpContext private readonly DefaultHttpRequest _request; private readonly DefaultHttpResponse _response; - private DefaultConnectionInfo _connection; - private DefaultWebSocketManager _websockets; + private DefaultConnectionInfo? _connection; + private DefaultWebSocketManager? _websockets; /// /// Initializes a new instance of the class. @@ -92,7 +93,7 @@ public void Uninitialize() /// /// /// - public FormOptions FormOptions { get; set; } + public FormOptions FormOptions { get; set; } = default!; /// /// Gets or sets the for this instance. @@ -100,29 +101,28 @@ public void Uninitialize() /// /// /// - public IServiceScopeFactory ServiceScopeFactory { get; set; } + public IServiceScopeFactory ServiceScopeFactory { get; set; } = default!; private IItemsFeature ItemsFeature => - _features.Fetch(ref _features.Cache.Items, _newItemsFeature); + _features.Fetch(ref _features.Cache.Items, _newItemsFeature)!; private IServiceProvidersFeature ServiceProvidersFeature => - _features.Fetch(ref _features.Cache.ServiceProviders, this, _newServiceProvidersFeature); + _features.Fetch(ref _features.Cache.ServiceProviders, this, _newServiceProvidersFeature)!; private IHttpAuthenticationFeature HttpAuthenticationFeature => - _features.Fetch(ref _features.Cache.Authentication, _newHttpAuthenticationFeature); + _features.Fetch(ref _features.Cache.Authentication, _newHttpAuthenticationFeature)!; private IHttpRequestLifetimeFeature LifetimeFeature => - _features.Fetch(ref _features.Cache.Lifetime, _newHttpRequestLifetimeFeature); + _features.Fetch(ref _features.Cache.Lifetime, _newHttpRequestLifetimeFeature)!; private ISessionFeature SessionFeature => - _features.Fetch(ref _features.Cache.Session, _newSessionFeature); + _features.Fetch(ref _features.Cache.Session, _newSessionFeature)!; - private ISessionFeature SessionFeatureOrNull => + private ISessionFeature? SessionFeatureOrNull => _features.Fetch(ref _features.Cache.Session, _nullSessionFeature); - private IHttpRequestIdentifierFeature RequestIdentifierFeature => - _features.Fetch(ref _features.Cache.RequestIdentifier, _newHttpRequestIdentifierFeature); + _features.Fetch(ref _features.Cache.RequestIdentifier, _newHttpRequestIdentifierFeature)!; /// public override IFeatureCollection Features => _features.Collection ?? ContextDisposed(); @@ -156,7 +156,7 @@ public override ClaimsPrincipal User } /// - public override IDictionary Items + public override IDictionary Items { get { return ItemsFeature.Items; } set { ItemsFeature.Items = value; } @@ -221,6 +221,7 @@ private static IFeatureCollection ContextDisposed() return null; } + [DoesNotReturn] private static void ThrowContextDisposed() { throw new ObjectDisposedException(nameof(HttpContext), $"Request has finished and {nameof(HttpContext)} disposed."); @@ -228,12 +229,12 @@ private static void ThrowContextDisposed() struct FeatureInterfaces { - public IItemsFeature Items; - public IServiceProvidersFeature ServiceProviders; - public IHttpAuthenticationFeature Authentication; - public IHttpRequestLifetimeFeature Lifetime; - public ISessionFeature Session; - public IHttpRequestIdentifierFeature RequestIdentifier; + public IItemsFeature? Items; + public IServiceProvidersFeature? ServiceProviders; + public IHttpAuthenticationFeature? Authentication; + public IHttpRequestLifetimeFeature? Lifetime; + public ISessionFeature? Session; + public IHttpRequestIdentifierFeature? RequestIdentifier; } } } diff --git a/src/Http/Http/src/Features/Authentication/HttpAuthenticationFeature.cs b/src/Http/Http/src/Features/Authentication/HttpAuthenticationFeature.cs index b2ba477f8600..8fb80cdda138 100644 --- a/src/Http/Http/src/Features/Authentication/HttpAuthenticationFeature.cs +++ b/src/Http/Http/src/Features/Authentication/HttpAuthenticationFeature.cs @@ -7,10 +7,6 @@ namespace Microsoft.AspNetCore.Http.Features.Authentication { public class HttpAuthenticationFeature : IHttpAuthenticationFeature { - public ClaimsPrincipal User - { - get; - set; - } + public ClaimsPrincipal? User { get; set; } } } diff --git a/src/Http/Http/src/Features/DefaultSessionFeature.cs b/src/Http/Http/src/Features/DefaultSessionFeature.cs index 6790133467c8..345d7357926b 100644 --- a/src/Http/Http/src/Features/DefaultSessionFeature.cs +++ b/src/Http/Http/src/Features/DefaultSessionFeature.cs @@ -9,6 +9,6 @@ namespace Microsoft.AspNetCore.Http.Features /// public class DefaultSessionFeature : ISessionFeature { - public ISession Session { get; set; } + public ISession Session { get; set; } = default!; } } diff --git a/src/Http/Http/src/Features/FormFeature.cs b/src/Http/Http/src/Features/FormFeature.cs index e4a849885111..c5a288e0bb10 100644 --- a/src/Http/Http/src/Features/FormFeature.cs +++ b/src/Http/Http/src/Features/FormFeature.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; using System.Threading; @@ -16,8 +17,8 @@ public class FormFeature : IFormFeature { private readonly HttpRequest _request; private readonly FormOptions _options; - private Task _parsedFormTask; - private IFormCollection _form; + private Task? _parsedFormTask; + private IFormCollection? _form; public FormFeature(IFormCollection form) { @@ -27,7 +28,10 @@ public FormFeature(IFormCollection form) } Form = form; + _request = default!; + _options = FormOptions.Default; } + public FormFeature(HttpRequest request) : this(request, FormOptions.Default) { @@ -48,12 +52,11 @@ public FormFeature(HttpRequest request, FormOptions options) _options = options; } - private MediaTypeHeaderValue ContentType + private MediaTypeHeaderValue? ContentType { get { - MediaTypeHeaderValue mt; - MediaTypeHeaderValue.TryParse(_request.ContentType, out mt); + MediaTypeHeaderValue.TryParse(_request.ContentType, out var mt); return mt; } } @@ -73,7 +76,7 @@ public bool HasFormContentType } } - public IFormCollection Form + public IFormCollection? Form { get { return _form; } set @@ -138,11 +141,11 @@ private async Task InnerReadFormAsync(CancellationToken cancell _request.EnableRewind(_options.MemoryBufferThreshold, _options.BufferBodyLengthLimit); } - FormCollection formFields = null; - FormFileCollection files = null; + FormCollection? formFields = null; + FormFileCollection? files = null; // Some of these code paths use StreamReader which does not support cancellation tokens. - using (cancellationToken.Register((state) => ((HttpContext)state).Abort(), _request.HttpContext)) + using (cancellationToken.Register((state) => ((HttpContext)state!).Abort(), _request.HttpContext)) { var contentType = ContentType; // Check the content-type @@ -270,7 +273,7 @@ private async Task InnerReadFormAsync(CancellationToken cancell return Form; } - private Encoding FilterEncoding(Encoding encoding) + private static Encoding FilterEncoding(Encoding? encoding) { // UTF-7 is insecure and should not be honored. UTF-8 will succeed for most cases. if (encoding == null || Encoding.UTF7.Equals(encoding)) @@ -280,13 +283,13 @@ private Encoding FilterEncoding(Encoding encoding) return encoding; } - private bool HasApplicationFormContentType(MediaTypeHeaderValue contentType) + private bool HasApplicationFormContentType([NotNullWhen(true)] MediaTypeHeaderValue? contentType) { // Content-Type: application/x-www-form-urlencoded; charset=utf-8 return contentType != null && contentType.MediaType.Equals("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase); } - private bool HasMultipartFormContentType(MediaTypeHeaderValue contentType) + private bool HasMultipartFormContentType([NotNullWhen(true)] MediaTypeHeaderValue? contentType) { // Content-Type: multipart/form-data; boundary=----WebKitFormBoundarymx2fSWqWSd0OxQqq return contentType != null && contentType.MediaType.Equals("multipart/form-data", StringComparison.OrdinalIgnoreCase); diff --git a/src/Http/Http/src/Features/HttpConnectionFeature.cs b/src/Http/Http/src/Features/HttpConnectionFeature.cs index 2e8d5b0a1c6d..47c1af821f60 100644 --- a/src/Http/Http/src/Features/HttpConnectionFeature.cs +++ b/src/Http/Http/src/Features/HttpConnectionFeature.cs @@ -7,14 +7,14 @@ namespace Microsoft.AspNetCore.Http.Features { public class HttpConnectionFeature : IHttpConnectionFeature { - public string ConnectionId { get; set; } + public string ConnectionId { get; set; } = default!; - public IPAddress LocalIpAddress { get; set; } + public IPAddress? LocalIpAddress { get; set; } public int LocalPort { get; set; } - public IPAddress RemoteIpAddress { get; set; } + public IPAddress? RemoteIpAddress { get; set; } public int RemotePort { get; set; } } -} \ No newline at end of file +} diff --git a/src/Http/Http/src/Features/HttpRequestIdentifierFeature.cs b/src/Http/Http/src/Features/HttpRequestIdentifierFeature.cs index dd4cde7bf873..5edee30fd6ba 100644 --- a/src/Http/Http/src/Features/HttpRequestIdentifierFeature.cs +++ b/src/Http/Http/src/Features/HttpRequestIdentifierFeature.cs @@ -15,7 +15,7 @@ public class HttpRequestIdentifierFeature : IHttpRequestIdentifierFeature // for a roughly increasing _requestId over restarts private static long _requestId = DateTime.UtcNow.Ticks; - private string _id = null; + private string? _id = null; public string TraceIdentifier { diff --git a/src/Http/Http/src/Features/HttpResponseFeature.cs b/src/Http/Http/src/Features/HttpResponseFeature.cs index a02a79088bcd..1b785df0d847 100644 --- a/src/Http/Http/src/Features/HttpResponseFeature.cs +++ b/src/Http/Http/src/Features/HttpResponseFeature.cs @@ -18,7 +18,7 @@ public HttpResponseFeature() public int StatusCode { get; set; } - public string ReasonPhrase { get; set; } + public string? ReasonPhrase { get; set; } public IHeaderDictionary Headers { get; set; } diff --git a/src/Http/Http/src/Features/ItemsFeature.cs b/src/Http/Http/src/Features/ItemsFeature.cs index 6e8cdb982e5e..37e2c80f8e45 100644 --- a/src/Http/Http/src/Features/ItemsFeature.cs +++ b/src/Http/Http/src/Features/ItemsFeature.cs @@ -12,6 +12,6 @@ public ItemsFeature() Items = new ItemsDictionary(); } - public IDictionary Items { get; set; } + public IDictionary Items { get; set; } } } diff --git a/src/Http/Http/src/Features/QueryFeature.cs b/src/Http/Http/src/Features/QueryFeature.cs index 985aceb6a511..885a577cae49 100644 --- a/src/Http/Http/src/Features/QueryFeature.cs +++ b/src/Http/Http/src/Features/QueryFeature.cs @@ -9,12 +9,12 @@ namespace Microsoft.AspNetCore.Http.Features public class QueryFeature : IQueryFeature { // Lambda hoisted to static readonly field to improve inlining https://github.com/dotnet/roslyn/issues/13624 - private readonly static Func _nullRequestFeature = f => null; + private readonly static Func _nullRequestFeature = f => null; private FeatureReferences _features; - private string _original; - private IQueryCollection _parsedValues; + private string? _original; + private IQueryCollection? _parsedValues; public QueryFeature(IQueryCollection query) { @@ -37,7 +37,7 @@ public QueryFeature(IFeatureCollection features) } private IHttpRequestFeature HttpRequestFeature => - _features.Fetch(ref _features.Cache, _nullRequestFeature); + _features.Fetch(ref _features.Cache, _nullRequestFeature)!; public IQueryCollection Query { diff --git a/src/Http/Http/src/Features/RequestBodyPipeFeature.cs b/src/Http/Http/src/Features/RequestBodyPipeFeature.cs index 61154c5f0ae5..c9c86c1f1083 100644 --- a/src/Http/Http/src/Features/RequestBodyPipeFeature.cs +++ b/src/Http/Http/src/Features/RequestBodyPipeFeature.cs @@ -10,8 +10,8 @@ namespace Microsoft.AspNetCore.Http.Features { public class RequestBodyPipeFeature : IRequestBodyPipeFeature { - private PipeReader _internalPipeReader; - private Stream _streamInstanceWhenWrapped; + private PipeReader? _internalPipeReader; + private Stream? _streamInstanceWhenWrapped; private HttpContext _context; public RequestBodyPipeFeature(HttpContext context) diff --git a/src/Http/Http/src/Features/RequestCookiesFeature.cs b/src/Http/Http/src/Features/RequestCookiesFeature.cs index cde35893f0e5..a89bb99f5df5 100644 --- a/src/Http/Http/src/Features/RequestCookiesFeature.cs +++ b/src/Http/Http/src/Features/RequestCookiesFeature.cs @@ -11,11 +11,11 @@ namespace Microsoft.AspNetCore.Http.Features public class RequestCookiesFeature : IRequestCookiesFeature { // Lambda hoisted to static readonly field to improve inlining https://github.com/dotnet/roslyn/issues/13624 - private readonly static Func _nullRequestFeature = f => null; + private readonly static Func _nullRequestFeature = f => null; private FeatureReferences _features; private StringValues _original; - private IRequestCookieCollection _parsedValues; + private IRequestCookieCollection? _parsedValues; public RequestCookiesFeature(IRequestCookieCollection cookies) { @@ -38,7 +38,7 @@ public RequestCookiesFeature(IFeatureCollection features) } private IHttpRequestFeature HttpRequestFeature => - _features.Fetch(ref _features.Cache, _nullRequestFeature); + _features.Fetch(ref _features.Cache, _nullRequestFeature)!; public IRequestCookieCollection Cookies { diff --git a/src/Http/Http/src/Features/RequestServicesFeature.cs b/src/Http/Http/src/Features/RequestServicesFeature.cs index 5a1a714998c2..8de4af3e7ac6 100644 --- a/src/Http/Http/src/Features/RequestServicesFeature.cs +++ b/src/Http/Http/src/Features/RequestServicesFeature.cs @@ -9,13 +9,13 @@ namespace Microsoft.AspNetCore.Http.Features { public class RequestServicesFeature : IServiceProvidersFeature, IDisposable, IAsyncDisposable { - private readonly IServiceScopeFactory _scopeFactory; - private IServiceProvider _requestServices; - private IServiceScope _scope; + private readonly IServiceScopeFactory? _scopeFactory; + private IServiceProvider? _requestServices; + private IServiceScope? _scope; private bool _requestServicesSet; private readonly HttpContext _context; - public RequestServicesFeature(HttpContext context, IServiceScopeFactory scopeFactory) + public RequestServicesFeature(HttpContext context, IServiceScopeFactory? scopeFactory) { _context = context; _scopeFactory = scopeFactory; @@ -32,7 +32,7 @@ public IServiceProvider RequestServices _requestServices = _scope.ServiceProvider; _requestServicesSet = true; } - return _requestServices; + return _requestServices!; } set diff --git a/src/Http/Http/src/Features/ResponseCookiesFeature.cs b/src/Http/Http/src/Features/ResponseCookiesFeature.cs index 64a11645b66a..f7a6f2d40138 100644 --- a/src/Http/Http/src/Features/ResponseCookiesFeature.cs +++ b/src/Http/Http/src/Features/ResponseCookiesFeature.cs @@ -13,10 +13,10 @@ namespace Microsoft.AspNetCore.Http.Features public class ResponseCookiesFeature : IResponseCookiesFeature { // Lambda hoisted to static readonly field to improve inlining https://github.com/dotnet/roslyn/issues/13624 - private readonly static Func _nullResponseFeature = f => null; + private readonly static Func _nullResponseFeature = f => null; private FeatureReferences _features; - private IResponseCookies _cookiesCollection; + private IResponseCookies? _cookiesCollection; /// /// Initializes a new instance. @@ -38,7 +38,7 @@ public ResponseCookiesFeature(IFeatureCollection features) /// and the . /// /// The , if available. - public ResponseCookiesFeature(IFeatureCollection features, ObjectPool builderPool) + public ResponseCookiesFeature(IFeatureCollection features, ObjectPool? builderPool) { if (features == null) { @@ -48,7 +48,7 @@ public ResponseCookiesFeature(IFeatureCollection features, ObjectPool _features.Fetch(ref _features.Cache, _nullResponseFeature); + private IHttpResponseFeature HttpResponseFeature => _features.Fetch(ref _features.Cache, _nullResponseFeature)!; /// public IResponseCookies Cookies @@ -58,7 +58,7 @@ public IResponseCookies Cookies if (_cookiesCollection == null) { var headers = HttpResponseFeature.Headers; - _cookiesCollection = new ResponseCookies(headers, null); + _cookiesCollection = new ResponseCookies(headers); } return _cookiesCollection; diff --git a/src/Http/Http/src/Features/RouteValuesFeature.cs b/src/Http/Http/src/Features/RouteValuesFeature.cs index e4a459e99168..1abde0a5e62e 100644 --- a/src/Http/Http/src/Features/RouteValuesFeature.cs +++ b/src/Http/Http/src/Features/RouteValuesFeature.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Routing; @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Http.Features /// public class RouteValuesFeature : IRouteValuesFeature { - private RouteValueDictionary _routeValues; + private RouteValueDictionary? _routeValues; /// /// Gets or sets the associated with the currrent @@ -31,4 +31,4 @@ public RouteValueDictionary RouteValues set => _routeValues = value; } } -} \ No newline at end of file +} diff --git a/src/Http/Http/src/Features/ServiceProvidersFeature.cs b/src/Http/Http/src/Features/ServiceProvidersFeature.cs index d1cf4e6cba83..815c957e43f8 100644 --- a/src/Http/Http/src/Features/ServiceProvidersFeature.cs +++ b/src/Http/Http/src/Features/ServiceProvidersFeature.cs @@ -7,6 +7,6 @@ namespace Microsoft.AspNetCore.Http.Features { public class ServiceProvidersFeature : IServiceProvidersFeature { - public IServiceProvider RequestServices { get; set; } + public IServiceProvider RequestServices { get; set; } = default!; } -} \ No newline at end of file +} diff --git a/src/Http/Http/src/Features/TlsConnectionFeature.cs b/src/Http/Http/src/Features/TlsConnectionFeature.cs index f9bfcdef7fee..cf4a36c40a6d 100644 --- a/src/Http/Http/src/Features/TlsConnectionFeature.cs +++ b/src/Http/Http/src/Features/TlsConnectionFeature.cs @@ -9,11 +9,11 @@ namespace Microsoft.AspNetCore.Http.Features { public class TlsConnectionFeature : ITlsConnectionFeature { - public X509Certificate2 ClientCertificate { get; set; } + public X509Certificate2? ClientCertificate { get; set; } - public Task GetClientCertificateAsync(CancellationToken cancellationToken) + public Task GetClientCertificateAsync(CancellationToken cancellationToken) { return Task.FromResult(ClientCertificate); } } -} \ No newline at end of file +} diff --git a/src/Http/Http/src/FormCollection.cs b/src/Http/Http/src/FormCollection.cs index b0e8c8e51ddf..fa8318d0b980 100644 --- a/src/Http/Http/src/FormCollection.cs +++ b/src/Http/Http/src/FormCollection.cs @@ -23,14 +23,14 @@ public class FormCollection : IFormCollection private static IFormFileCollection EmptyFiles = new FormFileCollection(); - private IFormFileCollection _files; + private IFormFileCollection? _files; private FormCollection() { // For static Empty } - public FormCollection(Dictionary fields, IFormFileCollection files = null) + public FormCollection(Dictionary? fields, IFormFileCollection? files = null) { // can be null Store = fields; @@ -46,7 +46,7 @@ public IFormFileCollection Files private set { _files = value; } } - private Dictionary Store { get; set; } + private Dictionary? Store { get; set; } /// /// Get or sets the associated value from the collection as a single string. diff --git a/src/Http/Http/src/FormFile.cs b/src/Http/Http/src/FormFile.cs index c861962919c7..eaeb5f1cd517 100644 --- a/src/Http/Http/src/FormFile.cs +++ b/src/Http/Http/src/FormFile.cs @@ -50,7 +50,7 @@ public string ContentType /// /// Gets the header dictionary of the uploaded file. /// - public IHeaderDictionary Headers { get; set; } + public IHeaderDictionary Headers { get; set; } = default!; /// /// Gets the file length in bytes. diff --git a/src/Http/Http/src/FormFileCollection.cs b/src/Http/Http/src/FormFileCollection.cs index 4672567a1724..afabe7ceb850 100644 --- a/src/Http/Http/src/FormFileCollection.cs +++ b/src/Http/Http/src/FormFileCollection.cs @@ -12,10 +12,10 @@ namespace Microsoft.AspNetCore.Http public class FormFileCollection : List, IFormFileCollection { /// - public IFormFile this[string name] => GetFile(name); + public IFormFile? this[string name] => GetFile(name); /// - public IFormFile GetFile(string name) + public IFormFile? GetFile(string name) { foreach (var file in this) { diff --git a/src/Http/Http/src/HeaderDictionary.cs b/src/Http/Http/src/HeaderDictionary.cs index 438302025471..275f79911e51 100644 --- a/src/Http/Http/src/HeaderDictionary.cs +++ b/src/Http/Http/src/HeaderDictionary.cs @@ -4,6 +4,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.Primitives; using Microsoft.Net.Http.Headers; @@ -25,7 +26,7 @@ public HeaderDictionary() { } - public HeaderDictionary(Dictionary store) + public HeaderDictionary(Dictionary? store) { Store = store; } @@ -35,8 +36,9 @@ public HeaderDictionary(int capacity) EnsureStore(capacity); } - private Dictionary Store { get; set; } + private Dictionary? Store { get; set; } + [MemberNotNull(nameof(Store))] private void EnsureStore(int capacity) { if (Store == null) @@ -93,7 +95,7 @@ public StringValues this[string key] /// StringValues IDictionary.this[string key] { - get { return Store[key]; } + get { return this[key]; } set { ThrowIfReadOnly(); diff --git a/src/Http/Http/src/HttpContextAccessor.cs b/src/Http/Http/src/HttpContextAccessor.cs index 762e454c4232..553b512dbb23 100644 --- a/src/Http/Http/src/HttpContextAccessor.cs +++ b/src/Http/Http/src/HttpContextAccessor.cs @@ -13,7 +13,7 @@ public class HttpContextAccessor : IHttpContextAccessor private static AsyncLocal _httpContextCurrent = new AsyncLocal(); /// - public HttpContext HttpContext + public HttpContext? HttpContext { get { @@ -39,7 +39,7 @@ public HttpContext HttpContext private class HttpContextHolder { - public HttpContext Context; + public HttpContext? Context; } } } diff --git a/src/Http/Http/src/HttpContextFactory.cs b/src/Http/Http/src/HttpContextFactory.cs index edddc658d3ec..0c97b14922a4 100644 --- a/src/Http/Http/src/HttpContextFactory.cs +++ b/src/Http/Http/src/HttpContextFactory.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Http [Obsolete("This is obsolete and will be removed in a future version. Use DefaultHttpContextFactory instead.")] public class HttpContextFactory : IHttpContextFactory { - private readonly IHttpContextAccessor _httpContextAccessor; + private readonly IHttpContextAccessor? _httpContextAccessor; private readonly FormOptions _formOptions; private readonly IServiceScopeFactory _serviceScopeFactory; @@ -23,7 +23,7 @@ public class HttpContextFactory : IHttpContextFactory /// /// Options to set when instantianting the HTTP context object. public HttpContextFactory(IOptions formOptions) - : this(formOptions, serviceScopeFactory: null) + : this(formOptions, serviceScopeFactory: null!) { } @@ -42,8 +42,8 @@ public HttpContextFactory(IOptions formOptions, IServiceScopeFactor /// /// Options to set when instantianting the HTTP context object. /// Object to be used to access the HTTP context instance. - public HttpContextFactory(IOptions formOptions, IHttpContextAccessor httpContextAccessor) - : this(formOptions, serviceScopeFactory: null, httpContextAccessor: httpContextAccessor) + public HttpContextFactory(IOptions formOptions, IHttpContextAccessor? httpContextAccessor) + : this(formOptions, serviceScopeFactory: null!, httpContextAccessor: httpContextAccessor) { } @@ -53,7 +53,7 @@ public HttpContextFactory(IOptions formOptions, IHttpContextAccesso /// Options to set when instantianting the HTTP context object. /// Factory object used to create the service scope for the HTTP context. /// Options to set when instantianting the Default HTTP context object. - public HttpContextFactory(IOptions formOptions, IServiceScopeFactory serviceScopeFactory, IHttpContextAccessor httpContextAccessor) + public HttpContextFactory(IOptions formOptions, IServiceScopeFactory serviceScopeFactory, IHttpContextAccessor? httpContextAccessor) { if (formOptions == null) { diff --git a/src/Http/Http/src/Internal/DefaultConnectionInfo.cs b/src/Http/Http/src/Internal/DefaultConnectionInfo.cs index 2c1dcfd11497..75064295ae6b 100644 --- a/src/Http/Http/src/Internal/DefaultConnectionInfo.cs +++ b/src/Http/Http/src/Internal/DefaultConnectionInfo.cs @@ -39,10 +39,10 @@ public void Uninitialize() } private IHttpConnectionFeature HttpConnectionFeature => - _features.Fetch(ref _features.Cache.Connection, _newHttpConnectionFeature); + _features.Fetch(ref _features.Cache.Connection, _newHttpConnectionFeature)!; private ITlsConnectionFeature TlsConnectionFeature=> - _features.Fetch(ref _features.Cache.TlsConnection, _newTlsConnectionFeature); + _features.Fetch(ref _features.Cache.TlsConnection, _newTlsConnectionFeature)!; /// public override string Id @@ -51,7 +51,7 @@ public override string Id set { HttpConnectionFeature.ConnectionId = value; } } - public override IPAddress RemoteIpAddress + public override IPAddress? RemoteIpAddress { get { return HttpConnectionFeature.RemoteIpAddress; } set { HttpConnectionFeature.RemoteIpAddress = value; } @@ -63,7 +63,7 @@ public override int RemotePort set { HttpConnectionFeature.RemotePort = value; } } - public override IPAddress LocalIpAddress + public override IPAddress? LocalIpAddress { get { return HttpConnectionFeature.LocalIpAddress; } set { HttpConnectionFeature.LocalIpAddress = value; } @@ -75,21 +75,21 @@ public override int LocalPort set { HttpConnectionFeature.LocalPort = value; } } - public override X509Certificate2 ClientCertificate + public override X509Certificate2? ClientCertificate { get { return TlsConnectionFeature.ClientCertificate; } set { TlsConnectionFeature.ClientCertificate = value; } } - public override Task GetClientCertificateAsync(CancellationToken cancellationToken = default) + public override Task GetClientCertificateAsync(CancellationToken cancellationToken = default) { return TlsConnectionFeature.GetClientCertificateAsync(cancellationToken); } struct FeatureInterfaces { - public IHttpConnectionFeature Connection; - public ITlsConnectionFeature TlsConnection; + public IHttpConnectionFeature? Connection; + public ITlsConnectionFeature? TlsConnection; } } } diff --git a/src/Http/Http/src/Internal/DefaultHttpRequest.cs b/src/Http/Http/src/Internal/DefaultHttpRequest.cs index 29312a5616e6..80ba2bd63104 100644 --- a/src/Http/Http/src/Internal/DefaultHttpRequest.cs +++ b/src/Http/Http/src/Internal/DefaultHttpRequest.cs @@ -18,8 +18,8 @@ internal sealed class DefaultHttpRequest : HttpRequest private const string Https = "https"; // Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624 - private readonly static Func _nullRequestFeature = f => null; - private readonly static Func _newQueryFeature = f => new QueryFeature(f); + private readonly static Func _nullRequestFeature = f => null; + private readonly static Func _newQueryFeature = f => new QueryFeature(f); private readonly static Func _newFormFeature = r => new FormFeature(r, r._context.FormOptions ?? FormOptions.Default); private readonly static Func _newRequestCookiesFeature = f => new RequestCookiesFeature(f); private readonly static Func _newRouteValuesFeature = f => new RouteValuesFeature(); @@ -52,39 +52,39 @@ public void Uninitialize() public override HttpContext HttpContext => _context; private IHttpRequestFeature HttpRequestFeature => - _features.Fetch(ref _features.Cache.Request, _nullRequestFeature); + _features.Fetch(ref _features.Cache.Request, _nullRequestFeature)!; private IQueryFeature QueryFeature => - _features.Fetch(ref _features.Cache.Query, _newQueryFeature); + _features.Fetch(ref _features.Cache.Query, _newQueryFeature)!; private IFormFeature FormFeature => - _features.Fetch(ref _features.Cache.Form, this, _newFormFeature); + _features.Fetch(ref _features.Cache.Form, this, _newFormFeature)!; private IRequestCookiesFeature RequestCookiesFeature => - _features.Fetch(ref _features.Cache.Cookies, _newRequestCookiesFeature); + _features.Fetch(ref _features.Cache.Cookies, _newRequestCookiesFeature)!; private IRouteValuesFeature RouteValuesFeature => - _features.Fetch(ref _features.Cache.RouteValues, _newRouteValuesFeature); + _features.Fetch(ref _features.Cache.RouteValues, _newRouteValuesFeature)!; private IRequestBodyPipeFeature RequestBodyPipeFeature => - _features.Fetch(ref _features.Cache.BodyPipe, this.HttpContext, _newRequestBodyPipeFeature); + _features.Fetch(ref _features.Cache.BodyPipe, this.HttpContext, _newRequestBodyPipeFeature)!; public override PathString PathBase { get { return new PathString(HttpRequestFeature.PathBase); } - set { HttpRequestFeature.PathBase = value.Value; } + set { HttpRequestFeature.PathBase = value.Value ?? string.Empty; } } public override PathString Path { get { return new PathString(HttpRequestFeature.Path); } - set { HttpRequestFeature.Path = value.Value; } + set { HttpRequestFeature.Path = value.Value ?? string.Empty; } } public override QueryString QueryString { get { return new QueryString(HttpRequestFeature.QueryString); } - set { HttpRequestFeature.QueryString = value.Value; } + set { HttpRequestFeature.QueryString = value.Value ?? string.Empty; } } public override long? ContentLength @@ -181,12 +181,12 @@ public override PipeReader BodyReader struct FeatureInterfaces { - public IHttpRequestFeature Request; - public IQueryFeature Query; - public IFormFeature Form; - public IRequestCookiesFeature Cookies; - public IRouteValuesFeature RouteValues; - public IRequestBodyPipeFeature BodyPipe; + public IHttpRequestFeature? Request; + public IQueryFeature? Query; + public IFormFeature? Form; + public IRequestCookiesFeature? Cookies; + public IRouteValuesFeature? RouteValues; + public IRequestBodyPipeFeature? BodyPipe; } } } diff --git a/src/Http/Http/src/Internal/DefaultHttpResponse.cs b/src/Http/Http/src/Internal/DefaultHttpResponse.cs index 1df8cfc9aa27..167cd10c40f7 100644 --- a/src/Http/Http/src/Internal/DefaultHttpResponse.cs +++ b/src/Http/Http/src/Internal/DefaultHttpResponse.cs @@ -14,9 +14,9 @@ namespace Microsoft.AspNetCore.Http internal sealed class DefaultHttpResponse : HttpResponse { // Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624 - private readonly static Func _nullResponseFeature = f => null; - private readonly static Func _nullResponseBodyFeature = f => null; - private readonly static Func _newResponseCookiesFeature = f => new ResponseCookiesFeature(f); + private readonly static Func _nullResponseFeature = f => null; + private readonly static Func _nullResponseBodyFeature = f => null; + private readonly static Func _newResponseCookiesFeature = f => new ResponseCookiesFeature(f); private readonly DefaultHttpContext _context; private FeatureReferences _features; @@ -43,13 +43,13 @@ public void Uninitialize() } private IHttpResponseFeature HttpResponseFeature => - _features.Fetch(ref _features.Cache.Response, _nullResponseFeature); + _features.Fetch(ref _features.Cache.Response, _nullResponseFeature)!; private IHttpResponseBodyFeature HttpResponseBodyFeature => - _features.Fetch(ref _features.Cache.ResponseBody, _nullResponseBodyFeature); + _features.Fetch(ref _features.Cache.ResponseBody, _nullResponseBodyFeature)!; private IResponseCookiesFeature ResponseCookiesFeature => - _features.Fetch(ref _features.Cache.Cookies, _newResponseCookiesFeature); + _features.Fetch(ref _features.Cache.Cookies, _newResponseCookiesFeature)!; public override HttpContext HttpContext { get { return _context; } } @@ -176,9 +176,9 @@ public override Task StartAsync(CancellationToken cancellationToken = default) struct FeatureInterfaces { - public IHttpResponseFeature Response; - public IHttpResponseBodyFeature ResponseBody; - public IResponseCookiesFeature Cookies; + public IHttpResponseFeature? Response; + public IHttpResponseBodyFeature? ResponseBody; + public IResponseCookiesFeature? Cookies; } } } diff --git a/src/Http/Http/src/Internal/DefaultWebSocketManager.cs b/src/Http/Http/src/Internal/DefaultWebSocketManager.cs index 8b03dfd753ba..43ad4c0907ea 100644 --- a/src/Http/Http/src/Internal/DefaultWebSocketManager.cs +++ b/src/Http/Http/src/Internal/DefaultWebSocketManager.cs @@ -13,8 +13,8 @@ namespace Microsoft.AspNetCore.Http internal sealed class DefaultWebSocketManager : WebSocketManager { // Lambdas hoisted to static readonly fields to improve inlining https://github.com/dotnet/roslyn/issues/13624 - private readonly static Func _nullRequestFeature = f => null; - private readonly static Func _nullWebSocketFeature = f => null; + private readonly static Func _nullRequestFeature = f => null; + private readonly static Func _nullWebSocketFeature = f => null; private FeatureReferences _features; @@ -39,10 +39,10 @@ public void Uninitialize() } private IHttpRequestFeature HttpRequestFeature => - _features.Fetch(ref _features.Cache.Request, _nullRequestFeature); + _features.Fetch(ref _features.Cache.Request, _nullRequestFeature)!; private IHttpWebSocketFeature WebSocketFeature => - _features.Fetch(ref _features.Cache.WebSockets, _nullWebSocketFeature); + _features.Fetch(ref _features.Cache.WebSockets, _nullWebSocketFeature)!; public override bool IsWebSocketRequest { @@ -60,7 +60,7 @@ public override IList WebSocketRequestedProtocols } } - public override Task AcceptWebSocketAsync(string subProtocol) + public override Task AcceptWebSocketAsync(string? subProtocol) { if (WebSocketFeature == null) { @@ -71,8 +71,8 @@ public override Task AcceptWebSocketAsync(string subProtocol) struct FeatureInterfaces { - public IHttpRequestFeature Request; - public IHttpWebSocketFeature WebSockets; + public IHttpRequestFeature? Request; + public IHttpWebSocketFeature? WebSockets; } } } diff --git a/src/Http/Http/src/Internal/ItemsDictionary.cs b/src/Http/Http/src/Internal/ItemsDictionary.cs index 3ca7ae95e11b..0ea54f92c1cd 100644 --- a/src/Http/Http/src/Internal/ItemsDictionary.cs +++ b/src/Http/Http/src/Internal/ItemsDictionary.cs @@ -3,25 +3,26 @@ using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.AspNetCore.Http { - internal class ItemsDictionary : IDictionary + internal class ItemsDictionary : IDictionary { - private IDictionary _items; + private IDictionary? _items; public ItemsDictionary() {} - public ItemsDictionary(IDictionary items) + public ItemsDictionary(IDictionary items) { _items = items; } - public IDictionary Items => this; + public IDictionary Items => this; // Replace the indexer with one that returns null for missing values - object IDictionary.this[object key] + object? IDictionary.this[object key] { get { @@ -38,16 +39,16 @@ object IDictionary.this[object key] } } - void IDictionary.Add(object key, object value) + void IDictionary.Add(object key, object? value) { EnsureDictionary(); _items.Add(key, value); } - bool IDictionary.ContainsKey(object key) + bool IDictionary.ContainsKey(object key) => _items != null && _items.ContainsKey(key); - ICollection IDictionary.Keys + ICollection IDictionary.Keys { get { @@ -60,16 +61,16 @@ ICollection IDictionary.Keys } } - bool IDictionary.Remove(object key) + bool IDictionary.Remove(object key) => _items != null && _items.Remove(key); - bool IDictionary.TryGetValue(object key, out object value) + bool IDictionary.TryGetValue(object key, out object? value) { value = null; return _items != null && _items.TryGetValue(key, out value); } - ICollection IDictionary.Values + ICollection IDictionary.Values { get { @@ -82,18 +83,18 @@ ICollection IDictionary.Values } } - void ICollection>.Add(KeyValuePair item) + void ICollection>.Add(KeyValuePair item) { EnsureDictionary(); _items.Add(item); } - void ICollection>.Clear() => _items?.Clear(); + void ICollection>.Clear() => _items?.Clear(); - bool ICollection>.Contains(KeyValuePair item) + bool ICollection>.Contains(KeyValuePair item) => _items != null && _items.Contains(item); - void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) + void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { if (_items == null) { @@ -104,11 +105,11 @@ void ICollection>.CopyTo(KeyValuePair>.Count => _items?.Count ?? 0; + int ICollection>.Count => _items?.Count ?? 0; - bool ICollection>.IsReadOnly => _items?.IsReadOnly ?? false; + bool ICollection>.IsReadOnly => _items?.IsReadOnly ?? false; - bool ICollection>.Remove(KeyValuePair item) + bool ICollection>.Remove(KeyValuePair item) { if (_items == null) { @@ -122,26 +123,27 @@ bool ICollection>.Remove(KeyValuePair(); + _items = new Dictionary(); } } - IEnumerator> IEnumerable>.GetEnumerator() + IEnumerator> IEnumerable>.GetEnumerator() => _items?.GetEnumerator() ?? EmptyEnumerator.Instance; IEnumerator IEnumerable.GetEnumerator() => _items?.GetEnumerator() ?? EmptyEnumerator.Instance; - private class EmptyEnumerator : IEnumerator> + private class EmptyEnumerator : IEnumerator> { // In own class so only initalized if GetEnumerator is called on an empty ItemsDictionary - public readonly static IEnumerator> Instance = new EmptyEnumerator(); - public KeyValuePair Current => default; + public readonly static IEnumerator> Instance = new EmptyEnumerator(); + public KeyValuePair Current => default; - object IEnumerator.Current => null; + object? IEnumerator.Current => null; public void Dispose() { } @@ -155,8 +157,8 @@ public void Reset() private static class EmptyDictionary { // In own class so only initalized if CopyTo is called on an empty ItemsDictionary - public readonly static IDictionary Dictionary = new Dictionary(); - public static ICollection> Collection => Dictionary; + public readonly static IDictionary Dictionary = new Dictionary(); + public static ICollection> Collection => Dictionary; } } } diff --git a/src/Http/Http/src/Internal/RequestCookieCollection.cs b/src/Http/Http/src/Internal/RequestCookieCollection.cs index 2fb738b51b74..6c74a6769bb9 100644 --- a/src/Http/Http/src/Internal/RequestCookieCollection.cs +++ b/src/Http/Http/src/Internal/RequestCookieCollection.cs @@ -4,6 +4,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNetCore.Http @@ -17,7 +18,7 @@ internal class RequestCookieCollection : IRequestCookieCollection private static readonly IEnumerator> EmptyIEnumeratorType = EmptyEnumerator; private static readonly IEnumerator EmptyIEnumerator = EmptyEnumerator; - private Dictionary Store { get; set; } + private Dictionary? Store { get; set; } public RequestCookieCollection() { @@ -33,7 +34,7 @@ public RequestCookieCollection(int capacity) Store = new Dictionary(capacity, StringComparer.OrdinalIgnoreCase); } - public string this[string key] + public string? this[string key] { get { @@ -47,8 +48,7 @@ public string this[string key] return null; } - string value; - if (TryGetValue(key, out value)) + if (TryGetValue(key, out var value)) { return value; } @@ -63,8 +63,7 @@ public static RequestCookieCollection Parse(IList values) return Empty; } - IList cookies; - if (CookieHeaderValue.TryParseList(values, out cookies)) + if (CookieHeaderValue.TryParseList(values, out var cookies)) { if (cookies.Count == 0) { @@ -72,7 +71,7 @@ public static RequestCookieCollection Parse(IList values) } var collection = new RequestCookieCollection(cookies.Count); - var store = collection.Store; + var store = collection.Store!; for (var i = 0; i < cookies.Count; i++) { var cookie = cookies[i]; @@ -119,7 +118,7 @@ public bool ContainsKey(string key) return Store.ContainsKey(key); } - public bool TryGetValue(string key, out string value) + public bool TryGetValue(string key, [MaybeNullWhen(false)] out string? value) { if (Store == null) { diff --git a/src/Http/Http/src/Internal/ResponseCookies.cs b/src/Http/Http/src/Internal/ResponseCookies.cs index 571df1068c48..1de354e342ca 100644 --- a/src/Http/Http/src/Internal/ResponseCookies.cs +++ b/src/Http/Http/src/Internal/ResponseCookies.cs @@ -3,8 +3,6 @@ using System; using System.Collections.Generic; -using System.Text; -using Microsoft.Extensions.ObjectPool; using Microsoft.Extensions.Primitives; using Microsoft.Net.Http.Headers; @@ -19,8 +17,7 @@ internal class ResponseCookies : IResponseCookies /// Create a new wrapper. /// /// The for the response. - /// The , if available. - public ResponseCookies(IHeaderDictionary headers, ObjectPool builderPool) + public ResponseCookies(IHeaderDictionary headers) { if (headers == null) { diff --git a/src/Http/Http/src/Microsoft.AspNetCore.Http.csproj b/src/Http/Http/src/Microsoft.AspNetCore.Http.csproj index a7fb56774bd1..d68725dd3c79 100644 --- a/src/Http/Http/src/Microsoft.AspNetCore.Http.csproj +++ b/src/Http/Http/src/Microsoft.AspNetCore.Http.csproj @@ -1,4 +1,4 @@ - + ASP.NET Core default HTTP feature implementations. @@ -9,6 +9,8 @@ true aspnetcore false + enable + preview diff --git a/src/Http/Http/src/MiddlewareFactory.cs b/src/Http/Http/src/MiddlewareFactory.cs index 5e5cd285f445..9722d5c259b5 100644 --- a/src/Http/Http/src/MiddlewareFactory.cs +++ b/src/Http/Http/src/MiddlewareFactory.cs @@ -1,11 +1,7 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Http @@ -22,7 +18,7 @@ public MiddlewareFactory(IServiceProvider serviceProvider) _serviceProvider = serviceProvider; } - public IMiddleware Create(Type middlewareType) + public IMiddleware? Create(Type middlewareType) { return _serviceProvider.GetRequiredService(middlewareType) as IMiddleware; } diff --git a/src/Http/Http/src/QueryCollection.cs b/src/Http/Http/src/QueryCollection.cs index b448269530c1..af2a3a4cf281 100644 --- a/src/Http/Http/src/QueryCollection.cs +++ b/src/Http/Http/src/QueryCollection.cs @@ -21,7 +21,7 @@ public class QueryCollection : IQueryCollection private static readonly IEnumerator> EmptyIEnumeratorType = EmptyEnumerator; private static readonly IEnumerator EmptyIEnumerator = EmptyEnumerator; - private Dictionary Store { get; set; } + private Dictionary? Store { get; set; } public QueryCollection() { diff --git a/src/Http/Http/src/StreamResponseBodyFeature.cs b/src/Http/Http/src/StreamResponseBodyFeature.cs index fd0d24dbd2d9..1152761e6e27 100644 --- a/src/Http/Http/src/StreamResponseBodyFeature.cs +++ b/src/Http/Http/src/StreamResponseBodyFeature.cs @@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Http /// public class StreamResponseBodyFeature : IHttpResponseBodyFeature { - private PipeWriter _pipeWriter; + private PipeWriter? _pipeWriter; private bool _started; private bool _completed; private bool _disposed; @@ -48,7 +48,7 @@ public StreamResponseBodyFeature(Stream stream, IHttpResponseBodyFeature priorFe /// /// The prior feature, if any. /// - public IHttpResponseBodyFeature PriorFeature { get; } + public IHttpResponseBodyFeature? PriorFeature { get; } /// /// A PipeWriter adapted over the given stream. diff --git a/src/Http/Http/test/ResponseCookiesTest.cs b/src/Http/Http/test/ResponseCookiesTest.cs index 52bf95c49d56..23eea058de1a 100644 --- a/src/Http/Http/test/ResponseCookiesTest.cs +++ b/src/Http/Http/test/ResponseCookiesTest.cs @@ -13,7 +13,7 @@ public class ResponseCookiesTest public void DeleteCookieShouldSetDefaultPath() { var headers = new HeaderDictionary(); - var cookies = new ResponseCookies(headers, null); + var cookies = new ResponseCookies(headers); var testCookie = "TestCookie"; cookies.Delete(testCookie); @@ -29,7 +29,7 @@ public void DeleteCookieShouldSetDefaultPath() public void DeleteCookieWithCookieOptionsShouldKeepPropertiesOfCookieOptions() { var headers = new HeaderDictionary(); - var cookies = new ResponseCookies(headers, null); + var cookies = new ResponseCookies(headers); var testCookie = "TestCookie"; var time = new DateTimeOffset(2000, 1, 1, 1, 1, 1, 1, TimeSpan.Zero); var options = new CookieOptions @@ -58,7 +58,7 @@ public void DeleteCookieWithCookieOptionsShouldKeepPropertiesOfCookieOptions() public void NoParamsDeleteRemovesCookieCreatedByAdd() { var headers = new HeaderDictionary(); - var cookies = new ResponseCookies(headers, null); + var cookies = new ResponseCookies(headers); var testCookie = "TestCookie"; cookies.Append(testCookie, testCookie); @@ -75,7 +75,7 @@ public void NoParamsDeleteRemovesCookieCreatedByAdd() public void ProvidesMaxAgeWithCookieOptionsArgumentExpectMaxAgeToBeSet() { var headers = new HeaderDictionary(); - var cookies = new ResponseCookies(headers, null); + var cookies = new ResponseCookies(headers); var cookieOptions = new CookieOptions(); var maxAgeTime = TimeSpan.FromHours(1); cookieOptions.MaxAge = TimeSpan.FromHours(1); @@ -111,7 +111,7 @@ public void EscapesKeyValuesBeforeSettingCookie( string expected) { var headers = new HeaderDictionary(); - var cookies = new ResponseCookies(headers, null); + var cookies = new ResponseCookies(headers); cookies.Append(key, value); diff --git a/src/Http/WebUtilities/ref/Microsoft.AspNetCore.WebUtilities.netcoreapp.cs b/src/Http/WebUtilities/ref/Microsoft.AspNetCore.WebUtilities.netcoreapp.cs index 29dc26ff745e..85a91f7d67db 100644 --- a/src/Http/WebUtilities/ref/Microsoft.AspNetCore.WebUtilities.netcoreapp.cs +++ b/src/Http/WebUtilities/ref/Microsoft.AspNetCore.WebUtilities.netcoreapp.cs @@ -204,7 +204,7 @@ public partial class MultipartSection { public MultipartSection() { } public long? BaseStreamOffset { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } - public System.IO.Stream? Body { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } + public System.IO.Stream Body { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public string? ContentDisposition { get { throw null; } } public string? ContentType { get { throw null; } } public System.Collections.Generic.Dictionary? Headers { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } diff --git a/src/Http/WebUtilities/src/MultipartSection.cs b/src/Http/WebUtilities/src/MultipartSection.cs index 69e14aa0828f..793bb04253ae 100644 --- a/src/Http/WebUtilities/src/MultipartSection.cs +++ b/src/Http/WebUtilities/src/MultipartSection.cs @@ -36,7 +36,10 @@ public string? ContentDisposition public Dictionary? Headers { get; set; } - public Stream? Body { get; set; } + /// + /// Gets or sets the body. + /// + public Stream Body { get; set; } = default!; /// /// The position where the body starts in the total multipart body. diff --git a/src/Shared/CopyOnWriteDictionary/CopyOnWriteDictionary.cs b/src/Shared/CopyOnWriteDictionary/CopyOnWriteDictionary.cs index 1408059ad923..85587c27bd01 100644 --- a/src/Shared/CopyOnWriteDictionary/CopyOnWriteDictionary.cs +++ b/src/Shared/CopyOnWriteDictionary/CopyOnWriteDictionary.cs @@ -1,17 +1,19 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.Extensions.Internal { - internal class CopyOnWriteDictionary : IDictionary + internal class CopyOnWriteDictionary : IDictionary where TKey : notnull { private readonly IDictionary _sourceDictionary; private readonly IEqualityComparer _comparer; - private IDictionary _innerDictionary; + private IDictionary? _innerDictionary; public CopyOnWriteDictionary( IDictionary sourceDictionary, @@ -112,7 +114,7 @@ public virtual bool Remove(TKey key) return WriteDictionary.Remove(key); } - public virtual bool TryGetValue(TKey key, out TValue value) + public virtual bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { return ReadDictionary.TryGetValue(key, out value); } @@ -152,4 +154,4 @@ IEnumerator IEnumerable.GetEnumerator() return GetEnumerator(); } } -} \ No newline at end of file +} diff --git a/src/Shared/CopyOnWriteDictionary/CopyOnWriteDictionaryHolder.cs b/src/Shared/CopyOnWriteDictionary/CopyOnWriteDictionaryHolder.cs index 7cd935e94018..cbb6f4161a75 100644 --- a/src/Shared/CopyOnWriteDictionary/CopyOnWriteDictionaryHolder.cs +++ b/src/Shared/CopyOnWriteDictionary/CopyOnWriteDictionaryHolder.cs @@ -1,15 +1,18 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +#nullable enable + using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.Extensions.Internal { - internal struct CopyOnWriteDictionaryHolder + internal struct CopyOnWriteDictionaryHolder where TKey : notnull { private readonly Dictionary _source; - private Dictionary _copy; + private Dictionary? _copy; public CopyOnWriteDictionaryHolder(Dictionary source) { @@ -128,7 +131,7 @@ public bool Remove(TKey key) return WriteDictionary.Remove(key); } - public bool TryGetValue(TKey key, out TValue value) + public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { return ReadDictionary.TryGetValue(key, out value); } From bc78cd9698fe20333d6081207855dce2fde2caea Mon Sep 17 00:00:00 2001 From: Pranav K Date: Sun, 14 Jun 2020 07:34:21 -0700 Subject: [PATCH 2/2] Annotate Hosting with nullable --- .../ref/Microsoft.AspNetCore.Hosting.Abstractions.csproj | 1 + .../Microsoft.AspNetCore.Hosting.Abstractions.netcoreapp.cs | 4 ++-- src/Hosting/Abstractions/src/IWebHostBuilder.cs | 6 +++--- .../src/Microsoft.AspNetCore.Hosting.Abstractions.csproj | 1 + src/Hosting/Abstractions/src/WebHostBuilderContext.cs | 4 ++-- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Hosting/Abstractions/ref/Microsoft.AspNetCore.Hosting.Abstractions.csproj b/src/Hosting/Abstractions/ref/Microsoft.AspNetCore.Hosting.Abstractions.csproj index 5191bb9a5da4..6bffd2511d1a 100644 --- a/src/Hosting/Abstractions/ref/Microsoft.AspNetCore.Hosting.Abstractions.csproj +++ b/src/Hosting/Abstractions/ref/Microsoft.AspNetCore.Hosting.Abstractions.csproj @@ -2,6 +2,7 @@ $(DefaultNetCoreTargetFramework) + annotations diff --git a/src/Hosting/Abstractions/ref/Microsoft.AspNetCore.Hosting.Abstractions.netcoreapp.cs b/src/Hosting/Abstractions/ref/Microsoft.AspNetCore.Hosting.Abstractions.netcoreapp.cs index 6b12e045e8dd..baa74b241193 100644 --- a/src/Hosting/Abstractions/ref/Microsoft.AspNetCore.Hosting.Abstractions.netcoreapp.cs +++ b/src/Hosting/Abstractions/ref/Microsoft.AspNetCore.Hosting.Abstractions.netcoreapp.cs @@ -93,8 +93,8 @@ public partial interface IWebHostBuilder Microsoft.AspNetCore.Hosting.IWebHostBuilder ConfigureAppConfiguration(System.Action configureDelegate); Microsoft.AspNetCore.Hosting.IWebHostBuilder ConfigureServices(System.Action configureServices); Microsoft.AspNetCore.Hosting.IWebHostBuilder ConfigureServices(System.Action configureServices); - string GetSetting(string key); - Microsoft.AspNetCore.Hosting.IWebHostBuilder UseSetting(string key, string value); + string? GetSetting(string key); + Microsoft.AspNetCore.Hosting.IWebHostBuilder UseSetting(string key, string? value); } public partial interface IWebHostEnvironment : Microsoft.Extensions.Hosting.IHostEnvironment { diff --git a/src/Hosting/Abstractions/src/IWebHostBuilder.cs b/src/Hosting/Abstractions/src/IWebHostBuilder.cs index 2cf3bc116327..1e67c11ad8c2 100644 --- a/src/Hosting/Abstractions/src/IWebHostBuilder.cs +++ b/src/Hosting/Abstractions/src/IWebHostBuilder.cs @@ -50,7 +50,7 @@ public interface IWebHostBuilder /// /// The key of the setting to look up. /// The value the setting currently contains. - string GetSetting(string key); + string? GetSetting(string key); /// /// Add or replace a setting in the configuration. @@ -58,6 +58,6 @@ public interface IWebHostBuilder /// The key of the setting to add or replace. /// The value of the setting to add or replace. /// The . - IWebHostBuilder UseSetting(string key, string value); + IWebHostBuilder UseSetting(string key, string? value); } -} \ No newline at end of file +} diff --git a/src/Hosting/Abstractions/src/Microsoft.AspNetCore.Hosting.Abstractions.csproj b/src/Hosting/Abstractions/src/Microsoft.AspNetCore.Hosting.Abstractions.csproj index 4def523880c5..31618baeb080 100644 --- a/src/Hosting/Abstractions/src/Microsoft.AspNetCore.Hosting.Abstractions.csproj +++ b/src/Hosting/Abstractions/src/Microsoft.AspNetCore.Hosting.Abstractions.csproj @@ -8,6 +8,7 @@ true aspnetcore;hosting false + enable diff --git a/src/Hosting/Abstractions/src/WebHostBuilderContext.cs b/src/Hosting/Abstractions/src/WebHostBuilderContext.cs index 23de0ad07c29..3f874154db12 100644 --- a/src/Hosting/Abstractions/src/WebHostBuilderContext.cs +++ b/src/Hosting/Abstractions/src/WebHostBuilderContext.cs @@ -13,11 +13,11 @@ public class WebHostBuilderContext /// /// The initialized by the . /// - public IWebHostEnvironment HostingEnvironment { get; set; } + public IWebHostEnvironment HostingEnvironment { get; set; } = default!; /// /// The containing the merged configuration of the application and the . /// - public IConfiguration Configuration { get; set; } + public IConfiguration Configuration { get; set; } = default!; } }