Skip to content

Commit

Permalink
Use ArgumentNullException.ThrowIfNull in Servers (#43502)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityamandaleeka committed Aug 24, 2022
1 parent 5c4112c commit 9de8e33
Show file tree
Hide file tree
Showing 26 changed files with 58 additions and 232 deletions.
10 changes: 2 additions & 8 deletions src/Servers/HttpSys/src/HttpSysListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,8 @@ internal sealed partial class HttpSysListener : IDisposable

public HttpSysListener(HttpSysOptions options, ILoggerFactory loggerFactory)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(loggerFactory);

if (!HttpApi.Supported)
{
Expand Down
15 changes: 3 additions & 12 deletions src/Servers/HttpSys/src/MessagePump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,8 @@ internal sealed partial class MessagePump : IServer, IServerDelegationFeature

public MessagePump(IOptions<HttpSysOptions> options, ILoggerFactory loggerFactory, IAuthenticationSchemeProvider authentication)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(loggerFactory);
_options = options.Value;
Listener = new HttpSysListener(_options, loggerFactory);
_logger = loggerFactory.CreateLogger<MessagePump>();
Expand Down Expand Up @@ -69,10 +63,7 @@ public MessagePump(IOptions<HttpSysOptions> options, ILoggerFactory loggerFactor

public Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken) where TContext : notnull
{
if (application == null)
{
throw new ArgumentNullException(nameof(application));
}
ArgumentNullException.ThrowIfNull(application);

var hostingUrlsPresent = _serverAddresses.Addresses.Count > 0;
var serverAddressCopy = _serverAddresses.Addresses.ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,7 @@ IHeaderDictionary IHttpResponseFeature.Headers

void IHttpResponseFeature.OnStarting(Func<object, Task> callback, object state)
{
if (callback == null)
{
throw new ArgumentNullException(nameof(callback));
}
ArgumentNullException.ThrowIfNull(callback);
if (_onStartingActions == null)
{
throw new InvalidOperationException("Cannot register new callbacks, the response has already started.");
Expand All @@ -448,10 +445,7 @@ void IHttpResponseFeature.OnStarting(Func<object, Task> callback, object state)

void IHttpResponseFeature.OnCompleted(Func<object, Task> callback, object state)
{
if (callback == null)
{
throw new ArgumentNullException(nameof(callback));
}
ArgumentNullException.ThrowIfNull(callback);
if (_onCompletedActions == null)
{
throw new InvalidOperationException("Cannot register new callbacks, the response has already completed.");
Expand Down
5 changes: 1 addition & 4 deletions src/Servers/HttpSys/src/RequestProcessing/RequestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,7 @@ protected void SetFatalResponse(int status)

internal unsafe void Delegate(DelegationRule destination)
{
if (destination == null)
{
throw new ArgumentNullException(nameof(destination));
}
ArgumentNullException.ThrowIfNull(destination);
if (Request.HasRequestBodyStarted)
{
throw new InvalidOperationException("This request cannot be delegated, the request body has already started.");
Expand Down
5 changes: 1 addition & 4 deletions src/Servers/HttpSys/src/RequestProcessing/RequestStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ internal void Abort()

private static void ValidateReadBuffer(byte[] buffer, int offset, int size)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
ArgumentNullException.ThrowIfNull(buffer);
if ((uint)offset > (uint)buffer.Length)
{
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
Expand Down
5 changes: 1 addition & 4 deletions src/Servers/HttpSys/src/RequestProcessing/ResponseBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,10 +524,7 @@ public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, As

public override void EndWrite(IAsyncResult asyncResult)
{
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
ArgumentNullException.ThrowIfNull(asyncResult);

TaskToApm.End(asyncResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,7 @@ private async Task CompleteInitializeResponseAwaited(Task initializeTask)
throw new ArgumentException($"{nameof(variableName)} should be non-empty string");
}

if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);

// Synchronize access to native methods that might run in parallel with IO loops
lock (_contextLock)
Expand Down
5 changes: 1 addition & 4 deletions src/Servers/IIS/IIS/src/WebHostBuilderIISExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public static class WebHostBuilderIISExtensions
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder UseIIS(this IWebHostBuilder hostBuilder)
{
if (hostBuilder == null)
{
throw new ArgumentNullException(nameof(hostBuilder));
}
ArgumentNullException.ThrowIfNull(hostBuilder);

// Check if in process
if (OperatingSystem.IsWindows() && NativeMethods.IsAspNetCoreModuleLoaded())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public static string CreateResponseKey(string requestKey)
// this concatenated value to obtain a 20-byte value and base64-encoding"
// https://tools.ietf.org/html/rfc6455#section-4.2.2

if (requestKey == null)
{
throw new ArgumentNullException(nameof(requestKey));
}
ArgumentNullException.ThrowIfNull(requestKey);

string merged = requestKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
byte[] mergedBytes = Encoding.UTF8.GetBytes(merged);
Expand Down
20 changes: 4 additions & 16 deletions src/Servers/IIS/IISIntegration/src/IISMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,10 @@ public class IISMiddleware
IAuthenticationSchemeProvider authentication,
IHostApplicationLifetime applicationLifetime)
{
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (applicationLifetime == null)
{
throw new ArgumentNullException(nameof(applicationLifetime));
}
ArgumentNullException.ThrowIfNull(next);
ArgumentNullException.ThrowIfNull(loggerFactory);
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(applicationLifetime);
if (string.IsNullOrEmpty(pairingToken))
{
throw new ArgumentException("Missing or empty pairing token.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public static class WebHostBuilderIISExtensions
/// <returns></returns>
public static IWebHostBuilder UseIISIntegration(this IWebHostBuilder hostBuilder)
{
if (hostBuilder == null)
{
throw new ArgumentNullException(nameof(hostBuilder));
}
ArgumentNullException.ThrowIfNull(hostBuilder);

// Check if `UseIISIntegration` was called already
if (hostBuilder.GetSetting(nameof(UseIISIntegration)) != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ public class IISApplicationDeployerFactory
/// <returns></returns>
public static ApplicationDeployer Create(DeploymentParameters deploymentParameters, ILoggerFactory loggerFactory)
{
if (deploymentParameters == null)
{
throw new ArgumentNullException(nameof(deploymentParameters));
}
ArgumentNullException.ThrowIfNull(deploymentParameters);

if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
ArgumentNullException.ThrowIfNull(loggerFactory);

switch (deploymentParameters.ServerType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ private async ValueTask<int> ReadAsyncInternal(Memory<byte> destination, Cancell
/// <inheritdoc />
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
if (destination == null)
{
throw new ArgumentNullException(nameof(destination));
}
ArgumentNullException.ThrowIfNull(destination);

if (bufferSize <= 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ IHeaderDictionary IHttpResponseTrailersFeature.Trailers
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);

_userTrailers = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ IHeaderDictionary IHttpResponseTrailersFeature.Trailers
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
ArgumentNullException.ThrowIfNull(value);

_userTrailers = value;
}
Expand Down
15 changes: 3 additions & 12 deletions src/Servers/Kestrel/Core/src/Internal/KestrelServerImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ internal KestrelServerImpl(IConnectionListenerFactory transportFactory, ServiceC
IEnumerable<IMultiplexedConnectionListenerFactory>? multiplexedFactories,
ServiceContext serviceContext)
{
if (transportFactories == null)
{
throw new ArgumentNullException(nameof(transportFactories));
}
ArgumentNullException.ThrowIfNull(transportFactories);

_transportFactory = transportFactories.LastOrDefault();
_multiplexedTransportFactory = multiplexedFactories?.LastOrDefault();
Expand All @@ -98,14 +95,8 @@ internal KestrelServerImpl(IConnectionListenerFactory transportFactory, ServiceC

private static ServiceContext CreateServiceContext(IOptions<KestrelServerOptions> options, ILoggerFactory loggerFactory, DiagnosticSource? diagnosticSource)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(loggerFactory);

var serverOptions = options.Value ?? new KestrelServerOptions();
var trace = new KestrelTrace(loggerFactory);
Expand Down
40 changes: 8 additions & 32 deletions src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ public KestrelConfigurationLoader Endpoint(string name, Action<EndpointConfigura
/// </summary>
public KestrelConfigurationLoader Endpoint(IPAddress address, int port, Action<ListenOptions> configure)
{
if (address == null)
{
throw new ArgumentNullException(nameof(address));
}
ArgumentNullException.ThrowIfNull(address);

return Endpoint(new IPEndPoint(address, port), configure);
}
Expand All @@ -120,14 +117,8 @@ public KestrelConfigurationLoader Endpoint(IPAddress address, int port, Action<L
/// </summary>
public KestrelConfigurationLoader Endpoint(IPEndPoint endPoint, Action<ListenOptions> configure)
{
if (endPoint == null)
{
throw new ArgumentNullException(nameof(endPoint));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgumentNullException.ThrowIfNull(endPoint);
ArgumentNullException.ThrowIfNull(configure);

EndpointsToAdd.Add(() =>
{
Expand All @@ -149,10 +140,7 @@ public KestrelConfigurationLoader Endpoint(IPEndPoint endPoint, Action<ListenOpt
/// </summary>
public KestrelConfigurationLoader LocalhostEndpoint(int port, Action<ListenOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgumentNullException.ThrowIfNull(configure);

EndpointsToAdd.Add(() =>
{
Expand All @@ -172,10 +160,7 @@ public KestrelConfigurationLoader LocalhostEndpoint(int port, Action<ListenOptio
/// </summary>
public KestrelConfigurationLoader AnyIPEndpoint(int port, Action<ListenOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgumentNullException.ThrowIfNull(configure);

EndpointsToAdd.Add(() =>
{
Expand All @@ -195,18 +180,12 @@ public KestrelConfigurationLoader AnyIPEndpoint(int port, Action<ListenOptions>
/// </summary>
public KestrelConfigurationLoader UnixSocketEndpoint(string socketPath, Action<ListenOptions> configure)
{
if (socketPath == null)
{
throw new ArgumentNullException(nameof(socketPath));
}
ArgumentNullException.ThrowIfNull(socketPath);
if (socketPath.Length == 0 || socketPath[0] != '/')
{
throw new ArgumentException(CoreStrings.UnixSocketPathMustBeAbsolute, nameof(socketPath));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgumentNullException.ThrowIfNull(configure);

EndpointsToAdd.Add(() =>
{
Expand All @@ -226,10 +205,7 @@ public KestrelConfigurationLoader UnixSocketEndpoint(string socketPath, Action<L
/// </summary>
public KestrelConfigurationLoader HandleEndpoint(ulong handle, Action<ListenOptions> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgumentNullException.ThrowIfNull(configure);

EndpointsToAdd.Add(() =>
{
Expand Down
Loading

0 comments on commit 9de8e33

Please sign in to comment.