|
| 1 | +using System.Buffers; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.IO.Pipelines; |
| 4 | +using System.Text; |
| 5 | +using Microsoft.AspNetCore.StaticFiles; |
| 6 | +using Microsoft.Extensions.FileProviders; |
| 7 | +using Microsoft.Extensions.Primitives; |
| 8 | +using Microsoft.Net.Http.Headers; |
| 9 | + |
| 10 | +namespace NpgsqlRestClient; |
| 11 | + |
| 12 | +public class AppStaticFileMiddleware |
| 13 | +{ |
| 14 | + private readonly RequestDelegate _next; |
| 15 | + private readonly IWebHostEnvironment _hostingEnv; |
| 16 | + private static readonly FileExtensionContentTypeProvider _fileTypeProvider = new(); |
| 17 | + |
| 18 | + private static bool _parse; |
| 19 | + private static string[]? _parsePatterns = default!; |
| 20 | + private static string? _userIdTag = default!; |
| 21 | + private static string? _userNameTag = default!; |
| 22 | + private static string? _userRolesTag = default!; |
| 23 | + private static Dictionary<string, StringValues>? _customClaimTags = default!; |
| 24 | + private static Serilog.ILogger? _logger = default!; |
| 25 | + |
| 26 | + private static readonly ConcurrentDictionary<string, bool> _pathInParsePattern = new(); |
| 27 | + |
| 28 | + const long StreamingThreshold = 1024 * 1024 * 10; // 10MB |
| 29 | + const int MaxBufferSize = 8192; // 8KB |
| 30 | + |
| 31 | + public static void ConfigureStaticFileMiddleware( |
| 32 | + bool parse, |
| 33 | + string[]? parsePatterns, |
| 34 | + string? userIdTag, |
| 35 | + string? userNameTag, |
| 36 | + string? userRolesTag, |
| 37 | + Dictionary<string, StringValues>? customClaimTags, |
| 38 | + Serilog.ILogger? logger) |
| 39 | + { |
| 40 | + _parse = parse; |
| 41 | + _parsePatterns = parsePatterns == null || parsePatterns.Length == 0 ? null : parsePatterns?.Where(p => !string.IsNullOrEmpty(p)).ToArray(); |
| 42 | + _userIdTag = string.IsNullOrEmpty(userIdTag) ? null : userIdTag; |
| 43 | + _userNameTag = string.IsNullOrEmpty(userNameTag) ? null : userNameTag; |
| 44 | + _userRolesTag = string.IsNullOrEmpty(userRolesTag) ? null : userRolesTag; |
| 45 | + _customClaimTags = customClaimTags == null || customClaimTags.Count == 0 ? null : customClaimTags; |
| 46 | + _logger = logger; |
| 47 | + } |
| 48 | + |
| 49 | + public AppStaticFileMiddleware(RequestDelegate next, IWebHostEnvironment hostingEnv) |
| 50 | + { |
| 51 | + _next = next ?? throw new ArgumentNullException(nameof(next)); |
| 52 | + _hostingEnv = hostingEnv ?? throw new ArgumentNullException(nameof(hostingEnv)); |
| 53 | + } |
| 54 | + |
| 55 | + public async Task InvokeAsync(HttpContext context) |
| 56 | + { |
| 57 | + string method = context.Request.Method; |
| 58 | + bool isGet = HttpMethods.IsGet(method); |
| 59 | + if (!isGet && !HttpMethods.IsHead(method)) |
| 60 | + { |
| 61 | + await _next(context); |
| 62 | + return; |
| 63 | + } |
| 64 | + PathString path = context.Request.Path; // Cache PathString |
| 65 | + IFileInfo fileInfo = _hostingEnv.WebRootFileProvider.GetFileInfo(path); |
| 66 | + if (!fileInfo.Exists || fileInfo.IsDirectory) |
| 67 | + { |
| 68 | + await _next(context); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + string contentType = fileInfo.PhysicalPath != null && _fileTypeProvider.TryGetContentType(fileInfo.PhysicalPath, out var ct) |
| 73 | + ? ct |
| 74 | + : "application/octet-stream"; |
| 75 | + |
| 76 | + DateTimeOffset lastModified = fileInfo.LastModified.ToUniversalTime(); |
| 77 | + long length = fileInfo.Length; |
| 78 | + long etagHash = lastModified.ToFileTime() ^ length; |
| 79 | + string etagString = string.Concat("\"", Convert.ToString(etagHash, 16), "\""); |
| 80 | + |
| 81 | + var ifNoneMatch = context.Request.Headers[HeaderNames.IfNoneMatch]; |
| 82 | + if (!string.IsNullOrEmpty(ifNoneMatch) && |
| 83 | + System.Net.Http.Headers.EntityTagHeaderValue.TryParse(ifNoneMatch, out var clientEtag) && |
| 84 | + string.Equals(etagString, clientEtag.ToString(), StringComparison.Ordinal)) |
| 85 | + { |
| 86 | + context.Response.StatusCode = StatusCodes.Status304NotModified; |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + var ifModifiedSince = context.Request.Headers[HeaderNames.IfModifiedSince]; |
| 91 | + if (!string.IsNullOrEmpty(ifModifiedSince) && DateTimeOffset.TryParse(ifModifiedSince, out var since) && since >= lastModified) |
| 92 | + { |
| 93 | + context.Response.StatusCode = StatusCodes.Status304NotModified; |
| 94 | + return; |
| 95 | + } |
| 96 | + context.Response.StatusCode = StatusCodes.Status200OK; |
| 97 | + context.Response.ContentType = contentType; |
| 98 | + context.Response.Headers[HeaderNames.LastModified] = lastModified.ToString("R"); |
| 99 | + context.Response.Headers[HeaderNames.ETag] = etagString; |
| 100 | + context.Response.Headers[HeaderNames.AcceptRanges] = "bytes"; |
| 101 | + |
| 102 | + if (isGet) |
| 103 | + { |
| 104 | + try |
| 105 | + { |
| 106 | + using var fileStream = new FileStream(fileInfo.PhysicalPath!, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 8192, useAsync: true); |
| 107 | + |
| 108 | + if (_parse is false || _parsePatterns is null || |
| 109 | + (_userIdTag is null && _userNameTag is null && _userRolesTag is null && _customClaimTags is null) |
| 110 | + ) |
| 111 | + { |
| 112 | + context.Response.ContentLength = length; |
| 113 | + await fileStream.CopyToAsync(context.Response.Body, context.RequestAborted); |
| 114 | + return; |
| 115 | + } |
| 116 | + var pathString = path.ToString(); |
| 117 | + if (_pathInParsePattern.TryGetValue(pathString, out bool isInParsePattern) is false) |
| 118 | + { |
| 119 | + isInParsePattern = false; |
| 120 | + for (int i = 0; i < _parsePatterns.Length; i++) |
| 121 | + { |
| 122 | + if (DefaultResponseParser.IsPatternMatch(pathString, _parsePatterns[i])) |
| 123 | + { |
| 124 | + isInParsePattern = true; |
| 125 | + break; |
| 126 | + } |
| 127 | + } |
| 128 | + _pathInParsePattern.TryAdd(pathString, isInParsePattern); |
| 129 | + } |
| 130 | + |
| 131 | + if (isInParsePattern is false) |
| 132 | + { |
| 133 | + context.Response.ContentLength = length; |
| 134 | + await fileStream.CopyToAsync(context.Response.Body, context.RequestAborted); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + var parser = new DefaultResponseParser( |
| 139 | + userIdParameterName: _userIdTag, |
| 140 | + userNameParameterName: _userNameTag, |
| 141 | + userRolesParameterName: _userRolesTag, |
| 142 | + ipAddressParameterName: null, |
| 143 | + customClaims: _customClaimTags, |
| 144 | + customParameters: null); |
| 145 | + |
| 146 | + |
| 147 | + if (fileInfo.Length < StreamingThreshold) |
| 148 | + { |
| 149 | + byte[] buffer = new byte[(int)fileInfo.Length]; |
| 150 | + await fileStream.ReadExactlyAsync(buffer, context.RequestAborted); |
| 151 | + int charCount = Encoding.UTF8.GetCharCount(buffer); |
| 152 | + char[] chars = ArrayPool<char>.Shared.Rent(charCount); |
| 153 | + try |
| 154 | + { |
| 155 | + Encoding.UTF8.GetChars(buffer, 0, buffer.Length, chars, 0); |
| 156 | + ReadOnlySpan<char> result = parser.Parse(new ReadOnlySpan<char>(chars, 0, charCount), context); |
| 157 | + var writer = PipeWriter.Create(context.Response.Body); |
| 158 | + try |
| 159 | + { |
| 160 | + int maxBytesNeeded = Encoding.UTF8.GetMaxByteCount(result.Length); |
| 161 | + Memory<byte> memory = writer.GetMemory(maxBytesNeeded); |
| 162 | + int actualBytesWritten = Encoding.UTF8.GetBytes(result, memory.Span); |
| 163 | + writer.Advance(actualBytesWritten); |
| 164 | + context.Response.ContentLength = actualBytesWritten; |
| 165 | + await writer.FlushAsync(context.RequestAborted); |
| 166 | + } |
| 167 | + finally |
| 168 | + { |
| 169 | + await writer.CompleteAsync(); |
| 170 | + } |
| 171 | + } |
| 172 | + finally |
| 173 | + { |
| 174 | + ArrayPool<char>.Shared.Return(chars); |
| 175 | + } |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + var writer = PipeWriter.Create(context.Response.Body); |
| 180 | + try |
| 181 | + { |
| 182 | + using var reader = new StreamReader(fileStream, Encoding.UTF8, leaveOpen: true); |
| 183 | + char[] chars = ArrayPool<char>.Shared.Rent(MaxBufferSize); |
| 184 | + try |
| 185 | + { |
| 186 | + int charsRead; |
| 187 | + while ((charsRead = await reader.ReadAsync(chars, context.RequestAborted)) > 0) |
| 188 | + { |
| 189 | + var result = parser.Parse(chars.AsSpan(0, charsRead), context); |
| 190 | + int bytesWritten = Encoding.UTF8.GetBytes(result, writer.GetSpan(result.Length)); |
| 191 | + writer.Advance(bytesWritten); |
| 192 | + await writer.FlushAsync(context.RequestAborted); |
| 193 | + } |
| 194 | + } |
| 195 | + finally |
| 196 | + { |
| 197 | + ArrayPool<char>.Shared.Return(chars); |
| 198 | + } |
| 199 | + } |
| 200 | + finally |
| 201 | + { |
| 202 | + await writer.CompleteAsync(); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + catch (IOException ex) |
| 207 | + { |
| 208 | + _logger?.Error(ex, "Failed to serve static file {Path}", path.ToString()); |
| 209 | + context.Response.Clear(); |
| 210 | + await _next(context); |
| 211 | + return; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + // HEAD request completes here |
| 216 | + } |
| 217 | +} |
0 commit comments