aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-11-08 10:15:14 -0600
committerGitHub <noreply@github.com>2026-11-08 10:15:14 -0600
commit169bb8a3c0098599a617d8553e93c694bd813795 (patch)
tree4d825302717fc76d543b6a24469ad23a605cd5c8 /src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
parent4eaa2b13e2da250d7b89073a15e9f1e077661fd7 (diff)
parentcefdcd02e28b1f02f546002fdd475de4fa03e839 (diff)
downloadslskd-169bb8a3c0098599a617d8553e93c694bd813795.tar.gz
Merge branch 'master' into dependabot/npm_and_yarn/src/web/follow-redirects-1.16.2
Diffstat (limited to 'src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs')
-rw-r--r--src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs74
1 files changed, 48 insertions, 26 deletions
diff --git a/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs b/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
index e91a2352..0c8a8fb2 100644
--- a/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
+++ b/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
@@ -87,9 +87,13 @@ namespace slskd
/// <returns></returns>
public async Task InvokeAsync(HttpContext context)
{
+ // accept types can have some metadata after the type, separated by a semicolon. example:
+ // text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
context.Request.Headers.TryGetValue("accept", out var accept);
- var requestedTypes = accept.ToString().Split(',');
- var injectableTypes = new[] { "text/html", "application/xhtml + xml", "application/xml" };
+
+ // split by comma, then drop the semicolon and everything after it (if present) for that type
+ var requestedTypes = accept.ToString().Split(',').Select(type => type.Split(';')[0].Trim());
+ var injectableTypes = new[] { "text/html", "application/xhtml+xml", "application/xml" };
var isRewriteableType = requestedTypes
.Intersect(injectableTypes, StringComparer.InvariantCultureIgnoreCase)
.Any();
@@ -106,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
{