Voeg WebsiteMode.PrerenderedPages toe: nginx-gelijke serving van een voorgerenderde website
- Pure WebsitePathResolver (canonieke 301's, echte 404, dotfiles geweigerd, gereserveerde prefixes) - PrerenderedSiteMiddleware, 404-fallback na de API-routes, gzip, Cache-Control, NoIndex/robots.txt - Beleid StrictSite met eigen AllowedSite*Origins; volledige headerset op alle websiteresponses - Tests: voorbeelden, FsCheck-properties en integratietests op een in-memory server Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
namespace SlpModularCms.Core.Hosting.Website;
|
||||
|
||||
/// <summary>
|
||||
/// Read-only access to the website files, confined to <c>wwwroot/web</c>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A file provider rather than <c>Path.Combine</c> with request data: <see cref="PhysicalFileProvider"/>
|
||||
/// refuses anything that resolves outside its root, which is the second line of defence behind
|
||||
/// <see cref="WebsitePathResolver"/>. Exclusion filters are switched off on purpose, because the
|
||||
/// default ones hide every dot-prefixed name and would make <c>/.well-known/</c> unreachable; dotfiles
|
||||
/// are refused earlier, by rule R3 of the resolver.
|
||||
///
|
||||
/// The root is looked up on every call instead of once at startup: the website is delivered
|
||||
/// independently of the host (a CI copy, or a symlink switched to a new release) and may appear or
|
||||
/// be replaced while the process runs. A missing root, a vanished file or an IO error is "not
|
||||
/// found", never an exception.
|
||||
/// </remarks>
|
||||
public sealed class WebsiteFileAccess : IFileProvider, IDisposable
|
||||
{
|
||||
private readonly string _root;
|
||||
private readonly ILogger _logger;
|
||||
private readonly Lock _gate = new();
|
||||
private PhysicalFileProvider? _provider;
|
||||
private bool _warnedMissing;
|
||||
|
||||
public WebsiteFileAccess(string root, ILogger logger)
|
||||
{
|
||||
_root = root;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>True when <paramref name="relativePath"/> names an existing file (not a directory).</summary>
|
||||
public bool Exists(string relativePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var info = GetFileInfo(relativePath);
|
||||
return info.Exists && !info.IsDirectory;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Checking a website file failed; treating it as not found.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public IFileInfo GetFileInfo(string subpath)
|
||||
{
|
||||
var provider = GetProvider();
|
||||
return provider is null ? new NotFoundFileInfo(subpath) : provider.GetFileInfo(subpath);
|
||||
}
|
||||
|
||||
public IDirectoryContents GetDirectoryContents(string subpath) => NotFoundDirectoryContents.Singleton;
|
||||
|
||||
public IChangeToken Watch(string filter) => NullChangeToken.Singleton;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_provider?.Dispose();
|
||||
_provider = null;
|
||||
}
|
||||
}
|
||||
|
||||
private PhysicalFileProvider? GetProvider()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (_provider is not null && Directory.Exists(_root))
|
||||
{
|
||||
return _provider;
|
||||
}
|
||||
|
||||
_provider?.Dispose();
|
||||
_provider = null;
|
||||
|
||||
if (!Directory.Exists(_root))
|
||||
{
|
||||
if (!_warnedMissing)
|
||||
{
|
||||
_warnedMissing = true;
|
||||
_logger.LogWarning(
|
||||
"The website directory {Root} does not exist; every website path answers 404 until it is delivered.",
|
||||
_root);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
_warnedMissing = false;
|
||||
_provider = new PhysicalFileProvider(_root, Microsoft.Extensions.FileProviders.Physical.ExclusionFilters.None);
|
||||
return _provider;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user