Skip to content

Commit

Permalink
Latest nullability updates
Browse files Browse the repository at this point in the history
- handle Microsoft.Extensions.FileProviders.Abstractions nullability annotations (dotnet/runtime#57405)
- handle Microsoft.Extensions.FileProviders.Physical nullability annotations (dotnet/runtime#57409)
- !fixup! improve `HttpConnectionDispatcher.GetConnectionToken(...)` and its use
  • Loading branch information
dougbu committed Aug 28, 2021
1 parent 0dc7277 commit 4bff1b7
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ManifestDirectoryInfo(ManifestDirectory directory, DateTimeOffset lastMod

public string? PhysicalPath => null;

public string? Name => Directory.Name;
public string Name => Directory.Name;

public DateTimeOffset LastModified { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ManifestFileInfo(Assembly assembly, ManifestFile file, DateTimeOffset las

public string? PhysicalPath => null;

public string? Name => ManifestFile.Name;
public string Name => ManifestFile.Name;

public DateTimeOffset LastModified { get; }

Expand Down
3 changes: 2 additions & 1 deletion src/Middleware/StaticFiles/src/DefaultFilesMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public Task Invoke(HttpContext context)
&& Helpers.IsGetOrHeadMethod(context.Request.Method)
&& Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out var subpath))
{
var dirContents = _fileProvider.GetDirectoryContents(subpath.Value);
// TryMatchPath will not output an empty subpath when it returns true.
var dirContents = _fileProvider.GetDirectoryContents(subpath.Value!);
if (dirContents.Exists)
{
// Check if any of our default files exist.
Expand Down
3 changes: 2 additions & 1 deletion src/Middleware/StaticFiles/src/DirectoryBrowserMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public Task Invoke(HttpContext context)

private bool TryGetDirectoryInfo(PathString subpath, out IDirectoryContents contents)
{
contents = _fileProvider.GetDirectoryContents(subpath.Value);
// TryMatchPath will not output an empty subpath when it returns true. This is called only in that case.
contents = _fileProvider.GetDirectoryContents(subpath.Value!);
return contents.Exists;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/Middleware/StaticFiles/src/StaticFileContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
Expand Down Expand Up @@ -47,6 +43,11 @@ internal struct StaticFileContext

public StaticFileContext(HttpContext context, StaticFileOptions options, ILogger logger, IFileProvider fileProvider, string? contentType, PathString subPath)
{
if (!subPath.HasValue)
{
throw new ArgumentNullException(nameof(subPath));
}

_context = context;
_options = options;
_request = context.Request;
Expand Down Expand Up @@ -109,11 +110,11 @@ private set

public string SubPath => _subPath.Value!;

public string PhysicalPath => _fileInfo.PhysicalPath;
public string PhysicalPath => _fileInfo.PhysicalPath ?? string.Empty;

public bool LookupFileInfo()
{
_fileInfo = _fileProvider.GetFileInfo(_subPath.Value);
_fileInfo = _fileProvider.GetFileInfo(SubPath);
if (_fileInfo.Exists)
{
_length = _fileInfo.Length;
Expand Down Expand Up @@ -264,7 +265,7 @@ public void ApplyResponseHeaders(int statusCode)
_response.ContentLength = _length;
}

_options.OnPrepareResponse(new StaticFileResponseContext(_context, _fileInfo!));
_options.OnPrepareResponse(new StaticFileResponseContext(_context, _fileInfo));
}

public PreconditionState GetPreconditionState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public FileProviderRazorProjectItem(IFileInfo fileInfo, string basePath, string
public override bool Exists => FileInfo.Exists;

/// <inheritdoc/>
public override string PhysicalPath => FileInfo.PhysicalPath;
public override string PhysicalPath => FileInfo.PhysicalPath ?? string.Empty;

/// <inheritdoc/>
public override string? RelativePhysicalPath
Expand Down
6 changes: 3 additions & 3 deletions src/Shared/MediaType/ReadOnlyMediaTypeHeaderValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ReadOnlyMediaTypeHeaderValue(string mediaType)
/// </summary>
/// <param name="mediaType">The <see cref="StringSegment"/> with the media type.</param>
public ReadOnlyMediaTypeHeaderValue(StringSegment mediaType)
: this(mediaType.Buffer, mediaType.Offset, mediaType.Length)
: this(mediaType.Buffer ?? string.Empty, mediaType.Offset, mediaType.Length)
{
}

Expand Down Expand Up @@ -146,7 +146,7 @@ private static bool TryGetSuffixLength(StringSegment subType, out int suffixLeng
var startPos = subType.Offset + subType.Length - 1;
for (var currentPos = startPos; currentPos >= subType.Offset; currentPos--)
{
if (subType.Buffer[currentPos] == '+')
if (subType.Buffer![currentPos] == '+')
{
suffixLength = startPos - currentPos;
return true;
Expand Down Expand Up @@ -357,7 +357,7 @@ public bool TryGetLastParameter(StringSegment parameterName, out StringSegment p
// charset.Value might be an invalid encoding name as in charset=invalid.
// For that reason, we catch the exception thrown by Encoding.GetEncoding
// and return null instead.
return charset.HasValue ? Encoding.GetEncoding(charset.Value) : null;
return charset.HasValue ? Encoding.GetEncoding(charset.Value!) : null;
}
catch (Exception)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/StaticWebAssets/StaticWebAssetsFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public StaticWebAssetsFileInfo(string name, IFileInfo source)

public long Length => _source.Length;

public string PhysicalPath => _source.PhysicalPath;
public string PhysicalPath => _source.PhysicalPath ?? string.Empty;

public DateTimeOffset LastModified => _source.LastModified;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task ExecuteAsync(HttpContext context, HttpConnectionDispatcherOpti

HttpConnectionContext? connectionContext = null;
var connectionToken = GetConnectionToken(context);
if (connectionToken != null)
if (!StringValues.IsNullOrEmpty(connectionToken))
{
_manager.TryGetConnection(connectionToken, out connectionContext);
}
Expand Down Expand Up @@ -381,7 +381,7 @@ private static bool ServerHasWebSockets(IFeatureCollection features)
return features.Get<IHttpWebSocketFeature>() != null;
}

private static string GetConnectionToken(HttpContext context) => context.Request.Query["id"].ToString();
private static StringValues GetConnectionToken(HttpContext context) => context.Request.Query["id"];

private async Task ProcessSend(HttpContext context, HttpConnectionDispatcherOptions options)
{
Expand Down

0 comments on commit 4bff1b7

Please sign in to comment.