mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-28 07:58:40 +00:00
Add Deflate compression for text/xml (#552)
* Add deflate compression support * Fix compression when content type contains encoding Co-authored-by: Dagg <daggintosh@outlook.com>
This commit is contained in:
parent
b896aee51a
commit
3b93f6ca2f
1 changed files with 25 additions and 3 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
using System.IO.Compression;
|
||||||
using LBPUnion.ProjectLighthouse.Configuration;
|
using LBPUnion.ProjectLighthouse.Configuration;
|
||||||
using LBPUnion.ProjectLighthouse.Helpers;
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
using LBPUnion.ProjectLighthouse.Middlewares;
|
using LBPUnion.ProjectLighthouse.Middlewares;
|
||||||
|
@ -14,6 +15,7 @@ public class DigestMiddleware : Middleware
|
||||||
this.computeDigests = computeDigests;
|
this.computeDigests = computeDigests;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !DEBUG
|
||||||
private static readonly HashSet<string> exemptPathList = new()
|
private static readonly HashSet<string> exemptPathList = new()
|
||||||
{
|
{
|
||||||
"/login",
|
"/login",
|
||||||
|
@ -25,6 +27,7 @@ public class DigestMiddleware : Middleware
|
||||||
"/network_settings.nws",
|
"/network_settings.nws",
|
||||||
"/ChallengeConfig.xml",
|
"/ChallengeConfig.xml",
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
public override async Task InvokeAsync(HttpContext context)
|
public override async Task InvokeAsync(HttpContext context)
|
||||||
{
|
{
|
||||||
|
@ -117,9 +120,28 @@ public class DigestMiddleware : Middleware
|
||||||
context.Response.Headers.Add("X-Digest-A", serverDigest);
|
context.Response.Headers.Add("X-Digest-A", serverDigest);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a content-length header if it isn't present to disable response chunking
|
if (responseBuffer.Length > 1000 && context.Request.Headers.AcceptEncoding.Contains("deflate") && context.Response.ContentType.Contains("text/xml"))
|
||||||
if (!context.Response.Headers.ContainsKey("Content-Length"))
|
{
|
||||||
context.Response.Headers.Add("Content-Length", responseBuffer.Length.ToString());
|
context.Response.Headers.Add("X-Original-Content-Length", responseBuffer.Length.ToString());
|
||||||
|
context.Response.Headers.Add("Vary", "Accept-Encoding");
|
||||||
|
MemoryStream resultStream = new();
|
||||||
|
await using ZLibStream stream = new(resultStream, CompressionMode.Compress, true);
|
||||||
|
await stream.WriteAsync(responseBuffer.ToArray());
|
||||||
|
stream.Close();
|
||||||
|
|
||||||
|
resultStream.Position = 0;
|
||||||
|
context.Response.Headers.Add("Content-Length", resultStream.Length.ToString());
|
||||||
|
context.Response.Headers.Add("Content-Encoding", "deflate");
|
||||||
|
responseBuffer.SetLength(0);
|
||||||
|
await resultStream.CopyToAsync(responseBuffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string headerName = !context.Response.Headers.ContentLength.HasValue
|
||||||
|
? "Content-Length"
|
||||||
|
: "X-Original-Content-Length";
|
||||||
|
context.Response.Headers.Add(headerName, responseBuffer.Length.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
// Copy the buffered response to the actual response stream.
|
// Copy the buffered response to the actual response stream.
|
||||||
responseBuffer.Position = 0;
|
responseBuffer.Position = 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue