ASP.NET 核心压缩中间件 - 空响应
ASP.NET Core Compression Middleware - Empty Reponse
我正在使用一些自定义压缩中间件 from this repository(粘贴在下方)。根据第一个请求,内容被压缩得很好。对于之后的每个请求,返回的响应都是空的(Content-Length 为 0)。
从 ASP.NET Core RC2 迁移到 RTM 后才开始发生这种情况。
有人知道为什么会这样吗?
压缩中间件:
public class CompressionMiddleware
{
private readonly RequestDelegate _next;
public CompressionMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if (IsGZipSupported(context))
{
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
var buffer = new MemoryStream();
var stream = context.Response.Body;
context.Response.Body = buffer;
await _next(context);
if (acceptEncoding.Contains("gzip"))
{
var gstream = new GZipStream(stream, CompressionLevel.Optimal);
context.Response.Headers.Add("Content-Encoding", new[] { "gzip" });
buffer.Seek(0, SeekOrigin.Begin);
await buffer.CopyToAsync(gstream);
gstream.Dispose();
}
else
{
var gstream = new DeflateStream(stream, CompressionLevel.Optimal);
context.Response.Headers.Add("Content-Encoding", new[] { "deflate" });
buffer.Seek(0, SeekOrigin.Begin);
await buffer.CopyToAsync(gstream);
gstream.Dispose();
}
}
else
{
await _next(context);
}
}
public bool IsGZipSupported(HttpContext context)
{
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
return !string.IsNullOrEmpty(acceptEncoding) &&
(acceptEncoding.Contains("gzip") || acceptEncoding.Contains("deflate"));
}
}
我在“Add HTTP compression middleware”问题中发现了以下内容:
我添加了 gzip 并且它起作用了,但是首先请求。我的意思是在第一个请求中,响应页面为空(context.Response.Body)但是当你刷新页面(仅一次)之后它可以正常工作。(我不知道为什么但我必须解决它)
问题的回复是:
You need to update
context.Response.Headers["Content-Length"] with actual compressed
buffer length.
及以上link压缩中间件的实现包含:
if (context.Response.Headers["Content-Length"].Count > 0)
{
context.Response.Headers["Content-Length"] = compressed.Length.ToString();
}
我正在使用一些自定义压缩中间件 from this repository(粘贴在下方)。根据第一个请求,内容被压缩得很好。对于之后的每个请求,返回的响应都是空的(Content-Length 为 0)。
从 ASP.NET Core RC2 迁移到 RTM 后才开始发生这种情况。
有人知道为什么会这样吗?
压缩中间件:
public class CompressionMiddleware
{
private readonly RequestDelegate _next;
public CompressionMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if (IsGZipSupported(context))
{
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
var buffer = new MemoryStream();
var stream = context.Response.Body;
context.Response.Body = buffer;
await _next(context);
if (acceptEncoding.Contains("gzip"))
{
var gstream = new GZipStream(stream, CompressionLevel.Optimal);
context.Response.Headers.Add("Content-Encoding", new[] { "gzip" });
buffer.Seek(0, SeekOrigin.Begin);
await buffer.CopyToAsync(gstream);
gstream.Dispose();
}
else
{
var gstream = new DeflateStream(stream, CompressionLevel.Optimal);
context.Response.Headers.Add("Content-Encoding", new[] { "deflate" });
buffer.Seek(0, SeekOrigin.Begin);
await buffer.CopyToAsync(gstream);
gstream.Dispose();
}
}
else
{
await _next(context);
}
}
public bool IsGZipSupported(HttpContext context)
{
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
return !string.IsNullOrEmpty(acceptEncoding) &&
(acceptEncoding.Contains("gzip") || acceptEncoding.Contains("deflate"));
}
}
我在“Add HTTP compression middleware”问题中发现了以下内容:
我添加了 gzip 并且它起作用了,但是首先请求。我的意思是在第一个请求中,响应页面为空(context.Response.Body)但是当你刷新页面(仅一次)之后它可以正常工作。(我不知道为什么但我必须解决它)
问题的回复是:
You need to update context.Response.Headers["Content-Length"] with actual compressed buffer length.
及以上link压缩中间件的实现包含:
if (context.Response.Headers["Content-Length"].Count > 0)
{
context.Response.Headers["Content-Length"] = compressed.Length.ToString();
}