aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-10-26 20:53:31 -0500
committerJP Dillingham <jp@dillingham.me>2026-10-26 20:53:31 -0500
commit70e99d376373a235186d6069728b90c5f43ede23 (patch)
treedbdc3227fa705c6b306c8b0a208f598abab54a47 /src
parent89a36d7b8fdadbf9f9905c6681d975b70fd0c6c0 (diff)
downloadslskd-70e99d376373a235186d6069728b90c5f43ede23.tar.gz
make rewrite resilent
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs66
1 files changed, 42 insertions, 24 deletions
diff --git a/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs b/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
index 542e2a6c..0c8a8fb2 100644
--- a/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
+++ b/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
@@ -110,36 +110,54 @@ namespace slskd
var stream = new MemoryStream();
context.Response.Body = stream;
- await Next.Invoke(context);
-
- if (context.Response.StatusCode == 200)
+ try
{
- // something downstream responded with a 200, meaning there's data in the body
- // we need to read it, so we can reset then play it back with the modified HTML
- context.Response.Body.Seek(0, SeekOrigin.Begin);
- var body = await new StreamReader(context.Response.Body).ReadToEndAsync();
-
- var headers = context.Response.Headers
- .ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
- .Where(kvp => !string.Equals(kvp.Key, "Content-Length", StringComparison.InvariantCultureIgnoreCase));
-
- context.Response.Clear();
+ await Next.Invoke(context);
- foreach (var header in headers)
+ if (context.Response.StatusCode == 200)
{
- context.Response.Headers.Add(header);
+ // something downstream responded with a 200, meaning there's data in the body
+ // we need to read it, so we can reset then play it back with the modified HTML
+ context.Response.Body.Seek(0, SeekOrigin.Begin);
+ var body = await new StreamReader(context.Response.Body).ReadToEndAsync();
+
+ var headers = context.Response.Headers
+ .ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
+ .Where(kvp => !string.Equals(kvp.Key, "Content-Length", StringComparison.InvariantCultureIgnoreCase));
+
+ context.Response.Clear();
+
+ foreach (var header in headers)
+ {
+ context.Response.Headers.Add(header);
+ }
+
+ // check the response content type to make sure it's still rewriteable. the server might return something other
+ // than what was requested. if it's no longer rewriteable, don't rewrite.
+ // drop the semicolon and everything after it (e.g. '; charset=utf-8') before comparing
+ var responseType = context.Response.ContentType?.Split(';')[0].Trim();
+ var isRewriteableResponseType = injectableTypes.Contains(responseType, StringComparer.InvariantCultureIgnoreCase);
+ if (!isRewriteableResponseType)
+ {
+ await context.Response.WriteAsync(body);
+ }
+ else
+ {
+ await context.Response.WriteAsync(Regex.Replace(body, Pattern, Replacement));
+ }
}
- await context.Response.WriteAsync(Regex.Replace(body, Pattern, Replacement));
+ // rewind the stream we injected to the beginning, then replay the data to the
+ // original stream
+ stream.Seek(0, SeekOrigin.Begin);
+ await stream.CopyToAsync(originalStream);
+ }
+ finally
+ {
+ // swap the original stream back in, even if something downstream threw, so that
+ // upstream middleware (e.g. the exception handler) can write to the response
+ context.Response.Body = originalStream;
}
-
- // rewind the stream we injected to the beginning, then replay the data to the
- // original stream
- stream.Seek(0, SeekOrigin.Begin);
- await stream.CopyToAsync(originalStream);
-
- // swap the original stream back in
- context.Response.Body = originalStream;
}
else
{