mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
limit http compression
This commit is contained in:
parent
e4ceec749d
commit
009e860f6f
@ -203,20 +203,12 @@ namespace Emby.Server.Implementations.HttpServer
|
|||||||
// Do not use the memoryStreamFactory here, they don't place nice with compression
|
// Do not use the memoryStreamFactory here, they don't place nice with compression
|
||||||
using (var ms = new MemoryStream())
|
using (var ms = new MemoryStream())
|
||||||
{
|
{
|
||||||
using (var compressionStream = GetCompressionStream(ms, compressionType))
|
ContentTypes.Instance.SerializeToStream(request, dto, ms);
|
||||||
{
|
ms.Position = 0;
|
||||||
ContentTypes.Instance.SerializeToStream(request, dto, compressionStream);
|
|
||||||
compressionStream.Dispose();
|
|
||||||
|
|
||||||
var compressedBytes = ms.ToArray();
|
var responseHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
var httpResult = new StreamWriter(compressedBytes, request.ResponseContentType, _logger);
|
return GetCompressedResult(ms, compressionType, responseHeaders, false, request.ResponseContentType).Result;
|
||||||
|
|
||||||
//httpResult.Headers["Content-Length"] = compressedBytes.Length.ToString(UsCulture);
|
|
||||||
httpResult.Headers["Content-Encoding"] = compressionType;
|
|
||||||
|
|
||||||
return httpResult;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -591,45 +583,53 @@ namespace Emby.Server.Implementations.HttpServer
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
string content;
|
|
||||||
|
|
||||||
using (var stream = await factoryFn().ConfigureAwait(false))
|
using (var stream = await factoryFn().ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
using (var reader = new StreamReader(stream))
|
return await GetCompressedResult(stream, requestedCompressionType, responseHeaders, isHeadRequest, contentType).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<IHasHeaders> GetCompressedResult(Stream stream,
|
||||||
|
string requestedCompressionType,
|
||||||
|
IDictionary<string,string> responseHeaders,
|
||||||
|
bool isHeadRequest,
|
||||||
|
string contentType)
|
||||||
{
|
{
|
||||||
content = await reader.ReadToEndAsync().ConfigureAwait(false);
|
using (var reader = new MemoryStream())
|
||||||
}
|
{
|
||||||
}
|
await stream.CopyToAsync(reader).ConfigureAwait(false);
|
||||||
|
|
||||||
var contents = Compress(content, requestedCompressionType);
|
reader.Position = 0;
|
||||||
|
var content = reader.ToArray();
|
||||||
|
|
||||||
responseHeaders["Content-Length"] = contents.Length.ToString(UsCulture);
|
if (content.Length >= 1024)
|
||||||
|
{
|
||||||
|
content = Compress(content, requestedCompressionType);
|
||||||
responseHeaders["Content-Encoding"] = requestedCompressionType;
|
responseHeaders["Content-Encoding"] = requestedCompressionType;
|
||||||
|
}
|
||||||
|
|
||||||
|
responseHeaders["Content-Length"] = content.Length.ToString(UsCulture);
|
||||||
|
|
||||||
if (isHeadRequest)
|
if (isHeadRequest)
|
||||||
{
|
{
|
||||||
return GetHttpResult(new byte[] { }, contentType, true);
|
return GetHttpResult(new byte[] { }, contentType, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return GetHttpResult(contents, contentType, true, responseHeaders);
|
return GetHttpResult(content, contentType, true, responseHeaders);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] Compress(string text, string compressionType)
|
private byte[] Compress(byte[] bytes, string compressionType)
|
||||||
{
|
{
|
||||||
if (compressionType == "deflate")
|
if (compressionType == "deflate")
|
||||||
return Deflate(text);
|
return Deflate(bytes);
|
||||||
|
|
||||||
if (compressionType == "gzip")
|
if (compressionType == "gzip")
|
||||||
return GZip(text);
|
return GZip(bytes);
|
||||||
|
|
||||||
throw new NotSupportedException(compressionType);
|
throw new NotSupportedException(compressionType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] Deflate(string text)
|
|
||||||
{
|
|
||||||
return Deflate(Encoding.UTF8.GetBytes(text));
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] Deflate(byte[] bytes)
|
private byte[] Deflate(byte[] bytes)
|
||||||
{
|
{
|
||||||
// In .NET FX incompat-ville, you can't access compressed bytes without closing DeflateStream
|
// In .NET FX incompat-ville, you can't access compressed bytes without closing DeflateStream
|
||||||
@ -644,11 +644,6 @@ namespace Emby.Server.Implementations.HttpServer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] GZip(string text)
|
|
||||||
{
|
|
||||||
return GZip(Encoding.UTF8.GetBytes(text));
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] GZip(byte[] buffer)
|
private byte[] GZip(byte[] buffer)
|
||||||
{
|
{
|
||||||
using (var ms = new MemoryStream())
|
using (var ms = new MemoryStream())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user