diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs index 3712a58f5..4c21d2eb7 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs @@ -435,10 +435,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer if (!compress || string.IsNullOrEmpty(requestedCompressionType)) { - var stream = await factoryFn().ConfigureAwait(false); - var rangeHeader = requestContext.GetHeader("Range"); + var stream = await factoryFn().ConfigureAwait(false); + if (!string.IsNullOrEmpty(rangeHeader)) { return new RangeRequestWriter(rangeHeader, stream, contentType, isHeadRequest); @@ -448,34 +448,54 @@ namespace MediaBrowser.Server.Implementations.HttpServer if (isHeadRequest) { + stream.Dispose(); + return GetHttpResult(new byte[] { }, contentType); } return new StreamWriter(stream, contentType, _logger); } - if (isHeadRequest) - { - return GetHttpResult(new byte[] { }, contentType); - } - string content; + long originalContentLength = 0; using (var stream = await factoryFn().ConfigureAwait(false)) { - using (var reader = new StreamReader(stream)) + using (var memoryStream = new MemoryStream()) { - content = await reader.ReadToEndAsync().ConfigureAwait(false); + await stream.CopyToAsync(memoryStream).ConfigureAwait(false); + memoryStream.Position = 0; + + originalContentLength = memoryStream.Length; + + using (var reader = new StreamReader(memoryStream)) + { + content = await reader.ReadToEndAsync().ConfigureAwait(false); + } } } if (!SupportsCompression) { + responseHeaders["Content-Length"] = originalContentLength.ToString(UsCulture); + + if (isHeadRequest) + { + return GetHttpResult(new byte[] { }, contentType); + } + return new HttpResult(content, contentType); } var contents = content.Compress(requestedCompressionType); + responseHeaders["Content-Length"] = contents.Length.ToString(UsCulture); + + if (isHeadRequest) + { + return GetHttpResult(new byte[] { }, contentType); + } + return new CompressedResult(contents, requestedCompressionType, contentType); } |
