-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAppStaticFileMiddleware.cs
More file actions
312 lines (289 loc) · 12.7 KB
/
Copy pathAppStaticFileMiddleware.cs
File metadata and controls
312 lines (289 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System.Buffers;
using System.Collections.Concurrent;
using System.IO.Pipelines;
using System.Text;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
using Microsoft.Net.Http.Headers;
using NpgsqlRest;
using NpgsqlRest.Auth;
namespace NpgsqlRestClient;
public class AppStaticFileMiddleware(RequestDelegate next, IWebHostEnvironment hostingEnv)
{
private readonly RequestDelegate _next = next ?? throw new ArgumentNullException(nameof(next));
private readonly IWebHostEnvironment _hostingEnv = hostingEnv ?? throw new ArgumentNullException(nameof(hostingEnv));
private static readonly FileExtensionContentTypeProvider _fileTypeProvider = new();
private static string[]? _parsePatterns = default!;
private static ILogger? _logger = default!;
private static readonly ConcurrentDictionary<string, bool> _pathInParsePattern = new();
private static ConcurrentDictionary<string, bool> _pathInAuthorizePattern = null!;
// New cache for parsed file contents
private static readonly ConcurrentDictionary<string, (byte[] Content, DateTimeOffset LastModified)> _parsedFileCache = new();
private static bool _cacheParsedFiles = true;
private static IAntiforgery? _antiforgery;
private static DefaultResponseParser? _parser;
private static string[]? _headers;
private static string[]? _authorizePaths;
private static string? _unauthorizedRedirectPath;
private static string? _unauthorizedReturnToQueryParameter;
private static bool _checkAuthorize = false;
private static string[] _defaultFileNames = default!;
public static void ConfigureStaticFileMiddleware(
bool parse,
string[]? parsePatterns,
NpgsqlRestAuthenticationOptions options,
bool cacheParsedFiles,
string? antiforgeryFieldNameTag,
string? antiforgeryTokenTag,
IAntiforgery? antiforgery,
string[]? headers,
string[]? authorizePaths,
string? unauthorizedRedirectPath,
string? unautorizedReturnToQueryParameter,
Dictionary<string, string?>? availableClaims,
Dictionary<string, string?>? availableEnvVars,
ILogger? logger)
{
_parsePatterns = parse == false || parsePatterns == null || parsePatterns.Length == 0 ? null : parsePatterns?.Where(p => !string.IsNullOrEmpty(p)).ToArray();
if (parse is false || _parsePatterns is null)
{
_parser = null;
}
else
{
_parser = new DefaultResponseParser(options, antiforgeryFieldNameTag, antiforgeryTokenTag, availableClaims, availableEnvVars);
}
_antiforgery = antiforgery;
_logger = logger;
_cacheParsedFiles = cacheParsedFiles;
_headers = headers;
_authorizePaths = authorizePaths;
_unauthorizedRedirectPath = unauthorizedRedirectPath;
_unauthorizedReturnToQueryParameter = unautorizedReturnToQueryParameter;
_checkAuthorize = authorizePaths is not null && authorizePaths.Length > 0;
if (_checkAuthorize is true)
{
_pathInAuthorizePattern = new();
var dfo = new DefaultFilesOptions();
_defaultFileNames = [.. dfo.DefaultFileNames];
}
}
public async Task InvokeAsync(HttpContext context)
{
string method = context.Request.Method;
bool isGet = HttpMethods.IsGet(method);
if (!isGet && !HttpMethods.IsHead(method))
{
await _next(context);
return;
}
PathString path = context.Request.Path; // Cache PathString
IFileInfo fileInfo = _hostingEnv.WebRootFileProvider.GetFileInfo(path);
if (!fileInfo.Exists || fileInfo.IsDirectory)
{
await _next(context);
return;
}
var pathString = path.ToString();
long length = fileInfo.Length;
DateTimeOffset lastModified = fileInfo.LastModified.ToUniversalTime();
bool isInParsePattern = false;
if (_checkAuthorize is true)
{
if (_pathInAuthorizePattern.TryGetValue(pathString, out bool isInAuthPattern) is false)
{
isInAuthPattern = false;
for (int i = 0; i < _authorizePaths?.Length; i++)
{
if (Parser.IsPatternMatch(pathString, _authorizePaths[i]))
{
isInAuthPattern = true;
break;
}
}
_pathInAuthorizePattern.TryAdd(pathString, isInAuthPattern);
}
if (isInAuthPattern is true)
{
if (context.User.Identity is not null && context.User.Identity.IsAuthenticated is false)
{
context.Response.Clear();
if (string.IsNullOrEmpty(_unauthorizedRedirectPath) is false)
{
if (string.IsNullOrEmpty(_unauthorizedReturnToQueryParameter) is false)
{
context.Response.Redirect(new StringBuilder()
.Append(_unauthorizedRedirectPath)
.Append(_unauthorizedRedirectPath.Contains('?') ? "&" : "?")
.Append(_unauthorizedReturnToQueryParameter)
.Append('=')
.Append(Uri.EscapeDataString(string.Concat(GetOriginalRequestPath(pathString), context.Request.QueryString.ToString())))
.ToString());
}
else
{
context.Response.Redirect(_unauthorizedRedirectPath);
}
}
else
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
}
return;
}
}
}
if (isGet)
{
if (_pathInParsePattern.TryGetValue(pathString, out isInParsePattern) is false)
{
isInParsePattern = false;
for (int i = 0; i < _parsePatterns?.Length; i++)
{
if (Parser.IsPatternMatch(pathString, _parsePatterns[i]))
{
isInParsePattern = true;
break;
}
}
_pathInParsePattern.TryAdd(pathString, isInParsePattern);
}
}
AntiforgeryTokenSet? tokenSet = null;
if (_antiforgery is not null)
{
if (pathString.EndsWith(".html") is true || pathString.EndsWith(".htm") is true)
{
tokenSet = _antiforgery.GetAndStoreTokens(context);
}
}
string contentType = fileInfo.PhysicalPath != null && _fileTypeProvider.TryGetContentType(fileInfo.PhysicalPath, out var ct)
? ct
: "application/octet-stream";
context.Response.StatusCode = StatusCodes.Status200OK;
context.Response.ContentType = contentType;
if (isInParsePattern is false)
{
long etagHash = lastModified.ToFileTime() ^ length;
string etagString = string.Concat("\"", Convert.ToString(etagHash, 16), "\"");
var ifNoneMatch = context.Request.Headers[HeaderNames.IfNoneMatch];
if (!string.IsNullOrEmpty(ifNoneMatch) &&
System.Net.Http.Headers.EntityTagHeaderValue.TryParse(ifNoneMatch, out var clientEtag) &&
string.Equals(etagString, clientEtag.ToString(), StringComparison.Ordinal))
{
context.Response.StatusCode = StatusCodes.Status304NotModified;
return;
}
var ifModifiedSince = context.Request.Headers[HeaderNames.IfModifiedSince];
if (!string.IsNullOrEmpty(ifModifiedSince) && DateTimeOffset.TryParse(ifModifiedSince, out var since) && since >= lastModified)
{
context.Response.StatusCode = StatusCodes.Status304NotModified;
return;
}
context.Response.Headers[HeaderNames.LastModified] = lastModified.ToString("R");
context.Response.Headers[HeaderNames.ETag] = etagString;
context.Response.Headers[HeaderNames.AcceptRanges] = "bytes";
}
else
{
if (_headers is not null)
{
for (int i = 0; i < _headers.Length; i++)
{
if (string.IsNullOrEmpty(_headers[i]))
{
continue;
}
var parts = _headers[i].Split(':', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (parts.Length != 2 || string.IsNullOrWhiteSpace(parts[0]) || string.IsNullOrWhiteSpace(parts[1]))
{
continue;
}
context.Response.Headers[parts[0]] = parts[1];
}
}
}
if (isGet)
{
try
{
byte[] buffer;
if (_parser is null || isInParsePattern is false)
{
using var fileStream = new FileStream(fileInfo.PhysicalPath!, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 8192, useAsync: true);
await fileStream.CopyToAsync(context.Response.Body, context.RequestAborted);
return;
}
// Check cache for parsed files
if (isInParsePattern && _cacheParsedFiles && _parsedFileCache.TryGetValue(pathString, out var cached) && cached.LastModified >= lastModified)
{
buffer = cached.Content;
}
else
{
// Read file from disk
using var fileStream = new FileStream(fileInfo.PhysicalPath!, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 8192, useAsync: true);
buffer = new byte[(int)fileInfo.Length];
await fileStream.ReadExactlyAsync(buffer, context.RequestAborted);
// Cache the file content if enabled
if (_cacheParsedFiles)
{
_parsedFileCache[pathString] = (buffer, lastModified);
}
}
int charCount = Encoding.UTF8.GetCharCount(buffer);
char[] chars = ArrayPool<char>.Shared.Rent(charCount);
try
{
Encoding.UTF8.GetChars(buffer, 0, buffer.Length, chars, 0);
ReadOnlySpan<char> result = _parser.Parse(new ReadOnlySpan<char>(chars, 0, charCount), context, tokenSet);
var writer = PipeWriter.Create(context.Response.Body);
try
{
int maxBytesNeeded = Encoding.UTF8.GetMaxByteCount(result.Length);
Memory<byte> memory = writer.GetMemory(maxBytesNeeded);
int actualBytesWritten = Encoding.UTF8.GetBytes(result, memory.Span);
writer.Advance(actualBytesWritten);
context.Response.ContentLength = actualBytesWritten;
await writer.FlushAsync(context.RequestAborted);
}
finally
{
await writer.CompleteAsync();
}
}
finally
{
ArrayPool<char>.Shared.Return(chars);
}
}
catch (IOException ex)
{
_logger?.LogError(ex, "Failed to serve static file {Path}", path);
context.Response.Clear();
await _next(context);
return;
}
}
// HEAD request completes here
}
private static string GetOriginalRequestPath(string path)
{
if (string.IsNullOrEmpty(path) || path.Length < 2)
{
return path;
}
ReadOnlySpan<char> pathSpan = path.AsSpan();
for (int i = 0; i < _defaultFileNames.Length; i++)
{
string defaultFile = _defaultFileNames[i];
if (pathSpan.EndsWith(defaultFile.AsSpan(), StringComparison.OrdinalIgnoreCase) &&
pathSpan.Length > defaultFile.Length && pathSpan[^(defaultFile.Length + 1)] == '/')
{
return path[..^defaultFile.Length];
}
}
return path;
}
}