aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-05-13 09:46:15 -0500
committerGitHub <noreply@github.com>2022-05-13 09:46:15 -0500
commita84143aff46f25e7edee60c5d85b59ac7d419fcd (patch)
tree427e5363242af90c01d1f88f2d8fad6660fecb53 /src
parentf314ff92c8ce41a94944019801f391deefc4cc1c (diff)
parent04b4ef849fde9d91336e112b778219df43767fa3 (diff)
downloadslskd-a84143aff46f25e7edee60c5d85b59ac7d419fcd.tar.gz
Merge pull request #460 from slskd/no-js
Replace headers on rewritten HTML responses
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs22
-rw-r--r--src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs21
2 files changed, 29 insertions, 14 deletions
diff --git a/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs b/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs
index 217b2bce..3e2c003a 100644
--- a/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs
+++ b/src/slskd/Common/Middleware/HTMLInjectionMiddleware.cs
@@ -69,16 +69,14 @@ namespace slskd
{
context.Request.Headers.TryGetValue("accept", out var accept);
var requestedTypes = accept.ToString().Split(',');
-
var injectableTypes = new[] { "text/html", "application/xhtml + xml", "application/xml" };
-
- var isApiRoute = context.Request.Path.ToString().StartsWith("/api");
- var isGET = context.Request.Method == "GET";
-
var isInjectableType = requestedTypes
.Intersect(injectableTypes, StringComparer.InvariantCultureIgnoreCase)
.Any();
+ var isApiRoute = context.Request.Path.ToString().StartsWith("/api");
+ var isGET = context.Request.Method == "GET";
+
if (!isApiRoute && isGET && isInjectableType)
{
var originalStream = context.Response.Body;
@@ -93,15 +91,25 @@ namespace slskd
if (context.Response.StatusCode == 200)
{
// 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 appended HTML
+ // 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);
+ }
+
await context.Response.WriteAsync(body + HTML);
}
- // rewind the stream we injected to the beginning, then replay the data to the
+ // 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);
diff --git a/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs b/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
index a15ba1e0..4b90d6da 100644
--- a/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
+++ b/src/slskd/Common/Middleware/HTMLRewriteMiddleware.cs
@@ -74,16 +74,14 @@ namespace slskd
{
context.Request.Headers.TryGetValue("accept", out var accept);
var requestedTypes = accept.ToString().Split(',');
-
- var rewriteableType = new[] { "text/html", "application/xhtml + xml", "application/xml" };
+ var injectableTypes = new[] { "text/html", "application/xhtml + xml", "application/xml" };
+ var isRewriteableType = requestedTypes
+ .Intersect(injectableTypes, StringComparer.InvariantCultureIgnoreCase)
+ .Any();
var isApiRoute = context.Request.Path.ToString().StartsWith("/api");
var isGET = context.Request.Method == "GET";
- var isRewriteableType = requestedTypes
- .Intersect(rewriteableType, StringComparer.InvariantCultureIgnoreCase)
- .Any();
-
if (!isApiRoute && isGET && isRewriteableType)
{
var originalStream = context.Response.Body;
@@ -98,12 +96,21 @@ namespace slskd
if (context.Response.StatusCode == 200)
{
// 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 appended HTML
+ // 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);
+ }
+
await context.Response.WriteAsync(Regex.Replace(body, Pattern, Replacement));
}